Systemctl是一个systemd工具,它负责控制systemd系统和服务管理程序。Systemd是一个系统管理守护进程,工具和库的集合,它用作替换System V init守护进程。Systemd功能为用于类Unix系统的中心管理和配置平台。
在Linux中,在除了少数例外的大部分标准Linux发行版中已经实现了生态Systemd。此文目标为介绍在运行systemd的系统上"如何控制系统和服务"。
1、首先检测在你的系统上是否安装了systemd,以及当前安装的systemd是说明版本。
[root@main-machine blctrl]# systemctl --version
systemd 239 (239-58.el8)
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
从以上示例中,我们安装了systemd 239版本。
2、检测systemd和systemctl的二进制文件和库被安装在哪里。
[root@main-machine blctrl]# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
[root@main-machine blctrl]# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
3、检测systemd是否正在运行。
[root@main-machine blctrl]# ps -eaf | grep [Ss]ystemd
root 1 0 0 Jul12 ? 00:00:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 16
root 1065 1 0 Jul12 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 1109 1 0 Jul12 ? 00:00:00 /usr/lib/systemd/systemd-udevd
dbus 1322 1 0 Jul12 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 1436 1 0 Jul12 ? 00:00:00 /usr/lib/systemd/systemd-logind
root 7274 1 0 Jul12 ? 00:00:00 /usr/lib/systemd/systemd --user
blctrl 47230 1 0 16:03 ? 00:00:00 /usr/lib/systemd/systemd --user
注意:systemd是以父守护进程(PID=1)正在运行。在以上示例中,ps命令中带(-e)选择所有进程,(-a)选择除了会话组长外的所有进程,(-f)用于完全格式化列出(即是:-eaf)。
4、分析systemd引导过程
[root@main-machine blctrl]# systemd-analyze
Startup finished in 1.977s (kernel) + 2.043s (initrd) + 18.805s (userspace) = 22.826s
multi-user.target reached after 2.849s in userspace
5、分析引导时分割进程占用的时间
[root@main-machine blctrl]# systemd-analyze blame
10.291s kdump.service
6.353s NetworkManager-wait-online.service
1.146s dracut-initqueue.service
687ms tuned.service
565ms firewalld.service
475ms [email protected]
434ms systemd-logind.service
...
6、分析引导时关键链
[root@main-machine blctrl]# 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.
multi-user.target @2.849s
└─tuned.service @2.161s +687ms
└─network.target @2.157s
└─NetworkManager.service @2.127s +28ms
└─network-pre.target @2.126s
└─firewalld.service @1.551s +565ms
└─polkit.service @1.418s +130ms
└─basic.target @1.409s
└─sockets.target @1.409s
└─dbus.socket @1.409s
└─sysinit.target @1.408s
└─systemd-update-done.service @1.398s +9ms
└─ldconfig.service @1.211s +185ms
└─local-fs.target @1.208s
└─home.mount @1.186s +21ms
└─dev-mapper-rl\x2dhome.device @1.182s
重要:systemctl以单元接受服务(.service),挂载点(.mount),套接字(.socket)和设备(.device)。
7、列出所有可用的单元
[root@main-machine blctrl]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
-.mount generated
boot.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
home.mount generated
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
...
8、列出所有正在运行的单元
[root@main-machine blctrl]# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Format>
sys-devices-pci0000:00-0000:00:11.5-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged WDC_WDS100T2B0A 1
sys-devices-pci0000:00-0000:00:11.5-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged WDC_WDS100T2B0A 2
sys-devices-pci0000:00-0000:00:11.5-ata1-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged WDC_WDS100T2B0A
sys-devices-pci0000:00-0000:00:11.5-ata5-host4-target4:0:0-4:0:0:0-block-sr0.device loaded active plugged DVDRW_GUD1N
...
9、列出所有出错的单元
[root@main-machine blctrl]# systemctl --failed
0 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@main-machine blctrl]# systemctl is-enabled crond.service
enabled
11、检测一个单元或服务是否正在运行?
[root@main-machine blctrl]# 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 Tue 2022-07-12 21:24:59 CST; 1 day 19h ago
Docs: man:firewalld(1)
Main PID: 1426 (firewalld)
Tasks: 3 (limit: 821235)
Memory: 33.2M
CGroup: /system.slice/firewalld.service
└─1426 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
1、列出所有服务(包括使能的或禁用的)
[root@main-machine blctrl]# systemctl list-unit-files --type=service
UNIT FILE STATE
arp-ethers.service disabled
atd.service enabled
auditd.service enabled
[email protected] enabled
blk-availability.service disabled
bolt.service static
[email protected] static
chrony-wait.service disabled
chronyd.service disabled
cockpit-motd.service static
cockpit-wsinstance-http.service static
[email protected] static
[email protected] static
cockpit.service
...
2、在Linux中如何启动、重启、停止,重载以及检查一个服务的状态(httpd.service)
[root@main-machine blctrl]# systemctl start httpd.service
[root@main-machine blctrl]# systemctl restart httpd.service
[root@main-machine blctrl]# systemctl reload httpd.service
[root@main-machine blctrl]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2022-07-14 17:20:58 CST; 27s ago
Docs: man:httpd.service(8)
Process: 50890 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 50663 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 821235)
Memory: 42.4M
CGroup: /system.slice/httpd.service
├─50663 /usr/sbin/httpd -DFOREGROUND
├─50891 /usr/sbin/httpd -DFOREGROUND
├─50892 /usr/sbin/httpd -DFOREGROUND
├─50893 /usr/sbin/httpd -DFOREGROUND
└─50894 /usr/sbin/httpd -DFOREGROUND
...
[root@main-machine blctrl]# systemctl stop httpd.service
注意:当我们对systemctl使用像start, restart, stop和reload命令时,我们在终端上将得不到任何输出,仅status命令将打印输出。
3、如何激活一个服务并且在引导时使能或禁用一个服务(在引导时自动启动服务)
[root@main-machine blctrl]# systemctl is-active httpd.service
inactive
[root@main-machine blctrl]# systemctl enable httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@main-machine blctrl]# systemctl disable httpd.service
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
4、如何屏蔽(使不能启动)或者解除屏蔽一个服务(httpd.service)
[root@main-machine blctrl]# systemctl mask httpd.service
Created symlink /etc/systemd/system/httpd.service → /dev/null.
[root@main-machine blctrl]# systemctl unmask httpd.service
Removed /etc/systemd/system/httpd.service.
5、如何使用systemctl命令杀死一个服务
[root@main-machine blctrl]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2022-07-14 17:31:55 CST; 4s ago
Docs: man:httpd.service(8)
Main PID: 51259 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 821235)
Memory: 40.2M
CGroup: /system.slice/httpd.service
├─51259 /usr/sbin/httpd -DFOREGROUND
├─51260 /usr/sbin/httpd -DFOREGROUND
├─51261 /usr/sbin/httpd -DFOREGROUND
├─51262 /usr/sbin/httpd -DFOREGROUND
└─51263 /usr/sbin/httpd -DFOREGROUND
...
[root@main-machine blctrl]# systemctl kill httpd.service
[root@main-machine blctrl]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd.service(8)
...
1、列出所有系统挂载点
[root@main-machine blctrl]# systemctl list-unit-files --type=mount
UNIT FILE STATE
-.mount generated
boot.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
home.mount generated
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
10 unit files listed.
2、如何在系统上挂载、卸载,重新挂载,重新装载系统挂载点以及检查挂载点状态
[root@main-machine blctrl]# systemctl start tmp.mount
[root@main-machine blctrl]# systemctl stop tmp.mount
[root@main-machine blctrl]# systemctl restart tmp.mount
[root@main-machine blctrl]# systemctl reload tmp.mount
[root@main-machine blctrl]# systemctl status tmp.mount
● tmp.mount - Temporary Directory (/tmp)
Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
Active: active (mounted) since Thu 2022-07-14 19:32:45 CST; 20s ago
Where: /tmp
What: tmpfs
Docs: man:hier(7)
https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Tasks: 0 (limit: 821235)
Memory: 4.0K
CGroup: /system.slice/tmp.mount
3、如何在引导时激活、使能或者禁用一个挂载点(系统引导时自动挂载)
[root@main-machine blctrl]# systemctl is-active tmp.mount
active
[root@main-machine blctrl]# systemctl enable tmp.mount
Created symlink /etc/systemd/system/local-fs.target.wants/tmp.mount → /usr/lib/systemd/system/tmp.mount.
[root@main-machine blctrl]# systemctl disable tmp.mount
Removed /etc/systemd/system/local-fs.target.wants/tmp.mount.
4、如何在Linux中屏蔽(使不可能启动)或者解除屏蔽一个挂载点
[root@main-machine blctrl]# systemctl mask tmp.mount
Created symlink /etc/systemd/system/tmp.mount → /dev/null.
[root@main-machine blctrl]# systemctl unmask tmp.mount
Removed /etc/systemd/system/tmp.mount.
1、列出所有可用的系统套接字
[root@main-machine blctrl]# systemctl list-unit-files --type=socket
UNIT FILE STATE
cockpit-wsinstance-http.socket static
cockpit-wsinstance-https-factory.socket static
[email protected] static
cockpit.socket disabled
dbus.socket static
dm-event.socket enabled
httpd.socket disabled
lvm2-lvmpolld.socket enabled
sshd.socket disabled
sssd-autofs.socket disabled
sssd-kcm.socket enabled
sssd-nss.socket disabled
sssd-pac.socket disabled
sssd-pam-priv.socket disabled
sssd-pam.socket disabled
sssd-ssh.socket disabled
sssd-sudo.socket disabled
syslog.socket static
systemd-coredump.socket static
systemd-initctl.socket static
systemd-journald-audit.socket static
systemd-journald-dev-log.socket static
systemd-journald.socket static
systemd-rfkill.socket static
systemd-udevd-control.socket static
systemd-udevd-kernel.socket static
26 unit files listed.
2、如何在Linux中启动,重启、停止、重新装载和检查一个套接字的状态(例如:cups.socket)
[root@main-machine blctrl]# systemctl start cups.socket
[root@main-machine blctrl]# systemctl restart cups.socket
[root@main-machine blctrl]# systemctl reload cups.socket
Failed to reload cups.socket: Job type reload is not applicable for unit cups.socket.
[root@main-machine blctrl]# systemctl status cups.socket
● cups.socket - CUPS Scheduler
Loaded: loaded (/usr/lib/systemd/system/cups.socket; enabled; vendor preset: enabled)
Active: active (listening) since Thu 2022-07-14 19:45:18 CST; 37s ago
Listen: /var/run/cups/cups.sock (Stream)
CGroup: /system.slice/cups.socket
Jul 14 19:45:18 main-machine systemd[1]: Listening on CUPS Scheduler.
[root@main-machine blctrl]# systemctl stop cups.socket
3、如何在引导时激活一个套接字并且使能或者禁用(在系统引导时自动启动套接字)
[root@main-machine blctrl]# systemctl is-active cups.socket
inactive
[root@main-machine blctrl]# systemctl enable cups.socket
[root@main-machine blctrl]# systemctl disable cups.socket
Removed /etc/systemd/system/sockets.target.wants/cups.socket.
4、如何屏蔽(使不可能启动)或者屏蔽一个陶杰字(cups.socket)
[root@main-machine blctrl]# systemctl mask cups.socket
Created symlink /etc/systemd/system/cups.socket → /dev/null.
[root@main-machine blctrl]# systemctl unmask cups.socket
Removed /etc/systemd/system/cups.socket.
1、获取一个服务的当前CPU份额
[root@main-machine blctrl]# systemctl show -p CPUShares httpd.service
CPUShares=[not set]
注意:默认每个服务有一个CPUShare=1024。你可以增加或者减少一个进程的CU份额。
2、限制一个服务(httpd.service)的CPU份额为2000 CPUShares
[root@main-machine blctrl]# systemctl set-property httpd.service CPUShares=2000
[root@main-machine blctrl]# systemctl show -p CPUShares httpd.service
CPUShares=2000
注意:当你为一个服务设置CPUSshare时,用这个服务名称(httpd.service.d)创建了一个目录,其包含了一个文件50-CPUShares.conf,这个文件包含了CPUShare限制信息。你可以查看这个文件:
cat /etc/systemd/system.control/httpd.service.d/50-CPUShares.conf
# This is a drop-in unit file extension, created via "systemctl set-property"
# or an equivalent operation. Do not edit.
[Service]
CPUShares=2000
3、检查一个服务的所有配置详情。
[root@main-machine blctrl]# systemctl show httpd Type=notify
Restart=no
NotifyAccess=main
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
RuntimeMaxUSec=infinity
WatchdogUSec=0
WatchdogTimestamp=Thu 2022-07-14 19:52:59 CST
WatchdogTimestampMonotonic=167286018051
PermissionsStartOnly=no
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
MainPID=53178
4、对一个服务(httpd)分析关键链
[root@main-machine blctrl]# 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 +37ms
└─network.target @2.157s
└─NetworkManager.service @2.127s +28ms
└─network-pre.target @2.126s
└─firewalld.service @1.551s +565ms
└─polkit.service @1.418s +130ms
└─basic.target @1.409s
└─sockets.target @1.409s
└─dbus.socket @1.409s
└─sysinit.target @1.408s
└─systemd-update-done.service @1.398s +9ms
└─ldconfig.service @1.211s +185ms
└─local-fs.target @1.208s
└─home.mount @1.186s +21ms
└─dev-mapper-rl\x2dhome.device @1.182s
5、获取一个服务的依赖列表(httpd)
[root@main-machine blctrl]# systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─httpd-init.service
● ├─system.slice
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● ├─dracut-shutdown.service
● ├─import-state.service
● ├─kmod-static-nodes.service
● ├─ldconfig.service
● ├─loadmodules.service
● ├─lvm2-lvmpolld.socket
● ├─lvm2-monitor.service
● ├─nis-domainname.service
● ├─proc-sys-fs-binfmt_misc.automount
● ├─selinux-autorelabel-mark.service
● ├─sys-fs-fuse-connections.mount
● ├─sys-kernel-config.mount
● ├─sys-kernel-debug.mount
● ├─systemd-ask-password-console.path
● ├─systemd-binfmt.service
● ├─systemd-firstboot.service
● ├─systemd-hwdb-update.service
● ├─systemd-journal-catalog-update.service
● ├─systemd-journal-flush.service
● ├─systemd-journald.service
● ├─systemd-machine-id-commit.service
● ├─systemd-modules-load.service
● ├─systemd-random-seed.service
● ├─systemd-sysctl.service
● ├─systemd-sysusers.service
● ├─systemd-tmpfiles-setup-dev.service
● ├─systemd-tmpfiles-setup.service
● ├─systemd-udev-trigger.service
● ├─systemd-udevd.service
● ├─systemd-update-done.service
● ├─systemd-update-utmp.service
● ├─cryptsetup.target
● ├─local-fs.target
● │ ├─-.mount
● │ ├─boot.mount
● │ ├─home.mount
● │ └─systemd-remount-fs.service
● └─swap.target
● └─dev-mapper-rl\x2dswap.swap
6、按层级列出控制组
[root@main-machine blctrl]# systemd-cgls
Control group /:
-.slice
├─user.slice
│ ├─user-0.slice
│ │ ├─session-1.scope
│ │ │ ├─1500 login -- root
│ │ │ └─7284 -bash
│ │ └─[email protected]
│ │ └─init.scope
│ │ ├─7274 /usr/lib/systemd/systemd --user
│ │ └─7277 (sd-pam)
...
7、根据CPU,内存,输入和输出列出控制组
[root@main-machine blctrl]# systemd-cgtop
Control Group Tasks %CPU Memory Input/s Output/s
/ 623 - 3.5G - -
/init.scope 1 - 36.1M - -
/system.slice 254 - 499.7M - -
/system.slice/NetworkManager.service 3 - 9.6M - -
/system.slice/atd.service 1 - 412.0K - -
/system.slice/auditd.service 2 - 3.7M - -
/system.slice/boot.mount - - 4.0K - -
/system.slice/crond.service 1 - 1.0M - -
1、如何启动一个系统救援模式
[root@main-machine blctrl]# systemctl rescue
2、如何进入紧急模式
[root@main-machine blctrl]# systemctl emergency
3、列出当前在用的运行级别
[root@main-machine blctrl]# systemctl get-default
multi-user.target
4、如何启动运行级别5,即图形模式
[root@main-machine blctrl]# systemctl isolate runlevel5.target
或
[root@main-machine blctrl]# systemctl isolate graphical.target
5、如何启动运行级别3, 即多用户模式
[root@main-machine blctrl]# systemctl isolate multi-user.target
或
[root@main-machine blctrl]# systemctl isolate runlevel3.target
6、如何设置多用户模式或图形模式为默认运行级别
[root@main-machine blctrl]# systemctl set-default graphical.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target.
7、如何重启、暂停,休眠或者使系统进入混合睡眠
# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hyrid-sleep
运行级别以及对应做什么:
可以使用systemctl命令行工具控制systemd系统和服务管理程序。你使你能够通过SSH协议管理本地或者在一台远程Linux机器上的systemd。
我们将在这里展示如何通过一个SSH会话管理在一台远程Linux机器上的systemd系统和服务管理程序。
要连接一个远程服务器,按如下运行带有--host或-H标记的systemctl。在以下命令中,我们以root用户连接到远程服务器,并且status使systemctl工具的子命令,用于查看远程Linux服务的httpd状态。
[blctrl@areadetector ~]$ systemctl --host [email protected] status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system.control/httpd.service.d
└─50-CPUShares.conf
Active: active (running) since Thu 2022-07-14 20:58:42 CST; 7h left
Docs: man:httpd.service(8)
Main PID: 54132
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 821235)
Memory: 39.8M
CGroup: /system.slice/httpd.service
├─54132 /usr/sbin/httpd -DFOREGROUND
├─54147 /usr/sbin/httpd -DFOREGROUND
├─54148 /usr/sbin/httpd -DFOREGROUND
├─54149 /usr/sbin/httpd -DFOREGROUND
└─54150 /usr/sbin/httpd -DFOREGROUND
# 或
[blctrl@areadetector ~]$ systemctl -H [email protected] status httpd.service
● httpd.service - The Apache HTTP Server
类似地,你也可以按以下显示启动、停止或重启远程systemd服务
[blctrl@areadetector ~]$ systemctl --host [email protected] start httpd.service
[blctrl@areadetector ~]$ systemctl --host [email protected] stop httpd.service
[blctrl@areadetector ~]$ systemctl --host [email protected] restart httpd.service