system及Centos 7啟動修復

啟動流程

POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs --> /sbin/init
    init: CentOS 5: SysV init
          CentOS 6: Upstart
          CentOS 7: Systemd
  • Systemd
    系統啟動和服務器守護進程管理器,負責在系統
    啟動或運行時,激活系統資源,服務器進程和其它進程
  • Systemd新特性
系統引導時實現服務并行啟動
按需啟動守護進程
自動化的服務依賴關系管理
同時采用socket式與D-Bus總線式激活服務
系統狀態快照
  • 核心概念:unit
    unit表示不同類型的systemd對象,通過配置文件進行標識和配置;文件中主要包含了系統服務、監聽socket 、保存的
    系統快照以及其它與init 相關的信息
  • 配置文件:
/usr/lib/systemd/system: 每個 服務最主要的啟動腳本設置,類似于之前的/etc/init.d/
/run/systemd/system :系統執行過程中所產生的服務腳本,比上面目錄優先運行
/etc/systemd/system :管理員建立的執行腳本,類似于/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先運行

Unit類型

Systemctl –t help  查看unit 類型
Service unit:文件擴展名為.service,用于定義系統服務
Target unit:文件擴展名為.target,用于模擬實現運行級別
Device unit:.device,用于定義內核識別的設備
Mount unit: .mount,定義文件系統掛載點
Socket unit:.socket,用于標識進程間通信用的socket文件,也可在系統啟動時,延遲啟動服務,實現按需啟動
Snapshot unit: .snapshot,管理系統快照
Swap unit:.swap,用于標識swap設備
Automount unit: .automount,文件系統的自動掛載點
Path unit:.path,用于定義文件系統中的一個文件或目錄使用,常用于當文件系統變化時,延遲激活服務,如:spool目錄

system特性

關鍵特性:
    基于socket的激活機制socket與服務程序分離
    基于d-bus的激活機制:
    基于device的激活機制:
    基于path的激活機制:
    系統快照:保存各unit 的當前狀態信息于持久存儲設備中
    向后兼容sysv init腳本
不兼容:
    systemctl命令固定不變,不可擴展
    非由systemd 啟動的服務,systemctl

system管理服務

管理系統服務:
    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
狀態: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

查看system服務

查看某服務當前激活與否的狀態:
    systemctl is-active name.service
查看所有已經激活的服務:
    systemctl list-units --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
用來列出該服務在哪些運行級別下啟用和禁用
    chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service
查看服務是否開機自啟:
    systemctl is-enabled name.service
其它命令:
    查看服務的依賴關系:
        systemctl list-dependencies name.service
殺掉進程:
    systemctl kill 進程名

服務狀態

systemctl list-unit-files --type service --all 顯示狀態
loaded:Unit 配置文件已處理
active(running):一次或多次持續處理的運行
active(exited):成功完成一次性的配置
active(waiting):運行中,等待一個事件
inactive: 不運行
enabled:開機啟動
disabled:開機不啟動
static:開機不啟動,但可被另一個啟用的服務激活

systemctl命令示例

顯示所有單元狀態
    systemctl 或 或 systemctl list-units
只顯示服務單元的狀態
    systemctl --type=service
顯示sshd 服務單元
    systemctl status sshd.service –l
驗證sshd 服務當前是否活動
    systemctl is-active sshd
啟動,停止和重啟sshd 服務
    systemctl start sshd.service
    systemctl stop sshd.service
    systemctl restart sshd.service
重新加載配置
    systemctl reload sshd.service
列出活動狀態的所有服務單元
    systemctl list-units --type=service
列出所有服務單元
    systemctl list-units --type=service --all
查看服務單元的啟用和禁用狀態
    systemctl list-unit-files --type=service
列出失敗的服務
    systemctl --failed --type=service
列出依賴的單元
    systemctl list-dependencies sshd
驗證sshd服務是否開機啟動
    systemctl is-enabled sshd
禁用network,使之不能自動啟動, 但手動可以
    systemctl disable network
啟用network
    systemctl enable network
禁用network,使之不能手動或自動啟動
    systemctl mask network
啟用network
    systemctl unmask network

服務的運行級別

target units:
    unit 配置文件:.target
    ls /usr/lib/systemd/system/*.target
    systemctl list-unit-files --type target --all
運行級別:
    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
查看依賴性:
    systemctl list-dependencies graphical.target
級別切換:init N ==> systemctl isolate name.target
    systemctl isolate multi-user.target
    注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes才能切換(修改文件需執行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
其它常用命令:
    傳統命令init,poweroff,halt,reboot都成為systemctl的軟鏈接
    關機:systemctl halt、systemctl poweroff
    重啟:systemctl reboot
    掛起:systemctl suspend
    休眠:systemctl hibernate
    休眠并掛起:systemctl hybrid-sleep

Centos 7引導順序

UEFi或BIOS初始化,運行POST開機自檢
選擇啟動設備
引導裝載程序, centos7 是grub2
加載裝載程序的配置文件:/etc/grub.d/
    /etc/default/grub /boot/grub2/grub.cfg
加載initramfs 驅動模塊
加載內核選項
內核初始化,centos7 使用systemd 代替init
執行initrd.target所有單元,包括掛載/etc/fstab
從initramfs根文件系統切換到磁盤根目錄
systemd執行默認target配置,配置文件 /etc/systemd/system/default.target
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

service unit 文件格式

  • /etc/systemd/system:系統管理員和用戶使用
    /usr/lib/systemd/system:發行版打包者使用
  • 以“#”開頭的行后面的內容會被認為是注釋
  • 相關布爾值,1 、yes 、on 、true都是開啟,0、no、off、false都是關閉
  • 時間單位默認是秒,所以要用毫秒(ms)分鐘(m)等須顯式說明
  • service unit file 文件通常由三部分組成:
[Unit]:定義與Unit 類型無關的通用選項;用于提供unit的描述信息、unit 行為及依賴關系等
[Service]:與特定類型相關的專用選項;此處為Service 類型
[Install] :定義由“systemctl enable” 以及"systemctl disable“命令在實現服務啟用或禁止時用到的一些選項

Unit段的常用選項:
    Description:描述信息
    After:定義unit 的啟動次序,表示當前unit 應該晚于哪些unit啟動,其功能與Before 相反
    Requires:依賴到的其它units ,強依賴,被依賴的units 無法激活時,當前unit也無法激活
    Wants:依賴到的其它units,弱依賴
    Conflicts :定義units
Service段的常用選項:
    Type:定義影響ExecStart 及相關參數的功能的unit 進程啟動類型
        simple:默認值,這個daemon 主要由ExecStart接的指令串來啟動,啟動后常駐于內存中
        forking:由ExecStart 啟動的程序透過spawns 延伸出其他子程序來作為此daemon的主要服務。原生父程序在啟動結束后就會終止
        oneshot:與simple 類似,不過這個程序在工作完畢后就結束了,不會常駐在內存中
        dbus:與simple類似,但這個daemon必須要在取得一個D-Bus的名稱后,才會繼續運作.因此通常也要同時設定BusNname=才行
        notify:在啟動完成后會發送一個通知消息。還需要配合NotifyAccess來讓Systemd接收消息
        idle:與simple類似,要執行這個daemon必須要所有的工作都順利執行完畢后才會執行。這類的daemon通常是開機到最后才執行即可的服務
    EnvironmentFile:環境配置文件
    ExecStart:指明啟動unit 要運行命令或腳本的絕對路徑
    ExecStartPre:ExecStart前運行
    ExecStartPost:ExecStart后運行
    ExecStop:指明停止unit要運行的命令或腳本
    Restart:當設定Restart=1時,則當次daemon服務意外終止后,會再次自動啟動此服務    

Install段的常用選項:
    Alias:別名,可使用systemctl command Alias.service
    RequiredBy:被哪些units所依賴,強依賴
    WantedBy:被哪些units所依賴,弱依賴
    Also:安裝本服務的時候還要安裝別的相關服務

    注意:對于新創建的unit文件,或者修改了的unit文件,要通知systemd重載此配置文件,而后可以選擇重啟
    systemctl daemon-reload

服務Unit文件示例:

vim /etc/systemd/system/bak.service
    [Unit]
    Description=backup /etc
    Requires=atd.service
    [Service]
    Type=simple
    ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"
    [Install]
    WantedBy=multi-user.target
  • systemctl daemon-reload
  • systemctl start bak

設置內核參數

  • 設置內核參數,只影響當次啟動
  • 啟動時,在linux16行后添加systemd.unit=desired.target
  • systemd.unit=emergency.target
  • systemd.unit=recure.target
  • recure.target 比emergency 支持更多的功能,例如日志等

啟動排錯

  • 文件系統損壞
    先嘗試自動修復,失敗則進入emergency shell,提示用戶修復
  • 在/etc/fstab不存在對應的設備和UUID
    等一段時間,如不可用,進入emergency shell
  • 在/etc/fstab不存在對應掛載點
    systemd嘗試創建掛載點,否則提示進入emergency shell.
  • 在/etc/fstab不正確的掛載選項
    提示進入emergency shell

破解Centos 7的root密碼

方式一

啟動時任意鍵暫停啟動
按e鍵進入編輯模式
將光標移動linux16 開始的行,添加內核參數rd.break
按ctrl-x 啟動
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot

方式二

啟動時任意鍵暫停啟動
按e鍵進入編輯模式
將光標移動linux16 開始的行,改為rw init=/sysroot/bin/sh
按ctrl-x 啟動
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot

修復GRUB2

  • GRUB “the Grand Unified Bootloader”
    引導提示時可以使用命令行界面
    可從文件系統引導
  • 主要配置文件 /boot/grub2/grub.cfg
  • 修復配置文件
grub2-mkconfig > /boot/grub2/grub.cfg
  • 修復grub
grub2-install /dev/sda BIOS 環境
grub2-install UEFI 環境
  • 調整默認啟動內核
vim /etc/default/grub
GRUB_DEFAULT=0

練習

  1. 為編譯安裝的httpd 服務,實現service unit文件
  2. 破解centos7 口令
  3. 修改默認的啟動內核為新編譯內核
  4. 啟動時臨時禁用SELinux
  5. 啟動時進入emergency 模式
  6. 卸載編譯安裝的新內核
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容