POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) -->rootfs --> /sbin/init
init:linux
CentOS 5: SysV init CentOS 6: Upstart CentOS 7: Systemd
Systemd
:系統啓動和服務器守護進程管理器,負責在系統啓動或運行時,激活系統資源,服務器進程和其它進程
shell
Systemd新特性
:vim
核心概念:unit
unit
表示不一樣類型的systemd對象,經過配置文件進行標識和配置;文件中主要包含了系統服務、監聽socket、保存的系統快照以及其它與init相關的信息
配置文件:centos
/usr/lib/systemd/system
:每一個服務最主要的啓動腳本設置,相似於以前的/etc/init.d//run/systemd/system
:系統執行過程當中所產生的服務腳本,比上面目錄優先運行/etc/systemd/system
:管理員創建的執行腳本,相似於/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先運行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 目錄關鍵特性:ssh
不兼容:socket
在/etc/systemd/system下的unit文件是系統管理員和用戶使用
在/usr/lib/systemd/system下的供發行版打包者使用
ide
在unit文件中,以「#」 開頭的行後面的內容會被認爲是註釋,相關布爾值,一、yes、on、true 都是開啓,0、no、off、false 都是關閉,時間單位默認是秒,因此要用毫秒(ms)分鐘(m)等須顯式說明ui
注意:
對於新建立的unit文件,或者修改了的unit文件,要通知systemd重載此配置文件,然後能夠選擇重啓
systemctldaemon-reload
centos7
CentOS 7: service unit
注意:能兼容早期的服務腳本
命令:
systemctl COMMAND name.service
- | centOS6 | CentOS7 |
---|---|---|
啓動 | 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 |
- | centOS6 | CentOS7 |
---|---|---|
查看某服務當前激活與否的狀態 | - | systemctl is-active name.service |
查看全部已經激活的服務 | - | systemctl list-units --type |
查看全部服務 | - | systemctl list-units --type service --all |
- | centOS6 | CentOS7 |
---|---|---|
設定某服務開機自啓 | 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 |
- | centOS6 | CentOS7 |
---|---|---|
查看服務的依賴關係 | - | systemctl list-dependencies name.service |
殺掉進程 | - | systemctl kill unitname |
切換至緊急救援模式 | - | systemctl rescue |
切換至emergency模式 | - | systemctl emergency |
關機 | - | systemctlhalt、systemctlpoweroff |
重啓 | - | systemctl reboot |
掛起 | - | systemctl suspend |
休眠 | - | systemctl hibernate |
休眠並掛起 | - | systemctlhybrid-sleep |
systemctl list-unit-files --type service --all #顯示狀態
顯示全部單元狀態
systemctl 或systemctl list-units
只顯示服務單元的狀態
systemctl --type=service
顯示sshd服務單元
systemctl –l status sshd.service
驗證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
在centOS7上運行級別的含義已經和以前不一樣了,運行級別就是經過開啓關閉不一樣的服務產生的效果,在從netOS7上,已然由.target來代替運行級別,咱們能夠稱target爲目標態
,咱們能夠經過target定製更符合咱們工做運行環境。
咱們能夠經過命令:ls /usr/lib/systemd/system/*.target
查看咱們的機器上有多少個target
"grub.cfg"140L,4397C written [root@CT731 grub2]#ls /usr/lib/systemd/system/*.target /usr/lib/systemd/system/anaconda.target /usr/lib/systemd/system/basic.target /usr/lib/systemd/system/bluetooth.target /usr/lib/systemd/system/printer.target ... /usr/lib/systemd/system/reboot.target /usr/lib/systemd/system/rescue.target /usr/lib/systemd/system/rpcbind.target /usr/lib/systemd/system/runlevel0.target /usr/lib/systemd/system/runlevel1.target /usr/lib/systemd/system/runlevel2.target ... /usr/lib/systemd/system/sleep.target ...
使用systemctl list-unit-files --type target --all
能夠查看全部目標態的狀態,或者systemctl list-dependencies xxx.target
命令查看目標態的依賴性。
在centOS7上所謂的目標態,其實就是由各類指定的服務和基礎target組合而成的。
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
在centOS6上,咱們切換級別使用init
,在centOS7上雖然也能使用,可是已經再也不是原來的程序了,如今咱們使用systemctlisolate name.target
來切換target。
好比,咱們想切換到字符界面,咱們就可使用systemctlisolate multi-user.target來進行切換。要想切換運行級別,在/lib/systemd/system/*.target
文件中AllowIsolate=yes
才能夠。(修改文件需執行systemctldaemon-reload才能生效)
在centOS7上如何查看運行的目標態呢,使用命令systemctl get-default
使用命令systemctl set-default name.target
來修改咱們的目標態。
咱們看一下咱們的默認目標態究竟爲什麼物。
[root@CT731 ~]#locate default.target /etc/systemd/system/default.target
[root@CT731 ~]#ll /etc/systemd/system/default.target lrwxrwxrwx.1 root root 36Aug2507:06/etc/systemd/system/default.target ->/lib/systemd/system/runlevel3.target
它其實就是建立了一個軟連接到指定的target上去了
在centOS7中,文件系統損壞,先嚐試自動修復,失敗則進入emergency shell
,提示用戶修復
啓動時任意鍵暫停啓動
按e鍵進入編輯模式
將光標移動linux16開始的行,添加內核參數rd.break
按ctrl-x啓動,而後:
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot
而後可使用設置的密碼登陸root
啓動時任意鍵暫停啓動(同方法1)
按e鍵進入編輯模式(同方法1)
將光標移動linux16開始的行,改成rw init=/sysroot/bin/sh
按ctrl-x啓動
chroot /sysroot
passwd root
touch /.autorelabel
exit
reboot
最後,用新密碼登陸root
在centOS6上,咱們的grub文件是/boot/grub/grub.conf
在centOS7上,文件改爲/boot/grub2/grub.cfg了,可是功能仍是大體同樣的都是用於加載內核的,不過在centOS7上設置默認啓動項發生了一些變化,假如咱們如今有兩個內核,咱們須要改變默認啓動應該如何作到呢?
首先,vim /etc/default/grub
打開以下文件:
GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet net.ifnames=0" GRUB_DISABLE_RECOVERY="true" ~
打開文件後,咱們修改GRUB_DEFAULT
的值,和centOS同樣,0表明第一個內核,1表明第二個,以此類推。
咱們在修改完成後,並無當即生效,使用grub2-mkconfig -o /boot/grub2/grub.cfg
命令來生成grub2.cfg文件,咱們在下次啓動的時候就會默認選擇新的默認內核。
第一步:破壞硬盤的前446字節:
dd if=/dev/zero of=/dev/sda bs=1 count=446
[root@CT731 ~]#dd if=/dev/zero of=/dev/sda bs=1 count=446 446+0 records in 446+0 records out 446 bytes (446 B) copied,0.000758682 s,588 kB/s [root@CT731 ~]#hexdump -C -n 512/dev/sda 0000000000000000000000000000000000000000|................| * 000001b000000000000000000000000000008020|...............| 000001c021008335700500080000000040000035|!..5p.......@..5| 000001d0710583 fe ff ff 000840000000400600 fe |q.......@...@...| 000001e0 ff ff 83 fe ff ff 000880060000400600 fe |............@...| 000001f0 ff ff 05 fe ff ff 0008 c0 0c00 f8 3f0c55 aa |............?.U.| 00000200
第二步:重啓計算機
因爲個人虛擬機掛載了光盤,因此一重啓就進入光盤啓動的界面
上面有三項,咱們選擇第三項進入troubleshooting
進入第三項後,點擊第二項,進入救援模式的centos的系統
而後咱們進入以下模式:
選擇1,繼續進行:
接下來,咱們就會進入到一個shell模式中,不須要切根,進行系統修復:
修復過程:
grub2-install --root-directory=/mnt/sysimage/ /dev/sda
咱們來查看一下:
hexdump -C -n 512 /dev/sda
重啓,修復完成。
第一步:刪除grub2
rm -rf grub2
[root@CT731 boot]#rm -rf grub2 [root@CT731 boot]#ls config-3.10.0-514.el7.x86_64 grub initramfs-0-rescue-618d3baeb5754f7092242f811c7aa740.img initramfs-3.10.0-514.el7.x86_64.img initramfs-3.10.0-514.el7.x86_64kdump.img initrd-plymouth.img symvers-3.10.0-514.el7.x86_64.gz System.map-3.10.0-514.el7.x86_64 vmlinuz-0-rescue-618d3baeb5754f7092242f811c7aa740 vmlinuz-3.10.0-514.el7.x86_64
第二步,重啓計算機
進入以下界面:
接下來,咱們重啓系統,按Esc,進入光盤救援模式:
選擇第三項,進入光盤救援(前提是掛載光盤)
如下步驟到進入shell同實驗一相同,不在過多演示;
進入救援模式後:
第一步:切根
而後執行命令
grub2-install
下圖中,咱們能夠看到在grub2文件夾中,尚未grub.cfg文件,接下來,咱們須要生成:
生成配置文件:
進入到grub2下,
grub2-mkconfig -o grub.cfg
exit而後,
重啓電腦:
完成!