sudo mount /dev/sdXY /mnt
挂载一些特殊分区:
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc
sudo mount --bind /sys /mnt/sys
(可选) 当您连接到网络时,使用Live环境的DNS服务器(否则可能无法解析主机名):
cp /etc/resolv.conf /mnt/etc/resolv.conf
/mnt
目录: sudo chroot /mnt
apt-get install linux-image-generic
(不需要sudo,因为在chroot之后您是root用户)成功安装内核后,退出chroot并卸载一些文件系统:
exit
sudo umount /mnt/sys
sudo umount /mnt/proc
sudo umount /mnt/dev
sudo umount /mnt
sudo reboot
/mnt
。通常Ubuntu会自动处理卸载/同步文件系统,但我认为这样更干净。 - Lekensteyn/dev/mdxxx
的驱动器 - 假设它是mdadm。 - Olichroot
进入mnt
目录时,我无法访问互联网。我该如何解决这个问题? - James/etc/resolv.conf
文件(如果它是一个符号链接则删除),并在其中添加类似 nameserver 8.8.8.8
的内容(谷歌公共 DNS 服务器)。如有需要,请进行替换。 - Lekensteyncp /etc/resolv.conf /mnt/etc/resolv.conf
。这对我来说有效。 - Peter Raevesapt-get install linux-image-generic
命令,直到我复制了resolv.config
文件。注意:在执行chroot
进入/mnt
之前,我必须先执行复制resolv.config
文件的命令。否则会出现“没有此文件或文件夹”的异常。 - MUG4Ncp: not writing through dangling symlink
。我通过使用命令sudo cp --remove-destination <path to resolv.conf> /mnt/etc/.
来解决了这个问题。 - Nick Desaulnierssudo dpkg-reconfigure resolvconf
命令,然后重新启动以恢复resolv.conf文件。 - Nick Desaulnierssudo chroot /mnt
不起作用。 ubuntu@ubuntu:/bin$ sudo chroot /mnt chroot: 无法运行命令 '/bin/bash':没有那个文件或目录
我尝试使用另一个安装在另一个分区上的操作系统,还尝试使用Live USB。/etc/resolve.conf
的内容如下- `# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)chroot
中无法连接到互联网,不知道要安装哪个内核包(在Ubuntu 12.10之前,并不总是linux-image-generic
),在一开始就不知道哪个分区甚至哪个物理驱动器包含/
文件系统,以及有一个单独的/boot
分区。
我没有参考其他任何程序来写这篇文章,但你会注意到一些相似之处。我确实基于这里的程序(虽然那些指令是给完全不同的情况用的,但我对其进行了广泛改编,只复制了一些命令,而非整段文字)。
您删除了所有的内核包,而Ubuntu在没有安装内核的情况下无法启动。所以解决办法是从一个活动的CD/DVD/USB中引导,通过chroot
进入已安装的系统,并在其中安装一个内核。
Boot from an Ubuntu live CD/DVD or live USB flash drive.
Select Try Ubuntu (not Install Ubuntu).
When the desktop comes up, make sure you are connected to the Internet. If you are not, connect to the Internet. One way to see if you are connected to the Internet is to open a web browser. You can even follow the rest of the instructions by bringing this Ask Ubuntu answer up in your web browser, in the live CD/DVD/USB system. I strongly recommend doing that.
Open a Terminal window with Ctrl+Alt+T.
In the Terminal window, run this command to list your partitions:
sudo parted -l
You'll see something like this (but it won't be exactly like this):
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 20.4GB 20.4GB primary ext4 boot
2 20.4GB 21.5GB 1072MB extended
5 20.4GB 21.5GB 1072MB logical linux-swap(v1)
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0
has been opened read-only.
Error: Can't have a partition outside the disk!
Examine the output you got, to determine the device name of the partition that contains the /
filesystem of the Ubuntu system that is installed on the hard drive (that you are repairing).
If you only have one ext4
partition, that's the one.
If you have more than one ext4
partition, it's probably the first one. However, if the first one is very small--less than a gigabyte--then that might be a separate /boot
partition (remember that one too).
Please note that whether or not boot
is listed under Flags
has very little to do with whether or not a partition is a separate /boot
partition. My system, whose information is listed above, does not have a separate /boot
partition.
The device name for the partition starts with the device name for the physical drive, stated immediately after Disk
in the second line. Then just add the partition number to the end of that. So, the device name for the partition that contains my /
filesystem is /dev/sda1
. Here are the two lines where I found that information:
Disk /dev/sda: 21.5GB
1 1049kB 20.4GB 20.4GB primary ext4 boot
If you have more than one physical drive, you'll get more than one listing like what is shown above. But unless you have another Unix-like system installed, you probably will only have one drive that contains ext4
partitions, at least without having created them intentionally on another drive. If you do have multiple drives with ext4
partitions, then the ext4
partition that contains your /
filesystem is probably on a drive that also contains a linux-swap
partition.
It's possible that your Ubuntu system's /
filesystem is on a partition of type other than ext4
. When this happens, it's almost always ext3
, and almost always on a quite old system. It's very uncommon for this to be the case, unless you intentionally set things up this way yourself.
Remember the device name of the partition that contained your /
filesystem (or write it down). If it's different from /dev/sda1
, then you'll replace /dev/sda1
with it in the steps below.
(If it looked like you have a separate /boot
partition, remember the device name for that, too.)
Mount the /
filesystem to /mnt
, and mount its /dev
filesystem:
sudo mount /dev/sda1 /mnt
sudo mount --bind /dev /mnt/dev
Check if the broken Ubuntu system you're repairing has a separate /boot
partition which must be mounted separately. (If you are sure it does not, you can skip this.)
To check, run:
ls /mnt/boot
If there is output (like grub memtest86+.bin memtest86+_multiboot.bin
, but not necessarily exactly that), then the broken system's /boot
is on the same partition as its /
and you don't have to mount anything to access it.
But if there is no output, then you will have to mount the /boot
filesystem:
sudo mount BOOT-PARTITION /mnt/boot
Replace BOOT-PARTITION
with the device name of the /boot
partition (see step 6 above).
chroot
into the broken system, mount the remaining important virtual filesystems, and set some important environment variables:
sudo chroot /mnt
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C
Determine if Internet access works from within the chroot
by ping
ing some reliable host that is known to respond normally to pings:
ping -c 5 www.google.com
You should see something like this:
PING www.l.google.com (74.125.131.147) 56(84) bytes of data.
64 bytes from vc-in-f147.1e100.net (74.125.131.147): icmp_req=1 ttl=44 time=61.3 ms
64 bytes from vc-in-f147.1e100.net (74.125.131.147): icmp_req=2 ttl=44 time=62.3 ms
64 bytes from vc-in-f147.1e100.net (74.125.131.147): icmp_req=3 ttl=44 time=61.8 ms
64 bytes from vc-in-f147.1e100.net (74.125.131.147): icmp_req=4 ttl=44 time=63.8 ms
64 bytes from vc-in-f147.1e100.net (74.125.131.147): icmp_req=5 ttl=44 time=66.6 ms
--- www.l.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 61.367/63.212/66.608/1.897 ms
If it looks mostly like that, and the number before % packet loss
is less than 100, then the Internet connection in the chroot
is working:
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
It's working, so you can skip step 11.
If it looks mostly like that, and the number before % packet loss
is 100, the connection needs troubleshooting. Make sure the connection on the live CD system (for example, through a web browser, or by running the same command in a separate, non-chroot
ed Terminal tab/window) works. Make sure you're typing the command correctly. Use www.google.com
if you haven't been.
If the output doesn't look like the above at all, but instead says ping: unknown host www.google.com
, then networking isn't working yet in the chroot
.
Set up networking in the chroot
. Skip this step unless you got an unknown host
error in step 10 above.
To set up networking, back up the broken system's hosts
file, and copy over the live CD system's hosts
and resolv.conf
files. (You don't have to back up the broken system's version of resolv.conf
, as that file is automatically regenerated on-the-fly.)
Open a new Terminal tab (Ctrl+Shift+T) or, if you prefer, a new Terminal window (Ctrl+Shift+N, or just Ctrl+Alt+T). Run these commands in it:
sudo cp /mnt/etc/hosts /mnt/etc/hosts.old
sudo cp /etc/hosts /mnt/etc/hosts
sudo cp /etc/resolv.conf /mnt/etc/resolv.conf
exit
(The exit
command at the end closes the new tab/window.)
Repeat step 10 above to make sure Internet access works now from within the chroot
. It should.
Figure out which kernel package should be installed. Usually, this will be linux-image-generic
. But not always.
If you're not sure which to install, it will depend partly on which Ubuntu release you have installed, and partly on other information. If you are not sure which Ubuntu release you have installed, find out by running this command (in the chroot
, not in a separate Terminal window/tab):
lsb_release -r
On Ubuntu 12.10 (the next Ubuntu release, currently in development), it always will be linux-image-generic
. (See this, this, and this.)
On Ubuntu 12.04 LTS, likely possibilities are linux-image-generic
and linux-image-generic-pae
. (Unlike previous versions, 12.04 no longer has separate server and desktop kernels.)
If the installed Ubuntu system (that you are fixing) is the 64-bit version, use linux-image-generic
. (linux-image-generic-pae
only applies to 32-bit systems.)
It's possible to have a 32-bit Ubuntu system installed on a 32-bit or 64-bit computer. Furthermore, you might be using a 32-bit or 64-bit live CD to fix a 32-bit installed system. So if you don't know whether the installed Ubuntu system is 32-bit or 64-bit, check by running this command (in the chroot
, not in a separate Terminal window/tab):
dpkg-architecture -qDEB_HOST_ARCH_BITS
The output will be either 32
or 64
.
(Please note that uname -m
is not a correct way to find this information, because even when run in the chroot
, that will tell you the architecture of the running kernel, which is the live CD system's kernel and not the installed (broken) system's kernel.)
If the installed Ubuntu system (that you are fixing) is the 32-bit version, the best kernel to use will depend on how much RAM you have. I recommend:
linux-image-generic
if you have less than 3 GB of RAMlinux-image-generic-pae
if you have 3 GB of RAM or more.(This is how Ubuntu's installer chooses which one to set up, ever since the installer gained the ability to install PAE kernels. See the resolution to this bug. If you want to learn what PAE is, see this Wikipedia article. If you want to learn about PAE in Ubuntu, see this Ubuntu wiki page.)
If you don't know how much RAM you have, run this command to find out:
grep MemTotal /proc/meminfo
That is listed in kilobytes. To convert to gigabytes, divide by 1,048,576 (10242).
On Ubuntu releases before 12.04, likely possibilities are linux-image-generic
, linux-image-generic-pae
, and linux-image-server
.
linux-image-server
.This is the moment you've been waiting for! Install a kernel in the broken system.
(Like before, except where explicitly indicated otherwise, these commands are run in the chroot
, not in a separate Terminal window/tab.)
apt-get update
apt-get -y install linux-image-generic
Replace linux-image-generic
with whatever other kernel package you decided to install in step 12 above, if different.
If you had to perform step 11 to set up networking in the chroot
, restore the old hosts
file. If you skipped step 11, skip this step too.
To restore it, run this command:
cp /etc/hosts.old /etc/hosts
Unmount filesystems, exit
ing out of the chroot
:
umount /proc || umount -lf /proc
umount /sys /dev/pts
exit
sudo umount /mnt/dev /mnt
Shut down the live CD/DVD/USB system, removing the live CD/DVD or USB flash drive. Boot into the system installed on the hard drive, that you just repaired. You've installed a kernel package in it (and as part of the installation, the kernel it provides will be added back to the GRUB2 boot menu). If everything worked correctly, your system should boot without problems. (I think it's possible that it will take a little longer to boot than usual, this time.)
/etc/resolv.conf
现在由 resolvconf
管理,当使用 NetworkManager 时会自动生成,并且可以安全地使用 rm /etc/resolv.conf
进行删除,正如你所发现的那样。 - Lekensteynupdate-grub
,GRUB也能看到我的内核,并且没有从chroot中再次运行update-grub
。安装任何内核包都应该更新GRUB,或者当你的内核元包引入了一个新的内核包时,你必须手动执行更新。你有编辑过GRUB配置文件吗?在chroot中安装了linux-image-generic
还是其他包?这是什么时候发生的?4.4.0-53已经不再是16.04最近的两个内核版本之一了。(顺便说一下,我在[chat]里,如果那样方便的话。) - Eliah Kagangrub
时遇到了问题,它无法找到新的映像,可能是因为我需要在chroot
环境中挂载efi或其他东西。但是通过恢复模式,我成功地启动了一个旧的内核,并手动更新了grub。之后重新启动就正常工作了。(Ubuntu 19.10) - SO_fix_the_vote_sorting_bugresolv.conf
,否则你的DNS将会出问题,而且由于登录用户是root,所以不需要在任何地方使用sudo。sudo mount /dev/sda2 /mnt
sudo cp /etc/resolv.conf /mnt/etc/
sudo cp /etc/hosts /mnt/etc/
sudo mount --bind /dev/ /mnt/dev
sudo chroot /mnt
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C
dbus-uuidgen > /var/lib/dbus/machine-id
dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl
apt-get update
apt-get install linux-image-2.6.32-26-generic
update-initramfs -cv -k all
update-grub
rm /etc/resolv.conf
rm /etc/hosts
rm /var/lib/dbus/machine-id
rm /sbin/initctl
dpkg-divert --rename --remove /sbin/initctl
umount /proc # if this doesn't work try umount -lf /proc
umount /sys
umount /dev/pts
exit
sudo umount /mnt
/etc/hosts
并删除/sbin/initctl
?它们存在是有原因的。 - Lekensteyn/sbin/initctl
、dbus-uuidgen
或dpkg-divert
做任何操作。你可能需要替换/etc/hosts
以连接到互联网,但通常情况下不需要,如果确实需要,应该将原始文件恢复回来。在已安装的系统上(而不是从引导然后解压缩squashfs文件系统的Live CD),安装内核后不必运行update-initramfs
甚至update-grub
。 - Eliah Kaganlinux-image-2.6.32-26-generic
对大多数人来说并不是适合安装的内核。 - Eliah KaganError: File not found
Error: You need to load the kernel first
linux-image-generic
。它确实安装了150 MB的新内核,但可悲的是这并没有解决问题。Boot-Repair
工具,我的系统恢复正常了。