1. 程式人生 > >centos7 systemctl取代service和chkconfig來實現系統管理

centos7 systemctl取代service和chkconfig來實現系統管理

Linux systemctl是一個系統管理守護程序、工具和庫的集合,用於取代System V、service和chkconfig命令)

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

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

daemon命令 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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]
  • 1

引數詳解:

command:
  • 1
  • 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]
  • 1

–reverse是用來檢查尋哪個unit使用了這個unit

應用舉例:

#獲得當前執行級別的target
[[email protected] ~]# systemctl get-default
multi-user.target

#檢視當前執行級別target(mult-user)啟動了哪些服務
[[email protected] ~]# 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
│ └─[email protected]
└─remote-fs.target

#檢視哪些target引用了當前執行級別的target
[[email protected] ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

7、關閉網路服務

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

使用systemctl檢視開啟的sshd服務

[[email protected] 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
  • 1
  • 2
  • 3
  • 4

可以看到系統同時開啟了 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
  • 1
  • 2
  • 3

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

yum -y install net-tools
  • 1

檢視是否關閉22埠

netstat -lnp |grep sshd
  • 1

8、關閉防火牆firewall

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

systemctl stop firewalld.service
systemctl disable firewalld.service
  • 1