CentOS7中systemctl的使用

CentOS 7.x開始,CentOS開始使用systemd服務(wù)來代替daemon,原來管理系統(tǒng)啟動(dòng)和管理系統(tǒng)服務(wù)的相關(guān)命令全部由systemctl命令來代替。

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

daemon命令systemctl命令說明

service [服務(wù)] startsystemctl start [unit type]啟動(dòng)服務(wù)

service [服務(wù)] stopsystemctl stop [unit type]停止服務(wù)

service [服務(wù)] restartsystemctl restart [unit type]重啟服務(wù)

此外還是二個(gè)systemctl參數(shù)沒有與service命令參數(shù)對應(yīng)

status:參數(shù)來查看服務(wù)運(yùn)行情況

reload:重新加載服務(wù),加載更新后的配置文件(并不是所有服務(wù)都支持這個(gè)參數(shù),比如network.service)

應(yīng)用舉例:

#啟動(dòng)網(wǎng)絡(luò)服務(wù)systemctl start network.service#停止網(wǎng)絡(luò)服務(wù)systemctl stop network.service#重啟網(wǎng)絡(luò)服務(wù)systemctl restart network.service#查看網(wǎng)絡(luò)服務(wù)狀態(tài)systemctl status network.serivce

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

2.1、設(shè)置開機(jī)啟動(dòng)/不啟動(dòng)

daemon命令systemctl命令說明

chkconfig [服務(wù)] onsystemctl enable [unit type]設(shè)置服務(wù)開機(jī)啟動(dòng)

chkconfig [服務(wù)] offsystemctl disable [unit type]設(shè)備服務(wù)禁止開機(jī)啟動(dòng)

應(yīng)用舉例:

#停止cup電源管理服務(wù)systemctl stop cups.service#禁止cups服務(wù)開機(jī)啟動(dòng)systemctl disable cups.service#查看cups服務(wù)狀態(tài)systemctl status cups.service#重新設(shè)置cups服務(wù)開機(jī)啟動(dòng)systemctlenablecups.service

2.2、查看系統(tǒng)上上所有的服務(wù)

命令格式:

systemctl [command] [–type=TYPE] [–all]

參數(shù)詳解:

command- list-units:依據(jù)unit列出所有啟動(dòng)的unit。加上 –all 才會(huì)列出沒啟動(dòng)的unit; - list-unit-files:依據(jù)/usr/lib/systemd/system/ 內(nèi)的啟動(dòng)文件,列出啟動(dòng)文件列表

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

應(yīng)用舉例:

systemctl命令說明

systemctl列出所有的系統(tǒng)服務(wù)

systemctl list-units列出所有啟動(dòng)unit

systemctl list-unit-files列出所有啟動(dòng)文件

systemctl list-units –type=service –all列出所有service類型的unit

systemctl list-units –type=service –all grep cpu列出 cpu電源管理機(jī)制的服務(wù)

systemctl list-units –type=target –all列出所有target

3、systemctl特殊的用法

systemctl命令說明

systemctl is-active [unit type]查看服務(wù)是否運(yùn)行

systemctl is-enable [unit type]查看服務(wù)是否設(shè)置為開機(jī)啟動(dòng)

systemctl mask [unit type]注銷指定服務(wù)

systemctl unmask [unit type]取消注銷指定服務(wù)

應(yīng)用舉例:

#查看網(wǎng)絡(luò)服務(wù)是否啟動(dòng)systemctl is-active network.service#檢查網(wǎng)絡(luò)服務(wù)是否設(shè)置為開機(jī)啟動(dòng)systemctl is-enable network.service#停止cups服務(wù)systemctl stop cups.service#注銷cups服務(wù)systemctl mask cups.service#查看cups服務(wù)狀態(tài)systemctl status cups.service#取消注銷cups服務(wù)systemctl unmask cups.service

4、init 命令與systemctl命令對比

init命令systemctl命令說明

init 0systemctl poweroff系統(tǒng)關(guān)機(jī)

init 6systemctl reboot重新啟動(dòng)

與開關(guān)機(jī)相關(guān)的其他命令:

systemctl命令說明

systemctl suspend進(jìn)入睡眠模式

systemctl hibernate進(jìn)入休眠模式

systemctl rescue強(qiáng)制進(jìn)入救援模式

systemctl emergency強(qiáng)制進(jìn)入緊急救援模式

5、設(shè)置系統(tǒng)運(yùn)行級別

5.1、運(yùn)行級別對應(yīng)表

init級別systemctl target

0shutdown.target

1emergency.target

2rescure.target

3multi-user.target

4無

5graphical.target

6無

此外還是一個(gè)getty.target用來設(shè)置tty的數(shù)量。

5.2、設(shè)置運(yùn)行級別

命令格式:

systemctl[command][unit.target]

參數(shù)詳解:

command:

get-default :取得當(dāng)前的target

set-default :設(shè)置指定的target為默認(rèn)的運(yùn)行級別

isolate :切換到指定的運(yùn)行級別

unit.target :為5.1表中列出的運(yùn)行級別

systemctl命令說明

systemctl get-default獲得當(dāng)前的運(yùn)行級別

systemctl set-default multi-user.target設(shè)置默認(rèn)的運(yùn)行級別為mulit-user

systemctl isolate multi-user.target在不重啟的情況下,切換到運(yùn)行級別mulit-user下

systemctl isolate graphical.target在不重啟的情況下,切換到圖形界面下

6、使用systemctl分析各服務(wù)之前的依賴關(guān)系

命令格式:

systemctl list-dependencies[unit][–reverse]

–reverse是用來檢查尋哪個(gè)unit使用了這個(gè)unit

應(yīng)用舉例:

#獲得當(dāng)前運(yùn)行級別的target[root@www ~]# systemctl get-defaultmulti-user.target#查看當(dāng)前運(yùn)行級別target(mult-user)啟動(dòng)了哪些服務(wù)[root@www ~]# systemctl list-dependenciesdefault.target├─abrt-ccpp.service├─abrt-oops.service├─vsftpd.service├─basic.target│ ├─alsa-restore.service│ ├─alsa-state.service.....(中間省略).....│ ├─sockets.target│ │ ├─avahi-daemon.socket│ │ ├─dbus.socket.....(中間省略).....│ ├─sysinit.target│ │ ├─dev-hugepages.mount│ │ ├─dev-mqueue.mount.....(中間省略).....│ └─timers.target│? └─systemd-tmpfiles-clean.timer├─getty.target│ └─getty@tty1.service└─remote-fs.target#查看哪些target引用了當(dāng)前運(yùn)行級別的target[root@www ~]# systemctl list-dependencies --reversedefault.target└─graphical.target

7、關(guān)閉網(wǎng)絡(luò)服務(wù)

在使用systemctl關(guān)閉網(wǎng)絡(luò)服務(wù)時(shí)有一些特殊 需要同時(shí)關(guān)閉unit.servce和unit.socket

使用systemctl查看開啟的sshd服務(wù)

[root@www system]#? systemctl list-units --all | grep sshdsshd-keygen.service loaded inactive dead? ? ? ? OpenSSH Server Key Generationsshd.service? ? ? ? loaded active? running? ? OpenSSH server daemonsshd.socket? ? ? ? loaded inactive dead? ? ? ? OpenSSH Server Socket

可以看到系統(tǒng)同時(shí)開啟了sshd.service和sshd.socket, 如果只閉關(guān)了sshd.service那么sshd.socket還在監(jiān)聽網(wǎng)絡(luò),在網(wǎng)絡(luò)上有要求連接 sshd 時(shí)就會(huì)啟動(dòng)sshd.service。因此如果想完全關(guān)閉sshd服務(wù)的話,需要同時(shí)停用sshd.service和sshd.socket。

systemctl stop sshd.service

systemctl stop sshd.socket

systemctl disable sshd.service sshd.socket

由于centos 7.x默認(rèn)沒有安裝net-tools,因此無法使用netstat 來查看主機(jī)開發(fā)的商品。需要通過yum安裝來獲得該工具包:

yum -y install net-tools

查看是否關(guān)閉22端口

netstat -lnp |grep sshd

8、關(guān)閉防火墻firewall

Centos 7.x 中取消了iptables, 用firewall取而代之。要關(guān)閉防火墻并禁止開機(jī)啟動(dòng)服務(wù)使用下面的命令:

systemctl stop firewalld.service

systemctl disable firewalld.service

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,565評論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,319評論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內(nèi)容