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

语法格式:systemctl 参数 服务

常用参数:

start 启动服务
stop 停止服务
restart 重启服务
enable使某服务开机自启
disable关闭某服务开机自启
status查看服务状态
list -units --type=service 列举所有已启动服务

参考实例

启动指定的服务:

[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 2022-05-12 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 12 17:02:08 linuxcool.com systemd[1]: Stopped OpenSSH server daemon.
May 12 17:02:08 linuxcool.com systemd[1]: Starting OpenSSH server daemon...
May 12 17:02:08 linuxcool.com sshd[3015]: Server listening on 0.0.0.0 port 22.
May 12 17:02:08 linuxcool.com sshd[3015]: Server listening on :: port 22.
May 12 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         
Author