20

I try to repair an broken Ubunu 14.04 with chroot. What I did, is to boot Ubuntu from USB mounted the original system that has to be repaired and changed to this system with chroot:

sudo mount /dev/sdXY /mnt 
sudo mount -o bind /dev /mnt/dev 
sudo mount -o bind /sys /mnt/sys 
sudo mount -t proc /proc /mnt/proc 
sudo cp /proc/mounts /mnt/etc/mtab 
sudo chroot /mnt /bin/bash 

That worked fine, but in chroot environment I don't have access to the internet, so apt isn't able to resolve hostnames. What am I supposed to do?

ping www.askubuntu.com

does not work either.

user5950
  • 6,056
  • 11
  • 59
  • 83

1 Answers1

35

On newer Ubuntu systems, name resolution is handled by the resolvconf service, and /etc/resolv.conf is a symbolic link to /run/resolvconf/resolv.conf. You can either add a bind mount to the /run filesystem along with your other bind mounts before executing the chroot command

sudo mount -o bind /run /mnt/run

so that the chroot system picks up the host system's DNS settings or, once you're in the chrooted system, temporarily create a static /etc/resolv.conf with nameserver(s) of your choice e.g.

echo 'nameserver 8.8.4.4' | sudo tee -a /etc/resolv.conf
steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • 2
    What are "newer Ubuntu systems"; in other words since which version is name resolution handled by resolvconf? – Pro Backup Aug 11 '14 at 16:04
  • @ProBackup at least from 12.04 I think, although it may have been backported - see [DNS in Ubuntu 12.04](https://www.stgraber.org/2012/02/24/dns-in-ubuntu-12-04/) – steeldriver Aug 11 '14 at 16:12
  • 2
    doing `sudo mount --bind /run /mnt/run` worked for me chrooting into a botched 17.10 upgrade. Thanks for the answer! – labyrinth Nov 17 '17 at 01:02
  • Is there a permanent solution? – birgersp Aug 28 '18 at 15:58
  • 2
    It’s probably a bit defeating the purpose of chroot if you bind the whole /run directory with a lot of sockets, so probably binding only the needed sockets is a better idea (resolvconf, maybe nscd and syslog) – eckes Mar 19 '19 at 09:23
  • on 18.04 it's apparently enough to just copy /etc/resolv.conf to jail/etc/resolv.conf (as an added security measure, i made sure it was owned by root and chmod'ed to 0444, but idk if that's important or not) – hanshenrik Oct 23 '19 at 11:05
  • @eckes it depends what "the purpose" is imho - in this case, the chroot is being used for system recovery rather than as a jail – steeldriver Oct 23 '19 at 12:05
  • Yes agreed @steeldriver – eckes Oct 23 '19 at 15:31
  • this does not work if the host is using systemd. At least it does not work on ClearLinux circa 2020 – Richard May 18 '20 at 22:45
  • Works fine on ubuntu 20.04 as host and finnix 122 as squashfs chrooted. – momo2047 May 02 '21 at 19:52