使用 Systemctl 命令 來(lái)管理系統(tǒng)服務(wù)

使用systemctl,可以啟動(dòng)、停止、重新加載、重啟服務(wù)、列出服務(wù)單元、檢查服務(wù)狀態(tài)、啟用/禁用服務(wù)、管理運(yùn)行級(jí)別和電源管理。在本文中將展示如何在Linux中使用systemctl命令來(lái)管理systemd服務(wù)。
systemctl start [service-name]。例如,啟動(dòng)firewalld服務(wù):[root@localhost?~]#?systemctl?start?firewalld
service命令相反,systemctl start命令不輸出任何內(nèi)容。
要停止服務(wù),請(qǐng)使用
systemctl stop [service-name]。例如,停止firewalld服務(wù):[root@localhost?~]#?systemctl?stop?firewalld

要重新啟動(dòng)服務(wù),請(qǐng)使用
systemctl restart [service-name],例如:[root@localhost?~]#?systemctl?restart?firewalld

要重新加載服務(wù)的配置(例如ssh)而不重新啟動(dòng)它,請(qǐng)使用
systemctl reload [service-name],例如:[root@localhost?~]#?systemctl?reload?sshd

systemctl status [service-name]來(lái)查看。[root@localhost?~]#?systemctl?status?firewalld

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.

同樣,disable時(shí)取消引導(dǎo)時(shí)啟用服務(wù):
[root@localhost?~]#?systemctl?disable?httpd.service

可以使用is-enabled選項(xiàng)檢查開(kāi)機(jī)是否啟動(dòng)該服務(wù),請(qǐng)運(yùn)行:
[root@localhost?~]#?systemctl?is-enabled?httpd.service

輸出的內(nèi)容
enabled表示開(kāi)機(jī)時(shí)啟動(dòng)該服務(wù),disabled表示開(kāi)機(jī)時(shí)不啟動(dòng)該服務(wù)。list-units選項(xiàng)。[root@localhost?~]#?systemctl?list-units

要列出所有活動(dòng)的服務(wù),請(qǐng)運(yùn)行:
[root@localhost?~]#?systemctl?list-units?-t?service

poweroff、shutdown命令一樣,systemctl命令可以關(guān)閉系統(tǒng),重啟或進(jìn)入休眠狀態(tài)。[root@localhost?~]#?systemctl?poweroff
[root@localhost?~]#?systemctl?reboot
[root@localhost?~]#?systemctl?hibernate
[root@localhost?~]#?systemctl?status?httpd?-H?root@192.168.0.12

-H選項(xiàng),指定遠(yuǎn)程主機(jī)的用戶名和密碼。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
[root@localhost?~]#?systemctl?get-default?
multi-user.target

設(shè)置默認(rèn)的運(yùn)行級(jí)別為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.

想要列出所有激活的target,可以使用下面命令:
[root@localhost?~]#?systemctl?list-units?-t?target

[root@localhost?~]#?journalctl

要查看所有引導(dǎo)消息,請(qǐng)運(yùn)行命令
journalctl -b[root@localhost?~]#?journalctl?-b
[root@localhost?~]#?journalctl?-f

[root@localhost?~]#?systemd-analyze
Startup?finished?in?497ms?(kernel)?+?1.836s?(initrd)?+?6.567s?(userspace)?=?8.901s<以上代碼可復(fù)制粘貼,可往左滑>
![]()
最后顯示系統(tǒng)啟動(dòng)時(shí)間為8.901秒。
[root@localhost?~]#?systemd-analyze?blame

[root@localhost?~]#?hostnamectl

推薦閱讀:
推薦一款,比 Navicat 還要好用,功能還很強(qiáng)大的 工具!
“華為天才少年”自制百大Up獎(jiǎng)杯,網(wǎng)友:技術(shù)難度不高侮辱性極強(qiáng)
5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「1024」,即可免費(fèi)獲取?。?/span>
評(píng)論
圖片
表情

