linux命令學習之:systemctl


  systemctl命令是系統服務管理器指令,主要負責控制systemd系統和服務管理器,它實際上將 service 和 chkconfig 這兩個命令組合到一起。

  CentOS 7.x開始,CentOS開始使用systemd服務來代替daemon,原來管理系統啟動和管理系統服務的相關命令全部由systemctl命令來代替。

  Systemd是一個系統管理守護進程、工具和庫的集合,用於取代System V初始進程。Systemd的功能是用於集中管理和配置類UNIX系統。

  在Linux生態系統中,Systemd被部署到了大多數的標准Linux發行版中,只有為數不多的幾個發行版尚未部署。Systemd通常是所有其它守護進程的父進程,但並非全是如此。

 

1、原來的 service 命令與 systemctl 命令對比

daemon命令 systemctl命令 說明
service [服務] start systemctl start [unit type] 啟動服務
service [服務] stop systemctl stop [unit type] 停止服務
service [服務] restart systemctl restart [unit type] 重啟服務

此外還是二個systemctl參數沒有與service命令參數對應

  • status:參數來查看服務運行情況
  • reload:重新加載服務,加載更新后的配置文件(並不是所有服務都支持這個參數,比如network.service)

  示例:

#啟動網絡服務
systemctl start network.service

#停止網絡服務
systemctl stop network.service

#重啟網絡服務
systemctl restart network.service

#查看網絡服務狀態
systemctl status network.serivce

 

2、原來的chkconfig 命令與 systemctl 命令對比

daemon命令 systemctl命令 說明
chkconfig [服務] on systemctl enable [unit type] 設置服務開機啟動
chkconfig [服務] off systemctl disable [unit type] 設備服務禁止開機啟動

  示例:

#停止cup電源管理服務
systemctl stop cups.service

#禁止cups服務開機啟動
systemctl disable cups.service

#查看cups服務狀態
systemctl status cups.service

#重新設置cups服務開機啟動
systemctl enable cups.service

 

3、查看系統上上所有的服務

  命令格式:systemctl [command] [–type=TYPE] [–all]

  參數詳解:

  command - list-units:依據unit列出所有啟動的unit。加上 –all 才會列出沒啟動的unit; - list-unit-files:依據/usr/lib/systemd/system/ 內的啟動文件,列出啟動文件列表

–type=TYPE - 為unit type, 主要有service, socket, target

  應用舉例:

systemctl命令 說明
systemctl 列出所有的系統服務
systemctl list-units 列出所有啟動unit
systemctl list-unit-files 列出所有啟動文件
systemctl list-units –type=service –all 列出所有service類型的unit
systemctl list-units –type=service –all grep cpu 列出 cpu電源管理機制的服務
systemctl list-units –type=target –all 列出所有target

 

4、systemctl特殊的用法

systemctl命令 說明
systemctl is-active [unit type] 查看服務是否運行
systemctl is-enable [unit type] 查看服務是否設置為開機啟動
systemctl mask [unit type] 注銷指定服務
systemctl unmask [unit type] 取消注銷指定服務

 

  應用舉例:

#查看網絡服務是否啟動
systemctl is-active network.service

#檢查網絡服務是否設置為開機啟動
systemctl is-enable network.service

#停止cups服務
systemctl stop cups.service

#注銷cups服務
systemctl mask cups.service

#查看cups服務狀態
systemctl status cups.service

#取消注銷cups服務
systemctl unmask cups.service

 

5、常用示例:

 

Systemctl基礎、體驗

1. 檢查系統中是否安裝有systemctl並確定當前安裝的版本

[root@CTU1000094955 ~]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

  上例中很清楚地表明,我們安裝了219版本的systemctl。

2. 檢查systemd和systemctl的二進制文件和庫文件的安裝位置

[root@CTU1000094955 ~]# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
[root@CTU1000094955 ~]# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

3. 檢查systemd是否運行

[root@CTU1000094955 ~]# ps -eaf | grep [s]ystemd
root          1      0  0 16:15 ?        00:00:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root        631      1  0 16:15 ?        00:00:24 /usr/lib/systemd/systemd-journald
root        661      1  0 16:15 ?        00:00:00 /usr/lib/systemd/systemd-udevd
dbus        813      1  0 16:15 ?        00:00:02 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root        841      1  0 16:15 ?        00:00:00 /usr/lib/systemd/systemd-logind

  注意:systemd是作為父進程(PID=1)運行的。在上面帶(-e)參數的ps命令輸出中,選擇所有進程,(-a)選擇除會話前導外的所有進程,並使用(-f)參數輸出完整格式列表(即 -eaf)。

  也請注意上例中后隨的方括號和例子中剩余部分。方括號表達式是grep的字符類表達式的一部分。

4. 分析systemd啟動進程

[root@CTU1000094955 ~]# systemd-analyze
Startup finished in 743ms (kernel) + 6.959s (initrd) + 17.696s (userspace) = 25.399s

5. 分析啟動時各個進程花費的時間

[root@CTU1000094955 ~]# systemd-analyze blame 11.859s dev-xvda1.device
          5.670s NetworkManager-wait-online.service
          4.610s plymouth-quit-wait.service
          3.757s systemd-udev-settle.service
          3.078s kdump.service
          2.257s mysql.service
          1.451s libvirtd.service
          1.228s postfix.service
           751ms uvp-monitor.service
           675ms spesserviced.service
           675ms messageserviced.service
           660ms tuned.service
           586ms network.service
           525ms accounts-daemon.service
           467ms NetworkManager.service
           457ms systemd-udevd.service
           416ms abrt-ccpp.service
           397ms firewalld.service
           365ms ModemManager.service

6. 分析啟動時的關鍵鏈

[root@CTU1000094955 ~]# 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.

graphical.target @17.682s
?..multi-user.target @17.682s
  ?..getty.target @17.682s
    ?..serial-getty@hvc0.service @17.681s
      ?..systemd-user-sessions.service @12.929s +18ms
        ?..remote-fs.target @12.921s
          ?..remote-fs-pre.target @12.921s
            ?..iscsi-shutdown.service @12.907s +12ms
              ?..network.target @12.900s
                ?..network.service @12.311s +586ms
                  ?..NetworkManager.service @6.166s +467ms
                    ?..basic.target @5.903s
                      ?..sockets.target @5.903s
                        ?..rpcbind.socket @5.903s
                          ?..sysinit.target @5.895s
                            ?..systemd-update-utmp.service @5.877s +16ms
                              ?..auditd.service @5.738s +135ms
                                ?..systemd-tmpfiles-setup.service @5.661s +73ms
                                  ?..rhel-import-state.service @5.526s +134ms
                                    ?..local-fs.target @5.525s
                                      ?..run-user-0.mount @14.041s
                                        ?..local-fs-pre.target @1.615s
                                          ?..systemd-tmpfiles-setup-dev.service @
                                            ?..kmod-static-nodes.service @1.473s 
                                              ?..systemd-journald.socket
                                                ?..-.slice

  重要:Systemctl接受服務(.service),掛載點(.mount),套接口(.socket)和設備(.device)作為單元。

7. 列出所有可用單元

[root@CTU1000094955 ~]# systemctl list-unit-files
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount                static  
brandbot.path                               disabled
cups.path                                   enabled 
systemd-ask-password-console.path           static

8. 列出所有運行中單元

[root@CTU1000094955 ~]# systemctl list-units
  UNIT                                                                               LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                                                  loaded active waiting   Arbitrary Executable File Formats File System Automount Point
  sys-devices-pci0000:00-0000:00:01.1-ata2-host1-target1:0:1-1:0:1:0-block-sr0.device loaded active plugged   QEMU_DVD-ROM
  sys-devices-platform-serial8250-tty-ttyS2.device                                   loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device                                   loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:08-tty-ttyS0.device                                            loaded active plugged   /sys/devices/pnp0/00:08/tty/ttyS0
  sys-devices-pnp0-00:09-tty-ttyS1.device                                            loaded active plugged   /sys/devices/pnp0/00:09/tty/ttyS1
  sys-devices-vbd\x2d51712-block-xvda-xvda1.device                                   loaded active plugged   /sys/devices/vbd-51712/block/xvda/xvda1
  sys-devices-vbd\x2d51712-block-xvda-xvda2.device                                   loaded active plugged   /sys/devices/vbd-51712/block/xvda/xvda2
  sys-devices-vbd\x2d51712-block-xvda.device                                         loaded active plugged   /sys/devices/vbd-51712/block/xvda
  sys-devices-vbd\x2d51776-block-xvde.device                                         loaded active plugged   LVM PV I2rhCm-www3-J79i-r3ei-1XCb-a1qS-3OWSg9 on /dev/xvde
  sys-devices-vif\x2d0-net-eth0.device                                               loaded active plugged   /sys/devices/vif-0/net/eth0

9. 列出所有失敗單元

[root@CTU1000094955 ~]# systemctl --failed
  UNIT              LOAD   ACTIVE SUB    DESCRIPTION
?.bluetooth.service loaded failed failed Bluetooth service
?.rc-local.service  loaded failed failed /etc/rc.d/rc.local Compatibility

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.

2 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)是否啟用

[root@CTU1000094955 ~]# systemctl is-enabled firewalld.service
disabled

11. 檢查某個單元或服務是否運行

[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2017-11-25 16:20:44 CST; 1h 29min ago
 Main PID: 7677 (firewalld)
   CGroup: /system.slice/firewalld.service
           ?..7677 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Nov 25 16:20:43 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 16:20:44 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.

 

使用Systemctl控制並管理服務

12. 列出所有服務(包括啟用的和禁用的)

[root@CTU1000094955 ~]# 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 
accounts-daemon.service                     enabled 
alsa-restore.service                        static  
alsa-state.service                          static  
alsa-store.service                          static  
anaconda-direct.service                     static  
anaconda-nm-config.service                  static  
anaconda-noshell.service                    static  
anaconda-shell@.service                     static

13. Linux中如何啟動、重啟、停止、重載服務以及檢查服務(如 firewalld.service)狀態

[root@CTU1000094955 ~]# systemctl start httpd.service
Failed to start httpd.service: Unit httpd.service failed to load: No such file or directory.
[root@CTU1000094955 ~]# systemctl start firewalld.service
[root@CTU1000094955 ~]# systemctl restart firewalld.service
[root@CTU1000094955 ~]# systemctl stop firewalld.service
[root@CTU1000094955 ~]# systemctl reload firewalld.service
Job for firewalld.service invalid.
[root@CTU1000094955 ~]# systemctl status firewalld.service ?.firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Nov 25 16:18:11 CTU1000094955 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Nov 25 16:20:43 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 16:20:44 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:52:25 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:52:38 CTU1000094955 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Nov 25 17:52:40 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 17:52:40 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:52:53 CTU1000094955 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Nov 25 17:52:55 CTU1000094955 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Nov 25 17:53:07 CTU1000094955 systemd[1]: Unit firewalld.service cannot be reloaded because it is inactive.
[root@CTU1000094955 ~]# systemctl restart firewalld.service
[root@CTU1000094955 ~]# systemctl reload firewalld.service
[root@CTU1000094955 ~]# systemctl status firewalld.service ?.firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2017-11-25 17:53:40 CST; 8s ago
  Process: 31721 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 31008 (firewalld)
   CGroup: /system.slice/firewalld.service
           ?..31008 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Nov 25 17:53:39 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 17:53:40 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:53:43 CTU1000094955 systemd[1]: Reloaded firewalld - dynamic firewall daemon.

  注意:當我們使用systemctl的start,restart,stop和reload命令時,我們不會從終端獲取到任何輸出內容,只有status命令可以打印輸出。

14. 如何激活服務並在啟動時啟用或禁用服務(即系統啟動時自動啟動服務)

[root@CTU1000094955 ~]# systemctl is-active firewalld.service
active
[root@CTU1000094955 ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@CTU1000094955 ~]# systemctl list-unit-files |grep firewall
firewalld.service                           enabled 
[root@CTU1000094955 ~]# 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 Sat 2017-11-25 17:53:40 CST; 4min 6s ago
 Main PID: 31008 (firewalld)
   CGroup: /system.slice/firewalld.service
           ?..31008 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Nov 25 17:53:39 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 17:53:40 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:53:43 CTU1000094955 systemd[1]: Reloaded firewalld - dynamic firewall daemon.
[root@CTU1000094955 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@CTU1000094955 ~]# systemctl list-unit-files |grep firewall
firewalld.service                           disabled

15. 如何屏蔽(讓它不能啟動)或顯示服務(如 httpd.service)

[root@CTU1000094955 ~]# systemctl mask firewalld.service
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@CTU1000094955 ~]# systemctl unmask firewalld.service
Removed symlink /etc/systemd/system/firewalld.service.

16. 使用systemctl命令殺死服務

[root@CTU1000094955 ~]# systemctl kill httpd
Failed to kill unit httpd.service: Unit httpd.service is not loaded.
[root@CTU1000094955 ~]# systemctl kill firewalld.service
[root@CTU1000094955 ~]# systemctl status firewalld.service
?.firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sat 2017-11-25 18:05:17 CST; 7s ago
 Main PID: 31008 (code=exited, status=0/SUCCESS)

Nov 25 17:53:39 CTU1000094955 systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 25 17:53:40 CTU1000094955 systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 25 17:53:43 CTU1000094955 systemd[1]: Reloaded firewalld - dynamic firewall daemon.

 

使用Systemctl控制並管理掛載點

17. 列出所有系統掛載點

[root@CTU1000094955 ~]# systemctl list-unit-files --type=mount
UNIT FILE                     STATE   
dev-hugepages.mount           static  
dev-mqueue.mount              static  
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount  static  

9 unit files listed.

18. 掛載、卸載、重新掛載、重載系統掛載點並檢查系統中掛載點狀態

[root@CTU1000094955 ~]# systemctl start tmp.mount
[root@CTU1000094955 ~]# systemctl stop tmp.mount
[root@CTU1000094955 ~]# systemctl reload tmp.mount
Job for tmp.mount invalid.
[root@CTU1000094955 ~]# systemctl status tmp.mount
?.tmp.mount - Temporary Directory
   Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2017-11-25 18:09:06 CST; 25s ago
    Where: /tmp
     What: tmpfs
     Docs: man:hier(7)
           http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
  Process: 40649 ExecUnmount=/bin/umount /tmp -n (code=exited, status=0/SUCCESS)
  Process: 40271 ExecMount=/bin/mount tmpfs /tmp -n -t tmpfs -o mode=1777,strictatime (code=exited, status=0/SUCCESS)

Nov 25 18:08:58 CTU1000094955 systemd[1]: Mounting Temporary Directory...
Nov 25 18:08:58 CTU1000094955 systemd[1]: Mounted Temporary Directory.
Nov 25 18:09:06 CTU1000094955 systemd[1]: Unmounting Temporary Directory...
Nov 25 18:09:06 CTU1000094955 systemd[1]: Unmounted Temporary Directory.
Nov 25 18:09:21 CTU1000094955 systemd[1]: Unit tmp.mount cannot be reloaded because it is inactive.
[root@CTU1000094955 ~]# systemctl restart tmp.mount
[root@CTU1000094955 ~]# systemctl status tmp.mount
?.tmp.mount - Temporary Directory
   Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
   Active: active (mounted) since Sat 2017-11-25 18:09:53 CST; 2s ago
    Where: /tmp
     What: tmpfs
     Docs: man:hier(7)
           http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
  Process: 40649 ExecUnmount=/bin/umount /tmp -n (code=exited, status=0/SUCCESS)
  Process: 41410 ExecMount=/bin/mount tmpfs /tmp -n -t tmpfs -o mode=1777,strictatime (code=exited, status=0/SUCCESS)

Nov 25 18:09:53 CTU1000094955 systemd[1]: tmp.mount: Directory /tmp to mount over is not empty, mounting anyway.
Nov 25 18:09:53 CTU1000094955 systemd[1]: Mounting Temporary Directory...
Nov 25 18:09:53 CTU1000094955 systemd[1]: Mounted Temporary Directory.
[root@CTU1000094955 ~]# systemctl reload tmp.mount

19. 在啟動時激活、啟用或禁用掛載點(系統啟動時自動掛載)

[root@CTU1000094955 ~]# systemctl is-active tmp.mount
active
[root@CTU1000094955 ~]# systemctl disable tmp.mount
Removed symlink /etc/systemd/system/local-fs.target.wants/tmp.mount.
[root@CTU1000094955 ~]# systemctl is-active tmp.mount
active
[root@CTU1000094955 ~]# systemctl enable tmp.mount
Failed to execute operation: Access denied

20. 在Linux中屏蔽(讓它不能啟用)或可見掛載點

[root@CTU1000094955 ~]# systemctl mask tmp.mount
ln -'/dev/null''/etc/systemd/system/tmp.mount'
[root@CTU1000094955 ~]# systemctl unmask tmp.mount
rm '/etc/systemd/system/tmp.mount'

 

使用Systemctl控制並管理套接口

21. 列出所有可用系統套接口

[root@CTU1000094955 ~]# systemctl list-unit-files --type=socket
UNIT FILE                    STATE   
avahi-daemon.socket          enabled 
cups.socket                  enabled 
dbus.socket                  static  
dm-event.socket              enabled 
iscsid.socket                enabled 
iscsiuio.socket              enabled 
libvirtd.socket              static  
lldpad.socket                disabled
lvm2-lvmetad.socket          enabled 
lvm2-lvmpolld.socket         enabled 
rpcbind.socket               enabled 
rsyncd.socket                disabled
sshd.socket                  disabled
syslog.socket                static  
systemd-initctl.socket       static  
systemd-journald.socket      static  
systemd-networkd.socket      disabled
systemd-shutdownd.socket     static  
systemd-udevd-control.socket static  
systemd-udevd-kernel.socket  static  
virtlockd.socket             disabled

21 unit files listed.

22. 在Linux中啟動、重啟、停止、重載套接口並檢查其狀態

[root@CTU1000094955 ~]# systemctl start sshd.socket
[root@CTU1000094955 ~]# systemctl restart sshd.socket
Failed to restart sshd.socket: Transaction contains conflicting jobs 'restart' and 'stop' for sshd-keygen.service. Probably contradicting requirement dependencies configured.
[root@CTU1000094955 ~]# systemctl status sshd.socket ?.sshd.socket - OpenSSH Server Socket
   Loaded: loaded (/usr/lib/systemd/system/sshd.socket; disabled; vendor preset: disabled)
   Active: active (listening) since Sat 2017-11-25 18:28:12 CST; 22s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
   Listen: [::]:22 (Stream)
 Accepted: 0; Connected: 0

Nov 25 18:28:12 CTU1000094955 systemd[1]: Listening on OpenSSH Server Socket.
Nov 25 18:28:12 CTU1000094955 systemd[1]: Starting OpenSSH Server Socket.
[root@CTU1000094955 ~]# systemctl stop sshd.socket
[root@CTU1000094955 ~]# systemctl reload sshd.socket
Failed to reload sshd.socket: Job type reload is not applicable for unit sshd.socket.
[root@CTU1000094955 ~]# systemctl status sshd.socket
?.sshd.socket - OpenSSH Server Socket
   Loaded: loaded (/usr/lib/systemd/system/sshd.socket; disabled; vendor preset: disabled)
   Active: inactive (dead) since Sat 2017-11-25 18:28:46 CST; 19s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
   Listen: [::]:22 (Stream)
 Accepted: 0; Connected: 0

Nov 25 18:28:12 CTU1000094955 systemd[1]: Listening on OpenSSH Server Socket.
Nov 25 18:28:12 CTU1000094955 systemd[1]: Starting OpenSSH Server Socket.
Nov 25 18:28:46 CTU1000094955 systemd[1]: Closed OpenSSH Server Socket.
Nov 25 18:28:46 CTU1000094955 systemd[1]: Stopping OpenSSH Server Socket.

24. 屏蔽(使它不能啟動)或顯示套接口

[root@CTU1000094955 ~]# systemctl mask sshd.socket
Created symlink from /etc/systemd/system/sshd.socket to /dev/null.
[root@CTU1000094955 ~]# systemctl unmask sshd.socket
Removed symlink /etc/systemd/system/sshd.socket.

 

服務的CPU利用率(分配額)

25. 獲取當前某個服務的CPU分配額(如httpd)

[root@CTU1000094955 ~]# systemctl show -p CPUShares httpd.service
CPUShares=18446744073709551615

  注意:各個服務的默認CPU分配份額=1024,你可以增加/減少某個進程的CPU分配份額。

26. 將某個服務(httpd.service)的CPU分配份額限制為2000 CPUShares/

[root@CTU1000094955 ~]# systemctl set-property httpd.service CPUShares=2000 
# systemctl show
-p CPUShares httpd.service
CPUShares
=2000

  注意:當你為某個服務設置CPUShares,會自動創建一個以服務名命名的目錄(如 httpd.service),里面包含了一個名為90-CPUShares.conf的文件,該文件含有CPUShare限制信息,你可以通過以下方式查看該文件:

[root@CTU1000094955 ~]# vi /etc/systemd/system/httpd.service.d/90-CPUShares.conf 
[Service]
CPUShares
=2000

27. 檢查某個服務的所有配置細節

[root@CTU1000094955 ~]# systemctl show httpd
Restart=no
NotifyAccess=none
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=1min 30s
WatchdogUSec=0
WatchdogTimestampMonotonic=0
StartLimitInterval=10000000
StartLimitBurst=5
StartLimitAction=none
FailureAction=none
... ... 中間省略部分內容

28. 分析某個服務(httpd)的關鍵鏈

[root@CTU1000094955 ~]# systemd-analyze critical-chain firewalld.service
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.

firewalld.service +380ms
?..basic.target @5.903s
  ?..sockets.target @5.903s
    ?..rpcbind.socket @5.903s
      ?..sysinit.target @5.895s
        ?..systemd-update-utmp.service @5.877s +16ms
          ?..auditd.service @5.738s +135ms
            ?..systemd-tmpfiles-setup.service @5.661s +73ms
              ?..rhel-import-state.service @5.526s +134ms
                ?..local-fs.target @5.525s
                  ?..run-user-0.mount @14.041s
                    ?..local-fs-pre.target @1.615s
                      ?..systemd-tmpfiles-setup-dev.service @1.519s +92ms
                        ?..kmod-static-nodes.service @1.473s +44ms
                          ?..systemd-journald.socket
                            ?..-.slice

29. 獲取某個服務(httpd)的依賴性列表

[root@CTU1000094955 ~]# systemctl list-dependencies firewalld.service
firewalld.service
?.?..system.slice
?.?..basic.target
?.  ?..alsa-restore.service
?.  ?..alsa-state.service
  ?.?.....
?.  ?..microcode.service
?.  ?..rhel-autorelabel-mark.service
?.  ?..rhel-autorelabel.service
?.  ?..rhel-configure.service
?.  ?..rhel-dmesg.service
?.  ?..rhel-loadmodules.service
?.  ?..paths.target
?.  ?..slices.target
... ....  后面省略部分內容

30. 按等級列出控制組

[root@CTU1000094955 ~]# systemd-cgls
?..1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
?..user.slice
?.?..user-988.slice
?.?.?..session-c1.scope
?.?.  ?..3150 gdm-session-worker [pam/gdm-launch-environment]
?.?.  ?..3187 /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart --session gnome-initial-setup
?.?.  ?..3190 /usr/bin/dbus-launch --exit-with-session /usr/bin/gnome-session --autostart /usr/share/gdm/greeter/autostart --session gnome-initial-setup
?.?.  ?..3199 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
?.?.  ?..3288 /usr/libexec/at-spi-bus-launcher
?.?.  ?..3305 /bin/dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
?.?.  ?..3309 /usr/libexec/at-spi2-registryd --use-gnome-session
?.?.  ?..3410 gnome-shell --mode=initial-setup
...  ...  中間省略部分內容
?.?.  ?..3767 /usr/libexec/ibus-engine-simple
?.?.  ?..3879 gnome-keyring-daemon --unlock
?.?..user-0.slice

31. 按CPU、內存、輸入和輸出列出控制組

[root@CTU1000094955 ~]# systemd-cgtop
Path                                                                                                                                                    Tasks   %CPU   Memory  Input/s Output/s

/                                                                                                                                                         418      -     2.8G        -        -
/system.slice/ModemManager.service                                                                                                                          1      -        -        -        -
/system.slice/NetworkManager.service                                                                                                                        1      -        -        -        -
/system.slice/abrt-oops.service                                                                                                                             1      -        -        -        -
/system.slice/abrt-xorg.service                                                                                                                             1      -        -        -        -
/system.slice/abrtd.service                                                                                                                                 1      -        -        -        -
/system.slice/accounts-daemon.service                                                                                                                       1      -        -        -        -
/system.slice/alsa-state.service                                                                                                                            1      -        -        -        -
/system.slice/atd.service                                                                                                                                   1      -        -        -        -
/system.slice/auditd.service                                                                                                                                3      -        -        -        -

 

控制系統運行等級

32. 啟動系統救援模式

[root@CTU1000094955 ~]# systemctl rescue
Broadcast message from root@tecmint on pts/0(Wed2015-04-2911:31:18 IST):
The system is going down to rescue mode NOW!

33. 進入緊急模式

[root@CTU1000094955 ~]# systemctl emergency
Welcome to emergency mode!After logging in, type "journalctl -xb" to view
system logs,"systemctl reboot" to reboot,"systemctl default" to try again
to boot intodefault mode.

34. 列出當前使用的運行等級

  運行等級說明如下。

  • Runlevel 0 : 關閉系統  shutdown.target  
  • Runlevel 1 : 緊急?維護模式  emergency.target
  • Runlevel 2 : 救援?維護模式  rescure.target
  • Runlevel 3 : 多用戶,無圖形系統  multi-user.target
  • Runlevel 4 : 多用戶,無圖形系統  無
  • Runlevel 5 : 多用戶,圖形化系統  graphical.target
  • Runlevel 6 : 關閉並重啟機器  無
[root@CTU1000094955 ~]# systemctl get-default
graphical.target

35. 啟動運行等級5,即圖形模式

# systemctl isolate runlevel5.target
或
# systemctl isolate graphical.target
 

[root@CTU1000094955 ~]# systemctl isolate runlevel5.target
PolicyKit daemon disconnected from the bus.
We are no longer a registered authentication agent.

36. 啟動運行等級3,即多用戶模式(命令行)

# systemctl isolate runlevel3.target
或
# systemctl isolate multiuser.target

36. 設置多用戶模式或圖形模式為默認運行等級

# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target

37. 重啟、停止、掛起、休眠系統或使系統進入混合睡眠

# systemctl reboot  重新啟動
# systemctl halt
# systemctl suspend  進入睡眠模式
# systemctl hibernate  進入休眠模式
# systemctl hybrid-sleep
# systemctl rescue  強制進入救援模式
# systemctl emergency  強制進入緊急救援模式
# systemctl poweroff  系統關機


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM