Sometimes you need to make sure ssh is running (when I debug cloud-init for example). Or you might just want to run it on a different port.
This is an example unit that you can add to Ubuntu 16.04+ that will do that.
It is mostly a copy of the ssh.service
in Ubuntu, but:
- adds the generating of ssh rsa key if it is not there.
- runs much earlier in boot
- runs on port 2222
By default, Ubuntu's ssh does not permit login as root, and cloud images do not enable password auth. If you want to let root login with password see the commented out line and modify accordingly.
add ssh-custom.service to this file to /lib/systemd/system/ssh-custom.service
.
wget https://gist.githubusercontent.com/smoser/b32bb1c33564d1d46971cd9ded2e8477/raw/ssh-custom.service -O /lib/systemd/system/ssh-custom.service
ln -s /lib/systemd/system/ssh-custom.service /etc/systemd/system/multi-user.target.wants/
systemctl daemon-reload
systemctl start ssh-custom
The root console in root-shell.service will just put a login on the second virtual console (alt+f2).
No password required, just go there and start being root.