天天看點

Linux 常見的trouble shooting故障排錯

Linux 常見的trouble shooting故障排錯

備份開機所必須運作的程式對一個運維人員來說是非常有必要的。在實際生産環境中,系統和資料基本都是安裝在不同的硬碟上面,因為企業最關心的還是資料,系統崩潰了,最壞的方法就是重新安裝系統,但是資料丢失了,那會直接給企業帶來損失,如果系統和資料都放在同一個硬碟上面,那系統都進不去了,何談資料。解決常見的trouble shooting,不要在系統出現故障的時候,想到的第一個就是重裝系統。為解決常見的trouble shooting,首先必須先了解系統的啟動流程。

常見的trouble shooting:

1.關于grub的故障

a)grub.conf的配置檔案的丢失

          故障系統資訊

<a target="_blank" href="http://blog.51cto.com/attachment/201308/092610208.png"></a>

系統開機時,直接出現grub提示符,表示你的grub的配置檔案損壞或者已經丢失。

思路:這種情況下,直接在grub提示符下,重新grub配置檔案即可。

<a target="_blank" href="http://blog.51cto.com/attachment/201308/102343951.png"></a>

怎樣獲知系統根的挂載點,一般用于RHEL6.x版本上面,5.x版本不需要指定根的路徑。

<a target="_blank" href="http://blog.51cto.com/attachment/201308/102958196.png"></a>

插入CD光牒選擇第三個 Rescue,急救模式,或者連續按Esc鍵兩次,在boot:提示符下面輸入 linux rescue然後按Enter鍵。

<a target="_blank" href="http://blog.51cto.com/attachment/201308/103557158.png"></a>

選擇本地CD光牒

<a target="_blank" href="http://blog.51cto.com/attachment/201308/103715979.png"></a>

選擇Continue,繼續。

<a target="_blank" href="http://blog.51cto.com/attachment/201308/103816429.png"></a>

進入急救模式之後,系統會把你的真正的根系統挂載到/mnt/sysimage目錄下面,如果你想進入真正的根choot /mnt/sysimage切換你真正的根目錄

<a target="_blank" href="http://blog.51cto.com/attachment/201308/105847412.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201308/110012841.png"></a>

2)boot分區損壞

當系統的boot分區損壞時,系統也無法正常進入

思路1:用一塊新硬碟去接入到能進入系統的主機,且主機的核心版本資訊,必須和壞掉系統boot分區的一樣,為這塊新的硬碟安裝grub,且複制vmlinuz核心檔案,initramfs核心鏡像檔案,然後建立grub的配置檔案,在把這個新的硬碟接入到損壞boot分區的主機上面,用這個新硬碟的boot來引導系統啟動

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<code>[root@Redhat6 boot]</code><code># fdisk  /dev/sdb  #為新添加的磁盤分區</code>

<code>Command (m </code><code>for</code> <code>help): n</code>

<code>Command action</code>

<code>   </code><code>e   extended</code>

<code>   </code><code>p   primary partition (1-4)</code>

<code>p</code>

<code>Partition number (1-4): 1</code>

<code>First cylinder (1-2610, default 1314):</code>

<code>Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):+50M</code>

<code>Command (m </code><code>for</code> <code>help): w</code>

<code>[root@Redhat6 ~]</code><code># partprobe</code>

<code>[root@Redhat6 ~]</code><code>#mkfs -t ext4 /dev/sdb1  #格式化分區</code>

<code>[root@Redhat6 ~]</code><code># mkdir /mnt/boot  #為新添加磁盤的分區建立一個挂載點</code>

<code>[root@Redhat6 ~]</code><code># mount /dev/sdb1 /mnt/boot/</code>

<code>[root@Redhat6 ~]</code><code># cd /mnt/boot/</code>

<code>[root@Redhat6 boot]</code><code># ls</code>

<code>lost+found</code>

<code>[root@Redhat6 boot]</code><code># grub-install --root-directory=/mnt /dev/sdb #為新添加的硬碟安裝grub</code>

<code>Probing devices to guess BIOS drives. This may take a long </code><code>time</code><code>.</code>

<code>Installation finished. No error reported.</code>

<code>This is the contents of the device map </code><code>/mnt/boot/grub/device</code><code>.map.</code>

<code>Check </code><code>if</code> <code>this is correct or not. If any of the lines is incorrect,</code>

<code>fix it and re-run the script `grub-</code><code>install</code><code>'.</code>

<code>(fd0)   </code><code>/dev/fd0</code>

<code>(hd0)   </code><code>/dev/sda</code>

<code>(hd1)   </code><code>/dev/sdb</code>

<code>grub  lost+found</code>

<code>[root@Redhat6 boot]</code><code># cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/  #複制系統盤的核心檔案到新添加的盤</code>

<code>[root@Redhat6 boot]</code><code># cp /boot/initramfs-2.6.32-358.el6.x86_64.img  /mnt/boot/ #複制系統盤的核心鏡像檔案到新添加的盤</code>

<code>grub                                 lost+found</code>

<code>initramfs-2.6.32-358.el6.x86_64.img  vmlinuz-2.6.32-358.el6.x86_64</code>

把新的硬碟安裝好grub分區後,接入到無法啟動的主機上,作boot引導。

<a target="_blank" href="http://blog.51cto.com/attachment/201308/121925996.png"></a>

思路2:進入急救模式,為系統安裝grub,然後挂載CD光牒,把CD光牒裡面的vmlinuz核心檔案和initrd.img核心的鏡像檔案拷貝到boot目錄下面,然後手動編輯grub配置檔案

<code>#設定為CD光牒啟動,進行Rescue模式</code>

<code>bash</code><code>-4.1</code><code>#chroot /mnt/sysimage</code>

<code>sh-4.1</code><code>#mount /dev/cdrom /media</code>

<code>sh-4.1</code><code>#cd boot</code>

<code>sh-4.1</code><code>#grub-install --root-directory=/ /dev/sda  #為硬碟安裝grub</code>

<code>sh-4.1</code><code>#cp /media/isolinux/vmlinuz /boot/vmlinuz-2.6.32-358.el6.x86_64 #複制CD光牒的核心檔案,并改名</code>

<code>sh-4.1</code><code>#cp /media/isolinux/initrd.img /boot/initramfs-2.6.32-358.el6.x86_64.img #複制CD光牒的核心鏡像檔案,并改名</code>

<code>sh-4.1</code><code>#cat &gt;&gt; /boot/grub/grub.conf &lt;&lt; EOF    #手動建立grub配置檔案</code>

<code>&gt; default=0</code>

<code>&gt; timeout=5</code>

<code>&gt; title CentOS (2.6.32-358.el6.x86_64)</code>

<code>&gt; root (hd0,0)</code>

<code>&gt; kernel </code><code>/vmlinuz-2</code><code>.6.32-358.el6.x86_64 ro root=</code><code>/dev/mapper/vg0-root</code>

<code>&gt; initrd </code><code>/initramfs-2</code><code>.6.32-358.el6.x86_64.img</code>

<code>&gt; EOF</code>

<code>sh-4.1</code><code>#exit</code>

<code>bash</code><code>-4.1</code><code>#reboot</code>

<code>#然後更改啟動項,設定為本地硬碟啟動</code>

2、/etc/inittab檔案丢失

思路:檢視inittab檔案是由那個rpm包安裝的,然後進入急救模式,挂載CD光牒重新安裝rpm

<code>[root@Redhat5 ~]</code><code># rpm -qf /etc/inittab #檢視RHEL5.x版本的inittab檔案是由那個軟體包安裝的</code>

<code>initscripts-8.45.42-1.el5</code>

插入CD光牒進入急救模式

<code>sh-4.1</code><code>#cd /media/Packages/</code>

<code>sh-4.1</code><code>#rpm -ivh --replacepkgs initscripts-8.45.42-1.el5.rpm</code>

3、bash損壞無法進入登入界面

思路:檢視bash指令是由那個rpm包安裝的,然後進入急救模式,挂載CD光牒重新安裝rpm

<code>sh-4.1</code><code>#rpm -ivh --replacepkgs bash-4.1.2-14.el6.x86_64.rpm</code>

4、遺忘root密碼

1)、grub沒有加密,修改root密碼

          進入單使用者模式設定密碼即可

<a target="_blank" href="http://blog.51cto.com/attachment/201308/144748873.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201308/144951327.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201308/145455355.png"></a>

2)、把密碼添加到grub的配置檔案啟用核心鏡像保護,修改root的密碼

<a target="_blank" href="http://blog.51cto.com/attachment/201308/151917692.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201308/152608901.png"></a>

3)、啟用grub編輯保護,修改root的密碼

<a target="_blank" href="http://blog.51cto.com/attachment/201308/152949954.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201308/153518538.png"></a>

5、MBR扇區故障

  系統啟動盤的MBR扇區損壞

<code>[root@Redhat6 ~]</code><code># mkdir /mnt/backup</code>

<code>[root@Redhat6 ~]</code><code># mount /dev/sdb1  /mnt/backup/</code>

<code>[root@Redhat6 ~]</code><code># dd if=/dev/sda of=/mnt/backup/sda.mbr.bak bs=512 count=1 #備份系統盤的MBR扇區,一定是備份到另外的一塊盤上面</code>

思路:進入急救模式,把之前備份的系統盤的MBR扇區恢複過來

<code>sh-4.1</code><code>#mkdir /dir #為備份有系統盤的MBR建立一個目錄用于挂載</code>

<code>sh-4.1</code><code>#mount /dev/sdb1 /dir  #挂載帶有備份檔案的分區</code>

<code>sh-4.1</code><code>#dd if=/dir/sda.mbr.bak of=/dev/sda bs=512 count=1 #恢複備份資料</code>

6、修複檔案系統,修複inode節點的異常

<code>[root@Redhat6  /]</code><code># touch /mnt/backup/myfile</code>

<code>touch</code><code>: cannot </code><code>touch</code> <code>"/mnt/backup/myfile"</code> <code>:device does not have space  </code><code>#建立檔案顯示沒有可用空間</code>

<code>[root@Redhat6 /]</code><code># df -lh /dev/sdb1  #檢視磁盤顯示還有空間</code>

<code>Filesystem            Size  Used Avail Use% Mounted on</code>

<code>/dev/sdb1</code>             <code>9.9G  780M  8.9G   9% </code><code>/mnt/backup</code>

<code>[root@Redhat6  /]</code><code># df -i /dev/sdb1 #檢視inode節點,發現以沒有可用inode節點</code>

<code>Filesystem            Inodes   IUsed   IFree IUse% Mounted on</code>

<code>/dev/sdb1</code>             <code>655776   655776    0    100% </code><code>/mnt/backup</code>

<code>[root@Redhat6  /]</code><code>#  find /mnt/backup/ -empty -a -type f -exec rm -rf {} \; #删除該分區為空的檔案</code>

<code>[root@Redhat6 /]</code><code># df -lh /dev/sdb1</code>

<code>[root@Redhat6  /]</code><code># touch /mnt/backup/myfile  #現在可以建立</code>

  作為一個合格的運維人員,系統的關鍵檔案要提前先做好備份操作,以免發生“亡羊補牢,為時已晚”的情況發生。

本文轉自 jie783213507 51CTO部落格,原文連結:http://blog.51cto.com/litaotao/1263946,如需轉載請自行聯系原作者

繼續閱讀