systemd和systemctl(centos7)
Systemd是一種新的linux系統服務管理器。它替換了init系統,能夠管理系統啟動過程和一些系統服務,一旦啟動起來,就將監管整個系統。在centos7系統中,PID 1被systemd所使用了:如下,在centos6中:
init─┬─NetworkManager
├─abrtd
├─acpid
├─atd
├─auditd─┬─audispd─┬─sedispatch
│ │ └─{audispd}
│ └─{auditd}
├─automount───4*[{automount}]
├─certmonger
├─crond
├─cupsd
├─dbus-daemon───{dbus-daemon}
├─dhclient
├─fcoemon
├─hald─┬─hald-runner─┬─hald-addon-acpi
│ │ └─hald-addon-inpu
│ └─{hald}
├─irqbalance
├─lldpad
├─master─┬─pickup
│ └─qmgr
├─mcelog
├─6*[mingetty]
├─modem-manager
├─rpc.statd
├─rpcbind
├─rsyslogd───3*[{rsyslogd}]
├─sshd─┬─sshd───bash───pstree
│ └─sshd───bash
├─udevd───2*[udevd]
├─wpa_supplicant
└─xinetd
在centos7中:
systemd─┬─abrt-watch-log
├─abrtd
├─agetty
├─atd
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon───{dbus-daemon}
├─irqbalance
├─lsmd
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─rngd
├─rsyslogd───2*[{rsyslogd}]
├─smartd
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
ststemd可以并行地啟動系統服務進程,并且最初僅啟動確實被依賴的服務,極大減少了系統的引導時間,這也就是為什么centos7系統啟動速度比centos6快許多的原因了。
一、知識整理
1、在systemd的管理體系里,以前的運行級別的概念被新的運行目標(target)所取代。tartget的命令類似于multi-user.target等這種形式,由于不再使用runlevel概念,所以/etc/inittab也不再被系統使用。而在systemd的管理體系里面,默認的target(相當于以前的默認運行級別)是通過軟鏈接實現的:
[root@localhost system]# ll 總用量 8 drwxr-xr-x. 2 root root 30 7月 19 05:55 basic.target.wants lrwxrwxrwx. 1 root root 41 9月 19 14:56 default.target -> /usr/lib/systemd/system/multi-user.target
在/lib/systemd/system下面定義runlevelX.target文件目的主要是為了能夠兼容以前的運行級別,事實上/lib/systemd/system/runlevel3.target,同樣是被鏈接到multi-user.target。
2、在systemd管理體系中,被管理的deamon(守護進程)稱作unit(單元),對于單元的管理是通過命令systemctl來進行控制的。unit表示不同類型的systemd對象,通過配置文件進行標識和配置;文件主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息。
3、systemd新特性:
系統引導時實現服務并行啟動;
按需啟動守護進程;
自動化的服務依賴關系
同時采用socket式與D-Bus總線式激活服務;
系統狀態快照。
4、配置文件:
每個服務最主要的啟動腳本設置,類似于之前的/etc/init.d/
[root@localhost system]# cd /usr/lib/systemd/system [root@localhost system]# ls abrt-ccpp.service lvm2-lvmetad.socket smartd.service abrtd.service lvm2-lvmpolld.service snmpd.service abrt-oops.service lvm2-lvmpolld.socket snmptrapd.service abrt-pstoreoops.service lvm2-monitor.service sockets.target
系統執行過程中所產生的服務腳本,比上面目錄優先運行:
[root@localhost system]# cd /run/systemd/system/ [root@localhost system]# ls session-1.scope session-1.scope.d
管理員建立的執行腳本,類似于/etc/rc.d/rcN.d/SXX類的功能,比上面目錄優先運行:
[root@localhost system]# cd /etc/systemd/system/ [root@localhost system]# ls basic.target.wants default.target.wants multi-user.target.wants sysinit.target.wants default.target getty.target.wants sockets.target.wants system-update.target.wants
5、unit類型:使用如下命令查看:
[root@localhost system]# systemctl -t help Available unit types:
service :文件擴展名為.service,用于定義系統服務
socket :用于標識進程間通信用的socket文件,也可在系統啟動時,延遲啟動服務,實現按需啟動。
busname
target :用于模擬實現運行級別;
snapshot :.snapshot,管理系統快照;
device :.device,用于定義內核識別的設備;
mount :.mount,定義文件系統掛載點
automount :文件系統的自動掛載點
swap :.swap,用于標識swap設備;
timer
path :用于定義文件系統中的一個文件或者目錄使用,常用于當文件系統變化時,延遲激活服務,如spool目錄。
slice
scope
6、關鍵特性:
基于socket的激活機制,socket與程序分離;
基于bus的激活機制
基于device的激活機制
基于path的激活機制;
系統快照:保存個unit的當前狀態信息與持久存儲設備中;
向后兼容sysv init腳本,放在/etc/init.d/
不兼容:systemctl的命令是固定不變的;
非由systemd啟動的服務,systemctl無法與之通信。
7、centos的系統啟動:
設置內核參數,只影響當次啟動:在選擇內核界面按e。在linux16一行后添加:
systemd.unit= XXX.target|emergency.target|recure.target
當出現文件系統損壞、掛載文件損壞等情況后,進入emergency模式修復,此模式比rescue模式功能更少。
修復grub2:修復配置文件:grub2-mkconfig > /boot/grub2/grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg
修復grub:grub2-install /dev/sda BIOS環境
grub2-install UEFI環境
一、命令詳解和事例
1、systemctl命令:
用法:systemctl [OPT] COMMAND [NAME]…
啟動服務:service NAME start = systemctl start NAME.service
停止服務:service NAME stop = systemctl stop NAME.service
重啟服務:service NAME restart = systemctl restart NAME.service
服務狀態:service NAME status = systemctl status NAME.service
條件式重啟,已啟動才重啟,否則不作操作:service NAME condrestart = systemctl try-restart NAME.service
重載或重啟服務,先加載再啟動:systemctl reload-or-restart NAME.service
重載或條件式重啟:systemctl reload-or-try-restart NAME.service
禁止自動和手動啟動:systemctl mask NAME.service
取消禁止:systemctl unmask NAME.service
查看某服務當前激活與否的狀態:systemctl is-active NAME.service
查看所有已經激活的服務:systemctl list-unit –type|-t service
查看所有服務:systemctl list-units –type service –all|-a
chkconfig命令的對應關系:
設定某服務開機自啟動:
chkconfig NAME on==>systemctl enable NAME.service
設定某服務開機禁止啟動:
chkconfig NAME off==> systemctl disable NAME.service
查看所有服務的開機自啟動狀態:
chkconfig –list==> systemctl list-unit-files –type service
[root@localhost system]# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@localhost system]# systemctl disable httpd.service Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service. [root@localhost system]# systemctl mask httpd.service Created symlink from /etc/systemd/system/httpd.service to /dev/null. [root@localhost system]# systemctl unmask httpd.service Removed symlink /etc/systemd/system/httpd.service. [root@localhost system]# systemctl list-units --type service UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool atd.service loaded active running Job spooling tools
列出該服務在哪些運行級別下啟用和禁止:
chkconfig sshd -list==>ls /etc/systemd/system/*.wants/sshd.service
查看服務是否開機啟動:systemctl is-enabled NAME.service
[root@localhost system]# systemctl is-enabled httpd.service disabled
其它命令:查看服務的依賴關系
systemctl list-dependencies NAME.service
殺掉進程:systemctl kill (進程名)
列出失敗的任務:systemctl –failed –type=service
2、服務狀態:
[root@localhost system]# systemctl list-units --type service --all UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT abrt-xorg.service loaded inactive dead ABRT Xorg log watcher
loaded:unit配置文件已處理
active(running):一次或多次持續處理的運行
active(exited):成功完成一次性配置
active(waiting):運行中,等待一個事件
inactive:不運行
enable:開機啟動
disable:開機不啟動
static:開機不啟動,但可以被另一個啟用的服務激活。
3、運行級別:
0 :關機:poweroff.target
1 :單用戶模式:rescue.target
2 :默認不啟用NFS的多用戶:multi-user.target
3 :完全的多用戶:multi-user.target
4 :保留:mult-user.target
5 :圖形:graphical.target
6 :重啟:reboot.target
查看依賴性:
[root@localhost system]# systemctl list-dependencies graphical.target graphical.target ● ├─display-manager.service ● ├─network.service ● ├─systemd-update-utmp-runlevel.service ● └─multi-user.target ● ├─abrt-ccpp.service ● ├─abrt-oops.service ● ├─abrt-vmcore.service
級別切換:systemctl isolate NAME.target;只有當/lib/systemd/system/*.target文件中AllowIsolate=yes才能切換(修改文件需執行systemctl deamon-reload才能生效)。
獲取默認運行級別:systemctl get-default。
修改默認運行級別:systemctl set-default NAME.target
切換至救援模式:systemctl rescue
切換至緊急救援模式:systemctl emergency
關機:systemctl halt|poweroff
重啟:systemctl reboot
掛起:systemctl suspend
休眠:systemctl hibernate
休眠并掛起:systemctl hybrid-sleep
4、Centos7引導順序:
UEFI或BIOS初始化,運行POST開機自檢
選擇啟動設備;
引導裝載程序,centos7是grub2;
加載裝載程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg;
加載initramfs驅動模塊;
加載內核選項;
內核初始化,centos7使用system代替init;
執行initrd.target所有單元,包括掛載/etc/fstab;
從initramfs根文件系統切換到磁盤根目錄;
systemd執行默認target配置,配置文件為/etc/systemd/default.target /etc/systemd/system。
systemd執行sysinit.target初始化系統及basic.target準備操作系統;
systemd啟動multi-user.target下的本機與服務器服務;
systemd執行multi-user.target下的/etc/rc.d/rc.local;
systemd執行multi-user.target下的getty.target及登入服務;
systemd執行graphical需要的服務。
5、Unit文件格式:
/etc/systemd/system:系統管理員和用戶使用;
/usr/lib/systemd/system:發行版打包者使用;
默認單位是秒;通常由三部分組成:
[Unit]:定義與unit類型無關的通用選項;用于提供unit的描述信息,unit行為及依賴關系;
常用的選項:Description:描述信息
After:定義unit的啟動次序,表示當前unit應該晚于哪些unit啟動,其功能與Before相反;
Requires:依賴到的其它units,強依賴,被依賴的units無法激活時,當前unit即無法激活;
Wants:依賴到的其它units,弱依賴;
Conflicts:定義units間的沖突關系。
[Service]:與特定類型相關的專用選項;此處為service類型;
常用選項:Type:定義影響ExecStart及相關參數功能的unit進程啟動類型;
simple:默認值,這個daemon主要由ExecStart接的指令串來啟動,啟動后常駐于內存中;
forking:由ExacStart啟動的程序透過spawns延伸出其它子程序來作為此deamon的主要服務。原生父程序在啟動結束后就會終止。
oneshot:與simple類似,不過這個程序在完成工作后就結束,不常駐內存;
dbus:與simple類似,但這個daemon必須要在取得一個D-Bus的名稱后,才會繼續運作。因此通常也要同時設定BusName=才行;
notify:在啟動完成后會發送一個通知消息。還需要配合NotifyAccess來讓Systemd接收消息;
idle:與simple類似,要執行這個daemon必須要所有的工作都順利執行完畢后才會執行。這類的daemon通常是開機到最后才執行即可的服務。
EnvironmentFile:環境配置文件;
ExecStart:指明啟動unit要運行命令或腳本的絕對路徑;
ExecStartPre:在ExecStart之前運行;
ExecStartPost:在ExecStart之后運行;
ExecStop:指明停止unit要運行的命令或腳本;
Restart:當設定Restart=1時,則當次daemon服務意外終止后,會再次自動啟動。
[Install]:定義由”systemctl enable”以及”systemctl disable”命令在實現服務啟動或禁用時用到的一些選項。
Alias:別名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依賴,強依賴;
WantedBy:被哪些units所依賴,弱依賴;
Also:安裝本服務的時候還要安裝別的相關服務。
二、課后練習
1、為編譯安裝的httpd服務,實現service unit文件。
編譯安裝,更改配置文件。在/etc/systemd/system中編輯配置文件:
[root@localhost system]# vim httpd.service [Unit] Description=apache2 Requires=atd.service [Service] Type=simple ExecStart=/usr/local/bin/apachectl [Install] WantedBy=multi-user.target
重載并啟動:
[root@localhost system]# systemctl daemon-reload [root@localhost system]# systemctl start httpd [root@localhost system]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 52 10.1.54.250:22 10.1.252.66:56929 ESTABLISHED tcp 0 0 10.1.54.250:22 10.1.252.66:57750 ESTABLISHED tcp6 0 0 :::80 :::* LISTEN
2、破解centos7的root口令:啟動時按e進入編輯模式;在linux16行后添加參數rd.break;
再按ctrl+x啟動。啟動后重新按照rw格式掛載sysroot;切換根,更改root密碼。在根下創建.autorelabel空文件,使selinux強制檢測文件。
3、修改默認的啟動內核:
在centos7中最好不要修改grub2.cfg文件,先修改/etc/default/grub模板文件;
[root@localhost grub2]# cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=0 GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet" GRUB_DISABLE_RECOVERY="true"
將GRUB_DEFAULT=saved一行改為0;
最后使用grub2-mkconfig > /boot/grub2/grub.cfg命令重新生成grub2.cfg文件。
4、啟動時臨時禁用SELinux:
[root@localhost system]# getenforce Enforcing [root@localhost system]# setenforce 0 [root@localhost system]# getenforce Permissive
5、啟動時進入emergency模式:
首先進入e模式:
在linux16后添加:systemd.unit= emergency.target
ctrl+x啟動,進入:
6、刪除編譯安裝的新內核:
刪除boot中生成的文件;
刪除grub2/grub.cfg新內核相關幾行;
刪除/etc/modules中版本號相關文件;
刪除編譯安裝包等文件。
原創文章,作者:SilencePavilion,如若轉載,請注明出處:http://www.www58058.com/49806