命令比對:chkconfig、runlevel、init、shutdown、……shell
systemd unit:我把它翻譯成操做組件。
bash
<CentOS or RHEL 7.2.xx>
ssh
1.管理系統服務ide
1.1.對比命令語法
工具
對比下老工具的用法,控制命令:ui
service |
systemctl |
註解 |
---|---|---|
service name start |
systemctl start name.service |
啓動服務 |
service name status |
systemctl status name.service |
察看狀態 |
設置命令:spa
chkconfig |
systemctl |
註解 |
---|---|---|
chkconfig --list name |
systemctl status name.service |
|
chkconfig --list |
systemctl list-unit-files --type service systemctl list-dependencies --after systemctl list-dependencies --beforerest |
|
chkconfig name on |
systemctl enable name.service |
|
chkconfig name off |
systemctl disable name.service |
查看systemctl託管了那些服務
$ systemctl list-unit-files --type service $ systemctl list-unit-files -t service
服務名稱,能夠不帶擴展名:
# systemctl start sshd.service # systemctl start sshd
還能夠使用別名完成某些操做,查看別名以下:
[root@right sbin]# systemctl show runlevel6.target -p Names Names=runlevel6.target reboot.target
1.2.狀態信息
察看當前啓動的服務:
[root@hhh ~]# systemctl list-units --type service
察看全部加載的服務:
[root@hhh ~]# systemctl list-units --type service -all
參看加載的服務,當前設定的狀態:
[root@hhh ~]# systemctl list-unit-files --type service
參看服務的狀態:
[root@hhh ~]# systemctl status sshd [root@hhh ~]# systemctl is-active sshd active [root@hhh ~]# systemctl is-enabled sshd enabled [root@hhh ~]# systemctl status sshd --after [root@hhh ~]# systemctl status sshd --before
1.3.啓動、中止服務
管理一個服務經過操做該服務對應的target。
啓動sshd:
[root@right ~]# systemctl start sshd
中止sshd:
[root@right ~]# systemctl stop sshd
重啓sshd:
[root@right ~]# systemctl restart sshd
1.4.啓用、禁用服務
啓用sshd:
[root@right ~]# systemctl enable sshd Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service. [root@right ~]# systemctl disable sshd Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
查看sshd狀態:
[root@right ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) ......
啓用時,回顯信息是建立了符號連接,要是自己就是啓用的,那麼就不會建立。要是想從新建立該連接:
[root@right ~]# systemctl reenable sshd Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service. Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.
此外,能夠屏蔽服務,以防止其手動啓動或由被其餘服務啓動:
[root@right ~]# systemctl status sshd ● sshd.service Loaded: masked (/dev/null) ......
[root@right ~]# systemctl unmask sshd Removed symlink /etc/systemd/system/sshd.service. [root@right ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) ......
2.systemd targets
System Targets對應的運行級別:
Runlevel |
Target Units |
Description |
---|---|---|
0 |
runlevel0.target poweroff.target |
Shutdown the system. |
1 |
runlevel1.target rescue.target |
Setup a rescue shell. |
2 |
runlevel2.target multi-user.target |
multi-user system. |
3 |
runlevel3.target multi-user.target |
... |
4 |
runlevel4.target multi-user.target |
... |
5 |
runlevel5.target graphical.target |
... |
6 |
runlevel6.target reboot.target |
Shutdown and reboot the system |
查看當前運行級別
[root@right sbin]# systemctl get-default graphical.target
查看當前加載的target
[root@right sbin]# systemctl list-units -type target
修改默認加載target組
[root@right sbin]# systemctl set-default runlevel3.target
修改當前加載target組
[root@right sbin]# systemctl isolate runlevel3.target
進入救援模式:
[root@right sbin]# systemctl rescue
進入緊急模式:
[root@right sbin]# systemctl emergency
3.關機…
對比過去用過的關機命令。
Old Command |
New Command |
Description |
---|---|---|
halt |
systemctl halt |
Halts the system |
poweroff |
systemctl poweroff | Powers off the system |
reboot |
systemctl reboot | Restarts the system |
pm-suspend |
systemctl suspend | Suspends the system |
pm-hibernate |
systemctl hibernate | Hibernates the system |
pm-suspend-hybrid |
systemctl hybrid-sleep |
Hibernates and Suspends the system |
關機(關閉系統、關閉電源):
# systemctl poweroff
重啓系統:
# systemctl reboot
掛起系統(掛起到內存):
# systemctl suspend
休眠(掛起到硬盤):
# systemctl hibernate
4.Controlling on a Remote Machine
容許經過ssh協議控制機器,語法:「systemctl --host user@host command」;「--host root@r」表明選項、及其參數。
[root@right ~]# systemctl -H root@zc35 status firewalld root@zc35's password: ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) [root@right ~]# systemctl -H root@zc35 status sshd root@zc35's password: ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2016-12-16 09:57:32 CST; 3 weeks 2 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 871 CGroup: /system.slice/sshd.service
5.管理「操做組件」
操做組件對應一個操做組件文件,該文件是命令運行時的配置文件,文件名錶明組件操做時系統引用的操做對象。系統管理員的組件目錄位於「/etc/systemd/system/」。
目錄 |
優先級 |
描述 |
---|---|---|
/usr/lib/systemd/system/ |
低 |
rpm包安裝時存放操做組件文件的位置 |
/run/systemd/system/ |
中 |
運行時操做組件存放位置 |
/etc/systemd/system/ |
高 |
被「systemctl enable」建立的組件文件 |
5.1理解操做組件文件的語法
例如「sshd.service」:
[root@right ~]# vi sshd.service [Unit] Description=OpenSSH server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
理解上述文件選項含義:
選項 |
描述 |
---|---|
Description | 描述信息 |
Documentation | 參考文檔 |
After |
指定在誰後邊啓動 |
Requires |
指定強依賴 |
Wants |
指定弱依賴 |
Conflicts |
與「Requires」相反 |
5.2建立操做組件
5.3修改操做組件