1、系統啓動的流程回顧:
post(加電自檢,主要用來檢查支持系統啓動的硬件環境是否滿足)---> bootseqence(根據bios中設定啓動順序,去加載相應的啓動項設備)---> bootloader(根據啓動項設備的mbr去加載bootloader)---> kernel(加載內核至內存中)---> rootfs(根切換)---> init(/sbin/init)
不同系統的init程序不一樣:
centos5:sysV init //V表示版本號,第五版
centos6:upstart
centos7:systemd
2、systemd新特性介紹:
系統引導時實現服務並行啓動
按需激活進程:意思是隻有需要此進程運行時,才激活進程
支持系統狀態快照
基於依賴關係定義的服務控制邏輯。
3、systemd核心概念:unit
unit由其相關的配置文件進行標識識別和配置,文件中主要包含了:系統服務、監聽的socket、保存的快照以及其他的unit相關的信息,這些配置文件主要保存在/usr/lib/systemd/system,以及/etc/systemd/system/,以及/run/systemd/system/目錄下。
unit常見的類型:
unit類型 | 文件擴展名 | 用途 | 備註 |
service unit | .service | 定義系統服務 | |
taget unit | .target | 用於模擬實現init的運行級別 | |
device unit | .device | 用於定義內核識別的設備 | |
mount unit | .mount | 用於定義文件系統掛載點 | |
scoket unit | .scoket | 用於標識進程間通信用到的socket文件 | |
snapshot unit | .snapshot | 用於管理系統快照 | |
swap unit | .swap | 用於標識swap設備 | |
automount unit | .automount | 文件系統自動掛載點設置 | |
path unit | .path | 用於定義文件系統中的一個文件或目錄。 |
4、systemd的關鍵特性:
基於socket的激活機制,socket與程序分離
基於bus的激活機制
基於device的激活機制
機制path的激活機制
系統快照:保存各unit的當前狀態信息於持久存儲設備中
向後兼容sysV init腳本:存放在/etc/init.d/目錄下的腳本
5、systemd不兼容的功能特性:(與老版本不兼容的一些的特性)
systemctl的命令時固定不變的
非由systemd啓動的服務,systemctl無法與之通信,即systemctl無法控制此服務。6
6、centos7管理系統服務
centos7:service類型的unit文件
systemctl命令:#man systemctl
systemctl [OPTIONS...] COMMAND [NAME...]
centos7 | centos6 | 備註 | |
啓動服務 | systemctl start NAME[.service] | service NAME start | |
停止服務 | sytemctl stop NAME[.service] | service NAME stop | |
重啓服務 | systemctl restart NAME[.service] | service NAME restart | |
顯示狀態 | systemctl status NAME[.service] | service NAME status | |
條件式重啓 | systemctl try-restart NAME[.service] | service NAME condrestart | |
重載或重啓服務 | systemctl reload-or-restart NAME[.service] //如果支持重載就重載服務,不支持就重啓服務 | 不支持 | 重新讀取配置文件,而不用重啓服務 |
重啓或條件式重啓服務 | systemctl reload-or-tty-restart NAME[.service] | 不支持 | |
查看某服務當前激活與否狀態 | systemctl is-active NAME[.service] 如: ~]# systemctl is-active crond active | ||
查看所有已激活的服務 | ~]# systemctl list-units --type service | ||
查看所有服務(含已激活和未激活的) | # systemctl list-units --type service --all | chkconfig --list | |
設置服務隨系統引導自動開啓(開機自啓服務) | systemctl enable NAME[.service] | chkconfig NAME ON | |
禁止指定服務開機自啓動 | systemctl disable NAME[.service] | chkconfig NAME off | |
查看某服務能否開機自啓動 | systemctl is-enabled NAME[.service] 如: # systemctl is-enabled crond enabled | chkconfig --list NAME | |
禁止某服務設置爲開機自啓 | systemctl mask NAME.[service] 如: [root@localhost ~]# systemctl mask crond | ||
取消禁止某服務設置爲開機自啓 | systemctl unmask NAME.[service] | ||
查看服務的依賴關係 | systemctl list-dependencies NAME.[service] 如: ~]# systemctl list-dependencies crond crond.service | ||
關機 | systemctl halt systemctl poweroff | ||
重啓 | systemctl reboot | ||
掛起 | systemctl suspend | ||
快照 | systemctl hibernate | ||
快照並掛起 | systemctl hybrid-sleep |
6.1、target unit:管理運行級別的,
運行級別:
0:runlevel0.target,poweroff.target
1:runlevel1.target,rescue.target
2:runlevel2.target,multi-user.target
3:runlevel3.target,multi-user.target
4:runlevel4.target,multi-user.target
5:runlevel5.target,graphical.target
6:runlevel6.target,reboot.target
[root@localhost system]# cat /etc/inittab # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target # [root@localhost system]# |
6.2、運行級別runlevel#.target之間切換
centos6:init #
centos7:systemctl isolate NAME.target
6.3、查看當前運行級別:
centos5/6:#runlevel
或 #who -r
centos7:~]# systemctl list-units -t target
6.4、查看所有運行級別:含未激活的
~]# systemctl list-units -t target --all
6.5、獲取默認運行級別:
[root@localhost ~]# systemctl get-default
graphical.target
[root@localhost ~]#
6.6、修改默認運行級別:
sytemctl set-default NAME.target
6.7、切換至緊急救援模式:
#systemctl rescue
6.8、切換至emergency模式:
#systemctl emergency
7、service unit
7.1service unit文件位置:/usr/lib/systemd/system,
[root@localhost ~]# ll /usr/lib/systemd/system/default.target
lrwxrwxrwx. 1 root root 16 Aug 29 20:05 /usr/lib/systemd/system/default.target -> graphical.target
[root@localhost ~]#
注意:默認/etc/systemd/system/目錄下的文件多爲開機自動啓動的默認服務,一般是鏈接文件,鏈接到/usr/lib/systemd/sytem目錄下的相應服務。
7.2、service unit file格式規範:以/usr/lib/systemd/system/httpd.service 爲例介紹:
文件通常由三部分組成:[unit] 、[service] 、[install]
[unit]:
定義與unit類型無關的通用選項,用於提供unit的描述信息,unit行爲及依賴關係
如:
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[unit]常用選項:
Description:描述信息,服務的意義性描述
After:定義unit的啓動次序,表示當前的unit應該晚於哪些unit啓動,其功能與Before相反
Wants:依賴到的其他unit,弱依賴關係,被依賴的unit無法激活時,對當前的unit沒有影響
Requies:依賴到的其他unit,強依賴關係,被依賴的unit無法激活時,則當前的unit無法激活
Conflicts:定義unit間的衝突關係
[service]:
定義與特定類型相關的專用選項,此處爲service類型
如:
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[service]常用選項:
Type:用於定義影響Execstart及相關參數的功能的unit進程啓動類型。
unit進程啓動的類型:simple、forking、oneshot、dbus、notify、idel
simple:有Execstart啓動的進程是主進程
forking:有Execstart啓動的進程中的一個子進程爲主進程。
notify:類似simple
EnvironmentFile:環境配置文件,一般放在命令之前
ExecStart:指明啓動unit要運行的命令或腳本,ExecStartpre,ExecStartposr
ExecStop:指明停止unit要運行的命令或腳本
[install]:
定義有“systemctl enable”以及“systemctl disable”命令在實現服務啓用或禁用時用到的一些選項。
如:
[Install]
WantedBy=multi-user.target
[install]常用選項:
Alias:當前unit所依賴
RequiredBy:被哪些units所依賴
WantedBy:被哪些units所依賴
注意:對於新創建的unit文件或被修改的unit文件,要通知systemctl重載此配置文件。(#systemctl daemon-reload )
8、練習:參考上述,爲當前系統的httpd服務提供一個unit文件。