一键搞定内网穿透 联行号查询|开户行查询 在线工具箱 藏经阁
当前位置:首页 / 杂记 / 正文
linux systemctl命令|systemd包
linux systemctl命令|systemd包
转https://linux.cn/article-5926-1.html

Systemctl是systemd包中的一个工具命令,主要负责控制systemd系统和服务管理器。Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。

最常用命令

systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务
Systemd 命令和 sysvinit 命令的对照表
Sysvinit 命令 Systemd 命令 备注
service foo start systemctl start foo.service 用来启动一个服务 (并不会重启现有的)
service foo stop systemctl stop foo.service 用来停止一个服务 (并不会重启现有的)。
service foo restart systemctl restart foo.service 用来停止并启动一个服务。
service foo reload systemctl reload foo.service 当支持时,重新装载配置文件而不中断等待操作。
service foo condrestart systemctl condrestart foo.service 如果服务正在运行那么重启它。
service foo status systemctl status foo.service 汇报服务是否正在运行。
ls /etc/rc.d/init.d/ systemctl list-unit-files --type=service 用来列出可以启动或停止的服务列表。
chkconfig foo on systemctl enable foo.service 在下次启动时或满足其他触发条件时设置服务为启用
chkconfig foo off systemctl disable foo.service 在下次启动时或满足其他触发条件时设置服务为禁用
chkconfig foo   systemctl is-enabled foo.service 用来检查一个服务在当前环境下被配置为启用还是禁用。
chkconfig –list   systemctl list-unit-files --type=service 输出在各个运行级别下服务的启用和禁用情况
chkconfig foo –list ls /etc/systemd/system/*.wants/foo.service 用来列出该服务在哪些运行级别下启用和禁用。
chkconfig foo –add systemctl daemon-reload 当您创建新服务文件或者变更设置时使用。

初体验和基础

   检测是否安装及版本

# systemctl --version
systemd 215 //安装了215版本的systemd

   检查systemd和systemctl的二进制文件和库文件的安装位置

# whereis systemd 
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

   分析systemd启动进程

# systemd-analyze
Startup finished in 487ms (kernel) + 2.776s (initrd) + 20.229s (userspace) = 23.493s

   分析启动时各个进程花费的时间

# systemd-analyze blame
8.565s mariadb.service
4.311s httpd.service
....

   分析启动时的关键链

# 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 @20.222s
└─mariadb.service @11.657s +8.565s
 └─network.target @11.168s
 └─network.service @9.456s +1.712s
  └─NetworkManager.service @8.858s +596ms
  ...
重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

   列出所有可用单元

# systemctl list-unit-files
UNIT FILE         STATE 
proc-sys-fs-binfmt_misc.automount   static
firewalld.service       enabled 
nginx.service        disabled
.....

   列出所有运行中单元

systemctl list-units

列出所有失败单元

systemctl --failed

   检查某个单元(如 cron.service)是否启用

# systemctl is-enabled crond.service
enabled

   检查某个单元或服务是否运行

[root@localhost ~]# systemctl status nginx.service 
nginx.service - nginx - high performance web server
 Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
 Active: active (running) since 六 2018-04-07 17:33:15 CST; 24min ago
  Docs: http://nginx.org/en/docs/
 Process: 2397 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
 Process: 2402 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Process: 2400 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 2405 (nginx)
 CGroup: /system.slice/nginx.service
   ├─2405 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
   └─2406 nginx: worker process

4月 07 17:33:15 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
4月 07 17:33:15 localhost.localdomain nginx[2400]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
4月 07 17:33:15 localhost.localdomain nginx[2400]: nginx: configuration file /etc/nginx/nginx.conf test is successful
4月 07 17:33:15 localhost.localdomain systemd[1]: Started nginx - high performance web server.

使用Systemctl控制并管理服务

   列出所有服务(包括启用的和禁用的)

# systemctl list-unit-files --type=service
UNIT FILE         STATE 
blk-availability.service     disabled
brandbot.service       static 
crond.service        enabled 
....

   启动、重启、停止、重载服务以及检查服务

Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
注意:当我们使用systemctl的start,restart,stop和reload命令时,我们不会从终端获取到任何输出内容,只有status命令可以打印输出。

   启用/禁用服务

如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)
# systemctl is-active httpd.service
# systemctl enable httpd.service
# systemctl disable httpd.service

   屏蔽/取消屏蔽服务

如何屏蔽(让它不能启动)或显示服务(如 httpd.service)
# systemctl mask httpd.service
ln -s '/dev/null' '/etc/systemd/system/httpd.service'
# systemctl unmask httpd.service
rm '/etc/systemd/system/httpd.service'

   使用systemctl杀死服务

# systemctl kill httpd
# systemctl status httpd

使用Systemctl控制并管理挂载点

   列出所有系统挂载点

# systemctl list-unit-files --type=mount
UNIT FILE      STATE 
sys-kernel-debug.mount  static 
tmp.mount      disabled

   挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount

   在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

# systemctl is-active tmp.mount
# systemctl enable tmp.mount
# systemctl disable tmp.mount

   在Linux中屏蔽(让它不能启用)或可见挂载点

# systemctl mask tmp.mount
ln -s '/dev/null' '/etc/systemd/system/tmp.mount'
# systemctl unmask tmp.mount
rm '/etc/systemd/system/tmp.mount'

使用Systemctl控制并管理套接口

   列出所有可用系统套接口

# systemctl list-unit-files --type=socket
UNIT FILE     STATE 
dbus.socket     static 
lvm2-lvmetad.socket   enabled 
rsyncd.socket    disabled
syslog.socket    static

   在Linux中启动、重启、停止、重载套接口并检查其状态

# systemctl start cups.socket
# systemctl restart cups.socket
# systemctl stop cups.socket
# systemctl reload cups.socket
# systemctl status cups.socket

   在启动时激活套接口,并启用或禁用它(系统启动时自启动)

# systemctl is-active cups.socket
# systemctl enable cups.socket
# systemctl disable cups.socket

   屏蔽(使它不能启动)或显示套接口

# systemctl mask cups.socket
ln -s '/dev/null' '/etc/systemd/system/cups.socket'
# systemctl unmask cups.socket
rm '/etc/systemd/system/cups.socket'

服务的CPU利用率(分配额)

获取当前某个服务的CPU分配额(如httpd)
# systemctl show -p CPUShares httpd.service
CPUShares=1024
注意:各个服务的默认CPU分配份额=1024,你可以增加/减少某个进程的CPU分配份额。
将某个服务(httpd.service)的CPU分配份额限制为2000 CPUShares/
# systemctl set-property httpd.service CPUShares=2000
# systemctl show -p CPUShares httpd.service
CPUShares=2000
注意:当你为某个服务设置CPUShares,会自动创建一个以服务名命名的目录(如 httpd.service),里面包含了一个名为90-CPUShares.conf的文件,该文件含有CPUShare限制信息,你可以通过以下方式查看该文件:
# vi /etc/systemd/system/httpd.service.d/90-CPUShares.conf 
[Service]
CPUShares=2000

检查某个服务的所有配置细节

# systemctl show httpd
Id=httpd.service
Names=httpd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice
Description=The Apache HTTP Server
LoadState=loaded
ActiveState=active
SubState=running
FragmentPath=/usr/lib/systemd/system/httpd.service
....

分析某个服务(httpd)的关键链

# 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 +142ms
└─network.target @11.168s
 └─network.service @9.456s +1.712s
 └─NetworkManager.service @8.858s +596ms
  └─firewalld.service @4.931s +3.926s
  └─basic.target @4.916s
  ...

获取某个服务(httpd)的依赖性列表

# systemctl list-dependencies httpd.service
httpd.service
├─system.slice
└─basic.target
 ├─microcode.service
 ...

按等级列出控制组

# systemd-cgls
├─1 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
├─user.slice
│ └─user-0.slice
│ └─session-1.scope
│  ├─4521 systemd-cgls
│  └─4522 systemd-cgls
└─system.slice
 ├─httpd.service
 │ └─4446 /usr/sbin/httpd -DFOREGROUND
 ├─polkit.service
 │ └─721 /usr/lib/polkit-1/polkitd --no-debug
....

按CPU、内存、输入和输出列出控制组

# systemd-cgtop
Path                Tasks %CPU Memory Input/s Output/s
/                 83 1.0 437.8M  -  -
/system.slice               - 0.1  -  -  -
/system.slice/mariadb.service           2 0.1  -  -  -
/system.slice/tuned.service           1 0.0  -  -  -
/system.slice/auditd.service           1  -  -  -  -
/system.slice/crond.service           1  -  -  -  -

控制系统运行等级

启动系统救援模式
# systemctl rescue
Broadcast message from root@tecmint on pts/0 (Wed 2015-04-29 11:31:18 IST):
The system is going down to rescue mode NOW!
进入紧急模式
# systemctl emergency
Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" to try again
to boot into default mode.
列出当前使用的运行等级
# systemctl get-default
multi-user.target
启动运行等级5,即图形模式
# systemctl isolate runlevel5.target
或
# systemctl isolate graphical.target
启动运行等级3,即多用户模式(命令行)
# systemctl isolate runlevel3.target
或
# systemctl isolate multiuser.target
设置多用户模式或图形模式为默认运行等级
# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target
重启、停止、挂起、休眠系统或使系统进入混合睡眠
# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep