CentOS7 的systemctl命令相關

配置systemctl命令tab自動補全

系統版本
[root@test ~]# uname -r
3.10.0-229.el7.x86_64

[root@test ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

執行命令
yum install -y bash-completion
安裝完成后退出終端重新連接服務器后生效

systemctl服務相關

systemctl服務主體存貯目錄:
/usr/lib/systemd/system/

systemctl服務開機啟動鏈接存貯目錄:
/etc/systemd/system/basic.target.wants/

列出所有開機自啟的服務
systemctl list-unit-files|grep enabled

列出所有失敗單元
systemctl –-failed?

列出防火墻服務的自啟狀態
systemctl list-unit-files|grep firewalld

列出所有服務的層級和依賴關系,可以指定某個服務
systemctl list-dependencies [服務名稱]

CentOS 7.x開始

CentOS開始使用systemd服務來代替daemon,原來管理系統啟動和管理系統服務的相關命令全部由systemctl命令來代替。

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

service [服務] start systemctl start [unit type] 啟動服務
service [服務] stop systemctl stop [unit type] 停止服務
service [服務] restart systemctl restart [unit type] 重啟服務

此外還是二個systemctl參數沒有與service命令參數對應
status:參數來查看服務運行情況
reload:重新加載服務,加載更新后的配置文件(并不是所有服務都支持這個參數,比如network.service)

應用舉例:

#啟動網絡服務
systemctl start network.service

#停止網絡服務
systemctl stop network.service

#重啟網絡服務
systemctl restart network.service

#查看網絡服務狀態
systemctl status network.serivce

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

2.1、設置開機啟動/不啟動

daemon命令 systemctl命令 說明
chkconfig [服務] on systemctl enable [unit type] 設置服務開機啟動
chkconfig [服務] off systemctl disable [unit type] 設備服務禁止開機啟動

應用舉例:

#停止cup電源管理服務
systemctl stop cups.service

#禁止cups服務開機啟動
systemctl disable cups.service

#查看cups服務狀態
systemctl status cups.service

#重新設置cups服務開機啟動
systemctl enable cups.service

2.2、查看系統上上所有的服務

命令格式:
systemctl [command] [–type=TYPE] [–all]

參數詳解:

command - list-units:依據unit列出所有啟動的unit。加上 –all 才會列出沒啟動的unit; - list-unit-files:依據/usr/lib/systemd/system/ 內的啟動文件,列出啟動文件列表

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

應用舉例:

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

3、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

4、init 命令與systemctl命令對比

init命令 systemctl命令 說明

init 0 systemctl poweroff 系統關機
init 6 systemctl reboot 重新啟動

與開關機相關的其他命令:

systemctl命令 說明

systemctl suspend 進入睡眠模式
systemctl hibernate 進入休眠模式
systemctl rescue 強制進入救援模式
systemctl emergency 強制進入緊急救援模式

5、設置系統運行級別

5.1、運行級別對應表

init級別 systemctl target
0 shutdown.target
1 emergency.target
2 rescure.target
3 multi-user.target
4 無
5 graphical.target
6 無

此外還是一個getty.target用來設置tty的數量。

5.2、設置運行級別

命令格式:
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 在不重啟的情況下,切換到圖形界面下

6、使用systemctl分析各服務之前的依賴關系

命令格式:
systemctl list-dependencies [unit] [–reverse]
–reverse是用來檢查尋哪個unit使用了這個unit

應用舉例:

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

#查看當前運行級別target(mult-user)啟動了哪些服務
[root@www ~]# systemctl list-dependencies
default.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引用了當前運行級別的target
[root@www ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target

7、關閉網絡服務

在使用systemctl關閉網絡服務時有一些特殊 需要同時關閉unit.servce和unit.socket

使用systemctl查看開啟的sshd服務

[root@www system]#? 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

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

yum -y install net-tools

查看是否關閉22端口

netstat -lnp |grep sshd

8、關閉防火墻firewall

Centos 7.x 中取消了iptables, 用firewall取而代之。要關閉防火墻并禁止開機啟動服務使用下面的命令:

systemctl stop firewalld.service

systemctl disable firewalld.service

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

推薦閱讀更多精彩內容