一,systemd的用途?
Systemd 是 Linux 系統工具,用來啟動守護進程,已成為大多數發行版的標准配置
Systemd 的優點是功能強大,使用方便,
缺點是體系龐大,非常復雜
在centos6中,系統的啟動管理使用initd,
從centos7開始,systemd成為了系統的默認啟動守護進程
systemctl是systemd的管理控制工具
附上systemd的架構圖:(來自網絡)
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看systemctl所屬的包
[root@blog ~]$ whereis systemctl systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz [root@blog ~]$ rpm -qf /usr/bin/systemctl systemd-239-13.el8_0.5.x86_64
三,查看systemctl的版本和幫助
1,查看版本
[root@blog ~]$ systemctl --version systemd 239 +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2
default-hierarchy=legacy
2,查看幫助
[root@blog ~]$ systemctl --help
四,systemd的核心概念unit是什么?
在systemd中,unit表示不同類型的systemd對象,通過配置文件進行標識和配置;
文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息
五,Systemd中Unit 文件的存放目錄
1,
Unit 文件按照 Systemd 約定,被放置指定的三個系統目錄之一。
三個目錄是有優先級的,如下所示,越上面的優先級越高。
在三個目錄中有同名文件的時候,只有優先級最高的目錄里的文件會被使用。
-
/etc/systemd/system:系統或用戶自定義的配置文件
-
/run/systemd/system:軟件運行時生成的配置文件
-
/usr/lib/systemd/system:系統或第三方軟件安裝時添加的配置文件。
2,
Systemd 默認從目錄 /etc/systemd/system/ 讀取配置文件,
里面存放的大部分文件都是符號鏈接,指向目錄 /usr/lib/systemd/system/
說明1:
centos8中,/usr/lib/systemd/system 也是lib/systemd/system
因為 /lib目錄已經變成了 /usr/lib的一個符號鏈接
[root@blog systemd]# ll / | grep lib lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
說明2:
/etc/systemd/system目錄下通常不放置unit文件本身,
而是放置/usr/lib/systemd/system中unit文件的符號鏈接
六,unit的類型
1,所有類型
Service unit:系統服務
Target unit:多個 Unit 構成的一個組
Device Unit:硬件設備
Mount Unit:文件系統的掛載點
Automount Unit:自動掛載點
Path Unit:文件或路徑
Scope Unit:不是由 Systemd 啟動的外部進程
Slice Unit:進程組
Socket Unit:進程間通信的 socket
Swap Unit:swap 文件
Timer Unit:定時器
snapshot Unit:表示由 systemctl snapshot 命令創建的 Systemd Units 運行狀態快照
2,原始文檔請參見:
[root@centos8 ~]# man systemd ... The following unit types are available: 1. Service units, which start and control daemons and the processes they consist of. For details, see systemd.service(5). 2. Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation. For details about socket units, see systemd.socket(5), for details on socket-based activation and other forms of activation, see daemon(7). 3. Target units are useful to group units, or provide well-known synchronization points during boot-up, see systemd.target(5). 4. Device units expose kernel devices in systemd and may be used to implement device-based activation. For details, see systemd.device(5). 5. Mount units control mount points in the file system, for details see systemd.mount(5). 6. Automount units provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up. See systemd.automount(5). 7. Timer units are useful for triggering activation of other units based on timers. You may find details in systemd.timer(5). 8. Swap units are very similar to mount units and encapsulate memory swap partitions or files of the operating system. They are described in systemd.swap(5). 9. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5). 10. Slice units may be used to group units which manage system processes (such as service and scope units) in a hierarchical tree for resource management purposes. See systemd.slice(5). 11. Scope units are similar to service units, but manage foreign processes instead of starting them as well. See systemd.scope(5).
七,例子一:管理服務的常用命令:
1,停止firewalld服務
# status 查詢服務的狀態
[root@centos8 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-04-07 17:22:23 CST; 2min 0s ago Docs: man:firewalld(1) Main PID: 1125 (firewalld) Tasks: 2 (limit: 23837) Memory: 31.6M CGroup: /system.slice/firewalld.service └─1125 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid 4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon... 4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.
可以看到狀態是active
#is-active 只返回是否在運行中
[root@centos8 ~]# systemctl is-active firewalld
active
#stop: 用來停止服務
[root@centos8 ~]# systemctl stop firewalld
#再次查詢狀態:注意已變成了inactive
[root@centos8 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead) since Tue 2020-04-07 17:24:34 CST; 3s ago Docs: man:firewalld(1) Process: 1125 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS) Main PID: 1125 (code=exited, status=0/SUCCESS) 4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon... 4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon. 4月 07 17:24:33 centos8 systemd[1]: Stopping firewalld - dynamic firewall daemon... 4月 07 17:24:34 centos8 systemd[1]: Stopped firewalld - dynamic firewall daemon.
#is-active 只返回是否在運行中
[root@centos8 ~]# systemctl is-active firewalld
inactive
2,啟動firewalld服務
#start 用來啟動服務
[root@centos8 ~]# systemctl start firewalld
#is-active 只返回是否在運行中
[root@centos8 ~]# systemctl is-active firewalld
active
3,禁止firewalld開機自啟動
#is-enabled 判斷是否開機自啟動
[root@centos8 ~]# systemctl is-enabled firewalld
enabled
#disable 用來禁止開機自啟動
[root@centos8 ~]# systemctl disable firewalld Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
#is-enabled 判斷是否開機自啟動
[root@centos8 ~]# systemctl is-enabled firewalld
disabled
4,實現firewalld開機自啟動
#eable 用來實現開機自啟動
[root@centos8 ~]# systemctl enable firewalld Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service. Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
可以看到:使開機自啟動,systemctl就是給service文件在/etc/systemd/system/multi-user.target.wants目錄下創建了符號鏈接
#is-enabled 判斷是否開機自啟動
[root@centos8 ~]# systemctl is-enabled firewalld
enabled
5,重新啟動firewalld服務
[root@centos8 ~]# systemctl restart firewalld
6,注銷cups服務
#mask用來注銷服務,
[root@centos8 ~]# systemctl mask cups Created symlink /etc/systemd/system/cups.service → /dev/null.
說明:可以看到它的操作是把cups服務創建成為了/dev/null的符號鏈接
看是否還可以做enable和start操作?
[root@centos8 ~]# systemctl enable cups Failed to enable unit: Unit file /etc/systemd/system/cups.service is masked. [root@centos8 ~]# systemctl start cups Failed to start cups.service: Unit cups.service is masked.
系統都會給出提示已經masked
7,取消注銷cups服務
[root@centos8 ~]# systemctl unmask cups
Removed /etc/systemd/system/cups.service.
八,例子二:對所有unit的操作
1,列出所有ative的unit,注意:列出的unit是顯示是否active
[root@centos8 ~]# systemctl list-units
說明:systemctl 與 systemctl list-units功能相同
2,列出所有unit,包括inactive狀態的
[root@centos8 ~]# systemctl list-units --all
3,列出所有未運行的unit,即狀態是inactive的
[root@blog ~]# systemctl list-units --all --state=inactive
4,列出當前活動狀態(active)的所有服務單元
[root@centos8 ~]# systemctl list-units --type=service
5,列出所有狀態(包括active和inactive)的所有服務unit
[root@centos8 ~]# systemctl list-units --type=service --all
6,列出所有的unit文件,注意:這里顯示是否enabled
[root@centos8 ~]# systemctl list-unit-files
針對四種state的說明:
enabled:已建立自啟動鏈接
disabled:沒建立自啟動鏈接
static:該配置文件沒有[Install]部分(無法執行),只能作為其他配置文件的依賴
masked:該配置文件被禁止建立啟動鏈接
7,列出一個服務所依賴的單元
#list-dependencies 列出一個 Unit 的所有依賴
[root@centos8 ~]# systemctl list-dependencies firewalld.service
8,查看unit文件
#cat : 用來指定要查看的unit文件
[root@blog ~]# systemctl cat openresty.service # /usr/lib/systemd/system/openresty.service [Unit] Description=The OpenResty Application Platform After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/openresty/nginx/logs/nginx.pid ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t ExecStart=/usr/local/openresty/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
9,重新加載所有修改過的unit文件
#daemon-reload : 當我們手動修改/添加 unit文件后,需要執行daemon-reload
[root@blog ~]# systemctl daemon-reload
10,顯示指定unit的所有底層參數
[root@blog ~]# systemctl show openresty.service
也可只看其中的一項:
[root@blog ~]# systemctl show -p PIDFile openresty.service
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
九,service文件的格式說明:
1,看一個例子:
# openresty的service文件
[root@centos8 ~]# more /usr/lib/systemd/system/openresty.service [Unit] Description=The OpenResty Application Platform After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/openresty/nginx/logs/nginx.pid ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t ExecStart=/usr/local/openresty/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
2,[Unit]
區塊
[Unit]
區塊通常是配置文件的第一個區塊,用來定義 Unit 的元數據,以及配置與其他 Unit 的關系
Description:簡短描述
Documentation:文檔地址
Requires:當前 Unit 依賴的其他 Unit,如果它們沒有運行,當前 Unit 會啟動失敗.(強依賴)
Wants:與當前 Unit 配合的其他 Unit,如果它們沒有運行,當前 Unit 不會啟動失敗.(弱依賴)
BindsTo:與Requires類似,它指定的 Unit 如果退出,會導致當前 Unit 停止運行
Before:如果該字段指定的 Unit 也要啟動,那么必須在當前 Unit 之后啟動
After:如果該字段指定的 Unit 也要啟動,那么必須在當前 Unit 之前啟動
Conflicts:這里指定的 Unit 不能與當前 Unit 同時運行
Condition...:當前 Unit 運行必須滿足的條件,否則不會運行
Assert...:當前 Unit 運行必須滿足的條件,否則會報啟動失敗
3,[Install]
通常是配置文件的最后一個區塊,用來定義如何啟動,以及是否開機啟動
WantedBy:它的值是一個或多個 Target,當前 Unit 激活時(enable)符號鏈接會放入/etc/systemd/system目錄下面以 Target 名 + .wants后綴構成的子目錄中(弱依賴)
RequiredBy:它的值是一個或多個 Target,當前 Unit 激活時,符號鏈接會放入/etc/systemd/system目錄下面以 Target 名 + .required后綴構成的子目錄中(強依賴)
Alias:當前 Unit 可用於啟動的別名
Also:當前 Unit 激活(enable)時,會被同時激活的其他 Unit
4,[Service]
區塊用來 Service 的配置,只有 Service 類型的 Unit 才有這個區塊
Type:定義啟動時的進程行為它有以下幾種值
Type=simple:默認值,執行ExecStart指定的命令,啟動主進程
Type=forking:以 fork 方式從父進程創建子進程,創建后父進程會立即退出
Type=oneshot:一次性進程,Systemd 會等當前服務退出,再繼續往下執行
Type=dbus:當前服務通過D-Bus啟動
Type=notify:當前服務啟動完畢,會通知Systemd,再繼續往下執行
Type=idle:若有其他任務執行完畢,當前服務才會運行
ExecStart:啟動當前服務的命令
ExecStartPre:啟動當前服務之前執行的命令
ExecStartPost:啟動當前服務之后執行的命令
ExecReload:重啟當前服務時執行的命令
ExecStop:停止當前服務時執行的命令
ExecStopPost:停止當其服務之后執行的命令
RestartSec:自動重啟當前服務間隔的秒數
Restart:定義何種情況 Systemd 會自動重啟當前服務,可能的值包括always(總是重啟)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
TimeoutSec:定義 Systemd 停止當前服務之前等待的秒數
Environment:指定環境變量
PrivateTmp: 否使用私有的tmp目錄
十,查看centos的版本
[root@blog ~]$ cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)
十一,systemctl管理系統的運行級別
參見