📅  最后修改于: 2020-10-31 13:27:00             🧑  作者: Mango
systemd是在Linux上运行服务的新方式。 systemd具有取代的sysvinit 。 systemd为Linux带来了更快的启动时间,现在已成为管理Linux服务的标准方法。虽然稳定,但systemd仍在发展。
systemd作为一个初始化系统,用于管理引导Linux内核后需要更改状态的服务和守护程序。通过状态更改,可以应用启动,停止,重新加载和调整服务状态。
首先,让我们检查当前在服务器上运行的systemd的版本。
[centos@localhost ~]$ systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP
+GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[centos@localhost ~]$
从CentOS版本7开始,在撰写本文时已完全更新,系统版本219是当前的稳定版本。
我们还可以使用systemd-analyze分析上次服务器启动时间
[centos@localhost ~]$ systemd-analyze
Startup finished in 1.580s (kernel) + 908ms (initrd) + 53.225s (userspace) = 55.713s
[centos@localhost ~]$
当系统启动时间较慢时,我们可以使用systemd-analyze blame命令。
[centos@localhost ~]$ systemd-analyze blame
40.882s kdump.service
5.775s NetworkManager-wait-online.service
4.701s plymouth-quit-wait.service
3.586s postfix.service
3.121s systemd-udev-settle.service
2.649s tuned.service
1.848s libvirtd.service
1.437s network.service
875ms packagekit.service
855ms gdm.service
514ms firewalld.service
438ms rsyslog.service
436ms udisks2.service
398ms sshd.service
360ms boot.mount
336ms polkit.service
321ms accounts-daemon.service
使用systemd时,了解单位的概念很重要。单位是系统知道如何解释的资源。单位分为以下12种类型-
在大多数情况下,我们将使用.service作为目标单元。建议对其他类型进行进一步研究。因为只有.service单位才适用于启动和停止systemd服务。
每个单元均在位于以下位置的文件中定义-
/ lib / systemd / system-基本单元文件
/ etc / systemd / system-修改后的单位文件在运行时启动
要使用systemd ,我们需要非常熟悉systemctl命令。以下是systemctl最常用的命令行开关。
Switch | Action |
---|---|
-t | Comma separated value of unit types such as service or socket |
-a | Shows all loaded units |
–state | Shows all units in a defined state, either: load, sub, active, inactive, etc.. |
-H | Executes operation remotely. Specify Host name or host and user separated by @. |
systemctl [operation]
example: systemctl --state [servicename.service]
快速浏览一下我们包装盒上运行的所有服务。
[root@localhost rdc]# systemctl -t service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service loaded active running Accounts Service
alsa-state.service loaded active running Manage Sound Card State (restore and store)
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service loaded active exited Availability of block devices
bluetooth.service loaded active running Bluetooth service
chronyd.service loaded active running NTP client/server
首先,停止蓝牙服务。
[root@localhost]# systemctl stop bluetooth
[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service loaded inactive dead Bluetooth service
[root@localhost]#
如我们所见,蓝牙服务现在处于非活动状态。
重新启动蓝牙服务。
[root@localhost]# systemctl start bluetooth
[root@localhost]# systemctl --all -t service | grep bluetooth
bluetooth.service loaded active running Bluetooth service
[root@localhost]#
注意-我们没有指定bluetooth.service,因为隐含.service 。最好考虑将单元类型附加到我们要处理的服务中。因此,从这里开始,我们将使用.service扩展名来阐明我们正在处理服务单元操作。
可以对服务执行的主要操作是-
Start | Starts the service |
Stop | Stops a service |
Reload | Reloads the active configuration of a service w/o stopping it (like kill -HUP in system v init) |
Restart | Starts, then stops a service |
Enable | Starts a service at boot time |
Disable | Stops a service from automatically starting at run time |
以上操作主要用于以下情况-
Start | To bring a service up that has been put in the stopped state. |
Stop | To temporarily shut down a service (for example when a service must be stopped to access files locked by the service, as when upgrading the service) |
Reload | When a configuration file has been edited and we want to apply the new changes while not stopping the service. |
Restart | In the same scenario as reload, but the service does not support reload. |
Enable | When we want a disabled service to run at boot time. |
Disable | Used primarily when there is a need to stop a service, but it starts on boot. |
检查服务状态-
[root@localhost]# systemctl status network.service
network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: active (exited) since Sat 2017-01-14 04:43:48 EST; 1min 31s ago
Docs: man:systemd-sysv-generator(8)
Process: 923 ExecStart = /etc/rc.d/init.d/network start (code=exited, status = 0/SUCCESS)
localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking...
localhost.localdomain network[923]: Bringing up loopback interface: [ OK ]
localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.
[root@localhost]#
向我们显示网络服务的当前状态。如果我们想查看所有与网络相关的服务,我们可以使用-
[root@localhost]# systemctl --all -t service | grep -i network
network.service loaded active exited LSB: Bring up/
NetworkManager-wait-online.service loaded active exited Network Manager
NetworkManager.service loaded active running Network Manager
ntpd.service loaded inactive dead Network Time
rhel-import-state.service loaded active exited Import network
[root@localhost]#
对于那些熟悉sysinit管理服务方法的人,重要的是过渡到systemd 。 systemd是在Linux中启动和停止守护程序服务的新方法。