linux 命令:service、systemd、systemctl

From:https://linux.cn/article-5926-1.html

1、service 命令基本使用

服务(service)管理

  • 服务(service) 本质就是进程,但是是运行在后台的,通常都会监听某个端口,等待其它程序的请求,比如(mysqld , sshd、防火墙等),因此我们又称为守护进程,是 Linux 中非常重要的知识点。

service 管理 指令

service 服务名 [start | stop | restart | reload | status]

service 指令管理的服务在 /etc/init.d 查看

查看 服务 状态

service 管理指令案例

[注意:可以在虚拟系统演示,因为网络连接会关闭]

service network status
service network stop
service network start

查看 服务名

进入到 /etc/init.d 查看:ls -l /etc/init.d

服务的运行级别 (runlevel)

Linux 系统有 7 种运行级别(runlevel):常用的是级别 3 和 5

  • 运行级别 0:系统停机状态,系统默认运行级别不能设为 0,否则不能正常启动
  • 运行级别 1:单用户工作状态,root 权限,用于系统维护,禁止远程登陆
  • 运行级别 2:多用户状态(没有 NFS),不支持网络
  • 运行级别 3:完全的多用户状态(有 NFS),无界面,登陆后进入控制台命令行模式
  • 运行级别 4:系统未使用,保留
  • 运行级别 5:X11 控制台,登陆后进入图形 GUI 模式
  • 运行级别 6:系统正常关闭并重启,默认运行级别不能设为 6,否则不能正常启动

开机的流程说明:

查看当前运行级别:systemctl get-default

设置模式级别类型, run(运行):systemctl set-default TARGET.target

切换成图形界面(init 5 运行级别):systemctl set-default graphical.target

在上述切换运行级别的基础上重启系统后,依然会进入到 init 3 级别的终端界面,就不会再进入到图形界面了。

2、systemd 和 systemctl

systemd

  • Systemd 是一个系统管理守护进程、工具和库的集合,用于取代 System V 初始进程。Systemd 的功能是用于集中管理和配置类 UNIX 系统。在Linux生态系统中,Systemd 被部署到了大多数的标准Linux发行版中,Systemd 通常是所有其它守护进程的父进程,但并非总是如此。

systemctl (是 systemd 的一个管理工具)

  • Systemctl 是 systemd 的一个管理工具,主要负责控制 systemd 系统和服务管理器。systemctl 实际上将 service 和 chkconfig 这两个命令组合到一起。

服务的 "启用、禁用":

  • 启用服务就是在当前“runlevel”的配置文件目录/etc/systemd/system/multi-user.target.wants/里,建立/usr/lib/systemd/system里面对应服务配置文件的软链接;
  • 禁用服务就是删除此软链接。

任务

旧指令

新指令

使某服务自动启动

chkconfig --level 3 httpd on

systemctl enable httpd.service

使某服务不自动启动

chkconfig --level 3 httpd off

systemctl disable httpd.service

检查服务状态

service httpd status

systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service(仅显示是否 Active)

显示所有已启动的服务

chkconfig --list

systemctl list-units --type=service

启动某服务

service httpd start

systemctl start httpd.service

停止某服务

service httpd stop

systemctl stop httpd.service

重启某服务

service httpd restart

systemctl restart httpd.service

systemctl 使用示例

1.启动 nfs 服务 systemctl start nfs-server.service 
2.设置开机自启动 systemctl enable nfs-server.service 
3.停止开机自启动 systemctl disable nfs-server.service 
4.查看服务当前状态 systemctl status nfs-server.service 
5.重新启动某服务 systemctl restart nfs-server.service 
6.查看所有已启动的服务 systemctl list -units --type=service 
7.开启防火墙22端口 iptables -I INPUT -p tcp --dport 22 -j accept 

如果仍然有问题,就可能是SELinux导致的 关闭SElinux: 
修改/etc/selinux/config文件中的SELINUX=””为disabled,然后重启。 

彻底关闭防火墙: 
sudo systemctl status firewalld.service 
sudo systemctl stop firewalld.service           
sudo systemctl disable firewalld.service

简单总结

启动一个服务:systemctl start postfix.service
关闭一个服务:systemctl stop postfix.service
重启一个服务:systemctl restart postfix.service
显示一个服务的状态:systemctl status postfix.service
在开机时启用一个服务:systemctl enable postfix.service
在开机时禁用一个服务:systemctl disable postfix.service
查看服务是否开机启动:systemctl is-enabled postfix.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled

systemctl 的使用

查看版本:systemctl --version

查看位置:whereis systemd、whereis systemctl

查看运行:ps -eaf | grep systemd

  • 注意:systemd是作为父进程(PID=1)运行的。在上面带(-e)参数的ps命令输出中,选择所有进程,(-a)选择除会话前导外的所有进程,并使用(-f)参数输出完整格式列表(即 -eaf)。也请注意上例中后随的方括号和例子中剩余部分。方括号表达式是grep的字符类表达式的一部分。

分析systemd启动进程:systemd-analyze

分析启动时各个进程花费的时间:systemd-analyze blame

分析启动时的关键链:systemd-analyze critical-chain

列出所有可用单元:systemctl list-unit-files

列出所有运行中单元:systemctl list-units

列出所有失败单元:systemctl --failed

检查某个单元(如 cron.service)是否启用:systemctl is-enabled crond.service

列出所有服务(包括启用的和禁用的):systemctl list-unit-files --type=service

Linux 中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

        # systemctl start httpd.service
        # systemctl restart httpd.service
        # systemctl stop httpd.service
        # systemctl reload httpd.service
        # systemctl status httpd.service

  • 注意:当我们使用systemctl的start,restart,stop和reload命令时,我们不会从终端获取到任何输出内容,只有status命令可以打印输出。

如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

        # systemctl is-active httpd.service
        # systemctl enable httpd.service
        # systemctl disable httpd.service

如何屏蔽(让它不能启动)或显示服务(如 httpd.service)

        # systemctl mask httpd.service
        ln -s '/dev/null' '/etc/systemd/system/httpd.service'
        # systemctl unmask httpd.service
        rm '/etc/systemd/system/httpd.service'

使用systemctl命令杀死服务

        # systemctl kill httpd
        # systemctl status httpd

使用Systemctl控制并管理挂载点

17. 列出所有系统挂载点

# systemctl list-unit-files --type=mount
UNIT FILE                     STATE   
dev-hugepages.mount           static  
dev-mqueue.mount              static  
proc-sys-fs-binfmt_misc.mount static  
sys-fs-fuse-connections.mount static  
sys-kernel-config.mount       static  
sys-kernel-debug.mount        static  
tmp.mount                     disabled

18. 挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount
tmp.mount - Temporary Directory
   Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled)
   Active: active (mounted) since Tue 2015-04-28 17:46:06 IST; 2min 48s ago
    Where: /tmp
     What: tmpfs
     Docs: man:hier(7)
http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
  Process: 3908 ExecMount=/bin/mount tmpfs /tmp -t tmpfs -o mode=1777,strictatime (code=exited, status=0/SUCCESS)
Apr 28 17:46:06 tecmint systemd[1]: Mounting Temporary Directory...
Apr 28 17:46:06 tecmint systemd[1]: tmp.mount: Directory /tmp to mount over is not empty, mounting anyway.
Apr 28 17:46:06 tecmint systemd[1]: Mounted Temporary Directory.

19. 在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

# systemctl is-active tmp.mount
# systemctl enable tmp.mount
# systemctl disable  tmp.mount

20. 在Linux中屏蔽(让它不能启用)或可见挂载点

# systemctl mask tmp.mount
ln -s '/dev/null' '/etc/systemd/system/tmp.mount'
# systemctl unmask tmp.mount
rm '/etc/systemd/system/tmp.mount'

使用 Systemctl 控制并管理套接口

21. 列出所有可用系统套接口

# systemctl list-unit-files --type=socket
UNIT FILE                    STATE   
dbus.socket                  static  
dm-event.socket              enabled 
lvm2-lvmetad.socket          enabled 
rsyncd.socket                disabled
sshd.socket                  disabled
syslog.socket                static  
systemd-initctl.socket       static  
systemd-journald.socket      static  
systemd-shutdownd.socket     static  
systemd-udevd-control.socket static  
systemd-udevd-kernel.socket  static  
11 unit files listed.

22. 在Linux中启动、重启、停止、重载套接口并检查其状态

# systemctl start cups.socket
# systemctl restart cups.socket
# systemctl stop cups.socket
# systemctl reload cups.socket
# systemctl status cups.socket
cups.socket - CUPS Printing Service Sockets
   Loaded: loaded (/usr/lib/systemd/system/cups.socket; enabled)
   Active: active (listening) since Tue 2015-04-28 18:10:59 IST; 8s ago
   Listen: /var/run/cups/cups.sock (Stream)
Apr 28 18:10:59 tecmint systemd[1]: Starting CUPS Printing Service Sockets.
Apr 28 18:10:59 tecmint systemd[1]: Listening on CUPS Printing Service Sockets.

23. 在启动时激活套接口,并启用或禁用它(系统启动时自启动)

# systemctl is-active cups.socket
# systemctl enable cups.socket
# systemctl disable cups.socket

24. 屏蔽(使它不能启动)或显示套接口

# systemctl mask cups.socket
ln -s '/dev/null' '/etc/systemd/system/cups.socket'
# systemctl unmask cups.socket
rm '/etc/systemd/system/cups.socket'

服务的CPU利用率(分配额)

25. 获取当前某个服务的CPU分配额(如httpd)

# systemctl show -p CPUShares httpd.service
CPUShares=1024

注意:各个服务的默认CPU分配份额=1024,你可以增加/减少某个进程的CPU分配份额。

26. 将某个服务(httpd.service)的CPU分配份额限制为2000 CPUShares/

# systemctl set-property httpd.service CPUShares=2000
# systemctl show -p CPUShares httpd.service
CPUShares=2000

注意:当你为某个服务设置CPUShares,会自动创建一个以服务名命名的目录(如 httpd.service),里面包含了一个名为90-CPUShares.conf的文件,该文件含有CPUShare限制信息,你可以通过以下方式查看该文件:

# vi /etc/systemd/system/httpd.service.d/90-CPUShares.conf 
[Service]
CPUShares=2000

27. 检查某个服务的所有配置细节

# systemctl show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=The Apache HTTP Server
LoadState=loaded
ActiveState=active
SubState=running
FragmentPath=/usr/lib/systemd/system/httpd.service
....

28. 分析某个服务(httpd)的关键链

# systemd-analyze critical-chain httpd.service
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.
httpd.service +142ms
└─network.target @11.168s
  └─network.service @9.456s +1.712s
    └─NetworkManager.service @8.858s +596ms
      └─firewalld.service @4.931s +3.926s
        └─basic.target @4.916s
          └─sockets.target @4.916s
            └─dbus.socket @4.916s
              └─sysinit.target @4.905s
                └─systemd-update-utmp.service @4.864s +39ms
                  └─auditd.service @4.563s +301ms
                    └─systemd-tmpfiles-setup.service @4.485s +69ms
                      └─rhel-import-state.service @4.342s +142ms
                        └─local-fs.target @4.324s
                          └─boot.mount @4.286s +31ms
                            └─systemd-fsck@dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\x2d85d196080964.service @4.092s +149ms
                              └─dev-disk-by\x2duuid-79f594ad\x2da332\x2d4730\x2dbb5f\x2d85d196080964.device @4.092s

29. 获取某个服务(httpd)的依赖性列表

# systemctl list-dependencies httpd.service
httpd.service
├─system.slice
└─basic.target
  ├─firewalld.service
  ├─microcode.service
  ├─rhel-autorelabel-mark.service
  ├─rhel-autorelabel.service
  ├─rhel-configure.service
  ├─rhel-dmesg.service
  ├─rhel-loadmodules.service
  ├─paths.target
  ├─slices.target
  │ ├─-.slice
  │ └─system.slice
  ├─sockets.target
  │ ├─dbus.socket
....

30. 按等级列出控制组

# systemd-cgls
├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
├─user.slice
│ └─user-0.slice
│   └─session-1.scope
│     ├─2498 sshd: root@pts/0    
│     ├─2500 -bash
│     ├─4521 systemd-cgls
│     └─4522 systemd-cgls
└─system.slice
  ├─httpd.service
  │ ├─4440 /usr/sbin/httpd -DFOREGROUND
  │ ├─4442 /usr/sbin/httpd -DFOREGROUND
  │ ├─4443 /usr/sbin/httpd -DFOREGROUND
  │ ├─4444 /usr/sbin/httpd -DFOREGROUND
  │ ├─4445 /usr/sbin/httpd -DFOREGROUND
  │ └─4446 /usr/sbin/httpd -DFOREGROUND
  ├─polkit.service
  │ └─721 /usr/lib/polkit-1/polkitd --no-debug
....

31. 按CPU、内存、输入和输出列出控制组

# systemd-cgtop
Path                                                              Tasks   %CPU   Memory  Input/s Output/s
/                                                                    83    1.0   437.8M        -        -
/system.slice                                                         -    0.1        -        -        -
/system.slice/mariadb.service                                         2    0.1        -        -        -
/system.slice/tuned.service                                           1    0.0        -        -        -
/system.slice/httpd.service                                           6    0.0        -        -        -
/system.slice/NetworkManager.service                                  1      -        -        -        -
/system.slice/atop.service                                            1      -        -        -        -
/system.slice/atopacct.service                                        1      -        -        -        -
/system.slice/auditd.service                                          1      -        -        -        -
/system.slice/crond.service                                           1      -        -        -        -
/system.slice/dbus.service                                            1      -        -        -        -
/system.slice/firewalld.service                                       1      -        -        -        -
/system.slice/lvm2-lvmetad.service                                    1      -        -        -        -
/system.slice/polkit.service                                          1      -        -        -        -
/system.slice/postfix.service                                         3      -        -        -        -
/system.slice/rsyslog.service                                         1      -        -        -        -
/system.slice/system-getty.slice/getty@tty1.service                   1      -        -        -        -
/system.slice/systemd-journald.service                                1      -        -        -        -
/system.slice/systemd-logind.service                                  1      -        -        -        -
/system.slice/systemd-udevd.service                                   1      -        -        -        -
/system.slice/webmin.service                                          1      -        -        -        -
/user.slice/user-0.slice/session-1.scope                              3      -        -        -        -

控制系统运行等级

32. 启动系统救援模式

# systemctl rescue
Broadcast message from root@tecmint on pts/0 (Wed 2015-04-29 11:31:18 IST):
The system is going down to rescue mode NOW!

33. 进入紧急模式

# systemctl emergency
Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" to try again
to boot into default mode.

34. 列出当前使用的运行等级

# systemctl get-default
multi-user.target

35. 启动运行等级5,即图形模式

# systemctl isolate runlevel5.target
或
# systemctl isolate graphical.target

36. 启动运行等级3,即多用户模式(命令行)

# systemctl isolate runlevel3.target
或
# systemctl isolate multiuser.target

36. 设置多用户模式或图形模式为默认运行等级

# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target

37. 重启、停止、挂起、休眠系统或使系统进入混合睡眠

# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep

对于不知运行等级为何物的人,说明如下。

  • Runlevel 0 : 关闭系统
  • Runlevel 1 : 救援?维护模式
  • Runlevel 3 : 多用户,无图形系统
  • Runlevel 4 : 多用户,无图形系统
  • Runlevel 5 : 多用户,图形化系统
  • Runlevel 6 : 关闭并重启机器

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值