HOWTO: /dev file system in chroot-ed environment

As a a traditional UNIX system, Linux systems also followed the static device nodes in /dev directory. During the installation process, /dev directory is filled with most commonly used device files. But modern linux system is not so. While booting the system, the the device nodes are created for the available (only) hardware devices. This makes the /dev not cluttered too much with mostly unused nodes.

But this behaviour results in absence of /dev/<nodes> in chroot-ed environment. This will result in failure of many commands based on /dev/<nodes>. The following is an example of such failure in a chroot-ed environment. In this example, a user tries to change root passwd of chroot-ed system.

dreambox:/# passwd
Changing password for root.
New Password:
Bad password: too simple
Reenter New Password:
Cannot open /dev/urandom for reading: No such file or directory
Cannot create salt for blowfish crypt
Error: Password NOT changed.

passwd: Authentication token manipulation error
dreambox:/#

Here is how to solve this issue:

1. Mount new root partition in a directory. (e.g.): # mount /dev/hda2 /mnt/newroot

2. Bind the current /dev with would-be root. (e.g.): # mount –bind /dev /mnt/newroot/dev

3. changing the root file system. (e.g.): # chroot /mnt/newroot /bin/bash

That is it. This will be useful during rescue process of a system.

Share this post

7 comments

  1. Thanks for this bind option in rescue system.

    In openSuse 10.2 the “-o bind” option works.

    Rescue:~ # mount -o rw /dev/rootpartion /mnt/newroot
    Rescue:~ # mount -o bind /dev /mnt/newroot/dev

    Then

    Rescue:~ # chroot /mnt/newroot

    Then one can change root password etc.

    Rescue:/ # passwd root

  2. Thanks toolman for pointing out the syntax error. It is a typo mistake. “bind” should be preceded with two hyphens as shown below:

    # mount –bind /dev /mnt/newroot/dev

  3. This isn’t quite correct, at least for my distro (ubuntu). the command at step 2 is:

    mount -o bind /dev /location/of/mountpoint/dev

Leave a Reply

Your email address will not be published. Required fields are marked *