Centos7中systemctl命令詳解
LinuxSystemctl是一個系統管理守護進程、工具和庫的集合,用於取代System V、service和chkconfig命令,初始進程主要負責控制systemd系統和服務管理器。通過Systemctl –help可以看到該命令主要分為:查詢或發送控制命令給systemd服務,管理單元服務的命令,服務文件的相關命令,任務、環境、快照相關命令,systemd服務的配置重載,系統開機關機相關的命令。
1. 列出所有可用單元
# systemctl list-unit-files
2. 列出所有運行中單元
# systemctl list-units
3. 列出所有失敗單元
# systemctl –failed
4. 檢查某個單元(如 crond.service)是否啟用
# systemctl is-enabledcrond.service
5. 列出所有服務
# systemctl list-unit-files –type=service
6. Linux中如何啟動、重啟、停止、重載服務以及檢查服務(如 httpd.service)狀態
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
注意:當我們使用systemctl的start,restart,stop和reload命令時,終端不會輸出任何內容,只有status命令可以打印輸出。
7. 如何激活服務並在開機時啟用或禁用服務(即系統啟動時自動啟動mysql.service服務)
# systemctl is-active mysql.service
# systemctl enable mysql.service
# systemctl disable mysql.service
8. 如何屏蔽(讓它不能啟動)或顯示服務(如ntpdate.service)
# systemctl mask ntpdate.service
ln -s ‘/dev/null”/etc/systemd/system/ntpdate.service’
# systemctl unmask ntpdate.service
rm ‘/etc/systemd/system/ntpdate.service’
9. 使用systemctl命令殺死服務
# systemctl killcrond
10. 列出所有系統掛載點
# systemctl list-unit-files –type=mount
11. 掛載、卸載、重新掛載、重載系統掛載點並檢查系統中掛載點狀態
# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount
12. 在啟動時激活、啟用或禁用掛載點(系統啟動時自動掛載)
# systemctl is-active tmp.mount
# systemctl enable tmp.mount
# systemctl disable tmp.mount
13. 在Linux中屏蔽(讓它不能啟用)或可見掛載點
# systemctl mask tmp.mount
ln -s ‘/dev/null”/etc/systemd/system/tmp.mount’
# systemctl unmask tmp.mount
rm ‘/etc/systemd/system/tmp.mount’
14. 列出所有可用系統套接口
# systemctl list-unit-files –type=socket
15. 檢查某個服務的所有配置細節
# systemctl showmysql
16. 獲取某個服務(httpd)的依賴性列表
# systemctl list-dependencies httpd.service
17. 啟動救援模式
# systemctl rescue
18. 進入緊急模式
# systemctl emergency
19. 列出當前使用的運行等級
# systemctl get-default
20. 啟動運行等級5,即圖形模式
# systemctl isolate runlevel5.target
或
# systemctl isolate graphical.target
21. 啟動運行等級3,即多用戶模式(命令行)
# systemctl isolate runlevel3.target
或
# systemctl isolate multiuser.target
22. 設置多用戶模式或圖形模式為默認運行等級
# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target
23. 重啟、停止、掛起、休眠系統或使系統進入混合睡眠
# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep
對於不知運行等級為何物的,說明如下。
Runlevel 0 : 關閉系統
Runlevel 1 : 救援,維護模式
Runlevel 3 : 多用戶,無圖形系統
Runlevel 4 : 多用戶,無圖形系統
Runlevel 5 : 多用戶,圖形化系統
Runlevel 6 : 關閉並重啟機器
對service和chkconfig兩個命令都不陌生,systemctl 是管制服務的主要工具, 它整合chkconfig 與 service功能於一體。
systemctl is-enabled iptables.service
systemctl is-enabled servicename.service #查詢服務是否開機啟動
systemctl enable *.service #開機運行服務
systemctl disable *.service #取消開機運行
systemctl start *.service #啟動服務
systemctl stop *.service #停止服務
systemctl restart *.service #重啟服務
systemctl reload *.service #重新加載服務配置文件
systemctl status *.service #查詢服務運行狀態
systemctl –failed #顯示啟動失敗的服務
註:*代表某個服務的名字,如http的服務名為httpd
例如在CentOS 7 上安裝http
[root@CentOS7 ~]# yum -y install httpd
啟動服務(等同於service httpd start)
systemctl start httpd.service
停止服務(等同於service httpd stop)
systemctl stop httpd.service
重啟服務(等同於service httpd restart)
systemctl restart httpd.service
查看服務是否運行(等同於service httpd status)
systemctl status httpd.service
開機自啟動服務(等同於chkconfig httpd on)
systemctl enable httpd.service
開機時禁用服務(等同於chkconfig httpd on)
systemctl disable httpd.service
查看服務是否開機啟動 (等同於chkconfig –list)
systemctl & systemd的相關資料整理
1、腳本目錄:
/usr/lib/systemd/
2、服務配置文件目錄(開機自啟動):
/usr/lib/systemd/system
3、服務文件
服務格式:
*.service
服務文件格式:
[Unit] //用戶服務說明
Description=xxx //描述服務
After=xxx //描述服務類別
[Service] //服務具體運行參數
Type=forking //服務後台運行
PIDFile=/path/pid //PID的路徑
ExecStart= //服務運行的命令與參數(絕對路徑)
ExecReload= //服務重啟的命令與參數(絕對路徑)
ExecStop= //服務停止運行的命令與參數(絕對路徑)
PrivateTmp=True //為服務分配獨立的臨時空間
[Install] //服務安裝的配置
4、服務使用方法
systemctl start [服務文件名]
systemctl restart [服務文件名]
systemctl stop [服務文件名]
systemctl status [服務文件名]
5、相關命令
任務 舊指令 新指令
使某服務自動啟動 chkconfig –level 3 httpd on systemctl enable httpd.service
使某服務不自動啟動 chkconfig –level 3 httpd off systemctl disable httpd.service
檢查服務狀態 service httpd status systemctl status httpd.service (服務詳細信息) systemctl is-active httpd.service (僅顯示是否 Active)
顯示所有已啟動的服務 chkconfig –list systemctl list-units –type=service
啟動某服務 service httpd start systemctl start httpd.service
停止某服務 service httpd stop systemctl stop httpd.service
重啟某服務 service httpd restart systemctl restart httpd.service
以下文章點擊率最高
Loading…