RHEL7使用systemctl管理服務


1、
 
systemctl命令
說明
systemctl    start   [unit type]
啟動服務
systemctl    stop   [unit type]
停止服務
systemctl    restart   [unit type]
重啟服務
 
 
  • status:參數來查看服務運行情況
  • reload:重新加載服務,加載更新后的配置文件(並不是所有服務都支持這個參數,比如network.service)
 
2、設置開機啟動/不啟動服務
 
systemctl命令
說明
systemctl enable [unit type]
設置服務開機啟動
systemctl disable [unit type]
設備服務禁止開機啟動
 
 
3、查看系統上的所有服務
 
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
 
關閉sshd服務
在使用systemctl關閉網絡服務時有一些特殊 需要同時關閉unit.servce和unit.socket
[root@localhost ~]# systemctl list-units --all |grep sshd
  sshd-keygen.service          loaded    inactive dead      OpenSSH Server Key Generation
  sshd.service                          loaded    active   running   OpenSSH server daemon
  sshd.socket                          loaded    inactive dead      OpenSSH Server Socket
 
可以看到系統同時開啟了 sshd.service 和 sshd.socket , 如果只閉關了 sshd.service 那么 sshd.socket還在監聽網絡,在網絡上有要求連接 sshd 時就會啟動 sshd.service 。因此如果想完全關閉sshd服務的話,需要同時停用 sshd.service 和 sshd.socket 。
systemctl stop sshd.service
systemctl stop sshd.socket
systemctl disable sshd.service sshd.socket
 
 
5、init命令與systemctl命令對比
 
init命令
systemctl命令
說明
init 0
systemctl poweroff
系統關機
init 6
systemctl reboot
重新啟動
 
 
與開關機相關的其他命令:
systemctl命令
說明
systemctl suspend
進入睡眠模式
systemctl hibernate
進入休眠模式
systemctl rescue
強制進入救援模式
systemctl emergency
強制進入緊急救援模式
 
 
 
 
6、設置系統運行級別
 
運行級別對應表:
 
init級別
systemctl target
0
shutdown.target
1
emergency.target
2
rescure.target
3
multi-user.target
4
5
graphical.target
6
 
此外還有一個getty.target用來設置tty的數量。
 
 
設置運行級別:
命令格式:
systemctl     [command]      [unit.target]
 
參數詳解:
command:
  • get-default :取得當前的target
  • set-default :設置指定的target為默認的運行級別
  • isolate :切換到指定的運行級別
  • unit.target :為5.1表中列出的運行級別
 
 
 
systemctl命令
說明
systemctl get-default
獲得當前的運行級別
systemctl set-default multi-user.target
設置默認的運行級別為mulit-user
systemctl isolate multi-user.target
在不重啟的情況下,切換到運行級別mulit-user下
systemctl isolate graphical.target
在不重啟的情況下,切換到圖形界面下
 
 
 
7、使用systemctl分析各服務之間的依賴關系
命令格式
systemctl    list-dependencies    [unit]    [–reverse]
 
示例:

 

#獲得當前運行級別的target
[root@www ~]# systemctl get-default
multi-user.target
 

 

#查看當前運行級別target(mult-user)啟動了哪些服務
[root@www ~]# systemctl list-dependencies
 

 

#查看哪些target引用了當前運行級別的target
[root@www ~]# systemctl list-dependencies --reverse
 
 
8、開機關機
Table 4.2 systemctl Commands for Shutting Down, Suspending, or Rebooting a System
 
systemctl Command
Description
systemctl halt
Halt the system.
systemctl hibernate
Put the system into hibernation.
systemctl hybrid-sleep
Put the system into hibernation and suspend its operation.
systemctl poweroff
Halt and power off the system.
systemctl reboot
Reboot the system.
systemctl suspend
Suspend the system.


免責聲明!

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



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