This post introduces how to ssh to ubuntu machine in safe mode

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

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

Otherwise, if this file exists, the system will show a warn 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 do not have ability to open port in your modem, starting SSHD is not enough.

However, if you have another PC with port being exposing to the internet. You can access to any machine via 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 foregoing mentioned.