linux命令学习之:systemctl

时间:2021-12-18 15:08:33

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

  CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替。

  Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

  在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非全是如此。

1、原来的 service 命令与 systemctl 命令对比

daemon命令 systemctl命令 说明
service [服务] start systemctl start [unit type] 启动服务
service [服务] stop systemctl stop [unit type] 停止服务
service [服务] restart systemctl restart [unit type] 重启服务

此外还是二个systemctl参数没有与service命令参数对应

  • status:参数来查看服务运行情况
  • reload:重新加载服务,加载更新后的配置文件(并不是所有服务都支持这个参数,比如network.service)

  示例:

#启动网络服务
systemctl start network.service #停止网络服务
systemctl stop network.service #重启网络服务
systemctl restart network.service #查看网络服务状态
systemctl status network.serivce

2、原来的chkconfig 命令与 systemctl 命令对比

daemon命令 systemctl命令 说明
chkconfig [服务] on systemctl enable [unit type] 设置服务开机启动
chkconfig [服务] off systemctl disable [unit type] 设备服务禁止开机启动

  示例:

#停止cup电源管理服务
systemctl stop cups.service #禁止cups服务开机启动
systemctl disable cups.service #查看cups服务状态
systemctl status cups.service #重新设置cups服务开机启动
systemctl enable cups.service

3、查看系统上上所有的服务

  命令格式:systemctl [command] [–type=TYPE] [–all]

  参数详解:

  command - list-units:依据unit列出所有启动的unit。加上 –all 才会列出没启动的unit; - list-unit-files:依据/usr/lib/systemd/system/ 内的启动文件,列出启动文件列表

–type=TYPE - 为unit type, 主要有service, socket, target

  应用举例:

systemctl命令 说明
systemctl 列出所有的系统服务
systemctl list-units 列出所有启动unit
systemctl list-unit-files 列出所有启动文件
systemctl list-units –type=service –all 列出所有service类型的unit
systemctl list-units –type=service –all grep cpu 列出 cpu电源管理机制的服务
systemctl list-units –type=target –all 列出所有target

4、systemctl特殊的用法

systemctl命令 说明
systemctl is-active [unit type] 查看服务是否运行
systemctl is-enable [unit type] 查看服务是否设置为开机启动
systemctl mask [unit type] 注销指定服务
systemctl unmask [unit type] 取消注销指定服务

  应用举例:

#查看网络服务是否启动
systemctl is-active network.service #检查网络服务是否设置为开机启动
systemctl is-enable network.service #停止cups服务
systemctl stop cups.service #注销cups服务
systemctl mask cups.service #查看cups服务状态
systemctl status cups.service #取消注销cups服务
systemctl unmask cups.service

5、常用示例:

Systemctl基础、体验

1. 检查系统中是否安装有systemctl并确定当前安装的版本

[root@CTU1000094955 ~]# systemctl --version
systemd
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

  上例中很清楚地表明,我们安装了219版本的systemctl。

2. 检查systemd和systemctl的二进制文件和库文件的安装位置

[root@CTU1000094955 ~]# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd..gz
[root@CTU1000094955 ~]# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl..gz

3. 检查systemd是否运行

[root@CTU1000094955 ~]# ps -eaf | grep [s]ystemd
root : ? :: /usr/lib/systemd/systemd --switched-root --system --deserialize
root : ? :: /usr/lib/systemd/systemd-journald
root : ? :: /usr/lib/systemd/systemd-udevd
dbus : ? :: /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root : ? :: /usr/lib/systemd/systemd-logind

  注意:systemd是作为父进程(PID=1)运行的。在上面带(-e)参数的ps命令输出中,选择所有进程,(-a)选择除会话前导外的所有进程,并使用(-f)参数输出完整格式列表(即 -eaf)。

  也请注意上例中后随的方括号和例子中剩余部分。方括号表达式是grep的字符类表达式的一部分。

4. 分析systemd启动进程

[root@CTU1000094955 ~]# systemd-analyze
Startup finished in 743ms (kernel) + .959s (initrd) + .696s (userspace) = .399s

5. 分析启动时各个进程花费的时间

[root@CTU1000094955 ~]# systemd-analyze blame
.859s dev-xvda1.device
.670s NetworkManager-wait-online.service
.610s plymouth-quit-wait.service
.757s systemd-udev-settle.service
.078s kdump.service
.257s mysql.service
.451s libvirtd.service
.228s postfix.service
751ms uvp-monitor.service
675ms spesserviced.service
675ms messageserviced.service
660ms tuned.service
586ms network.service
525ms accounts-daemon.service
467ms NetworkManager.service
457ms systemd-udevd.service
416ms abrt-ccpp.service
397ms firewalld.service
365ms ModemManager.service

6. 分析启动时的关键链

[root@CTU1000094955 ~]# systemd-analyze critical-chain
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. graphical.target @.682s
?..multi-user.target @.682s
?..getty.target @.682s
?..serial-getty@hvc0.service @.681s
?..systemd-user-sessions.service @.929s +18ms
?..remote-fs.target @.921s
?..remote-fs-pre.target @.921s
?..iscsi-shutdown.service @.907s +12ms
?..network.target @.900s
?..network.service @.311s +586ms
?..NetworkManager.service @.166s +467ms
?..basic.target @.903s
?..sockets.target @.903s
?..rpcbind.socket @.903s
?..sysinit.target @.895s
?..systemd-update-utmp.service @.877s +16ms
?..auditd.service @.738s +135ms
?..systemd-tmpfiles-setup.service @.661s +73ms
?..rhel-import-state.service @.526s +134ms
?..local-fs.target @.525s
?..run-user-.mount @.041s
?..local-fs-pre.target @.615s
?..systemd-tmpfiles-setup-dev.service @
?..kmod-static-nodes.service @.473s
?..systemd-journald.socket
?..-.slice

  重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

7. 列出所有可用单元

[root@CTU1000094955 ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
cups.path enabled
systemd-ask-password-console.path static

8. 列出所有运行中单元

[root@CTU1000094955 ~]# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:-::01.1-ata2-host1-target1::-:::-block-sr0.device loaded active plugged QEMU_DVD-ROM
sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS3
sys-devices-pnp0-:-tty-ttyS0.device loaded active plugged /sys/devices/pnp0/:/tty/ttyS0
sys-devices-pnp0-:-tty-ttyS1.device loaded active plugged /sys/devices/pnp0/:/tty/ttyS1
sys-devices-vbd\x2d51712-block-xvda-xvda1.device loaded active plugged /sys/devices/vbd-/block/xvda/xvda1
sys-devices-vbd\x2d51712-block-xvda-xvda2.device loaded active plugged /sys/devices/vbd-/block/xvda/xvda2
sys-devices-vbd\x2d51712-block-xvda.device loaded active plugged /sys/devices/vbd-/block/xvda
sys-devices-vbd\x2d51776-block-xvde.device loaded active plugged LVM PV I2rhCm-www3-J79i-r3ei-1XCb-a1qS-3OWSg9 on /dev/xvde
sys-devices-vif\x2d0-net-eth0.device loaded active plugged /sys/devices/vif-/net/eth0

9. 列出所有失败单元

[root@CTU1000094955 ~]# systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
?.bluetooth.service loaded failed failed Bluetooth service
?.rc-local.service loaded failed failed /etc/rc.d/rc.local Compatibility LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type. loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

10. 检查某个单元(如 cron.service)是否启用

[root@CTU1000094955 ~]# systemctl is-enabled firewalld.service
disabled

11. 检查某个单元或服务是否运行

[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Sat -- :: CST; 1h 29min ago
Main PID: (firewalld)
CGroup: /system.slice/firewalld.service
?.. /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.

使用Systemctl控制并管理服务

12. 列出所有服务(包括启用的和禁用的)

[root@CTU1000094955 ~]# systemctl list-unit-files --type=service
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-pstoreoops.service disabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
accounts-daemon.service enabled
alsa-restore.service static
alsa-state.service static
alsa-store.service static
anaconda-direct.service static
anaconda-nm-config.service static
anaconda-noshell.service static
anaconda-shell@.service static

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

[root@CTU1000094955 ~]# systemctl start httpd.service
Failed to start httpd.service: Unit httpd.service failed to load: No such file or directory.
[root@CTU1000094955 ~]# systemctl start firewalld.service
[root@CTU1000094955 ~]# systemctl restart firewalld.service
[root@CTU1000094955 ~]# systemctl stop firewalld.service
[root@CTU1000094955 ~]# systemctl reload firewalld.service
Job for firewalld.service invalid.
[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) Nov :: CTU1000094955 systemd[]: Stopped firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Stopping firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Stopping firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Stopped firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Unit firewalld.service cannot be reloaded because it is inactive.
[root@CTU1000094955 ~]# systemctl restart firewalld.service
[root@CTU1000094955 ~]# systemctl reload firewalld.service
[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Sat -- :: CST; 8s ago
Process: ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=/SUCCESS)
Main PID: (firewalld)
CGroup: /system.slice/firewalld.service
?.. /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Reloaded firewalld - dynamic firewall daemon.

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

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

[root@CTU1000094955 ~]# systemctl is-active firewalld.service
active
[root@CTU1000094955 ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@CTU1000094955 ~]# systemctl list-unit-files |grep firewall
firewalld.service enabled
[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat -- :: CST; 4min 6s ago
Main PID: (firewalld)
CGroup: /system.slice/firewalld.service
?.. /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Reloaded firewalld - dynamic firewall daemon.
[root@CTU1000094955 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@CTU1000094955 ~]# systemctl list-unit-files |grep firewall
firewalld.service disabled

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

[root@CTU1000094955 ~]# systemctl mask firewalld.service
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@CTU1000094955 ~]# systemctl unmask firewalld.service
Removed symlink /etc/systemd/system/firewalld.service.

16. 使用systemctl命令杀死服务

[root@CTU1000094955 ~]# systemctl kill httpd
Failed to kill unit httpd.service: Unit httpd.service is not loaded.
[root@CTU1000094955 ~]# systemctl kill firewalld.service
[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat -- :: CST; 7s ago
Main PID: (code=exited, status=/SUCCESS) Nov :: CTU1000094955 systemd[]: Starting firewalld - dynamic firewall daemon...
Nov :: CTU1000094955 systemd[]: Started firewalld - dynamic firewall daemon.
Nov :: CTU1000094955 systemd[]: Reloaded firewalld - dynamic firewall daemon.

使用Systemctl控制并管理挂载点

17. 列出所有系统挂载点

[root@CTU1000094955 ~]# systemctl list-unit-files --type=mount
UNIT FILE STATE
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount static unit files listed.

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

[root@CTU1000094955 ~]# systemctl start tmp.mount
[root@CTU1000094955 ~]# systemctl stop tmp.mount
[root@CTU1000094955 ~]# systemctl reload tmp.mount
Job for tmp.mount invalid.
[root@CTU1000094955 ~]# systemctl status tmp.mount
?.tmp.mount - Temporary Directory
Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
Active: inactive (dead) since Sat -- :: CST; 25s ago
Where: /tmp
What: tmpfs
Docs: man:hier()
http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Process: ExecUnmount=/bin/umount /tmp -n (code=exited, status=/SUCCESS)
Process: ExecMount=/bin/mount tmpfs /tmp -n -t tmpfs -o mode=,strictatime (code=exited, status=/SUCCESS) Nov :: CTU1000094955 systemd[]: Mounting Temporary Directory...
Nov :: CTU1000094955 systemd[]: Mounted Temporary Directory.
Nov :: CTU1000094955 systemd[]: Unmounting Temporary Directory...
Nov :: CTU1000094955 systemd[]: Unmounted Temporary Directory.
Nov :: CTU1000094955 systemd[]: Unit tmp.mount cannot be reloaded because it is inactive.
[root@CTU1000094955 ~]# systemctl restart tmp.mount
[root@CTU1000094955 ~]# systemctl status tmp.mount
?.tmp.mount - Temporary Directory
Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
Active: active (mounted) since Sat -- :: CST; 2s ago
Where: /tmp
What: tmpfs
Docs: man:hier()
http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Process: ExecUnmount=/bin/umount /tmp -n (code=exited, status=/SUCCESS)
Process: ExecMount=/bin/mount tmpfs /tmp -n -t tmpfs -o mode=,strictatime (code=exited, status=/SUCCESS) Nov :: CTU1000094955 systemd[]: tmp.mount: Directory /tmp to mount over is not empty, mounting anyway.
Nov :: CTU1000094955 systemd[]: Mounting Temporary Directory...
Nov :: CTU1000094955 systemd[]: Mounted Temporary Directory.
[root@CTU1000094955 ~]# systemctl reload tmp.mount

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

[root@CTU1000094955 ~]# systemctl is-active tmp.mount
active
[root@CTU1000094955 ~]# systemctl disable tmp.mount
Removed symlink /etc/systemd/system/local-fs.target.wants/tmp.mount.
[root@CTU1000094955 ~]# systemctl is-active tmp.mount
active
[root@CTU1000094955 ~]# systemctl enable tmp.mount
Failed to execute operation: Access denied

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

[root@CTU1000094955 ~]# systemctl mask tmp.mount
ln -s '/dev/null''/etc/systemd/system/tmp.mount'
[root@CTU1000094955 ~]# systemctl unmask tmp.mount
rm '/etc/systemd/system/tmp.mount'

使用Systemctl控制并管理套接口

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

[root@CTU1000094955 ~]# systemctl list-unit-files --type=socket
UNIT FILE STATE
avahi-daemon.socket enabled
cups.socket enabled
dbus.socket static
dm-event.socket enabled
iscsid.socket enabled
iscsiuio.socket enabled
libvirtd.socket static
lldpad.socket disabled
lvm2-lvmetad.socket enabled
lvm2-lvmpolld.socket enabled
rpcbind.socket enabled
rsyncd.socket disabled
sshd.socket disabled
syslog.socket static
systemd-initctl.socket static
systemd-journald.socket static
systemd-networkd.socket disabled
systemd-shutdownd.socket static
systemd-udevd-control.socket static
systemd-udevd-kernel.socket static
virtlockd.socket disabled unit files listed.

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

[root@CTU1000094955 ~]# systemctl start sshd.socket
[root@CTU1000094955 ~]# systemctl restart sshd.socket
Failed to restart sshd.socket: Transaction contains conflicting jobs 'restart' and 'stop' for sshd-keygen.service. Probably contradicting requirement dependencies configured.
[root@CTU1000094955 ~]# systemctl status sshd.socket
?.sshd.socket - OpenSSH Server Socket
Loaded: loaded (/usr/lib/systemd/system/sshd.socket; disabled; vendor preset: disabled)
Active: active (listening) since Sat -- :: CST; 22s ago
Docs: man:sshd()
man:sshd_config()
Listen: [::]: (Stream)
Accepted: ; Connected: Nov :: CTU1000094955 systemd[]: Listening on OpenSSH Server Socket.
Nov :: CTU1000094955 systemd[]: Starting OpenSSH Server Socket.
[root@CTU1000094955 ~]# systemctl stop sshd.socket
[root@CTU1000094955 ~]# systemctl reload sshd.socket
Failed to reload sshd.socket: Job type reload is not applicable for unit sshd.socket.
[root@CTU1000094955 ~]# systemctl status sshd.socket
?.sshd.socket - OpenSSH Server Socket
Loaded: loaded (/usr/lib/systemd/system/sshd.socket; disabled; vendor preset: disabled)
Active: inactive (dead) since Sat -- :: CST; 19s ago
Docs: man:sshd()
man:sshd_config()
Listen: [::]: (Stream)
Accepted: ; Connected: Nov :: CTU1000094955 systemd[]: Listening on OpenSSH Server Socket.
Nov :: CTU1000094955 systemd[]: Starting OpenSSH Server Socket.
Nov :: CTU1000094955 systemd[]: Closed OpenSSH Server Socket.
Nov :: CTU1000094955 systemd[]: Stopping OpenSSH Server Socket.

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

[root@CTU1000094955 ~]# systemctl mask sshd.socket
Created symlink from /etc/systemd/system/sshd.socket to /dev/null.
[root@CTU1000094955 ~]# systemctl unmask sshd.socket
Removed symlink /etc/systemd/system/sshd.socket.

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

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

[root@CTU1000094955 ~]# systemctl show -p CPUShares httpd.service
CPUShares=

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

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

[root@CTU1000094955 ~]# systemctl set-property httpd.service CPUShares= 
# systemctl show -p CPUShares httpd.service
CPUShares=

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

[root@CTU1000094955 ~]# vi /etc/systemd/system/httpd.service.d/-CPUShares.conf 
[Service]
CPUShares=

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

[root@CTU1000094955 ~]# systemctl show httpd
Restart=no
NotifyAccess=none
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
WatchdogUSec=
WatchdogTimestampMonotonic=
StartLimitInterval=
StartLimitBurst=
StartLimitAction=none
FailureAction=none
... ... 中间省略部分内容

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

[root@CTU1000094955 ~]# systemd-analyze critical-chain firewalld.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. firewalld.service +380ms
?..basic.target @.903s
?..sockets.target @.903s
?..rpcbind.socket @.903s
?..sysinit.target @.895s
?..systemd-update-utmp.service @.877s +16ms
?..auditd.service @.738s +135ms
?..systemd-tmpfiles-setup.service @.661s +73ms
?..rhel-import-state.service @.526s +134ms
?..local-fs.target @.525s
?..run-user-.mount @.041s
?..local-fs-pre.target @.615s
?..systemd-tmpfiles-setup-dev.service @.519s +92ms
?..kmod-static-nodes.service @.473s +44ms
?..systemd-journald.socket
?..-.slice

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

[root@CTU1000094955 ~]# systemctl list-dependencies firewalld.service
firewalld.service
?.?..system.slice
?.?..basic.target
?. ?..alsa-restore.service
?. ?..alsa-state.service
?.?.....
?. ?..microcode.service
?. ?..rhel-autorelabel-mark.service
?. ?..rhel-autorelabel.service
?. ?..rhel-configure.service
?. ?..rhel-dmesg.service
?. ?..rhel-loadmodules.service
?. ?..paths.target
?. ?..slices.target
... .... 后面省略部分内容

30. 按等级列出控制组

[root@CTU1000094955 ~]# systemd-cgls
?.. /usr/lib/systemd/systemd --switched-root --system --deserialize
?..user.slice
?.?..user-.slice
?.?.?..session-c1.scope
?.?. ?.. gdm-session-worker [pam/gdm-launch-environment]
?.?. ?.. /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart --session gnome-initial-setup
?.?. ?.. /usr/bin/dbus-launch --exit-with-session /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart --session gnome-initial-setup
?.?. ?.. /bin/dbus-daemon --fork --print-pid --print-address --session
?.?. ?.. /usr/libexec/at-spi-bus-launcher
?.?. ?.. /bin/dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address
?.?. ?.. /usr/libexec/at-spi2-registryd --use-gnome-session
?.?. ?.. gnome-shell --mode=initial-setup
... ... 中间省略部分内容
?.?. ?.. /usr/libexec/ibus-engine-simple
?.?. ?.. gnome-keyring-daemon --unlock
?.?..user-.slice

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

[root@CTU1000094955 ~]# systemd-cgtop
Path Tasks %CPU Memory Input/s Output/s / - .8G - -
/system.slice/ModemManager.service - - - -
/system.slice/NetworkManager.service - - - -
/system.slice/abrt-oops.service - - - -
/system.slice/abrt-xorg.service - - - -
/system.slice/abrtd.service - - - -
/system.slice/accounts-daemon.service - - - -
/system.slice/alsa-state.service - - - -
/system.slice/atd.service - - - -
/system.slice/auditd.service - - - -

控制系统运行等级

32. 启动系统救援模式

[root@CTU1000094955 ~]# systemctl rescue
Broadcast message from root@tecmint on pts/(Wed2015--:: IST):
The system is going down to rescue mode NOW!

33. 进入紧急模式

[root@CTU1000094955 ~]# 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 intodefault mode.

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

  运行等级说明如下。

  • Runlevel 0 : 关闭系统  shutdown.target  
  • Runlevel 1 : 紧急?维护模式  emergency.target
  • Runlevel 2 : 救援?维护模式  rescure.target
  • Runlevel 3 : 多用户,无图形系统  multi-user.target
  • Runlevel 4 : 多用户,无图形系统  无
  • Runlevel 5 : 多用户,图形化系统  graphical.target
  • Runlevel 6 : 关闭并重启机器  无
[root@CTU1000094955 ~]# systemctl get-default
graphical.target

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

# systemctl isolate runlevel5.target

# systemctl isolate graphical.target [root@CTU1000094955 ~]# systemctl isolate runlevel5.target
PolicyKit daemon disconnected from the bus.
We are no longer a registered authentication agent.

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
# systemctl rescue  强制进入救援模式
# systemctl emergency  强制进入紧急救援模式
# systemctl poweroff  系统关机