在Centos 中 systemctl 是设置系统服务的命令,即 service , 它融合之前service和chkconfig的功能于一体。

可以使用它永久性或只在当前会话中启用/禁用服务。

CentOS 7 的 /etc/rc.d/rc.local 是没有执行权限的, 系统建议创建 systemd service 启动服务。

1.查看服务列表状态:

 systemctl list-units --type=service 
ubuntu@ip-172-31-22-75:~$ systemctl list-units --type=service
  UNIT                          LOAD   ACTIVE SUB     DESCRIPTION
  accounts-daemon.service       loaded active running Accounts Service
  apparmor.service              loaded active exited  AppArmor initialization
  apport.service                loaded active exited  LSB: automatic crash repor
  atd.service                   loaded active running Deferred execution schedul
  avahi-daemon.service          loaded active running Avahi mDNS/DNS-SD Stack
  blk-availability.service      loaded active exited  Availability of block devi
  cloud-config.service          loaded active exited  Apply the settings specifi
  cloud-final.service           loaded active exited  Execute cloud user/final s
  cloud-init-local.service      loaded active exited  Initial cloud-init job (pr
  cloud-init.service            loaded active exited  Initial cloud-init job (me
  console-setup.service         loaded active exited  Set console font and keyma
  cron.service                  loaded active running Regular background program
  dbus.service                  loaded active running D-Bus System Message Bus
...
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

57 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-unit-files       
 #列出所有已经安装的  服务及状态  (可为人所读,  内容简略、清晰):
ubuntu@ip-172-31-22-75:~$
systemctl   list-unit-files
UNIT FILE                                      STATE
proc-sys-fs-binfmt_misc.automount              static
-.mount                                        generated
dev-hugepages.mount                            static
dev-mqueue.mount                               static
proc-sys-fs-binfmt_misc.mount                  static
snap-amazon\x2dssm\x2dagent-1455.mount         enabled
snap-amazon\x2dssm\x2dagent-1480.mount         enabled
snap-core-7713.mount                           disabled
snap-core-7917.mount                           disabled
sys-fs-fuse-connections.mount                  static
sys-kernel-config.mount                        static
sys-kernel-debug.mount                         static
acpid.path                                     enabled
apport-autoreport.path                         enabled
systemd-ask-password-console.path              static
systemd-ask-password-plymouth.path             static
systemd-ask-password-wall.path                 static
...

systemctl 可以列出正在运行的服务状态,如图:(基本不为人所读, 内容复杂、全面)

ubuntu@ip-172-31-22-75:~$ systemctl
  UNIT                        LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount loaded active waiting   Arbitrary Executable
  sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged   /sys/
  sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged   /sys/
  sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged   /sys/
  sys-devices-pnp0-00:06-tty-ttyS0.device loaded active plugged   /sys/devices/p
  sys-devices-vbd\x2d768-block-xvda-xvda1.device loaded active plugged   /sys/de
  sys-devices-vbd\x2d768-block-xvda.device loaded active plugged   /sys/devices/
  sys-devices-vif\x2d0-net-eth0.device loaded active plugged   /sys/devices/vif-
  sys-devices-virtual-block-loop0.device loaded active plugged   /sys/devices/vi
  sys-devices-virtual-block-loop1.device loaded active plugged   /sys/devices/vi
  sys-devices-virtual-block-loop2.device loaded active plugged   /sys/devices/vi
  sys-devices-virtual-block-loop3.device loaded active plugged   /sys/devices/vi
  sys-devices-virtual-misc-rfkill.device loaded active plugged   /sys/devices/vi
  sys-devices-virtual-tty-ttyprintk.device loaded active plugged   /sys/devices/
  sys-module-configfs.device  loaded active plugged   /sys/module/configfs
  sys-module-fuse.device      loaded active plugged   /sys/module/fuse
  sys-subsystem-net-devices-eth0.device loaded active plugged   /sys/subsystem/n
  ...

systemd-cgls 以树形列出正在运行的进程,它可以递归显示控制组内容。如图:


ubuntu@ip-172-31-22-75:~$ systemd-cgls
Control group /:
-.slice
├─user.slice
│ └─user-1000.slice
│   ├─session-175.scope
│   │ ├─15717 sshd: ubuntu [priv]
│   │ ├─15798 sshd: ubuntu@notty
│   │ └─15799 -bash
│   ├─user@1000.service
│   │ └─init.scope
│   │   ├─1455 /lib/systemd/systemd --user
│   │   └─1456 (sd-pam)
│   ├─session-199.scope
│   │ ├─21325 sshd: ubuntu [priv]
│   │ ├─21408 sshd: ubuntu@pts/1
│   │ ├─21409 -bash
│   │ ├─21614 systemd-cgls
│   │ └─21615 systemd-cgls
│   ├─session-196.scope
│   │ ├─20647 sshd: ubuntu [priv]
│   │ ├─20728 sshd: ubuntu@notty
│   │ └─20729 -bash
│   ├─session-183.scope
lines 1-23

2.如何启动/关闭、启用/禁用服务?

启动一个服务:

systemctl start postfix.service

关闭一个服务:

systemctl stop postfix.service

重启一个服务:

systemctl restart postfix.service

显示一个服务的状态:

systemctl status postfix.service

在开机时启用一个服务:systemctl enable postfix.service
在开机时禁用一个服务:systemctl disable postfix.service

查看服务是否开机启动: systemctl is-enabled postfix.service

查看已启动的服务列表: systemctl list-unit-files | grep enabled

查看启动失败的服务列表: systemctl --failed

PS:使用命令 systemctl is-enabled postfix.service 得到的值可以是enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。

说明:启用服务就是在当前runlevel的配置文件目录 /etc/systemd/system/multi-user.target.wants 里,建立 /usr/lib/systemd/system 里面对应服务配置文件的软链接;

禁用服务就是删除此软链接,添加服务就是添加软连接。


ubuntu@ip-172-31-22-75:~$ cd /etc/systemd/system/multi-user.target.wants/
ubuntu@ip-172-31-22-75:/etc/systemd/system/multi-user.target.wants$ ls
 ModemManager.service          ondemand.service
 atd.service                   open-vm-tools.service
 avahi-daemon.service          php7.2-fpm.service
 console-setup.service         pollinate.service
 containerd.service            remote-fs.target
 cron.service                  rsync.service
 docker.service                rsyslog.service
 ebtables.service             'snap-amazon\x2dssm\x2dagent-1455.mount'
 irqbalance.service           'snap-amazon\x2dssm\x2dagent-1480.mount'
 lxcfs.service                 snap.amazon-ssm-agent.amazon-ssm-agent.service
 lxd-containers.service        ssh.service
 mindocd.service               systemd-networkd.service
 mysql.service                 systemd-resolved.service
 networkd-dispatcher.service   ufw.service
 nginx.service                 unattended-upgrades.service
 nodebb.service                wpa_supplicant.service
ubuntu@ip-172-31-22-75:/etc/systemd/system/multi-user.target.wants$ ll
total 12
drwxr-xr-x  2 root root 4096 Nov  7 16:35  ./
drwxr-xr-x 13 root root 4096 Nov  7 16:34  ../
lrwxrwxrwx  1 root root   40 Feb 19  2019  ModemManager.service -> /lib/systemd/system/ModemManager.service
lrwxrwxrwx  1 root root   31 Sep 12  2018  atd.service -> /lib/systemd/system/atd.service
lrwxrwxrwx  1 root root   40 Feb 19  2019  avahi-daemon.service -> /lib/systemd/system/avahi-daemon.service
lrwxrwxrwx  1 root root   41 Sep 12  2018  console-setup.service -> /lib/systemd/system/console-setup.service
lrwxrwxrwx  1 root root   38 Feb 19  2019  containerd.service -> /lib/systemd/system/containerd.service
lrwxrwxrwx  1 root root   32 Sep 12  2018  cron.service -> /lib/systemd/system/cron.service
lrwxrwxrwx  1 root root   34 Feb 19  2019  docker.service -> /lib/systemd/system/docker.service
lrwxrwxrwx  1 root root   36 Sep 12  2018  ebtables.service -> /lib/systemd/system/ebtables.service
lrwxrwxrwx  1 root root   38 Sep 12  2018  irqbalance.service -> /lib/systemd/system/irqbalance.service
lrwxrwxrwx  1 root root   33 Sep 12  2018  lxcfs.service -> /lib/systemd/system/lxcfs.service
lrwxrwxrwx  1 root root   42 Sep 12  2018  lxd-containers.service -> /lib/systemd/system/lxd-containers.service
文档更新时间: 2019-11-07 17:07   作者:cklx