systemctl常用命令指南

一、概述

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。

 

使用Systemctl管理Linux服务

本文旨在阐明在运行systemd的系统上“如何控制系统和服务”。

 

二、Systemd初体验和Systemctl基础

1. 首先检查你的系统中是否安装有systemd并确定当前安装的版本

# systemctl  --version

systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

上例中很清楚地表明,我们安装了219版本的systemd。

2. 检查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

 

3. 检查systemd是否运行

# ps -elf |grep systemd
4 S root          1      0  0  80   0 - 11520 ep_pol 11:08 ?        00:00:05 /usr/lib/systemd/systemd --system --deserialize 20
4 S root        498      1  0  80   0 -  9204 ep_pol 11:08 ?        00:00:00 /usr/lib/systemd/systemd-journald
4 S root        824      1  0  80   0 -  6048 ep_pol 11:08 ?        00:00:00 /usr/lib/systemd/systemd-logind
4 S dbus        829      1  0  80   0 - 24597 ep_pol 11:08 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
4 S root      22256      1  0  80   0 - 11525 ep_pol 17:13 ?        00:00:00 /usr/lib/systemd/systemd-udevd
0 S root      22322   2104  0  80   0 - 28167 pipe_w 17:24 pts/0    00:00:00 grep --color=auto systemd

4. 分析systemd启动进程

# systemd-analyze
Startup finished in 10.958s (kernel) + 3.538s (initrd) + 12.088s (userspace) = 26.585s

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

# systemd-analyze blame
          4.969s NetworkManager-wait-online.service
          2.035s lvm2-pvscan@8:2.service
          1.546s lvm2-monitor.service
          1.468s dev-mapper-cl\x2droot.device
           889ms tuned.service
           834ms network.service
           770ms postfix.service
           731ms sshd-keygen.service
           536ms systemd-hwdb-update.service
           516ms polkit.service
           512ms firewalld.service
           447ms abrt-ccpp.service
           382ms systemd-logind.service
           377ms chronyd.service
           331ms systemd-user-sessions.service
           203ms systemd-fsck-root.service
           175ms NetworkManager.service
           121ms systemd-tmpfiles-setup-dev.service
           102ms dev-hugepages.mount

6. 分析启动时的关键链

# 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 @12.077s
└─tuned.service @11.187s +889ms
  └─network.target @11.181s
    └─NetworkManager.service @5.375s +175ms
      └─dbus.service @3.946s
        └─basic.target @3.938s
          └─sockets.target @3.938s
            └─dbus.socket @3.938s
              └─sysinit.target @3.935s
                └─systemd-update-utmp.service @3.923s +11ms
                  └─auditd.service @3.837s +84ms
                    └─systemd-tmpfiles-setup.service @3.800s +36ms
                      └─rhel-import-state.service @3.752s +46ms
                        └─local-fs.target @3.750s
                          └─boot.mount @3.653s +95ms
                            └─local-fs-pre.target @3.652s
                              └─lvm2-monitor.service @2.100s +1.546s
                                └─lvm2-lvmetad.service @2.208s
                                  └─lvm2-lvmetad.socket @2.095s
                                    └─-.slice

重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

7. 列出所有可用单元

# systemctl list-unit-files
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-sys-fs-binfmt_misc.mount               static  
sys-fs-fuse-connections.mount               static  
sys-kernel-config.mount                     static  
sys-kernel-debug.mount                      static  
tmp.mount                                   disabled
brandbot.path                               disabled
systemd-ask-password-console.path           static  
systemd-ask-password-plymouth.path          static  
……

 

9. 列出所有失败单元

# 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'.

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

# systemctl  is-enabled crond.service
enabled

 

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

#  systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2020-07-19 11:14:32 CST; 6h ago
     Docs: man:firewalld(1)
 Main PID: 1552 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1552 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

7月 19 11:14:31 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
7月 19 11:14:32 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

二、使用Systemctl控制并管理服务

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

#  systemctl list-unit-files --type=service
UNIT FILE                                   STATE   
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
arp-ethers.service                          disabled
auditd.service                              enabled 
autovt@.service                             enabled 
blk-availability.service                    disab

13. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

13. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
httpd.service -TheApache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since Tue2015-04-2817:21:30 IST;6s ago
Process:2876ExecStop=/bin/kill -WINCH ${MAINPID}(code=exited, status=0/SUCCESS)
Main PID:2881(httpd)
Status:"Processing requests..."
CGroup:/system.slice/httpd.service
├─2881/usr/sbin/httpd -DFOREGROUND
├─2884/usr/sbin/httpd -DFOREGROUND
├─2885/usr/sbin/httpd -DFOREGROUND
├─2886/usr/sbin/httpd -DFOREGROUND
├─2887/usr/sbin/httpd -DFOREGROUND
└─2888/usr/sbin/httpd -DFOREGROUND
Apr2817:21:30 tecmint systemd[1]:StartingTheApache HTTP Server...
Apr2817:21:30 tecmint httpd[2881]: AH00558: httpd:Couldnot reliably determine the server's fully q...ssage
Apr 28 17:21:30 tecmint systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
注意:当我们使用systemctl的start,restart,stop和reload命令时,我们不会从终端获取到任何输出内容,只有status命令可以打印输出。

14. 如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

# systemctl enable httpd.service
# systemctl disable httpd.service

15. 使用systemctl命令杀死服务

# systemctl kill 服务名

 

三、使用Systemctl控制并管理挂载点

16. 列出所有系统挂载点

# systemctl list-unit-files --type=mount
UNIT FILE STATE
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled

 

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

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

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

# systemctl enable tmp.mount
# systemctl disable tmp.mount

四、其他

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

# systemctl show  服务名

 

posted @ 2020-07-19 18:10  浪淘沙&  阅读(796)  评论(0编辑  收藏  举报