在上一章节中,介绍了有关於Linux作业系统上的process程序管理,以及取得主机上的CPU资讯等,本章节中,将要介绍背景服务的控制与管理。
systemd,就是当Linux作业系统启动的时候,第一个启动的背景服务(daemon),一般来说,包含了服务启动和服务管理。同时也在系统启动的後,启动了系统资源、背景服务器以及其他processes程序。process id为:1给systemd。
在systemd中,底下有几种不同种类的的service,每个种类的service用途与定义都不同,相关的种类如下面列表:
.service
,这个单位的类型被用来启动某一个背景服务(daemon)并挂到systemd底下,例如:web server网页服务器,httpd.service
。.socket
,这个通常用来IPC(inter-process communication,即程序之间的通讯)使用,比较像是短暂的服务的时候会启动,像是telnet,即为socket,而此类服务有RAM限制,因此会以暂存的服务方式启动。.path
,没事的时候,这个服务类型通常会在硬碟中休息,有事的时候再由systemd进行呼叫来进行使用。可以使用sudo systemctl list-units
来列出所有的服务,相关的指令执行後的结果讯息如下:
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File>
sys-devices-pci0000:00-0000:00:01.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged >
sys-devices-pci0000:00-0000:00:03.0-net-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet >
sys-devices-pci0000:00-0000:00:05.0-sound-card0.device loaded active plugged 82801AA AC'97 Audio Contr>
sys-devices-pci0000:00-0000:00:0d.0-ata3-host1-target1:0:0-1:0:0:0-block-sda-sda1.device loaded active plug>
sys-devices-pci0000:00-0000:00:0d.0-ata3-host1-target1:0:0-1:0:0:0-block-sda-sda2.device loaded active plug>
sys-devices-pci0000:00-0000:00:0d.0-ata3-host1-target1:0:0-1:0:0:0-block-sda.device loaded active plugged >
sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/ser>
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/ser>
sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/ser>
sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/ser>
sys-devices-virtual-block-dm\x2d0.device loaded active plugged /sys/devices/virtual/bloc>
sys-devices-virtual-block-dm\x2d1.device loaded active plugged /sys/devices/virtual/bloc>
sys-devices-virtual-net-virbr0.device loaded active plugged /sys/devices/virtual/net/>
sys-devices-virtual-net-virbr0\x2dnic.device loaded active plugged /sys/devices/virtual/net/>
sys-module-configfs.device loaded active plugged /sys/module/configfs
sys-module-fuse.device loaded active plugged /sys/module/fuse
sys-subsystem-net-devices-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet >
sys-subsystem-net-devices-virbr0.device loaded active plugged /sys/subsystem/net/device>
sys-subsystem-net-devices-virbr0\x2dnic.device loaded active plugged /sys/subsystem/net/device>
-.mount loaded active mounted Root Mount
boot.mount loaded active mounted /boot
dev-hugepages.mount loaded active mounted Huge Pages File System
dev-mqueue.mount loaded active mounted POSIX Message Queue File >
run-user-1000-gvfs.mount loaded active mounted /run/user/1000/gvfs
run-user-1000.mount loaded active mounted /run/user/1000
lines 1-27
从上述执行的明令结果来看,在systemd底下的服务单位列表示很多的,多到会让指令直接自动进入到阅读器的模式,最後一行有lines 1-27
之字样,就是列出第1到27行的讯息,要往下的话,则是按下「enter」控键即可以往下,若按下「q」键即可以离开读器模式,若是要筛选出其他的unit单位的类型,则可以加上--type
参数来加以筛选,下列指令执行执行就是筛选出所有有关於service单位类型的服务名称:
[rockylinux@workstation ~]$ sudo systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
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
chronyd.service loaded active running NTP client/server
colord.service loaded active running Manage, Install and Generate Color Profiles
crond.service loaded active running Command Scheduler
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
dracut-shutdown.service loaded active exited Restore /run/initramfs on shutdown
firewalld.service loaded active running firewalld - dynamic firewall daemon
gdm.service loaded active running GNOME Display Manager
gssproxy.service loaded active running GSSAPI Proxy Daemon
import-state.service loaded active exited Import network configuration from initramfs
irqbalance.service loaded active running irqbalance daemon
iscsi-shutdown.service loaded active exited Logout off all iSCSI sessions on shutdown
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device nodes for th>
ksm.service loaded active exited Kernel Samepage Merging
ksmtuned.service loaded active running Kernel Samepage Merging (KSM) Tuning Daemon
libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using d>
lvm2-pvscan@8:2.service loaded active exited LVM event activation on device 8:2
mcelog.service loaded active running Machine Check Exception Logging Daemon
ModemManager.service loaded active running Modem Manager
lines 1-27
若要观看一个指定的服务状态,可以使用status
这个动作,下列执行指令的结果与输出讯息如下:
[rockylinux@workstation ~]$ sudo systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-09-30 21:47:41 CST; 1h 23min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 997 (sshd)
Tasks: 1 (limit: 11262)
Memory: 4.3M
CGroup: /system.slice/sshd.service
└─997 /usr/sbin/sshd -D [email protected],[email protected],aes256-ct>
Sep 30 21:47:40 workstation systemd[1]: Starting OpenSSH server daemon...
Sep 30 21:47:41 workstation sshd[997]: Server listening on 0.0.0.0 port 22.
Sep 30 21:47:41 workstation sshd[997]: Server listening on :: port 22.
Sep 30 21:47:41 workstation systemd[1]: Started OpenSSH server daemon.
Sep 30 23:04:27 workstation sshd[2739]: Accepted password for rockylinux from 192.168.0.9 port 11203 ssh2
Sep 30 23:04:27 workstation sshd[2739]: pam_unix(sshd:session): session opened for user rockylinux by (uid=>
lines 1-17/17 (END)
指令有时候会像这样自动进入到阅读器的模式,若要退出阅读器模式,一样也是按下「q」键即可,若不要进入到此模式的话,则可以加上--no-pager
的参数,这样一来的话就不会自动进入到阅读器的模式了。相关的指令执行後的输出结果如下:
[rockylinux@workstation ~]$ sudo systemctl status sshd.service --no-pager
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-09-30 21:47:41 CST; 1h 25min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 997 (sshd)
Tasks: 1 (limit: 11262)
Memory: 4.3M
CGroup: /system.slice/sshd.service
└─997 /usr/sbin/sshd -D [email protected],[email protected],aes256-ct…
Sep 30 21:47:40 workstation systemd[1]: Starting OpenSSH server daemon...
Sep 30 21:47:41 workstation sshd[997]: Server listening on 0.0.0.0 port 22.
Sep 30 21:47:41 workstation sshd[997]: Server listening on :: port 22.
Sep 30 21:47:41 workstation systemd[1]: Started OpenSSH server daemon.
Sep 30 23:04:27 workstation sshd[2739]: Accepted password for rockylinux from 192.168.0.9 port 11203 ssh2
Sep 30 23:04:27 workstation sshd[2739]: pam_unix(sshd:session): session opened for user rockylinux b…(uid=0)
Hint: Some lines were ellipsized, use -l to show in full.
[rockylinux@workstation ~]$
sshd.service
是一种unit单位类型为服务的SSH server,作为远端连线所使用的,後面的章节会更详细的介绍这个功能,预设使用status
之动作来观看指定的服务状态,预设是观看单位类行为service的,因此若要观看这类型的,可以不需要加上.service
,因此执行指令的时候可以改成如下:
[rockylinux@workstation ~]$ sudo systemctl status sshd --no-pager
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-09-30 21:47:41 CST; 1h 28min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 997 (sshd)
Tasks: 1 (limit: 11262)
Memory: 4.3M
CGroup: /system.slice/sshd.service
└─997 /usr/sbin/sshd -D [email protected],[email protected],aes256-ct…
Sep 30 21:47:40 workstation systemd[1]: Starting OpenSSH server daemon...
Sep 30 21:47:41 workstation sshd[997]: Server listening on 0.0.0.0 port 22.
Sep 30 21:47:41 workstation sshd[997]: Server listening on :: port 22.
Sep 30 21:47:41 workstation systemd[1]: Started OpenSSH server daemon.
Sep 30 23:04:27 workstation sshd[2739]: Accepted password for rockylinux from 192.168.0.9 port 11203 ssh2
Sep 30 23:04:27 workstation sshd[2739]: pam_unix(sshd:session): session opened for user rockylinux b…(uid=0)
Hint: Some lines were ellipsized, use -l to show in full.
[rockylinux@workstation ~]$
输出的服务状态讯息中,栏位说明如下:
systemctl所指定查看服务的状态讯息会有以下这几种输出:
可以分别使用is-active
、is-enabled
以及is-failed
来确认目前指定的服务状态为何,下列以sshd.service这个来作范例,分别使用上述的三个指令来验证目前此SSHD服务之状态:
[rockylinux@workstation ~]$ sudo systemctl is-active sshd.service
active
[rockylinux@workstation ~]$ sudo systemctl is-failed sshd.service
active
[rockylinux@workstation ~]$ sudo systemctl is-enabled sshd.service
enabled
可以使用start
、stop
与restart
以及reload
来控制一个指定的服务,相关的动作说明如下:
start
就是让指定的服务启动。stop
就是让指定的服务停止。reload
是指让服务的相关设定档重新载入,并不会让服务重新启动。有一些服务没有reload
动作,要端看unit单位服务之设定档有没有填写reload
时候所使用的动作,相关的执行的方式如下:
[rockylinux@workstation ~]$ sudo yum install -y httpd
[sudo] password for rockylinux:
Last metadata expiration check: 1:01:27 ago on Thu 30 Sep 2021 10:36:25 PM CST.
Dependencies resolved.
============================================================================================================
Package Architecture Version Repository Size
============================================================================================================
Installing:
httpd x86_64 2.4.37-39.module+el8.4.0+571+fd70afb1 appstream 1.4 M
Installing dependencies:
apr x86_64 1.6.3-11.el8.1 appstream 124 k
apr-util x86_64 1.6.1-6.el8.1 appstream 104 k
httpd-filesystem noarch 2.4.37-39.module+el8.4.0+571+fd70afb1 appstream 37 k
httpd-tools x86_64 2.4.37-39.module+el8.4.0+571+fd70afb1 appstream 105 k
mod_http2 x86_64 1.15.7-3.module+el8.4.0+553+7a69454b appstream 153 k
rocky-logos-httpd noarch 84.5-8.el8 baseos 22 k
Installing weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8.1 appstream 23 k
apr-util-openssl x86_64 1.6.1-6.el8.1 appstream 26 k
Enabling module streams:
httpd 2.4
Transaction Summary
首先先把网页服务器套件给安装起来,接着安装好之後,可以执行下列的指令:
[rockylinux@workstation ~]$ sudo systemctl status httpd
● 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)
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ sudo systemctl is-enabled httpd
disabled
[rockylinux@workstation ~]$
利用status
动作可以看到httpd服务状态在预设是,关闭且不活跃的,而利用is-enabled
动作可以知道服务是disabled状态,意思是开机的时候,不会自动启动这个服务,若要自动启动服务,则可以使用下列的指令:
[rockylinux@workstation ~]$ sudo systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ sudo systemctl is-enabled httpd
enabled
[rockylinux@workstation ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-09-30 23:42:02 CST; 9s ago
Docs: man:httpd.service(8)
Main PID: 4889 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11262)
Memory: 17.2M
CGroup: /system.slice/httpd.service
├─4889 /usr/sbin/httpd -DFOREGROUND
├─4890 /usr/sbin/httpd -DFOREGROUND
├─4891 /usr/sbin/httpd -DFOREGROUND
├─4892 /usr/sbin/httpd -DFOREGROUND
└─4893 /usr/sbin/httpd -DFOREGROUND
Sep 30 23:42:01 workstation systemd[1]: Starting The Apache HTTP Server...
Sep 30 23:42:02 workstation httpd[4889]: AH00558: httpd: Could not reliably determine the server's fully qu>
Sep 30 23:42:02 workstation systemd[1]: Started The Apache HTTP Server.
Sep 30 23:42:02 workstation httpd[4889]: Server configured, listening on: port 80
lines 1-19/19 (END)
从上述的指令执行输出的讯息可以知道,目前Apache服务状态已经启用了,并会开机的时候自动启动,接着可以使用下列指令进行测试:
[rockylinux@workstation ~]$ sudo systemctl reload httpd
[rockylinux@workstation ~]$ sudo systemctl status httpd --no-pager
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-09-30 23:42:02 CST; 1min 23s ago
Docs: man:httpd.service(8)
Process: 5143 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 4889 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11262)
Memory: 27.1M
CGroup: /system.slice/httpd.service
├─4889 /usr/sbin/httpd -DFOREGROUND
├─5144 /usr/sbin/httpd -DFOREGROUND
├─5145 /usr/sbin/httpd -DFOREGROUND
├─5146 /usr/sbin/httpd -DFOREGROUND
└─5147 /usr/sbin/httpd -DFOREGROUND
Sep 30 23:42:01 workstation systemd[1]: Starting The Apache HTTP Server...
Sep 30 23:42:02 workstation httpd[4889]: AH00558: httpd: Could not reliably determine the server's f…message
Sep 30 23:42:02 workstation systemd[1]: Started The Apache HTTP Server.
Sep 30 23:42:02 workstation httpd[4889]: Server configured, listening on: port 80
Sep 30 23:43:15 workstation systemd[1]: Reloading The Apache HTTP Server.
Sep 30 23:43:15 workstation httpd[5143]: AH00558: httpd: Could not reliably determine the server's f…message
Sep 30 23:43:15 workstation systemd[1]: Reloaded The Apache HTTP Server.
Sep 30 23:43:15 workstation httpd[4889]: Server configured, listening on: port 80
Hint: Some lines were ellipsized, use -l to show in full.
[rockylinux@workstation ~]$ sudo systemctl restart httpd
[sudo] password for rockylinux:
[rockylinux@workstation ~]$ sudo systemctl status httpd --no-pager
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-10-01 00:26:10 CST; 2s ago
Docs: man:httpd.service(8)
Process: 5143 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 6186 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 11262)
Memory: 26.3M
CGroup: /system.slice/httpd.service
├─6186 /usr/sbin/httpd -DFOREGROUND
├─6188 /usr/sbin/httpd -DFOREGROUND
├─6189 /usr/sbin/httpd -DFOREGROUND
├─6190 /usr/sbin/httpd -DFOREGROUND
└─6191 /usr/sbin/httpd -DFOREGROUND
Oct 01 00:26:10 workstation systemd[1]: httpd.service: Succeeded.
Oct 01 00:26:10 workstation systemd[1]: Stopped The Apache HTTP Server.
Oct 01 00:26:10 workstation systemd[1]: Starting The Apache HTTP Server...
Oct 01 00:26:10 workstation httpd[6186]: AH00558: httpd: Could not reliably determine the server's f…message
Oct 01 00:26:10 workstation systemd[1]: Started The Apache HTTP Server.
Oct 01 00:26:10 workstation httpd[6186]: Server configured, listening on: port 80
Hint: Some lines were ellipsized, use -l to show in full.
[rockylinux@workstation ~]$
从上述的执行指令可以发现,reload
与restart
之机制不同,reload就是只会将设定档重新载入,因此main process id仍是一样的,但是当使用了restart
动作之後,则会整个Apache server服务进行重新启动,这时候使用status
才查看服务状态的时候,则会发现main process id已经改变了,那在实务上,有reload的动作可以用,就尽量要用,若用restart则会让服务有一瞬间会中断。
若不晓得此服务有没有支援reload
之动作,则可以使用reload-or-restart
来使用,这个动作会优先以reload
进行尝试,若没有的话才会使用restart
,相关的执行指令输出的讯息如下:
[rockylinux@workstation ~]$ sudo systemctl reload-or-restart httpd
[rockylinux@workstation ~]$ sudo systemctl status httpd --no-pager
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-10-01 00:26:10 CST; 12min ago
Docs: man:httpd.service(8)
Process: 6658 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 6186 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11262)
Memory: 27.1M
CGroup: /system.slice/httpd.service
├─6186 /usr/sbin/httpd -DFOREGROUND
├─6660 /usr/sbin/httpd -DFOREGROUND
├─6661 /usr/sbin/httpd -DFOREGROUND
├─6662 /usr/sbin/httpd -DFOREGROUND
└─6663 /usr/sbin/httpd -DFOREGROUND
Oct 01 00:26:10 workstation systemd[1]: httpd.service: Succeeded.
Oct 01 00:26:10 workstation systemd[1]: Stopped The Apache HTTP Server.
Oct 01 00:26:10 workstation systemd[1]: Starting The Apache HTTP Server...
Oct 01 00:26:10 workstation httpd[6186]: AH00558: httpd: Could not reliably determine the server's f…message
Oct 01 00:26:10 workstation systemd[1]: Started The Apache HTTP Server.
Oct 01 00:26:10 workstation httpd[6186]: Server configured, listening on: port 80
Oct 01 00:38:26 workstation systemd[1]: Reloading The Apache HTTP Server.
Oct 01 00:38:26 workstation httpd[6658]: AH00558: httpd: Could not reliably determine the server's f…message
Oct 01 00:38:26 workstation systemd[1]: Reloaded The Apache HTTP Server.
Oct 01 00:38:26 workstation httpd[6186]: Server configured, listening on: port 80
Hint: Some lines were ellipsized, use -l to show in full.
[rockylinux@workstation ~]$
有时候服务之间可能会有冲突,为了不要让服务之间在启动的时候会互相干扰,这个时候就可以使用mask之动作方式来将指定的服务给永久关闭,当服务被mask的时候,则其他的动作,如:enable、status以及start等将不会有作用,若要解除mask的状态的话,则可以使用unmask来做到,相关的执行指令输出的讯息如下:
[rockylinux@workstation ~]$ sudo systemctl mask httpd.service
[sudo] password for rockylinux:
Created symlink /etc/systemd/system/httpd.service → /dev/null.
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ sudo systemctl enable --now httpd.service
Failed to enable unit: Unit file /etc/systemd/system/httpd.service is masked.
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ sudo systemctl unmask httpd.service
Removed /etc/systemd/system/httpd.service.
[rockylinux@workstation ~]$ sudo systemctl enable --now httpd.service
[rockylinux@workstation ~]$
若要知道一个服务启动之後,所相依其服务的列表,则可以使用list-dependencies
之动作来做到,相关的执行指令之输出讯息如下:
[rockylinux@workstation ~]$ sudo systemctl list-dependencies sshd
sshd.service
● ├─system.slice
● ├─sshd-keygen.target
● │ ├─[email protected]
● │ ├─[email protected]
● │ └─[email protected]
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● ├─dracut-shutdown.service
● ├─import-state.service
● ├─iscsi-onboot.service
● ├─kmod-static-nodes.service
● ├─ldconfig.service
● ├─loadmodules.service
● ├─lvm2-lvmpolld.socket
● ├─lvm2-monitor.service
● ├─multipathd.service
● ├─nis-domainname.service
● ├─plymouth-read-write.service
● ├─plymouth-start.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
lines 1-27
sudo yum install -y httpd
指令将Apache server安装起来,这边照着做就好,之後章节会做yum指令的介绍。systemd
指令查看httpd服务的状态。systemd
指令将httpd服务重新启动设定档,不要让此服务中断。systemd
指令将httpd服务设定成自动开机会启动。
IT 这行真的学无止境,理想上可以主动追技术,比被技术追上好 “I am free and tha...
大家跟Large是否有点熟呀~~~就什麽大什麽大的嘛~~~ 喂~~但这里说的是Excel,作者呀~...
推荐的Vim、VSCodeVim的参考资源 [系列文目录] 这篇文章推荐几个Vim与VSCodeVi...
848. Shifting Letters https://leetcode.com/problem...
昨天制作了按钮由下往上的动画,今天要加上变化,让昨天的动画看起来不那麽单调。 加入< KeyF...