1. 程式人生 > 實用技巧 >第二十九章 系統管理,單使用者模式,救援模式

第二十九章 系統管理,單使用者模式,救援模式

系統管理

系統啟動過程


開機啟動流程
1. 按電源
2. BIOS自檢 硬體檢查
3. MBR引導 MBR的位置 0磁頭0磁軌1扇區
4. 進入GRUB選單 從這裡進入單使用者或者救援模式
5. 載入核心 kernel
6. 執行系統程序,使用system管理 在centos6中,用init進行管理
7. 讀取系統執行級別 [root@lxy ~]# ll /etc/systemd/system/default.target
8. 初始化系統 [root@lxy ~]# ll /usr/lib/systemd/system/sysinit.target
9. 啟動開機自啟動服務 [root@lxy ~]# ll /usr/lib/systemd/system/* 並行啟動
10. 執行getty檔案,進入登入頁面 [root@lxy ~]# ll /usr/lib/systemd/system/getty.target

centos-6 與 centos-7 啟動對比
序列啟動 並行啟動
啟動速度慢 啟動速度快
容易啟動異常 不會啟動異常
INIT程序管理 system管理

執行級別


分為7個執行級別

[root@lxy ~]# ll /usr/lib/systemd/system/runlevel*.target
1 root root 15 Nov 26 20:23 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
0 關機

1 root root 13 Nov 26 20:23 /usr/lib/systemd/system/runlevel1.target -> rescue.target
1 單使用者模式 修改root密碼

1 root root 17 Nov 26 20:23 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
2 多使用者模式

1 root root 17 Nov 26 20:23 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
3 多使用者模式

1 root root 17 Nov 26 20:23 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
4 多使用者模式

1 root root 16 Nov 26 20:23 /usr/lib/systemd/system/runlevel5.target -> graphical.target
5 桌面模式

1 root root 13 Nov 26 20:23 /usr/lib/systemd/system/runlevel6.target -> reboot.target
6 重啟模式

執行級別相關設定

#centos-6設定 瞭解
[root@lxy ~]# runlevel #顯示執行級別
N 3
[root@lxy ~]# init 5 #修改執行級別
[root@lxy ~]# runlevel
3 5
[root@lxy ~]# init 3


#centos-7設定
[root@lxy ~]# systemctl get-default #顯示當前執行級別
multi-user.target

[root@lxy ~]# systemctl set-default graphical.target #設定執行級別
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[root@lxy ~]# systemctl get-default
graphical.target
[root@lxy ~]# systemctl set-default runlevel3.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
執行級別相關設定
執行級別相關設定

#centos-6設定 瞭解
[root@lxy ~]# runlevel #顯示執行級別
N 3
[root@lxy ~]# init 5 #修改執行級別
[root@lxy ~]# runlevel
3 5
[root@lxy ~]# init 3


#centos-7設定
[root@lxy ~]# systemctl get-default #顯示當前執行級別
multi-user.target

[root@lxy ~]# systemctl set-default
graphical.target #設定執行級別
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
[root@lxy ~]# systemctl get-default
graphical.target
[root@lxy ~]# systemctl set-default runlevel3.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

systemd

#歷史由來
從CentOS 7系列開始,RedHat正式將systemd用於系統管理,來取代CentOS 5的sysV init和CentOS 6的upstart體系;較之之前,systemd為linux系統提供了啟動與管理的一整套解決方案;

system daemon 守護程序
#什麼是systemd ?
systemd即為system daemon守護程序,systemd主要解決上文的問題而誕生,systemd的目標是,為系統的啟動和管理提供一套完整的解決方案。
是一種解決系統啟動跟系統管理的一種解決方案

1. 採用並行啟動
2. system管理 不在init.d的指令碼
3. 只關閉正在執行的程序, centos-6是所有的程序關閉
4. 支援快照
5. 關閉程序時,會關閉所有的相關程序

system相關的目錄

ll /usr/lib/systemd/system/* #所有系統開機自啟動的服務列表目錄
ll /etc/systemd/system/ #系統主要配置目錄
ll /run/systemd/system/ #系統執行過程中產生的臨時程式


system管理命令
system管理命令  
systemctl #系統管理命令

[root@lxy ~]# systemctl start nginx #啟動
[root@lxy ~]# systemctl status nginx #顯示啟動狀態
[root@lxy ~]# systemctl stop nginx #關閉
[root@lxy ~]# systemctl restart nginx #重啟
[root@lxy ~]# systemctl reload nginx #平滑重啟
[root@lxy ~]# systemctl mask nginx #禁用這個服務
[root@lxy ~]# systemctl unmask nginx #解除禁用
[root@lxy ~]# systemctl enable nginx #加入開機自啟動
[root@lxy ~]# systemctl disable nginx #取消開機自啟動


狀態資訊
loaded #程式已經完成一次載入

active (running) #服務正在執行狀態 ******
active (exited) #系統已經完成一次完整程式的啟動退出, 程式沒有在執行
active (waiting) #程式已經完成一次載入,但是需要等待某個事件
inactive #沒有執行 *****
enable #開機自啟動 *****
disable #沒有加入開機自啟動 ******
static #無法被系統管理
failed #配置檔案錯誤 *****

#顯示當前系統執行級別所有服務的啟動狀態
[root@lxy ~]# systemctl list-unit-files
使用systemctl進行關機重啟
#使用systemctl進行關機重啟
[root@lxy ~]# systemctl poweroff #關機
[root@lxy ~]# systemctl reboot #重啟
系統日誌
#系統日誌
[root@lxy ~]# ll /var/log/messages

[root@lxy ~]# journalctl -n 20 #顯示系統重要的日誌,最後20行內容
[root@lxy ~]# journalctl -f #實時顯示系統日誌輸出
[root@lxy ~]# journalctl -p err #按照日誌級別進行顯示
[root@lxy ~]# journalctl -u sshd #顯示指定的服務的日誌
[root@lxy ~]# journalctl -xe #到達對應的服務的錯誤位置
系統主機名配置
#系統主機名配置
[root@lxy ~]# hostname
lxy
[root@lxy ~]# hostnamectl
Static hostname: lxy
Icon name: computer-vm
Chassis: vm
Machine ID: 72c6d29133ef42fdb0e5fc182367c50a
Boot ID: 2bc360ca67004187b28de0862432ab1c
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64

[root@lxy ~]# ll /etc/hostname #主機名的配置檔案
-rw-r--r--. 1 root root 4 Nov 26 20:27 /etc/hostname
[root@lxy ~]# cat /etc/hostname
lxy

[root@lxy ~]# hostnamectl set-hostname web #永久修改主機名
[root@lxy ~]# cat /etc/hostname
web
[root@lxy ~]# bash #重新登入生效
[root@web ~]#

#系統的字符集
[root@web ~]# echo $LANG #顯示字符集
en_US.UTF-8

[root@web ~]# export LANG=zh_CN.UTF-8 #臨時修改,重新登入失效
[root@web ~]# echo $LANG
zh_CN.UTF-8

[root@web ~]# ll /etc/locale.conf #字符集配置檔案
-rw-r--r--. 1 root root 19 Nov 26 20:27 /etc/locale.conf
[root@web ~]# cat /etc/locale.conf
LANG="en_US.UTF-8"

#永久設定字符集,重新登入生效
[root@web ~]# localectl set-locale LANG=zh_CN.UTF-8
[root@web ~]# cat /etc/locale.conf
LANG=zh_CN.UTF-8

單使用者模式


忘記root密碼

1. 重啟伺服器
2. 在GRUB選單中輸入e,進入設定核心資訊
3. 在linux16這行最後面 加入 init=/bin/bash enforcing=0 前者分配一個shell視窗 ,後者關閉selinux
4. 執行ctrl + x 繼續下一步
5. 重新掛載根分割槽, mount -o rw,remount /
6. 修改root密碼
7. 關閉selinux,要永久關閉。或者建立一個檔案 touch /.autorelabel
#vim /etc/sysconfig/selinux
disabled
8. 重啟 exec /sbin/init
9. 登入測試


系統執行級別被設定為重啟級別
systemctl set-default runlevel6.target

1. 重啟伺服器
2. 在GRUB中輸入 e 進入核心設定
3. 在linux16這行最後面 加入rd.break
4. 執行ctrl + x 繼續下一步
5. 重新掛載根分割槽,mount -o rw,remount /sysroot #根分割槽名稱發生變化
6. 分配一個shell視窗 chroot /sysroot
7. 修改執行級別為多使用者
systemctl set-default multi-user.target
8. 退出當前視窗,重啟 exit reboot

救援模式


案例一:系統損壞,但裡面的資料很重要,需要備份

進入救援模式,掛載磁碟,將資料拷貝到新的磁碟中

1. 新增一塊磁碟
2. 選擇進入電源時進入韌體
3. 選擇映象啟動
4. 按照提示操作,獲得操作許可權
5. 掛載磁碟,進行備份資料
6. 退出,關機,移除磁碟


案例二:MBR引導損壞,無法進入系統

1. 進入韌體,選擇映象啟動,進入救援模式
2. 按照提示獲得操作許可權
3. 修復MBR grub2-install /dev/sda #重新安裝grub
4. 退出,重啟



案例三:grub目錄被誤刪除了

1. 啟動進入韌體
2. 選擇映象啟動
3. 進入救援模式
4. 按照提示獲得許可權
5. 修復grub grub2-install /dev/sda
6. 將其載入到引導目錄 grub2-mkconfig -o /boot/grub2/grub.cfg
7. 退出,關機
8. 選擇正常啟動