控制服务和守护进程-systemctl
控制服务和守护进程-systemctl
Linux当中运行的第一个进程:systemd
系统启动过程:
1、开机自检
2、MBR引导
3、GRUB菜单
4、加载linux内核
5、运行systemd进程
一、简介
systemd:
并行启动
系统启动和服务器进程由systemd系统和服务管理器进程管理
此程序可以在启动时和运行中的系统上激活系统资源、服务器守护进程和其他进程
守护进程是指执行各种任务时在后台等待或运行的程序
系统上的一个服务由一个或者多个守护进程组成
二、Systemd的新功能
并行化功能,它可以提高系统的启动速度
按需启动守护进程,而不需要单独的服务
自动服务依赖关系管理可以防止长时间的超时,例如在网络不可用时不启动网络服务
利用Linux控制组一起追踪相关进程的方式
三、systemctl管理systemd单元
1、单元类型—systemctl -t help(单元服务的后缀名)
.service —系统服务
.socket —进程间通信套接字
.path
.target ----
[root@stw ~]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
2、列出单元
systemctl 查看系统启动时所有单元的状态
[root@stw lib]# systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Aut
sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged VMware_Virtual
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged VMware_Virtual
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged VMware_Virtual
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active plugged VMware_Virtual
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda4.device loaded active plugged VMware_Virtual
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda5.device loaded active plugged LVM PV Csyf7D-
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda6.device loaded active plugged LVM PV 3OKM0N-
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda7.device loaded active plugged LVM PV LwZKD2-
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda8.device loaded active plugged LVM PV D2Pf7h-
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged VMware_Virtual_S
sys-devices-pci0000:00-0000:00:11.0-0000:02:00.0-usb2-2\x2d2-2\x2d2.1-2\x2d2.1:1.0-bluetooth-hci0-rfkill0.device loaded ac
sys-devices-pci0000:00-0000:00:11.0-0000:02:00.0-usb2-2\x2d2-2\x2d2.1-2\x2d2.1:1.0-bluetooth-hci0.device loaded active plu
sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded active plugged 82545EM Gigabit Ethernet Control
sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged ES1371/ES1373 / Creative Labs
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS1
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-00:05-tty-ttyS0.device loaded active plugged /sys/devices/pnp0/00:05/tty/ttyS0
sys-devices-virtual-block-dm\x2d0.device loaded active plugged /sys/devices/virtual/block/dm-0
sys-devices-virtual-block-dm\x2d1.device loaded active plugged /sys/devices/virtual/block/dm-1
sys-devices-virtual-net-virbr0.device loaded active plugged /sys/devices/virtual/net/virbr0
sys-devices-virtual-net-virbr0\x2dnic.device loaded active plugged /sys/devices/virtual/net/virbr0-nic
sys-module-configfs.device loaded active plugged /sys/module/configfs
sys-subsystem-bluetooth-devices-hci0.device loaded active plugged Virtual_Bluetooth_Adapter
sys-subsystem-net-devices-ens33.device loaded active plugged 82545EM Gigabit Ethernet Controller (Copper) (PRO
sys-subsystem-net-devices-virbr0.device loaded active plugged /sys/subsystem/net/devices/virbr0
sys-subsystem-net-devices-virbr0\x2dnic.device loaded active plugged /sys/subsystem/net/devices/virbr0-nic
sys-subsystem-rfkill-devices-rfkill0.device loaded active plugged Virtual_Bluetooth_Adapter
-.mount loaded active mounted /
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 System
lv0.mount loaded active mounted /lv0
lv1.mount loaded active mounted /lv1
proc-fs-nfsd.mount loaded active mounted NFSD configuration filesystem
run-user-0.mount loaded active mounted /run/user/0
run-user-42.mount loaded active mounted /run/user/42
sys-kernel-config.mount loaded active mounted Configuration File System
sys-kernel-debug.mount loaded active mounted Debug File System
var-lib-nfs-rpc_pipefs.mount loaded active mounted RPC Pipe File System
cups.path loaded active waiting CUPS Printer Service Spool
systemd-ask-password-plymouth.path loaded active waiting Forward Password Requests to Plymouth Directory W
systemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watch
session-1.scope loaded active running Session 1 of user root
session-c1.scope loaded active running Session c1 of user gdm
systemctl list-unit-files 查看系统中所有单元及当前状态
[root@stw ~]# 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
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-c1.scope static
abrt-ccpp.service enabled
......
systemctl --type=service 只显示服务单元类型的状态
systemctl list-units --type=service 列出所有已加载且处于活动状态的服务单元
systemctl list-unit-files | grep service
systemctl is-active 服务名 查询具体某个服务是否处于激活状态
systemctl is-enabled 服务名 查询具体某个服务下次启动的状态
这些命令用的都比较少,我们一般使用如下命令查看
3、查看服务状态
systemctl status 服务名称
[root@stw ~]# systemctl restart chronyd
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:14:01 CST; 2s agoDocs: man:chronyd(8)man:chrony.conf(5)Process: 10177 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)Process: 10171 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 10174 (chronyd)Tasks: 1CGroup: /system.slice/chronyd.service└─10174 /usr/sbin/chronydAug 06 11:14:01 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:14:01 stw.example.com chronyd[10174]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:14:01 stw.example.com systemd[1]: Started NTP client/server.
Hint: Some lines were ellipsized, use -l to show in full.
一些关键的命令
status 状态
start 启动
stop 停止
restart 重启
enable 下一次启动
disable 下一次不启动/禁用
reload 重新加载
mask 屏蔽
unmask 解除屏蔽
(1)start(启动) stop (停止)-------启动、停止的是当前状态
[root@stw ~]# systemctl start chronyd
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:14:01 CST; 7min agoDocs: man:chronyd(8)man:chrony.conf(5)Process: 10177 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)Process: 10171 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 10174 (chronyd)Tasks: 1CGroup: /system.slice/chronyd.service└─10174 /usr/sbin/chronydAug 06 11:14:01 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:14:01 stw.example.com chronyd[10174]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:14:01 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:14:07 stw.example.com chronyd[10174]: Selected source 202.118.1.130
Aug 06 11:14:11 stw.example.com chronyd[10174]: Source 162.159.200.123 replaced with 84.16.67.12
Hint: Some lines were ellipsized, use -l to show in full.
[root@stw ~]# systemctl stop chronyd
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:chronyd(8)man:chrony.conf(5)Aug 06 11:14:01 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:14:01 stw.example.com chronyd[10174]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:14:01 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:14:07 stw.example.com chronyd[10174]: Selected source 202.118.1.130
Aug 06 11:14:11 stw.example.com chronyd[10174]: Source 162.159.200.123 replaced with 84.16.67.12
Aug 06 11:21:20 stw.example.com chronyd[10174]: chronyd exiting
Aug 06 11:21:20 stw.example.com systemd[1]: Stopping NTP client/server...
Aug 06 11:21:20 stw.example.com systemd[1]: Stopped NTP client/server.
Hint: Some lines were ellipsized, use -l to show in full.
(2)restart(重启) enable(下一次启动) disable(下一次不启动)
[root@stw ~]# systemctl restart chronyd
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:26:08 CST; 2s agoDocs: man:chronyd(8)man:chrony.conf(5)Process: 10363 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)Process: 10358 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 10361 (chronyd)Tasks: 1CGroup: /system.slice/chronyd.service└─10361 /usr/sbin/chronydAug 06 11:26:08 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:26:08 stw.example.com chronyd[10361]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:26:08 stw.example.com chronyd[10361]: Frequency -3.556 +/- 14.071 ppm read from /var/lib/chrony/drift
Aug 06 11:26:08 stw.example.com systemd[1]: Started NTP client/server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@stw ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:26:08 CST; 13s agoDocs: man:chronyd(8)man:chrony.conf(5)Main PID: 10361 (chronyd)CGroup: /system.slice/chronyd.service└─10361 /usr/sbin/chronydAug 06 11:26:08 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:26:08 stw.example.com chronyd[10361]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:26:08 stw.example.com chronyd[10361]: Frequency -3.556 +/- 14.071 ppm read from /var/lib/chrony/drift
Aug 06 11:26:08 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:26:15 stw.example.com chronyd[10361]: Selected source 162.159.200.1
Aug 06 11:26:19 stw.example.com chronyd[10361]: Selected source 111.230.189.174
Hint: Some lines were ellipsized, use -l to show in full.
[root@stw ~]# systemctl disable chronyd
Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.
[root@stw ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:26:08 CST; 1min 12s agoDocs: man:chronyd(8)man:chrony.conf(5)Main PID: 10361 (chronyd)CGroup: /system.slice/chronyd.service└─10361 /usr/sbin/chronydAug 06 11:26:08 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:26:08 stw.example.com chronyd[10361]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:26:08 stw.example.com chronyd[10361]: Frequency -3.556 +/- 14.071 ppm read from /var/lib/chrony/drift
Aug 06 11:26:08 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:26:15 stw.example.com chronyd[10361]: Selected source 162.159.200.1
Aug 06 11:26:19 stw.example.com chronyd[10361]: Selected source 111.230.189.174
Aug 06 11:27:21 stw.example.com chronyd[10361]: Source 162.159.200.123 replaced with 116.203.151.74
Hint: Some lines were ellipsized, use -l to show in full.
restart和reload的区别
restart:会先关闭,再改配置文件,再启动
reload:直接把旧的配置文件替换成新的配置文件
一般用reload启用网卡(network)
启用reload
systemctl daemon-reload
(3)mask(屏蔽) unmask(解除屏蔽)
系统中有些服务同时启动可能产生冲突,为了防止管理员意外启动这些冲突的服务,
可以尝试屏蔽服务
[root@stw ~]# systemctl status ntpd
Unit ntpd.service could not be found.
[root@stw ~]# systemctl status chronyd //ntpd和chronyd服务不能同时存在
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)Active: active (running) since Wed 2025-08-06 11:26:08 CST; 10min agoDocs: man:chronyd(8)man:chrony.conf(5)Main PID: 10361 (chronyd)CGroup: /system.slice/chronyd.service└─10361 /usr/sbin/chronydAug 06 11:26:08 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:26:08 stw.example.com chronyd[10361]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:26:08 stw.example.com chronyd[10361]: Frequency -3.556 +/- 14.071 ppm read from /var/lib/chrony/drift
Aug 06 11:26:08 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:26:15 stw.example.com chronyd[10361]: Selected source 162.159.200.1
Aug 06 11:26:19 stw.example.com chronyd[10361]: Selected source 111.230.189.174
Aug 06 11:27:21 stw.example.com chronyd[10361]: Source 162.159.200.123 replaced with 116.203.151.74
Aug 06 11:28:25 stw.example.com chronyd[10361]: Selected source 202.118.1.81
Hint: Some lines were ellipsized, use -l to show in full.
[root@stw ~]# systemctl mask chronyd //屏蔽掉chronyd服务
Created symlink from /etc/systemd/system/chronyd.service to /dev/null.
//将这个软链接文件丢到空设备里面去了,
[root@stw ~]# systemctl status chronyd //可以查看状态为mask
● chronyd.serviceLoaded: masked (/dev/null; bad)Active: active (running) since Wed 2025-08-06 11:26:08 CST; 10min agoMain PID: 10361 (chronyd)CGroup: /system.slice/chronyd.service└─10361 /usr/sbin/chronydAug 06 11:26:08 stw.example.com systemd[1]: Starting NTP client/server...
Aug 06 11:26:08 stw.example.com chronyd[10361]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDRO...EBUG)
Aug 06 11:26:08 stw.example.com chronyd[10361]: Frequency -3.556 +/- 14.071 ppm read from /var/lib/chrony/drift
Aug 06 11:26:08 stw.example.com systemd[1]: Started NTP client/server.
Aug 06 11:26:15 stw.example.com chronyd[10361]: Selected source 162.159.200.1
Aug 06 11:26:19 stw.example.com chronyd[10361]: Selected source 111.230.189.174
Aug 06 11:27:21 stw.example.com chronyd[10361]: Source 162.159.200.123 replaced with 116.203.151.74
Aug 06 11:28:25 stw.example.com chronyd[10361]: Selected source 202.118.1.81
Hint: Some lines were ellipsized, use -l to show in full.
[root@stw ~]# systemctl stop chronyd
[root@stw ~]# systemctl enable chronyd
Failed to execute operation: Cannot send after transport endpoint shutdown
[root@stw ~]# systemctl restart chronyd //屏蔽掉之后不能对这个服务进行操作,但可以停止
Failed to restart chronyd.service: Unit is masked.
[root@stw ~]# systemctl unmask chronyd
Removed symlink /etc/systemd/system/chronyd.service.
[root@stw ~]# systemctl restart chronyd
[root@stw ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
(4)enable/disable启用和禁用服务
在系统启动时,服务是否会自动启动取决于systemd配置目录中是否存在该服务的链接文件。
启用服务会在systemd的配置目录中创建该服务的链接文件,服务在系统启动时自动启动。
禁用服务会删除systemd配置目录中该服务的链接文件,服务在系统启动时不会自动启动
[root@stw ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
/etc/systemd/system/multi-user.target.wants/chronyd.service
enable(启用)时,将链接文件存放在这个文件里面,系统启动时,这个文件里面的所有服务都会自动启动
[root@stw ~]# systemctl disable chronyd
Removed symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.
disable(禁用)时,将链接文件从这个文件中移除