Systemd負責啟動Linux 系統資源 和伺服器daemon和其他程序
daemon 在後台執行各種任務的程序,大多程序名稱都已d結尾。 如 : httpd
RHEL 啟動的第一個程序就是systemd,提供的功能包括:
- 並行啟動 (同時啟動多個服務),可提高系統啟動速度
- 按需啟動後台程序而無須單獨的服務
- 自動服務相依管理,防止長時間超時。 如: 網路相關服務在網路可用前不會啟動
- 使用Linux控制組,一起追蹤相關程序。
systemctl 用於管理不同的系統對象,稱之為units
- Service unit : 系統服務, 文件副檔名 .service
- Target unit : 運行級別
- Device unit : 定義核心識別的設備。
- Mount unit : 定義文件掛載點。
- Socket unit: 程序之間通信用的文件。
- Snapshot unit : 管理系統快照。
- Swap unit : 標示swap設備。
- Automount unit : 檔案系統的自動掛載點。
- Path unit : 根據檔案系統上的特定對象變化來啟動其他服務。 <如印表機>
- Timer unit : 管理任務計畫。
==systemctl status systemd-tmpfiles-clean.timer 用於定期清理/tmp 及/var/tmp
- Slice unit : 用於資源管理。 > 系統優化
查看已載入的服務
1
| [root@svr1 ~]# systemctl list-units
|
查看全部載入(含未載入的)
1
| root@svr1 ~]# systemctl list-unit-files
|
通常都是用於查看已載入的
1
| [root@svr1 ~]# systemctl list-units | grep timer
|
unit配置文件目錄:
如sshd在各目錄下都有,則以優先級別1為最高
- 本地配置的系統單元: /etc/systemd/system
- 運行時配置的系統單元: /run/systemd/system
- 套件包安裝的系統單元: /usr/lib/systemd/system
1 2
| [root@svr1 system]# man -k systemd systemd: nothing appropriate. >> 跳出時可執行 mandb 更新資料庫
|
控制系統服務:
查看服務狀態:
1
| [root@svr1 ~]# systemctl status sshd
|
1
| [root@svr1 system]# systemctl is-enabled sshd
|
1
| [root@svr1 system]# systemctl is-active sshd
|
1 2
| [root@svr1 ~]# systemctl enable httpd --now Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
|
1 2
| [root@svr1 ~]# systemctl mask httpd Created symlink /etc/systemd/system/httpd.service → /dev/null.
|
1 2
| [root@svr1 ~]# systemctl start httpd Failed to start httpd.service: Unit httpd.service is masked.
|
- 刪除 配置文件 /etc/systemd/system/httpd.service後 重啟daemon
1 2 3 4
| [root@svr1 ~]# rm -r /etc/systemd/system/httpd.service rm: remove symbolic link '/etc/systemd/system/httpd.service'? y [root@svr1 ~]# [root@svr1 ~]# systemctl daemon-reload
|
- 使用 systemctl unmask > 也是進行刪除動作
1 2
| [root@svr1 ~]# systemctl unmask httpd Removed "/etc/systemd/system/httpd.service".
|
- 看multi-user.target 啟動前要啟動哪些服務
1
| [root@svr1 ~]# systemctl list-dependencies multi-user.target
|
multi-user.target
● ├─atd.service
● ├─auditd.service
● ├─avahi-daemon.service
● ├─chronyd.service
● ├─crond.service
● ├─cups.path
● ├─cups.service
● ├─firewalld.service
● ├─httpd.service
● ├─irqbalance.service
● ├─kdump.service
● ├─libstoragemgmt.service
○ ├─libvirtd.service
● ├─mcelog.service
○ ├─mdmonitor.service
● ├─ModemManager.service
● ├─NetworkManager.service
○ ├─ostree-readonly-sysroot-migration.service
● ├─plymouth-quit-wait.service
○ ├─plymouth-quit.service
● ├─rpcbind.service
● ├─rsyslog.service
● ├─run-vmblock\x2dfuse.mount
○ ├─smartd.service
- 查看 multi-user-target 反向依賴關係,啟動
1
| [root@svr1 ~]# systemctl list-dependencies multi-user.target --reverse
|
multi-user.target
● └─graphical.target
啟動graphical.target前必須啟動 multi-user.target
- 切換system target 方式 (此例為切換文字及圖形介面)
1 2
| [root@svr1 ~]# systemctl isolate multi-user.target [root@svr1 ~]# systemctl isolate graphical.target
|
1 2
| [root@svr1 ~]# systemctl set-default graphical.target [root@svr1 ~]# systemctl set-default multi-user.target
|
#Linux回顧