1烈和、systemd
啟動流程:POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs--> /sbin/init
init:CentOS 5: SysVinit
CentOS 6: Upstart
CentOS 7: Systemd
Systemd:系統(tǒng)啟動和服務器守護進程管理器,負責在系統(tǒng)啟動或運行時扁誓,激活系統(tǒng)資源,服務器進程和其它進程
Systemd新特性:
系統(tǒng)引導時實現(xiàn)服務并行啟動
按需啟動守護進程
自動化的服務依賴關系管理
同時采用socket式與D-Bus總線式激活服務
系統(tǒng)狀態(tài)快照
核心概念:unit
unit表示不同類型的systemd對象绅络,通過配置文件進行標識和配置挨队;文件中主要包含了系統(tǒng)服務、監(jiān)聽socket蔑匣、保存的系統(tǒng)快照以及其它與init相關的信息
配置文件:
/usr/lib/systemd/system:每個服務最主要的啟動腳本設置劣欢,類似于之前的/etc/init.d/
/run/systemd/system:系統(tǒng)執(zhí)行過程中所產(chǎn)生的服務腳本棕诵,比上面目錄優(yōu)先運行
/etc/systemd/system:管理員建立的執(zhí)行腳本,類似于/etc/rc.d/rcN.d/Sxx類的功能凿将,比上面目錄優(yōu)先運行
2校套、Unit類型
Systemctl –t help 查看unit類型
Service unit: 文件擴展名為.service, 用于定義系統(tǒng)服務
Target unit: 文件擴展名為.target,用于模擬實現(xiàn)運行級別
Device unit: .device, 用于定義內(nèi)核識別的設備
Mount unit: .mount, 定義文件系統(tǒng)掛載點
Socket unit: .socket, 用于標識進程間通信用的socket文件牧抵,也可在系統(tǒng)啟動時笛匙,延遲啟動服務,實現(xiàn)按需啟動
Snapshot unit: .snapshot, 管理系統(tǒng)快照
Swap unit: .swap, 用于標識swap設備
Automount unit: .automount犀变,文件系統(tǒng)的自動掛載點
Path unit: .path妹孙,用于定義文件系統(tǒng)中的一個文件或目錄使用,常用于當文件系統(tǒng)變化時,延遲激活服務获枝,如:spool 目錄
3蠢正、管理服務
管理系統(tǒng)服務:
CentOS 7: service unit
注意:能兼容早期的服務腳本
命令格式:systemctl COMMAND name.service
啟動:service name start ==> systemctl start name.service
停止:service name stop ==> systemctl stop name.service
重啟:service name restart ==> systemctl restart name.service
狀態(tài):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比service管理服務的優(yōu)勢是可以一次性啟動或者關閉嚣崭、查看多個服務。
4萨西、服務查看
?查看某服務當前激活與否的狀態(tài):
systemctl is-active name.service ---可以用于腳本中判斷服務是否啟動有鹿,啟動時echo $?=0,未啟動echo $?不等于0
?查看所有已經(jīng)激活的服務:
systemctl list-units --type service
?查看所有服務:
systemctl list-units --type service --all|-a
?chkconfig命令的對應關系:
?設定某服務開機自啟:
chkconfig name on ==> systemctl enable name.service
?設定某服務開機禁止啟動:
chkconfig name off ==> systemctl disable name.service
?查看服務單元的啟用和禁用狀態(tài)
systemctl list-unit-files --type=service
?用來列出該服務在哪些運行級別下啟用和禁用
chkconfig sshd–list ==>
ls /etc/systemd/system/*.wants/sshd.service
?查看服務是否開機自啟:
systemctl is-enabled name.service
其它命令:
查看服務的依賴關系:
systemctl list-dependencies name.service
?殺掉進程:
systemctl kill unitname
?列出失敗的服務
systemctl --failed --type service
systemctl reload name.service ---服務已經(jīng)啟動,重新加載服務
systemcl daemon-reload ----服務已經(jīng)啟動谎脯,修改了配置文件葱跋,讓文件生效
5、運行級別
target units:
unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target -a 查看運行狀態(tài)
運行級別:
0 ==> runlevel 0.target, poweroff.target
1 ==> runlevel 1.target, rescue.target ---單用戶
2 ==> runlevel 2.target, multi-user.target
3 ==> runlevel 3.target, multi-user.target ---字符界面
4 ==> runlevel 4.target, multi-user.target
5 ==> runlevel 5.target, graphical.target ---圖形
6 ==> runlevel 6.target, reboot.target
查看依賴性:
systemctl list-dependencies graphical.target
級別切換:initN ==> systemctl isolate name.target
systemctl isolate multi-user.target
注:只有/usr/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切換(修改文件需執(zhí)行systemctl daemon-reload才能生效)
查看target:
runlevel; who -r
systemctl list-units --type target
獲取開機默認運行級別:
/etc/inittab==> systemctl get-default
修改開機默認級別:
/etc/inittab==> systemctl set-default name.target
systemctl set-default multi-user.target
ls –l /etc/systemd/system/default.target
切換至緊急救援模式:
systemctl rescue
切換至emergency模式:
systemctl emergency
其它常用命令:
傳統(tǒng)命令init源梭,poweroff娱俺,halt,reboot都成為systemctl的軟鏈接
關機:systemctl halt废麻、systemctl poweroff
重啟:systemctl reboot
掛起:systemctl suspend
休眠:systemctl hibernate
休眠并掛起:systemctl hybrid-sleep
6荠卷、centos7引導順序
UEFi或BIOS初始化,運行POST開機自檢
選擇啟動設備
引導裝載程序, centos7是grub2
加載裝載程序的配置文件:/etc/grub.d/ /etc/default/grub /boot/grub2/grub.cfg
加載initramfs驅(qū)動模塊
加載內(nèi)核選項
內(nèi)核初始化烛愧,centos7使用systemd代替init
執(zhí)行initrd.target所有單元油宜,包括掛載/etc/fstab
從initramfs根文件系統(tǒng)切換到磁盤根目錄
systemd執(zhí)行默認target配置,配置文件/etc/systemd/system/default.target
systemd執(zhí)行sysinit.target初始化系統(tǒng)及basic.target準備操作系統(tǒng)
systemd啟動multi-user.target下的本機與服務器服務
systemd執(zhí)行multi-user.target下的/etc/rc.d/rc.local
systemd執(zhí)行multi-user.target下的getty.target及登錄服務
systemd執(zhí)行graphical需要的服務
7怜姿、service unit文件格式
/etc/systemd/system:系統(tǒng)管理員和用戶使用慎冤,運行級別要比下面的配置文件運行級別要高,把寫好的unit文件放到這個目錄下沧卢,就可以用systemctl命令來管理這個服務蚁堤,相當于centos6中的/etc/init.d目錄,不同是centos6中使用service來管理服務但狭,并且/etc/init.d目錄下全是有執(zhí)行權限的服務腳本披诗,而這個目錄下的格式和centos6中完全不同撬即。
/usr/lib/systemd/system:發(fā)行版打包者使用,用戶也可以將寫好的unit文件放到這個目錄下呈队,就可以用systemctl命令來管理服務剥槐,但這個目錄下的文件太多,不好管理宪摧。
- 服務Unit文件示例
vim /etc/systemd/system/bak.service
[Unit]
Description=backup /etc
Requires=atd.service
[Service]
Type=simple
ExecStart=/bin/bash -c "echo /app/bak.sh|at now" ---表示立刻執(zhí)行
一個計劃任務才沧,為執(zhí)行/app/bak.sh這個腳本
[Install]
WantedBy=multi-user.target
chmod a+x bak.service ---別忘記加執(zhí)行權限
[root@redhat7 app]#cat bak.sh ---要執(zhí)行的腳本的內(nèi)容
cp -a /etc/ /app/`date "+%F-%T"`/
systemctl daemon-reload ---重新加載一下配置文件,讓文件生效
systemctl start bak ---就可以用這個命令來管理這個服務
8绍刮、設置內(nèi)核參數(shù)
設置內(nèi)核參數(shù),只影響當次啟動挨摸。
啟動時孩革,在啟動菜單下面按e進入編輯內(nèi)核參數(shù),在linux16行后添加如下命令得运,可以實現(xiàn)臨時禁用selinux和開機后進入各種運行模式膝蜈,最后按ctrl+x進行啟動。
selinux=0 ---表示啟動時臨時禁用selinux
systemd.unit=multi-user.target ---表示啟動后進入字符運行模式
systemd.unit=emergency.target ---啟動后進入更低級的模式
systemd.unit=rescue.target ---相當于centos6里的1模式熔掺,啟動后進入該模式
rescue.target 比emergency 支持更多的功能饱搏,例如日志等
9、centos7中服務和socket是分離的
socket是ip地址加上tcp或者udp的端口號
[root@redhat7 app]#systemctl status rpcbind.service
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
Active: inactive (dead) ---此時服務是不活動的狀態(tài)
[root@redhat7 app]#systemctl status rpcbind.socket
● rpcbind.socket - RPCbind Server Activation Socket
Loaded: loaded (/usr/lib/systemd/system/rpcbind.socket; enabled; vendor preset: enabled)
Active: active (listening) since Thu 2017-09-07 21:56:03 CST; 25s ago
Listen: /var/run/rpcbind.sock (Stream)
[::]:111 (Stream) ---可看到正處于監(jiān)聽狀態(tài)置逻,并且監(jiān)聽的端口是111
0.0.0.0:111 (Stream)
Sep 07 21:56:03 redhat7.4.magedu.com systemd[1]: Stopping RPCbind Server Ac...
Sep 07 21:56:03 redhat7.4.magedu.com systemd[1]: Listening on RPCbind Serve...
Sep 07 21:56:03 redhat7.4.magedu.com systemd[1]: Starting RPCbind Server Ac...
Hint: Some lines were ellipsized, use -l to show in full.
[root@redhat7 app]#ss -nat
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 172.18.21.7:22 172.18.252.32:52370
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
我們發(fā)現(xiàn)rpcbind.service 和rpcbind.socket是分離的推沸,因為此時服務并沒有開啟,是socket幫著監(jiān)聽的券坞,當有人訪問時鬓催,會激活服務,/usr/lib/systemd/system/rpcbind.socket文件是用來幫著服務監(jiān)聽端口用的恨锚。