systemctl命令来自英文词组“system control”的缩写,其功能是用于管理系统服务。从RHEL/CentOS7版本之后初始化进程服务init被替代成了systemd服务,systemd初始化进程服务的管理是通过systemctl命令完成的,从功能上涵盖了之前service、chkconfig、init、setup等多个命令的大部分功能。

语法格式:systemctl 参数 [动作] [服务名]

常用参数:

-a 显示所有单位
-f 覆盖任何冲突的符号链接
-H 设置要连接的主机名
-M 设置要连接的容器名
-n 设置要显示的日志行数
-o 设置要显示的日志格式
-q 静默执行模式
-r 显示本地容器的单位
-s 设置要发送的进程信号
-t 设置单元类型
--help 显示帮助信息
--version 显示版本信息

常用动作:

start 启动服务
stop 停止服务
restart 重启服务
enable 设置服务开机自启
disable 取消服务开机自启
status 查看服务状态
list 显示所有已启动服务

参考示例

启动指定的服务:

[root@linuxcool ~]# systemctl start sshd

停止指定的服务:

[root@linuxcool ~]# systemctl stop sshd

重启指定的服务:

[root@linuxcool ~]# systemctl restart sshd

查看指定服务的运行状态:

[root@linuxcool ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset>
Active: active (running) since Thu 2023-05-18 17:02:08 CST; 23s ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3015 (sshd)
Tasks: 1 (limit: 12391)
Memory: 1.5M
CGroup: /system.slice/sshd.service
└─3015 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-p>
May 18 17:02:08 linuxcool.com systemd[1]: Stopped OpenSSH server daemon.
May 18 17:02:08 linuxcool.com systemd[1]: Starting OpenSSH server daemon...
May 18 17:02:08 linuxcool.com sshd[3015]: Server listening on 0.0.0.0 port 22.
May 18 17:02:08 linuxcool.com sshd[3015]: Server listening on :: port 22.
May 18 17:02:08 linuxcool.com systemd[1]: Started OpenSSH server daemon.
lines 1-16/16 (END)

将指定的服务加入到开机启动项中:

[root@linuxcool ~]# systemctl enable sshd

将指定的服务从开机启动项中取消:

[root@linuxcool ~]# systemctl disable sshd

显示系统中所有已启动的服务列表信息:

[root@linuxcool ~]# systemctl list-units --type=service
UNIT                          LOAD   ACTIVE SUB     DESCRIPTION
accounts-daemon.service       loaded active running Accounts Service
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
bolt.service                  loaded active running Thunderbolt system service
colord.service                loaded active running Manage, Install and Gener>
crond.service                 loaded active running Command Scheduler
cups.service                  loaded active running CUPS Scheduler         
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。