bosnadev

№ 9 · 2014-12-29 · 1 min · pro-tips

Pro tip: How to repair an Ubuntu system with a Live CD

A colleague ran chown -R on the whole root file system. Booting an Ubuntu Live CD, mounting the installed partition and using chroot to fix the owners.

written before the past year · entry 9 of 31 · 10 in the past year

This article is out of date
Written in 2014. Versions, APIs and advice may have changed since.

Few days I ago, I had a funny situation. Colleague of mine accidentally executed:

sudo chown -R www-data:www-data /*

so instead for current directory ./* he changed owner for entire root file system. Sure, he tried to change back owner to the root:root , but root did not have any permissions anymore, since /bin is owned by another user.

Fortunately, this kind of mess on Linux systems we can fix very quickly using Live CD. So reboot your system and run Live CD.

Mounting a File System

First you need to mount the partition your Ubuntu installation is on. It will be something like sda1 or sda6 , if you are not sure about drive letter or partition number you can check it using GParted which is included in the Live CD. You mount partition simply by execution mount command:

terminal2 lines
sudo mkdir /mnt/ubuntusudo mount /dev/sdXY /mnt/ubuntu

Chrooting

Now you need to bind these directories to the chroot:

terminal4 lines
sudo mount --bind /dev /mnt/ubuntu/dev &&sudo mount --bind /dev/pts /mnt/ubuntu/dev/pts &&sudo mount --bind /proc /mnt/ubuntu/proc &&sudo mount --bind /sys /mnt/ubuntu/sys

And finally you can enter in using chroot:

terminal1 line
sudo chroot /mnt/ubuntu /bin/bash

Now you can fix whatever you want in your system, in this case I restored root as owner.

Related

Awesome Looking Terminal With Oh-My-Zshpro-tipsPro tip: Display all validation errors in Blade Viewpro-tipsSSH Authentication With Keys Instead Passwordsserver