systemctl 命令详解及使用教程

在本教程中,我们将向您介绍在运行systemd的linux系统上如何使用systemctl命令工具有效的控制系统和服务.
Systemctl 介绍Systemctl是一个systemd工具 , 主要负责控制systemd系统和服务管理器 。
Systemd是一个系统管理守护进程、工具和库的集合 , 用于取代System V初始进程 。Systemd的功能是用于集中管理和配置类UNIX系统 。
在Linux生态系统中 , Systemd被部署到了大多数的标准Linux发行版中 , 只有为数不多的几个发行版尚未部署 。Systemd通常是所有其它守护进程的父进程 , 但并非总是如此 。
开始Systemd和Systemctl 基础工具之旅01、首先检查系统上是否安装了systemd以及当前安装的Systemd的版本是什么?
# systemd --versionsystemd 215+PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -AppARMOR从上面的例子可以清楚地看出 , 我们已经安装了systemd 215版本 。
02.检查systemd和systemctl的二进制文件和库的安装位置 。
# whereis systemd systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz# whereis systemctlsystemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz03.检查systemd是否正在运行 。
# ps -eaf | grep [s]ystemdroot100 16:27 ?00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23root44410 16:27 ?00:00:00 /usr/lib/systemd/systemd-journaldroot46910 16:27 ?00:00:00 /usr/lib/systemd/systemd-udevdroot55510 16:27 ?00:00:00 /usr/lib/systemd/systemd-loginddbus55610 16:27 ?00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

注意:systemd作为父守护进程运行(PID = 1) 。在上面的命令ps中使用(-e)选择所有进程 , ( - a)选择除会话前导之外的所有进程和(-f)选择完整格式列表(即-eaf) 。
另请注意上面示例中的方括号以及其他示例 。Square Bracket表达式是grep的字符类模式匹配的一部分 。
04.分析systemd启动过程
# systemd-analyzeStartup finished in 487ms (kernel) + 2.776s (initrd) + 20.229s (userspace) = 23.493s05.分析每个进程在引导时花费的时间
# systemd-analyze blame8.565s mariadb.service7.991s webmin.service6.095s postfix.service4.311s httpd.service3.926s firewalld.service3.780s kdump.service3.238s tuned.service1.712s.NETwork.service1.394s lvm2-monitor.service1.126s systemd-logind.service....06.分析启动时的关键链
# systemd-analyze critical-chainThe 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.multi-user.target @20.222s└─mariadb.service @11.657s +8.565s└─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-byx2duuid-79f594adx2da332x2d4730x2dbb5fx2d85d19608096└─dev-disk-byx2duuid-79f594adx2da332x2d4730x2dbb5fx2d85d196080964.device @4
重要:Systemctl接受服务(.service) , 挂载点(.mount) , 套接字(.socket)和设备(.device)作为单位 。
07、列出所有可用的单位
# systemctl list-unit-files
UNIT FILESTATEproc-sys-fs-binfmt_misc.automountstaticdev-hugepages.mountstaticdev-mqueue.mountstaticproc-sys-fs-binfmt_misc.mountstaticsys-fs-fuse-connections.mountstaticsys-kernel-config.mountstaticsys-kernel-debug.mountstatictmp.mountdisabledbrandbot.pathdisabled.....08、列出所有运行单元
# systemctl list-unitsUNITLOADACTIVE SUBDESCRIPTIONproc-sys-fs-binfmt_misc.automountloaded active waitingArbitrary Executable File Formats File Systesys-devices-pc...0-1:0:0:0-block-sr0.device loaded active pluggedVBOX_CD-ROMsys-devices-pc...:00:03.0-net-enp0s3.device loaded active pluggedPRO/1000 MT Desktop Adaptersys-devices-pc...00:05.0-sound-card0.device loaded active plugged82801AA AC'97 Audio Controllersys-devices-pc...:0:0-block-sda-sda1.device loaded active pluggedVBOX_HARDDISKsys-devices-pc...:0:0-block-sda-sda2.device loaded active pluggedLVM PV Qzyo3l-qYaL-uRUa-Cjuk-pljo-qKtX-VgBQ8sys-devices-pc...0-2:0:0:0-block-sda.device loaded active pluggedVBOX_HARDDISKsys-devices-pl...erial8250-tty-ttyS0.device loaded active plugged/sys/devices/platform/serial8250/tty/ttyS0sys-devices-pl...erial8250-tty-ttyS1.device loaded active plugged/sys/devices/platform/serial8250/tty/ttyS1sys-devices-pl...erial8250-tty-ttyS2.device loaded active plugged/sys/devices/platform/serial8250/tty/ttyS2sys-devices-pl...erial8250-tty-ttyS3.device loaded active plugged/sys/devices/platform/serial8250/tty/ttyS3sys-devices-virtual-block-dmx2d0.deviceloaded active plugged/sys/devices/virtual/block/dm-0sys-devices-virtual-block-dmx2d1.deviceloaded active plugged/sys/devices/virtual/block/dm-1sys-module-configfs.deviceloaded active plugged/sys/module/configfs...


推荐阅读