0
点赞
收藏
分享

微信扫一扫

42.可能是全网最详细实用的systemctl讲解,多维度查看系统的unit,管理服务,管理开关机,管理操作系统运行级别,管理挂载,管理套接字

可能是全网最详细实用的systemctl讲解,多维度查看系统的unit,管理服务,管理开关机,管理操作系统运行级别,管理挂载,管理套接字。 下一章节会讲解如何添加服务,及unit配置文件详解。

文章目录


systemd概念

Systemd:系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进 程和其它进程。CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替。
systemd对应的进程管理命令是systemctl,systemctl命令兼容了service,systemctl也会去/etc/init.d目录下,查看执行相关程序。

查看系统上的unit

systemd所管理的所有系统资源都称作Unit,通过systemd命令集可以方便的对这些Unit进行管理。unit不仅仅包含服务,还有其他类型。

语法

systemctl [command] [–type=TYPE] [–all]
  • command - list-units:依据unit列出所有启动的unit。加上 –all 才会列出没启动的unit;
    list-unit-files:依据/usr/lib/systemd/system/ 内的启动文件,列出启动文件列表

  • –type=TYPE - 为unit type, 主要有service, socket, target,mount

systemctl	列出所有的系统服务
systemctl list-units	列出所有启动unit
systemctl list-unit-files	列出所有启动文件
systemctl list-units –type=service –all	列出所有service类型的unit
systemctl list-units –type=service –all grep cpu	列出 cpu电源管理机制的服务
systemctl list-units –type=target –all	列出所有target
systemctl list-unit-files –type=mount  //显示系统所有挂载点

unit类型

systemd核心概念unit(单元)类型:unit表示不同类型的systemd对象,通过配置文件进行标识和配置;
文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息

  • service :文件扩展名为.service, 用于定义系统服务
  • target :文件扩展名为.target,用于模拟实现运行级别
  • device :用于定义内核识别的设备
  • mount:定义文件系统挂载点
  • socket:用于标识进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动
  • snapshot :管理系统快照
  • swap:用于标识swap设备
  • automount :文件系统的自动挂载点
  • path:用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务

systemctl status

systemctl status命令,查看指定Unit状态

[root@shuge ~]# systemctl status httpd.service
● httpd.service - LSB: starts Apache Web Server
   Loaded: loaded (/etc/rc.d/init.d/httpd; bad; vendor preset: disabled)
   Active: active (running) since Tue 2022-01-04 11:16:52 CST; 3h 22min ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/httpd.service
           ├─ 939 /usr/local/apache/bin/httpd -k start
           ├─1106 /usr/local/apache/bin/httpd -k start
           ├─1107 /usr/local/apache/bin/httpd -k start
           └─1108 /usr/local/apache/bin/httpd -k start
Jan 04 11:16:45 shuge.cn systemd[1]: Starting LSB: starts Apache Web Server....
Jan 04 11:16:52 shuge.cn systemd[1]: Started LSB: starts Apache Web Server..

列出所有失败的单元

[root@shuge ~]# 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'.

systemctl list-units命令,查看系统中Unit信息

systemctl list-units  #列出正在运行的Unit
systemctl list-units --all #列出所有Unit,包括缺失配置文件或启动失败的
systemctl list-units --all --state=inactive #列出所有没有运行的Unit
systemctl list-units --failed  #列出所有加载失败的Unit
systemctl list-units --type=service  #列出所有正在运行的类型为service的Unit

systemctl list-dependencies

查看Unit依赖关系,比如A依赖于B则代表Systemd在启动A的时候同时会启动 B。

systemctl list-dependencies [unit]  //查询这个服务的子服务
systemctl list-dependencies [unit] [–reverse]  //查询这个服务的上级服务
[root@shuge ~]# systemctl list-dependencies  slices.target   
slices.target
● ├─-.slice
● └─system.slice
[root@shuge ~]# systemctl list-dependencies  slices.target --reverse
slices.target
● └─basic.target
●   ├─atd.service
●   ├─cpupower.service
●   ├─crond.service
●   ├─dbus.service
...........
●   ├─initrd.target
●   └─multi-user.target
●     └─graphical.target

systemctl list-unit-filis (列出指定类型的配置)

用于列出Unit配置文件与启用状态。STATE分为了enabled(已建立开机启动软链接)、disabled(没有建立开机启动)、static(该配置文件没有[Install]部分所以无法启动,只能作为其它Unit的依赖文件)、masked(禁止建立启动软链接文件)四种状态。

systemctl list-unit-files #列出所有配置文件

[root@shuge ~]# systemctl list-unit-files --type=service 
UNIT FILE                                     STATE   
arp-ethers.service                            disabled
atd.service                                   enabled 
auditd.service                                enabled 
autovt@.service                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
console-getty.service                         disabled
console-shell.service                         disabled
container-getty@.service                      static  
cpupower.service                              disabled
crond.service                                 enabled 
dbus-org.freedesktop.hostname1.service        static  
dbus-org.freedesktop.import1.service          static  
dbus-org.freedesktop.locale1.service          static  
dbus-org.freedesktop.login1.service           static  
dbus-org.freedesktop.machine1.service         static  
dbus-org.freedesktop.nm-dispatcher.service    enabled 
dbus-org.freedesktop.timedate1.service        static  
.....

列出所有系统挂载点

[root@shuge ~]# systemctl list-unit-files --type=mount
UNIT FILE                     STATE   
dev-hugepages.mount           static  
dev-mqueue.mount              static  
proc-sys-fs-binfmt_misc.mount static  
run-vmblock\x2dfuse.mount     disabled
sys-fs-fuse-connections.mount static  
sys-kernel-config.mount       static  
sys-kernel-debug.mount        static  
tmp.mount                     disabled

8 unit files listed.

列出所有可用系统套接口

[root@shuge ~]# systemctl list-unit-files --type=socket
UNIT FILE                    STATE   
dbus.socket                  static  
rsyncd.socket                disabled
sshd.socket                  disabled
syslog.socket                static  
systemd-initctl.socket       static  
systemd-journald.socket      static  
systemd-shutdownd.socket     static  
systemd-udevd-control.socket static  
systemd-udevd-kernel.socket  static  

9 unit files listed.

使用systemctl管理服务

服务的启停

daemon命令systemctl命令说明
service [服务] startsystemctl start [unit type]启动服务
service [服务] stopsystemctl stop [unit type]停止服务
service [服务] restartsystemctl restart [unit type]重启服务
systemctl reload [unit type]重新加载指定的服务
systemctl daemon-reload重新加载所有被修改过的服务配置,否则配置不会生效
systemctl kill [unit type]如果服务无法正常停止则使用kill方式终止
systemctl is-active [unit type]服务是否正在运行
[root@shuge ~]# systemctl restart firewalld.service
[root@shuge ~]# systemctl stop firewalld.service
[root@shuge ~]# systemctl start firewalld.service
[root@shuge ~]# systemctl reload firewalld.service
[root@shuge ~]# systemctl daemon-reload
[root@shuge ~]# systemctl kill firewalld.service

查看服务状态

[root@shuge ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2022-01-04 09:57:46 CST; 8s ago
     Docs: man:firewalld(1)
 Main PID: 1475 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1475 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Jan 04 09:57:45 shuge.cn systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 04 09:57:46 shuge.cn systemd[1]: Started firewalld - dynamic firewall daemon.
Jan 04 09:57:46 shuge.cn firewalld[1475]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It wi... it now.
Hint: Some lines were ellipsized, use -l to show in full.
[root@shuge ~]# systemctl is-active firewalld.service
active
[root@shuge ~]# systemctl kill firewalld.service
[root@shuge ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2022-01-04 16:25:50 CST; 5s ago
     Docs: man:firewalld(1)
  Process: 2335 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 2335 (code=exited, status=0/SUCCESS)
[root@shuge ~]# systemctl is-active firewalld.service
inactive

开机自启动/关闭

daemon命令systemctl命令说明
chkconfig [服务] onsystemctl enable [unit type]设置服务开机启动
chkconfig [服务] offsystemctl disable [unit type]设备服务禁止开机启动
systemctl is-enabled [unit type]服务是否设置了开机自启动。
[root@shuge ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@shuge ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@shuge ~]# systemctl is-enabled firewalld.service
disable

注销服务

systemctl命令说明
systemctl mask [unit type]注销服务。
systemctl unmask [unit type]取消对服务的注销。
[root@shuge ~]# systemctl mask firewalld.service
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@shuge ~]# systemctl unmask firewalld.service
Removed symlink /etc/systemd/system/firewalld.service.

使用systemctl管理开关机

开机/关机操作

initsystemctl命令说明
init 0systemctl poweroff系统关机
init 6systemctl reboot重新启动
systemctl suspend进入睡眠模式
systemctl hibernate进入休眠模式
systemctl rescue强制进入救援模式
systemctl emergency强制进入紧急救援模式
[root@shuge ~]# systemctl reboot
Connection closing...Socket close.

使用systemctl管理操作系统运行级别

语法介绍

systemctl [command] [unit.target]
  • command:
    systemctl get-default 获得当前的运行级别
    systemctl set-default multi-user.target 设置默认的运行级别为mulit-user
    systemctl isolate multi-user.target 在不重启的情况下,切换到运行级别mulit-user下

  • unit.target

init级别systemctl target
0shutdown.target
1emergency.target
2rescure.target
3multi-user.target
4
5graphical.target
6

示例

[root@shuge ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@shuge ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[root@shuge ~]# systemctl get-default
graphical.target

使用systemctl管理挂载

列出所有系统安装点

[root@shuge ~]# systemctl list-unit-files --type=mount
UNIT FILE                     STATE   
dev-hugepages.mount           static  
dev-mqueue.mount              static  
proc-sys-fs-binfmt_misc.mount static  
run-vmblock\x2dfuse.mount     disabled
sys-fs-fuse-connections.mount static  
sys-kernel-config.mount       static  
sys-kernel-debug.mount        static  
tmp.mount                     disabled

8 unit files listed.

装载,卸载,以及检查挂载点的状态

[root@shuge ~]# systemctl start tmp.mount
[root@shuge ~]# systemctl stop tmp.mount
[root@shuge ~]# systemctl restart tmp.mount
[root@shuge ~]# systemctl reload tmp.mount
[root@shuge ~]# systemctl status  tmp.mount
● tmp.mount - Temporary Directory
   Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
   Active: active (mounted) since Tue 2022-01-04 15:50:09 CST; 8s ago
    Where: /tmp
     What: tmpfs
     Docs: man:hier(7)
           http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
  Process: 2060 ExecRemount=/bin/mount tmpfs /tmp -o remount,mode=1777,strictatime -t tmpfs (code=exited, status=0/SUCCESS)
  Process: 2052 ExecMount=/bin/mount tmpfs /tmp -t tmpfs -o mode=1777,strictatime (code=exited, status=0/SUCCESS)

Jan 04 15:50:09 shuge.cn systemd[1]: tmp.mount: Directory /tmp to mount over is not empty, mounting anyway.
Jan 04 15:50:09 shuge.cn systemd[1]: Mounting Temporary Directory...
Jan 04 15:50:09 shuge.cn systemd[1]: Mounted Temporary Directory.
Jan 04 15:50:13 shuge.cn systemd[1]: Reloading Temporary Directory.
Jan 04 15:50:13 shuge.cn systemd[1]: Reloaded Temporary Directory.
[root@shuge ~]# systemctl is-active tmp.mount
active

如何在引导时激活,启用或禁用装入点(在系统引导时自动装入)

[root@shuge ~]# systemctl enable tmp.mount
Created symlink from /etc/systemd/system/local-fs.target.wants/tmp.mount to /usr/lib/systemd/system/tmp.mount.
[root@shuge ~]# systemctl disable tmp.mount
Removed symlink /etc/systemd/system/local-fs.target.wants/tmp.mount.
[root@shuge ~]# systemctl is-enabled tmp.mount
disabled

如何在Linux中屏蔽(使其无法启动)或取消屏蔽挂载点

[root@shuge ~]# systemctl mask tmp.mount
Created symlink from /etc/systemd/system/tmp.mount to /dev/null.
[root@shuge ~]# systemctl unmask tmp.mount
Removed symlink /etc/systemd/system/tmp.mount.

使用Systemctl管理套接字

列出所有可用的系统套接字

[root@shuge ~]# systemctl list-unit-files --type=socket
UNIT FILE                    STATE   
dbus.socket                  static  
rsyncd.socket                disabled
sshd.socket                  disabled
syslog.socket                static  
systemd-initctl.socket       static  
systemd-journald.socket      static  
systemd-shutdownd.socket     static  
systemd-udevd-control.socket static  
systemd-udevd-kernel.socket  static  

9 unit files listed.

启动,停止,加载和检查套接字的状态

[root@shuge ~]# systemctl start systemd-journald.socket
[root@shuge ~]# systemctl stop systemd-journald.socket
[root@shuge ~]# systemctl restart systemd-journald.socket
[root@shuge ~]# systemctl reload cups.socket
[root@shuge ~]# systemctl status systemd-journald.socket

● systemd-journald.socket - Journal Socket
   Loaded: loaded (/usr/lib/systemd/system/systemd-journald.socket; static; vendor preset: disabled)
   Active: active (running) since Tue 2022-01-04 15:56:55 CST; 1min 38s ago
     Docs: man:systemd-journald.service(8)
           man:journald.conf(5)
   Listen: /run/systemd/journal/stdout (Stream)
           /run/systemd/journal/socket (Datagram)
           /dev/log (Datagram)
[root@shuge ~]# systemctl is-active systemd-journald.socket
active

启用或禁用(系统引导时自动启动套接字)

[root@shuge ~]# systemctl enable systemd-journald.socket
[root@shuge ~]# systemctl disable systemd-journald.socket

如何屏蔽或取消屏蔽

[root@shuge ~]# systemctl mask  systemd-journald.socket
Created symlink from /etc/systemd/system/systemd-journald.socket to /dev/null.
[root@shuge ~]# systemctl unmask  systemd-journald.socket
Removed symlink /etc/systemd/system/systemd-journald.socket.

总结

可能是全网最详细实用的systemctl讲解,多维度查看系统的unit,管理服务,管理开关机,管理操作系统运行级别,管理挂载,管理套接字.下一章节会讲解如何添加服务,及unit配置文件详解。

举报

相关推荐

0 条评论