1. 程式人生 > 實用技巧 >systemctl常用命令指南

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 
[email protected]                             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  服務名