使用 Systemctl 命令來管理系統(tǒng)服務(wù)
Systemctl是systemd用于管理系統(tǒng)和管理服務(wù)的工具。許多現(xiàn)代Linux發(fā)行版,如Ubuntu、Debian、Fedora、Linux Mint、OpenSuSE、Redhat都采用systemd作為默認(rèn)的init系統(tǒng)。
使用systemctl,可以啟動、停止、重新加載、重啟服務(wù)、列出服務(wù)單元、檢查服務(wù)狀態(tài)、啟用/禁用服務(wù)、管理運(yùn)行級別和電源管理。在本文中將展示如何在Linux中使用systemctl命令來管理systemd服務(wù)。
*使用systemctl命令 Start/Stop/Restart/Reload服務(wù)
使用systemctl啟動服務(wù)時(shí),命令格式:systemctl start [service-name]。例如,啟動firewalld服務(wù):
- [root@localhost ~]# systemctl start firewalld
<以上代碼可復(fù)制粘貼,可往左滑>
與以前老版本的linux中的service命令相反,systemctl start命令不輸出任何內(nèi)容。
要停止服務(wù),請使用systemctl stop [service-name]。例如,停止firewalld服務(wù):
- [root@localhost ~]# systemctl stop firewalld
<以上代碼可復(fù)制粘貼,可往左滑>
要重新啟動服務(wù),請使用systemctl restart [service-name],例如:
- [root@localhost ~]# systemctl restart firewalld
<以上代碼可復(fù)制粘貼,可往左滑>
要重新加載服務(wù)的配置(例如ssh)而不重新啟動它,請使用systemctl reload [service-name],例如:
- [root@localhost ~]# systemctl reload sshd
<以上代碼可復(fù)制粘貼,可往左滑>
*systemctl檢查服務(wù)狀態(tài)
為了查看服務(wù)是否正在運(yùn)行,我們可以使用systemctl status [service-name]來查看。
- [root@localhost ~]# systemctl status firewalld
<以上代碼可復(fù)制粘貼,可往左滑>
*檢查服務(wù)是否設(shè)置為開機(jī)啟動
要在引導(dǎo)時(shí)啟用服務(wù),請使用systemctl enable [service-name],例如:
- [root@localhost ~]# systemctl enable httpd.service
- Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
<以上代碼可復(fù)制粘貼,可往左滑>
同樣,disable時(shí)取消引導(dǎo)時(shí)啟用服務(wù):
- [root@localhost ~]# systemctl disable httpd.service
<以上代碼可復(fù)制粘貼,可往左滑>
可以使用is-enabled選項(xiàng)檢查開機(jī)是否啟動該服務(wù),請運(yùn)行:
- [root@localhost ~]# systemctl is-enabled httpd.service
<以上代碼可復(fù)制粘貼,可往左滑>
輸出的內(nèi)容enabled表示開機(jī)時(shí)啟動該服務(wù),disabled表示開機(jī)時(shí)不啟動該服務(wù)。
*systemctl列出單元要列出所有激活的單元,使用list-units選項(xiàng)。
- [root@localhost ~]# systemctl list-units
<以上代碼可復(fù)制粘貼,可往左滑>
要列出所有活動的服務(wù),請運(yùn)行:
- [root@localhost ~]# systemctl list-units -t service
<以上代碼可復(fù)制粘貼,可往左滑>
*使用systemctl重啟、關(guān)機(jī)系統(tǒng)
像poweroff、shutdown命令一樣,systemctl命令可以關(guān)閉系統(tǒng),重啟或進(jìn)入休眠狀態(tài)。關(guān)機(jī):
- [root@localhost ~]# systemctl poweroff
<以上代碼可復(fù)制粘貼,可往左滑>
重啟:
- [root@localhost ~]# systemctl reboot
<以上代碼可復(fù)制粘貼,可往左滑>
系統(tǒng)休眠:
- [root@localhost ~]# systemctl hibernate
<以上代碼可復(fù)制粘貼,可往左滑>
*使用systemclt管理遠(yuǎn)程系統(tǒng)
通常,上述所有systemctl命令都可以用于通過systemctl命令本身管理遠(yuǎn)程主機(jī)。這將使用ssh與遠(yuǎn)程主機(jī)進(jìn)行通信。如下所示:
- [root@localhost ~]# systemctl status httpd -H root@192.168.0.12
<以上代碼可復(fù)制粘貼,可往左滑>
-H選項(xiàng),指定遠(yuǎn)程主機(jī)的用戶名和密碼。
*管理Targets
Systemd具有Targets的概念,這些Targets的目的與sysVinit系統(tǒng)中的運(yùn)行級別相似。sysVinit中的運(yùn)行級別主要是數(shù)字(0,1,2,-6)。以下是sysVinit中的運(yùn)行級別及其對應(yīng)的systemd中的target:
- 0 runlevel0.target, poweroff.target
- 1 runlevel1.target, rescue.target
- 2,3,4 runlevel2.target, runlevel3.target,runlevel4.target, multi-user.target
- 5 runlevel5.target, graphical.target
- 6 runlevel6.target, reboot.target
<以上代碼可復(fù)制粘貼,可往左滑>
如果想要查看當(dāng)前的運(yùn)行級別,可以使用如下命令:
- [root@localhost ~]# systemctl get-default
- multi-user.target
<以上代碼可復(fù)制粘貼,可往左滑>
設(shè)置默認(rèn)的運(yùn)行級別為graphical,命令如下:
- [root@localhost ~]# systemctl set-default graphical.target
- Removed symlink /etc/systemd/system/default.target.
- Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
<以上代碼可復(fù)制粘貼,可往左滑>
想要列出所有激活的target,可以使用下面命令:
- [root@localhost ~]# systemctl list-units -t target
<以上代碼可復(fù)制粘貼,可往左滑>
*systemd工具的其他命令
journalctl日志收集
systemd有自己的日志系統(tǒng),稱為journald。它替換了sysVinit中的syslogd。
- [root@localhost ~]# journalctl
<以上代碼可復(fù)制粘貼,可往左滑>
要查看所有引導(dǎo)消息,請運(yùn)行命令journalctl -b
- [root@localhost ~]# journalctl -b
<以上代碼可復(fù)制粘貼,可往左滑>
以下命令實(shí)時(shí)跟蹤系統(tǒng)日志(類似于tail -f):
- [root@localhost ~]# journalctl -f
<以上代碼可復(fù)制粘貼,可往左滑>
查詢系統(tǒng)啟動過程的持續(xù)時(shí)間
- [root@localhost ~]# systemd-analyze
- Startup finished in 497ms (kernel) + 1.836s (initrd) + 6.567s (userspace) = 8.901s
<以上代碼可復(fù)制粘貼,可往左滑>
最后顯示系統(tǒng)啟動時(shí)間為8.901秒。查看服務(wù)的啟動時(shí)間:
- [root@localhost ~]# systemd-analyze blame
<以上代碼可復(fù)制粘貼,可往左滑>
hostnamectl命令
查看主機(jī)名稱:
- [root@localhost ~]# hostnamectl
<以上代碼可復(fù)制粘貼,可往左滑>
*總 結(jié)在本文學(xué)習(xí)了systemctl命令來管理Linux發(fā)行版中的系統(tǒng)服務(wù)。希望對你們有所幫助。
本文轉(zhuǎn)載自微信公眾號「Linux就該這么學(xué)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Linux就該這么學(xué)公眾號。