瞭解Linux下的Grub

來源:互聯網
上載者:User

grub相關設定檔:
[root@lyy grub]# ll
總計 199
-rw-r--r-- 1 root root     60 10-26 20:05 device.map        #grub裝置對應檔案
-rw-r--r-- 1 root root   7584 10-26 20:05 e2fs_stage1_5    #ext2/ext3檔案系統之定義檔
-rw-r--r-- 1 root root   7456 10-26 20:05 fat_stage1_5    #FAT檔案系統之定義檔
-rw-r--r-- 1 root root   6720 10-26 20:05 ffs_stage1_5    #FFS檔案系統之定義檔
-rw-r--r-- 2 root root    195 11-20 12:21 grub.conf        #grub 在Red Hat的設定檔
-rw-r--r-- 1 root root   6720 10-26 20:05 iso9660_stage1_5    #光碟機檔案系統之定義檔
-rw-r--r-- 1 root root   8192 10-26 20:05 jfs_stage1_5    #檔案系統之定義檔
-rw-r--r-- 2 root root    195 11-20 12:21 menu.lst        #開機菜單設定檔
-rw-r--r-- 1 root root   6880 10-26 20:05 minix_stage1_5    #檔案系統之定義檔
-rw-r--r-- 1 root root   9248 10-26 20:05 reiserfs_stage1_5    #檔案系統之定義檔
-rw-r--r-- 1 root root    512 10-26 20:05 stage1
-rw-r--r-- 1 root root 104988 10-26 20:05 stage2
-rw-r--r-- 1 root root   7072 10-26 20:05 ufs2_stage1_5    #檔案系統之定義檔
-rw-r--r-- 1 root root   6272 10-26 20:05 vstafs_stage1_5    #檔案系統之定義檔
-rw-r--r-- 1 root root   8904 10-26 20:05 xfs_stage1_5
硬碟與分區在grub中的代號
grub主程式主要是從磁碟中載入核心檔案,所以它必須認識硬碟。grub對硬碟的代號設定與
傳統的linux磁碟代號可完全是不同的,如:
    (hd0,0)
硬碟代號以小括弧()包起來;
硬碟以hd表示,後面會接一組數字;
以【搜尋順序】做為硬碟的編號,而不是依照硬碟線的排序;
第一個搜尋的硬碟為0號,第二個為1號,以此類推;
每顆硬碟的第一個分區代號為0,依序類推。
第一顆硬碟的第一個邏輯分區代號為(hd0,4)。
/boot/grub/menu.lst設定檔
[root@lyy grub]# cat menu.lst
default=0    #預設開機,預設第一個title項目
timeout=5    #停留五秒,末動鍵盤選擇黙認
splashimage=(hd0,0)/grub/splash.xpm.gz        #背景圖示所在檔案
hiddenmenu    #預設隱藏完整的開機菜單
title redhat enterprise server 5.4
      root (hd0,0)        #核心檔案的分區
      kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet rhgb
      initrd /initrd-2.6.18-164.el5.img
root(hd0,0) 核心檔案的分區,而不是根目錄。
kernel 後接核心名之後就是核心參數。 root=LABEL=/ 掛載/目錄 rhgb為彩色顯示 quiet安靜模式
initrd 後接虛擬檔案系統名
其中 root=LABEL=/ 可以寫成 root=/dev/hda2
一、測試與grub安裝
使用grub-install 將一些必要的檔案複製到/boot/grub裡面去。
例:將grub安裝在目前系統的MBR下,我的系統為:/dev/hda
[root@lyy grub]# grub-install /dev/hda
如果grub發生錯誤,連grub 都無法啟動,可以利用具有grub開機的CD來開,然後再以CD的grub
的線上編輯功能使用硬碟上面的核心檔案來開機。
二、為個別選單加上密碼
例:給系統第一個開機菜單加上用MD5的密碼,同時鎖定線上編輯功能。
1.建立用MD加密過的密碼
[root@lyy ~]# grub-md5-crypt
Password:
Retype password:
$1$vX3Mu/$msyisCLVKaZh1offoGYF2.    #經過MD5處理的密碼
2.把產生的密碼加入到/boot/grub/menu.lst
[root@lyy grub]# vi menu.lst
default=0
timeout=5
hiddenmenu
title redhat enterprise server 5.4
      password --md5 $1$eU4Mu/$0BvMemF2AVPO.BYbh3/e2/
      root (hd0,0)_
      kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet rhgb
      initrd /initrd-2.6.18-164.el5.img
注意這個密碼加在title以下的第一行
3.經過以上簡單的步驟,就完成了密碼,但grub的線上編輯功能還是可以跳過密碼,所以我們
要把線上編輯功能鎖住。
default=0
timeout=5
password --md5 $1$eU4Mu/$0BvMemF2AVPO.BYbh3/e2/    #控制全域
hiddenmenu
title redhat enterprise server 5.4
      lock        #加上了這個功能
      root (hd0,0)_
      kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet rhgb
      initrd /initrd-2.6.18-164.el5.img
三、解決init設定檔錯誤
我們都知道 run level 0-6 都會讀取/etc/inittab ,如果些檔案配置錯誤,導致無法讀取,單使用者
run level也無法讀取/etc/inittab進行開機,這時,我們可以告訴核心不要執行init 改呼叫bash。
例:開機在grub菜單中用線上編輯功能,修改以下
grub edit> kernel /vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet init=/bin/bash
這樣系統呼叫的第一個程式就改成了/bin/bash,因此/sbin/init就不會執行了。
但此時除了根目錄外,其他目錄沒有被掛載而且根目錄成為了唯讀狀態,我們還需要以下命令
來重複掛載根目錄
mount -o remount,rw /    #重新掛載成為可擦寫
mount -a         #根據/etc/fstab的內容重新掛載檔案系統

作者“Liyy學習筆記”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.