Linux-tips

Fork me on GitHub

I. Linux tips

9. Modify files of your linux without booting on it

If you broke your linux and need to undo a change, you can do it by either using a bootable key and change the file through it like showed in here or avoid running /sbin/init. You wonder why? Then read what follows!

Change boot line in grub

The first thing to do is to edit the boot line of your distribution when you're on the grub. Generally, it's the key 'e' but it might change, depending on your system. Then go to the line starting by "linux".


grub screenshot

In this line, add at the end (with a whitespace before):

init=/bin/sh

(Or another shell, like bash if you know its binary path.)

Or even more simple:

break

Then press the button to boot (in my case, this is CTRL-X or F10). Once done, you should be on a command line. Congrats, we have prevent the init program to run!

Change files

We can now explore our root partition but not modify files. If you want to modify files, you'll have to remount the root partition with write rights:

mount -o remount,rw /

We can now edit files as we want. If you want to mount other partitions, run (but you'll need a write access):

vgchange -ay

Then check the format of the partition you want to mount with:

blkid

So it'll display in my case:

> blkid
/dev/sda1: PARTUUID="c3554sd-sdfdsq-a454-01154875qs"
/dev/mapper/vg00-lvroot: UUID="05547454-c54d-4999-bfbf-cc54557574" TYPE="ext3"
/dev/mapper/vg00-lvopt: UUID="5af4d5ee-aaa4-9ef6-e254-dde475s47d" TYPE="ext3"
/dev/mapper/vg00-lvvar: UUID="4599fab2-a4b7-f65e-5d4e-c47d6654ec" TYPE="ext4"
/dev/mapper/vg00-lvsub: UUID="19fab277-47b1-5dfe-4235-d66dfe54ec" TYPE="ext4"

And then mount them:

mount -t [format] /dev/vg00/[partition] /[where]

So in my case (sub is a child of var):

mkdir /mnt
mount -t ext3 /dev/vg00/lvroot /mnt
mount -t ext3 /dev/vg00/lvopt /mnt/opt
mount -t ext4 /dev/vg00/lvopt /mnt/var
mount -t ext4 /dev/vg00/lvopt /mnt/var/sub

Now you have everything you want in /mnt. Then you chroot in it to make your life a bit easier:

chroot /mnt