当前位置: 首页>>技术问答>>正文


我该如何修复grub? (如何在安装Windows之后重新启动Ubuntu?)

, , , ,

问题描述

我安装了Windows 7,它使用Ubuntu的启动文件。当启动计算机时,它现在直接进入Windows,而不给我启动Ubuntu的选项。

我怎样才能让Ubuntu回归?

最佳解决思路

当您安装Windows时,Windows会认为它是计算机上唯一的操作系统(OS),或者至少它不占Linux。所以它用自己的引导装载程序取代了GRUB。您必须做的是用GRUB替换Windows引导加载程序。我已经看到了各种用GRUB命令替换GRUB的说明,但对我来说,最简单的方法是将chroot简单地放入您的安装并运行update-grubchroot非常棒,因为它可以让你在实际的安装上工作,而不是试图在这里和那里重定向。它非常干净。

就是这样:

  1. 从”Try Ubuntu”模式下的live CD或live USB启动。

  2. 确定主分区的分区号。 sudo fdisk -lsudo blkid或GParted(默认情况下,它应该已安装在实时会话中)可以在此帮助您。我将在这个答案中假设它是/dev/sda2,但确保你的系统使用了正确的分区号!如果您的主分区位于LVM中,则设备将位于/dev/mapper/中,很可能位于/dev/mapper/{volume}--{os}-root,其中{volume}是LVM卷名,{os}是操作系统。为确切的名称执行ls /dev/mapper

  3. 安装你的分区:

    sudo mount /dev/sda2 /mnt  #Replace sda2 with the partition from step 2
    

    如果您有单独的/boot,/var或/usr分区,请重复步骤2和3,分别将这些分区挂载到/mnt /boot,/mnt /var和/mnt /usr。例如,

    sudo mount /dev/sdXW /mnt/boot
    sudo mount /dev/sdXY /mnt/var
    sudo mount /dev/sdXZ /mnt/usr
    

    用相应的分区号替换sdXWsdXYsdXZ

  4. 绑定一些其他必要的东西:

    for i in /sys /proc /run /dev; do sudo mount --bind "$i" "/mnt$i"; done
    
  5. 如果Ubuntu以EFI模式(see this answer if you’re unsure)安装,请使用sudo fdisk -l | grep -i efi或GParted查找EFI分区。它将有一个EFI标签。安装此分区,将sdXY替换为系统的实际分区号:

    sudo mount /dev/sdXY /mnt/boot/efi
    
  6. chroot到您的Ubuntu安装:

    sudo chroot /mnt
    
  7. 此时,您处于安装状态,而不是实时会话,并以root用户身份运行。更新grub:

    update-grub
    

    如果出现错误或者如果执行步骤7没有解决您的问题,请转到步骤8.(否则,它是可选的。)

  8. 根据您的情况,您可能需要重新安装grub:

    grub-install /dev/sda
    update-grub # In order to find and add windows to grub menu.
    
  9. 如果Ubuntu以EFI模式安装并且EFI分区UUID已更改,则可能需要在/etc/fstab中对其进行更新。比较一下:

    blkid | grep -i efi
    grep -i efi /etc/fstab
    

    如果当前的EFI分区UUID(来自blkid)与/etc/fstab中的不同,请使用当前UUID更新/etc/fstab

  10. 如果一切正常无误,那么你就全都定下了:

    exit
    sudo reboot
    
  11. 此时,您应该能够正常启动。

如果您无法正常启动,并且没有执行步骤8,因为没有错误消息,请重试步骤8。

  • 有时为GRUB2提供正确的分区配置是不够的,而且您必须将其安装(或重新安装)到主引导记录中,步骤8即可。在聊天中帮助用户的体验表明,即使没有显示错误消息,步骤8有时也是必需的。

次佳解决思路

Windows安装程序不关心系统中的其他操作系统。所以它会在主引导记录上写入自己的代码。幸运的是,解决方案也很简单。

您需要修复MBR。请执行下列操作

使用ubuntu的live usb /cd启动。使用boot-repair来解决问题。

使用live usb /cd启动后,在终端中运行以下命令:

sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair

使用Recomended Repair

dual-boot,grub2,windows,boot-repair,ubuntu

更多信息 – https://help.ubuntu.com/community/Boot-Repair

第三种解决思路

我从来没有遇到过使用这些指令的麻烦:https://wiki.ubuntu.com/Grub2#Recover

首先,你必须从一张live cd启动你的系统。然后

METHOD 3 – CHROOT

This method of installation uses the chroot command to gain access to the broken system’s files. Once the chroot command is issued, the LiveCD treats the broken system’s / as its own. Commands run in a chroot environment will affect the broken systems filesystems and not those of the LiveCD.

  1. Boot to the LiveCD Desktop (Ubuntu 9.10 or later). Please note that the Live CD must be the same as the system you are fixing – either 32-bit or 64-bit (if not then the chroot will fail).

  2. Open a terminal (Applications > Accessories > Terminal).

  3. Determine your normal system partition – (the switch is a lowercase “L”)

    sudo fdisk -l 

    If you aren’t sure, run

    df -Th   

    Look for the correct disk size and ext3 or ext4 format.

  4. Mount your normal system partition:

    Substitute the correct partition: sda1, sdb5, etc.

    sudo mount /dev/sdXX /mnt   

    Example: sudo mount /dev/sda1 /mnt

  5. Only if you have a separate boot partition: sdYY is the /boot partition designation (for example sdb3)

    sudo mount /dev/sdYY /mnt/boot 
  6. Mount the critical virtual filesystems:

    sudo mount --bind /dev /mnt/dev sudo mount --bind /dev/pts /mnt/dev/pts sudo mount --bind /proc /mnt/proc sudo mount --bind /sys /mnt/sys 
  7. Chroot into your normal system device:

    sudo chroot /mnt 
  8. If there is no /boot/grub/grub.cfg or it’s not correct, create one using

    update-grub 
  9. Reinstall GRUB 2:

    Substitute the correct device – sda, sdb, etc. Do not specify a partition number.

    grub-install /dev/sdX 
  10. Verify the install (use the correct device, for example sda. Do not specify a partition):

    sudo grub-install --recheck /dev/sdX 
  11. Exit chroot: CTRL-D on keyboard

  12. Unmount virtual filesystems:

    sudo umount /mnt/dev/pts sudo umount /mnt/dev sudo umount /mnt/proc sudo umount /mnt/sys 
  13. If you mounted a separate /boot partition:

    sudo umount /mnt/boot 
  14. Unmount the LiveCD’s /usr directory:

    sudo umount /mnt/usr 
  15. Unmount last device:

    sudo umount /mnt 
  16. Reboot.

    sudo reboot 

第四种思路

通过以下步骤从ubuntu上启动实时Ubuntu USB随身碟或CD并安装Boot-Repair

打开终端并运行以下命令

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install boot-repair

完成安装后,如果您使用Gnome,则可以从System->管理 – > Boot-Repair菜单启动它;如果使用Unity,则可以在短划线中搜索”boot-repair”。然后按照以下截图进行操作:

方法1

  • 点击高级选项

dual-boot,grub2,windows,boot-repair,ubuntu

  • 勾选下面显示的选项

dual-boot,grub2,windows,boot-repair,ubuntu

  • 将选项卡更改为Grub Location选项卡并勾选图中显示的选项

dual-boot,grub2,windows,boot-repair,ubuntu

按应用并重启系统

方法2

  • 选择推荐的引导修复选项,如第一个屏幕截图所示

Documentation :

第五种思路

只需在Windows 7中安装easyBCD并执行

添加新条目> Linux /BSD> (选择)Grub2> (推)添加条目

然后,您可以选择Windows 7引导加载程序中的Ubuntu以转到Grub2(之前的引导加载程序)。

第六种思路

现在有一个更简单的解决方案:

  1. 重新启动,然后输入计算机的BIOS选项(F2或有时为F11)。

  2. 转到Boot菜单,然后选择Boot Device Priority

  3. 检查Windows启动管理器是否在主启动驱动器(通常是SATA HDD …或IDE HDD …)之上。如果是,请将引导磁盘的优先级高于Windows引导管理器的优先级。

  4. 保存你的BIOS选项,并退出(通常是F10)。

这已经在双系统启动Windows 8和Ubuntu 13.10的三星Series 7 Chronos笔记本电脑上进行了测试,禁用了安全启动,启用了UEFI和传统启动。

第七种思路

Boot-Repair为我工作。使用图形应用程序非常简单,您不需要使用命令行,只需单击按钮即可:)

所有可用的修复选项都在Ubuntu documentation中进行了介绍,并且有一个separate page解释如何启动Boot-Repair(通过创建可引导磁盘或将其安装到现有的Ubuntu活动磁盘中)以及如何使用它。

只需启动Ubuntu live CD,安装Boot-Repair并运行它。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/106.html,未经允许,请勿转载。