SSH to ubuntu machine in safe mode

This post introduces how to ssh to the Ubuntu machine in safe mode.

  • Boot into safe mode (recovery mode)
  • Enable networking
  • Log in to a root shell
  • Enable ssh server via sudo systemctl start sshd

By initial, Ubuntu disables SSH login when booting up for security purposes. You need to manually remove the /var/run/nologin file to force enabling this.
Deleting this file does not affect the system because it is a temporary file while the PC is booting up.

Otherwise, if this file exists, the system will show a warning as following and deny you to ssh into it.

"System is booting up. Unprivileged users are not permitted to log in yet. Please come back later. For technical details, see pam_nologin(8)."
Authentication failed.

Source

If the machine does not open to the world, i.e., you cannot open the port in your modem, starting SSHD is not enough.

However, if you have another PC with the port being exposing to the internet. You can access any machine via the SSH reverse tunnel trick.
I have already introduced the trick here. The post is written in Vietnamese in the first half. No worry, you only need to read the latter half, it is in English, and that part is enough for the purpose I have preceding mentioned.