How to recover from a chmod -R 000 /bin?
Even as root
, you can't execute files that have no x
permission bit set. What you can do though is call ld.so
on it (provided they're dynamically linked executables):
$ echo /lib/*/ld*.so
/lib/i386-linux-gnu/ld-2.27.so /lib/x86_64-linux-gnu/ld-2.27.so
Use the one that matches the architecture of chmod
executable. In my case the x86_64
one:
sudo /lib/x86_64-linux-gnu/ld-2.27.so /bin/chmod 755 /bin /bin/chmod
Or call something in /usr/bin
or elsewhere to do the chmod
like perl
:
sudo perl -e 'chmod 0755, "/bin", "/bin/chmod"
Beware when restoring permissions that some files in /bin
like mount
or su
are meant to have permissions other than 0755.
If you've rebooted, however, you might not be able to get to the point where you can run perl
or ld.so
though. You can fix things from the initramfs
though (pass an incorrect root directory to get a recovery shell in the initramfs; see also the break=bottom
or break=init
kernel parameter on Debian, for the initramfs to give you a shell after the root file system was mounted (read-only though)). Or boot your VM from a live CD image, or fix by mounting the VM file system on the host as others suggested.
Fixing the initramfs way:
In grub
, edit the boot entry and remove the root=
parameter from the linux
command:
setparams 'Ubuntu, with Linux 3.2.0-27-generic'
recordfail
gfxmode $linux_gfx_mode
insmod gzio
insmod ext2
set root='(hd1)'
search --no-floppy --fs-uuid --set=root dc02b07c-88ef-4804-afe0-4f02db2\
94561
linux /boot/vmlinuz-3.2.0-27-generic
initrd /boot/initrd.img-3.2.0-27-generic
Ctrl-X to boot. Ubuntu's initramfs won't find the root file system so start a recovery sh
. Then mount the root filesystem (in my case /dev/vdb
, adapt to your machine) and fix things there:
Target filesystem doesn't have requested /sbin/init.
No init found. Try passing init= bootarg.
BusyBox v1.18.5 (Ubuntu 1:1.18.5-1ubuntu4) built-in shell (ash)
Enter 'help' for a list of built-in commands.
(initramfs) mkdir /x
(initramfs) mount /dev/vdb /x
[ 48.430071] EXT3-fs (vdb): error: couldn't mount because of unsupported optio
nal features (240)
[ 48.477406] EXT4-fs (vdb): recovery complete
[ 48.477747] EXT4-fs (vdb): mounted filesystem with ordered data mode. Opts: (
null)
(initramfs) chmod -R 755 /x/bin
(initramfs) umount /x
(initramfs) reboot
Once booted, fix the permissions of the files that are not meant to have 755 permissions by comparing with another system.
Fixing by running python
as init
:
In grub
, edit the boot entry, this time keep the root=
parameter, change ro
to rw
and add a init=/usr/bin/python
:
setparams 'Ubuntu, with Linux 3.2.0-27-generic'
recordfail
gfxmode $linux_gfx_mode
insmod gzio
insmod ext2
set root='(hd1)'
search --no-floppy --fs-uuid --set=root dc02b07c-88ef-4804-afe0-4f02db2\
94561
linux /boot/vmlinuz-3.2.0-27-generic root=UUID=dc02b07c-88ef-4804-afe0-\
4f02db294561 rw init=/usr/bin/python
initrd /boot/initrd.img-3.2.0-27-generic
Then, at the python prompt:
Begin: Running /scripts/init-bottom ... done.
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.chmod('/bin/sh',0755)
>>> os.chmod('/bin/chmod',0755)
>>> os.execl('/bin/sh','sh')
sh: 0: can't access tty; job control turned off
# chmod -R 0755 /bin
# mount -o remount,ro /
[ 100.704720] EXT4-fs (vdb): re-mounted. Opts: errors=remount-ro
# exec /sbin/init
Again, once booted, fix the permissions of the files that are not meant to have 755 permissions by comparing with another system.
Boot another clean OS, mount the file system and fix permissions.
As your broken file system lives in a VM, you should have your host system available and working. Mount your broken file system there and fix it.
In case of QEMU/KVM you can for example mount the file system using nbd.
Use python :)
$ python
>>> import os
>>> os.chmod('/bin', 0755)
That shouldn't need anything from /bin
to do its job. Obviously, I haven't tried this out...