Linux: Access directly to the shell in case of boot failure

Why

Sometimes, we mess up with Ubuntu (for example), then we are not able to boot to the graphical user interfaces. We may fix the issue with the command line, but we cannot access the command line because of failures during boot time.

In this case, we can edit the grub boot option, to access the shell. Then we made changes, then reboot.

With this trick, we can even reset the root password without remembering the current password.

How

When the grub shows the menu of operating systems to boot, press e to edit the menu of current entry.

Find the line that contains linux... vmlinux ... ro. We need to:

  1. ro => rw Any change has to be writen to the system.
  2. add init=/bin/sh
  3. Press Ctrl + X to boot

That’s it. If you know this trick, you are probably know how to fix your own issue.

I guess anyone who jumps into this post via search engine just doesn’t remember the exact parameter, but they know how already. Please comment if this post help you 🙂

Bash: set up a temporary SSH tunnel

You can do this cleanly with an ssh ‘control socket’. To talk to an already-running SSH process and get its PID, kill it etc. Use the ‘control socket’ (-M for master and -S for socket) as follows:


ssh -M -S my-ctrl-socket -fnNT -L 50000:localhost:3306 [email protected]

ssh -S my-ctrl-socket -O check [email protected]
Master running (pid=3517) 

ssh -S my-ctrl-socket -O exit [email protected]
Exit request sent.

Note that my-ctrl-socket will be an actual file that is created.


You can use -o ExitOnForwardFailure=yes with -f and SSH will wait for all remote port forwards to be successfully established before placing itself in the background. You can grep the output of ps to get the PID. For example, you can use

ssh -Cfo ExitOnForwardFailure=yes -N -L 9999:localhost:5900 $REMOTE_HOST
PID=$(pgrep -f 'N -L 9999:')
[ "$PID" ] || exit 1

and be pretty sure you’re getting the desired PID