關(guān)于 Linux中systemd的一些筆記
title: 關(guān)于 linux中systemd的一些筆記 tags:
systemctl
linux categories:
Linux toc: true recommend: 1 keywords: Linux uniqueId: '2022-04-03 03:12:18/關(guān)于 Linux中systemd操作Service unit的一些筆記.html' mathJax: false date: 2022-04-03 11:12:18 thumbnail:
title: 關(guān)于 Linux中systemd的一些筆記 tags:
systemctl
Linux categories:
Linux toc: true recommend: 1 keywords: Linux uniqueId: '2022-04-03 03:12:18/關(guān)于 Linux中systemd操作Service unit的一些筆記.html' mathJax: false date: 2022-04-03 11:12:18 thumbnail:
寫在前面
嗯,準(zhǔn)備RHCA,學(xué)習(xí)整理這部分知識(shí)
博文內(nèi)容涉及:
systemd簡(jiǎn)述
對(duì)于unit的信息的介紹
通過systemctl命令控制Service unit的Demo
Service unit配置文件內(nèi)容,權(quán)值的一些介紹
systemd簡(jiǎn)述
對(duì)于unit的信息的介紹
通過systemctl命令控制Service unit的Demo
Service unit配置文件內(nèi)容,權(quán)值的一些介紹
傍晚時(shí)分,你坐在屋檐下,看著天慢慢地黑下去,心里寂寞而凄涼,感到自己的生命被剝奪了。當(dāng)時(shí)我是個(gè)年輕人,但我害怕這樣生活下去,衰老下去。在我看來(lái),這是比死亡更可怕的事。--------王小波
一、Systemd 簡(jiǎn)述
systemd是Linux操作系統(tǒng)的系統(tǒng)和服務(wù)管理器。在系統(tǒng)啟動(dòng)時(shí)或者正在運(yùn)行的系統(tǒng)上的一個(gè)守護(hù)進(jìn)程,systemd負(fù)責(zé)激活系統(tǒng)資源、服務(wù)器守護(hù)程序和其他進(jìn)程,systemd是RHEL 7之后出現(xiàn)的。
守護(hù)進(jìn)程(daemon)是在后臺(tái)執(zhí)行各種任務(wù)的進(jìn)程。通常,守護(hù)程序在引導(dǎo)時(shí)自動(dòng)啟動(dòng)并繼續(xù)運(yùn)行直到系統(tǒng)關(guān)閉或手動(dòng)停止。許多守護(hù)程序的名稱的約定以字母d結(jié)尾。
關(guān)于RHEL啟動(dòng)進(jìn)程管理:
在RHEL 5的時(shí)候,使用 Sys init,啟動(dòng)速度最慢的,無(wú)論進(jìn)程相互之間有無(wú)依賴關(guān)系,都是串行啟動(dòng)過程,
RHEL 6的時(shí)候,使用 Upstart init相對(duì)啟動(dòng)速度快一點(diǎn)有所改進(jìn)。有依賴的進(jìn)程之間依次啟動(dòng)而其他與之沒有依賴關(guān)系的則并行同步啟動(dòng)。
RHEL 7 Systemd與以上都不同。所有進(jìn)程無(wú)論有無(wú)依賴關(guān)系則都是并行啟動(dòng)(很多時(shí)候進(jìn)程沒有真正啟動(dòng)而是只有一個(gè)信號(hào)或者說(shuō)是標(biāo)記而已,在真正利用的時(shí)候才會(huì)真正啟動(dòng))。
Systemd為了解決上文的問題而誕生。它的目標(biāo)是,為系統(tǒng)的啟動(dòng)和管理提供一套完整的解決方案,
RHEL啟動(dòng)的第一個(gè)進(jìn)程(PID 1)就是systemd,提供的功能包括:
并行化功能(同時(shí)啟動(dòng)多個(gè)服務(wù)),可提高系統(tǒng)的啟動(dòng)速度。
按需啟動(dòng)后臺(tái)程序而無(wú)需單獨(dú)的服務(wù)。
自動(dòng)管理服務(wù)依賴關(guān)系,可以防止長(zhǎng)時(shí)間超時(shí)。例如,網(wǎng)絡(luò)相關(guān)服務(wù)在網(wǎng)絡(luò)可用之前不會(huì)嘗試啟動(dòng)。
使用Linux控制組(Cgroup)一起跟蹤相關(guān)進(jìn)程的方法。
systemd Targets 啟動(dòng)級(jí)別
二、systemctl 命令管理系統(tǒng)
systemctl是 Systemd 的主命令,systemctl命令用于管理不同類型的系統(tǒng)對(duì)象,這些對(duì)象稱之為units。
Service unit:用于定義系統(tǒng)服務(wù),文件擴(kuò)展名為.service
Target unit:用于模擬實(shí)現(xiàn)“運(yùn)行級(jí)別”,文件擴(kuò)展名為.target,可以理解Target 就是一個(gè) Unit 組,包含許多相關(guān)的 Unit
Device unit:用于定義內(nèi)核識(shí)別的設(shè)備,文件擴(kuò)展名為.device
Mount unit:用于定義文件系統(tǒng)掛載點(diǎn),文件擴(kuò)展名為.mount
Socket unit:用于標(biāo)識(shí)進(jìn)程間通信用的socket文件,文件擴(kuò)展名為.socket
┌──[root@liruilongs.github.io]-[~] └─$systemctl status cockpit.socket #CocKpit駕駛艙 和SSH類似,用于遠(yuǎn)程控制,類似于阿里云的控制臺(tái)。為初級(jí)管理員提供基本操作,通過web端的控制臺(tái),訪問地址:服務(wù)器IP:9090 ● cockpit.socket - Cockpit Web Service Socket Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:cockpit-ws(8) Listen: [::]:9090 (Stream)
Snapshot unit:管理系統(tǒng)快照,文件擴(kuò)展名為.snapshot
Swap unit:用于標(biāo)識(shí)swap設(shè)備,文件擴(kuò)展名為.swap
Automount unit:文件系統(tǒng)的自動(dòng)掛載點(diǎn),文件擴(kuò)展名為.automount
Path unit:用于根據(jù)文件系統(tǒng)上特定對(duì)象的變化來(lái)啟動(dòng)其他服務(wù),文件擴(kuò)展名為.path
Timer unit:用于管理計(jì)劃任務(wù),文件擴(kuò)展名為.timer
┌──[root@liruilongs.github.io]-[~] └─$systemctl status systemd-tmpfiles-clean.timer #用于定時(shí)清理 `/tmp`,`/var/tmp`之類的臨時(shí)文件 ● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabl> Active: active (waiting) since Sun 2022-04-03 11:24:00 CST; 1h 48min ago Trigger: Mon 2022-04-04 11:39:07 CST; 22h left Docs: man:tmpfiles.d(5) man:systemd-tmpfiles(8) Apr 03 11:24:00 liruilongs.github.io systemd[1]: Started Daily Cleanup of Temporary Directories. ┌──[root@liruilongs.github.io]-[~] └─$
Slice unit:用于資源管理,文件擴(kuò)展名為.slice
1.查看units列表信息
列出loaded(配置文件以加載)的units
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File> .............
命令輸出說(shuō)明:
UNIT:服務(wù)單元名稱。
LOAD: systemd是否正確解析了單元的配置并將該單元加載到內(nèi)存中。
ACTIVE:單元的高級(jí)別激活狀態(tài)。此信息表明單元是否已成功啟動(dòng)。
SUB:單元的低級(jí)別激活狀態(tài)。此信息指示有關(guān)該單完的更多詳細(xì)信息。信息視單元類型、狀態(tài)以及單元的執(zhí)行方式而異。
DESCRIPTION:單元的簡(jiǎn)短描述。
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static -.mount generated boot.mount generated dev-hugepages.mount static dev-mqueue.mount static proc-sys-fs-binfmt_misc.mount static sys-fs-fuse-connections.mount static sys-kernel-config.mount static sys-kernel-debug.mount static tmp.mount disabled systemd-ask-password-console.path static .....
查看timer,系統(tǒng)定時(shí)任務(wù)
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units -i timer 0 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. ┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files -t timer UNIT FILE STATE dnf-makecache.timer enabled #yaml倉(cāng)庫(kù)定時(shí)更新 fstrim.timer disabled systemd-tmpfiles-clean.timer static # 臨時(shí)文件清理 unbound-anchor.timer enabled 4 unit files listed. ┌──[root@liruilongs.github.io]-[~] └─$
查看socket套接字
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-unit-files -t socket UNIT FILE STATE cockpit-wsinstance-http-redirect.socket static cockpit-wsinstance-http.socket static cockpit-wsinstance-https-factory.socket static cockpit-wsinstance-https@.socket static cockpit.socket enabled dbus.socket static
列出類型為service,狀態(tài)為loaded和active的 unit
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units -t service UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service
列出類型為service,狀態(tài)為active和inactive 的unit
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-units --type service --all
┌──[root@liruilongs.github.io]-[~] └─$systemctl --failed -t service UNIT LOAD ACTIVE SUB DESCRIPTION ● kdump.service loaded failed failed Crash recovery kernel arming .....
三、systemctl操作Service unit
┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd ● sshd.service - OpenSSH server daemon #單元的描述信息 Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) #子狀態(tài),加載的配置文件,默認(rèn)開機(jī)自啟 Active: active (running) since Sun 2022-04-03 11:24:03 CST; 2h 16min ago #運(yùn)行狀態(tài),時(shí)間 Docs: man:sshd(8) #幫助文檔 man:sshd_config(5) Main PID: 880 (sshd) #主進(jìn)程 Tasks: 1 (limit: 6044) # 任務(wù)量 Memory: 7.2M #消耗的內(nèi)存 CGroup: /system.slice/sshd.service # 用的什么slice └─880 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes2> Apr 03 11:24:02 liruilongs.github.io systemd[1]: Starting OpenSSH server daemon... #啟動(dòng)日志,什么進(jìn)程引導(dǎo)激活的 Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on 0.0.0.0 port 22. #監(jiān)聽的端口 Apr 03 11:24:03 liruilongs.github.io sshd[880]: Server listening on :: port 22. Apr 03 11:24:03 liruilongs.github.io systemd[1]: Started OpenSSH server daemon. #啟動(dòng)服務(wù) Apr 03 11:25:30 liruilongs.github.io sshd[1112]: Accepted publickey for root from 172.25.254.254 port > #連接日志 Apr 03 11:25:30 liruilongs.github.io sshd[1112]: pam_unix(sshd:session): session opened for user root > lines 1-17/17 (END)
┌──[root@liruilongs.github.io]-[~] └─$systemctl is-active sshd active ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-enabled sshd enabled ┌──[root@liruilongs.github.io]-[~] └─$systemctl is- is-active is-enabled is-failed is-system-running
優(yōu)先級(jí)從高到低
本地配置的系統(tǒng)單元: /etc/systemd/system
運(yùn)行時(shí)配置的系統(tǒng)單元: /run/systemd/system
軟件包安裝的系統(tǒng)單元: /usr/lib/systemd/system
默認(rèn)的服務(wù)配置文件位置:/usr/lib/systemd/system
┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head -2 ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
添加高優(yōu)先級(jí)配置文件:/run/systemd/system
┌──[root@liruilongs.github.io]-[~] └─$cp /usr/lib/systemd/system/sshd.service /run/systemd/system/sshd.service ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head -2 ● sshd.service - OpenSSH server daemon Loaded: loaded (/run/systemd/system/sshd.service; enabled; vendor preset: enabled)
添加高優(yōu)先級(jí)配置文件,/etc/systemd/system
┌──[root@liruilongs.github.io]-[~] └─$cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/ ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl status sshd | head -2 ● sshd.service - OpenSSH server daemon Loaded: loaded (/etc/systemd/system/sshd.service; enabled; vendor preset: enabled) ┌──[root@liruilongs.github.io]-[~] └─$
┌──[root@liruilongs.github.io]-[~] └─$cat /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon #描述信息 Documentation=man:sshd(8) man:sshd_config(5) #幫助文檔 After=network.target sshd-keygen.target #依賴關(guān)系,在...之后執(zhí)行 Wants=sshd-keygen.target # 強(qiáng)依賴 [Service] Type=notify #notify類型 EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config #環(huán)境變量讀取 EnvironmentFile=-/etc/sysconfig/sshd # - 號(hào)為文件可以不存在 ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY # 啟動(dòng)命令 ExecReload=/bin/kill -HUP $MAINPID # reload從新初始化 KillMode=process #設(shè)置在單元停止時(shí),殺死進(jìn)程的方法 process 表示僅殺死主進(jìn)程 Restart=on-failure #僅在服務(wù)進(jìn)程異常退出時(shí)重啟 RestartSec=42s # 42秒后重啟 [Install] WantedBy=multi-user.target #屬于那個(gè)target ┌──[root@liruilongs.github.io]-[~] └─$
手冊(cè)查看,不錯(cuò)的翻譯 systemd.service 中文手冊(cè)
┌──[root@liruilongs.github.io]-[~] └─$man -k systemd
啟動(dòng)停止服務(wù)
systemctl start sshd.service systemctl stop sshd.service
重新加載配置文件
┌──[root@liruilongs.github.io]-[~] └─$systemctl reload sshd #還是原來(lái)的進(jìn)程,只是從新加載配置文件
重啟服務(wù)
┌──[root@liruilongs.github.io]-[~] └─$systemctl restart sshd #一個(gè)新的進(jìn)程
查看服務(wù)狀態(tài)
┌──[root@liruilongs.github.io]-[~] └─$systemctl status httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2022-04-03 11:46:36 CST; 3h 5min ago Docs: man:httpd.service(8) Main PID: 1616 (httpd) Status: "Running, listening on: port 80" Tasks: 213 (limit: 6044) Memory: 33.7M CGroup: /system.slice/httpd.service ├─1616 /usr/sbin/httpd -DFOREGROUND ├─1617 /usr/sbin/httpd -DFOREGROUND ├─1618 /usr/sbin/httpd -DFOREGROUND ├─1619 /usr/sbin/httpd -DFOREGROUND └─1620 /usr/sbin/httpd -DFOREGROUND Apr 03 11:46:35 liruilongs.github.io systemd[1]: Starting The Apache HTTP Server... Apr 03 11:46:36 liruilongs.github.io systemd[1]: Started The Apache HTTP Server. Apr 03 11:46:36 liruilongs.github.io httpd[1616]: Server configured, listening on: port 80
設(shè)置開機(jī)自啟,添加一個(gè)軟鏈接
┌──[root@liruilongs.github.io]-[~] └─$systemctl enable httpd --now #立刻激活 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-enabled httpd enabled ┌──[root@liruilongs.github.io]-[~] └─$systemctl is-active httpd active ┌──[root@liruilongs.github.io]-[~] └─$
設(shè)置服務(wù)開機(jī)不啟動(dòng)
┌──[root@liruilongs.github.io]-[~] └─$systemctl disable httpd Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
服務(wù)禁用
┌──[root@liruilongs.github.io]-[~] └─$systemctl mask httpd Created symlink /etc/systemd/system/httpd.service → /dev/null. ┌──[root@liruilongs.github.io]-[~] └─$systemctl start httpd Failed to start httpd.service: Unit httpd.service is masked. ┌──[root@liruilongs.github.io]-[~] └─$systemctl status httpd | head -2 ● httpd.service Loaded: masked (Reason: Unit httpd.service is masked.)
可以刪除軟鏈接取消禁用
┌──[root@liruilongs.github.io]-[~] └─$ll /etc/systemd/system/httpd.service lrwxrwxrwx. 1 root root 9 Apr 3 16:08 /etc/systemd/system/httpd.service -> /dev/null ┌──[root@liruilongs.github.io]-[~] └─$rm -r /etc/systemd/system/httpd.service rm: remove symbolic link '/etc/systemd/system/httpd.service'? y ┌──[root@liruilongs.github.io]-[~] └─$systemctl daemon-reload ┌──[root@liruilongs.github.io]-[~] └─$systemctl start httpd
可以通過unmask來(lái)取消禁用
┌──[root@liruilongs.github.io]-[~] └─$systemctl mask httpd Created symlink /etc/systemd/system/httpd.service → /dev/null. ┌──[root@liruilongs.github.io]-[~] └─$systemctl unmask httpd Removed /etc/systemd/system/httpd.service. ┌──[root@liruilongs.github.io]-[~] └─$
unit依賴
可以通過如下命令查看unit的啟動(dòng)依賴關(guān)系
systemctl list-dependencies cups.service systemctl list-dependencies cups.service --reverse
查看httpd Service 的正向依賴,在httpd之前啟動(dòng)
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies httpd httpd.service ● ├─-.mount ● ├─httpd-init.service ● ├─system.slice ● └─sysinit.target ● ├─dev-hugepages.mount ● ├─dev-mqueue.mount ● ├─dracut-shutdown.service ● ├─import-state.service ● ├─iscsi-onboot.service ● ├─kmod-static-nodes.service ...... ...... ┌──[root@liruilongs.github.io]-[~] └─$
查看httpd Service 的反向依賴,在httpd之后啟動(dòng)
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies httpd --reverse httpd.service ● └─multi-user.target ● └─graphical.target
查看multi-user.target的正向依賴是否有httpd
┌──[root@liruilongs.github.io]-[~] └─$systemctl list-dependencies multi-user.target | grep httpd ● ├─httpd.service
嗯,關(guān)于systemd和小伙伴們分享到這里,這塊涉及的內(nèi)容很多,這里簡(jiǎn)單介紹,在Linux生態(tài)系統(tǒng)中,systemd被部署到了大多數(shù)的標(biāo)準(zhǔn)Linux發(fā)行版中,只有為數(shù)不多的幾個(gè)發(fā)行版尚未部署。關(guān)于systemd啟動(dòng)過程、對(duì)其他unit的控制和管理、對(duì)系統(tǒng)運(yùn)行級(jí)別等的控制,感興趣小伙伴可以了解下。
Linux
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。