Start SSH automatically on boot
Completely brand new to linux and Ubnuntu. Setting up a machine to do deep learning/neural networks. So far it is going well. Got everything installed and running. I want to be able to ssh from my Mac laptop to use the system. I installed openssh and got that working.
However when I restart the machine, the ssh server doesn't restart. I check with sudo service ssh status
and it reports and error. I can then start it and everything works fine.
My first thought was I need to tell it to start on reboot, and I found this thread which says that it should start by default and I don't need to add a chrontab or anything like that. So I don't think that is it.
Then I found this thread which suggest using the command sudo update-rc.d ssh defaults
, but that thread is quite old and it seems that answer was maybe not right even back then?
So then I found this thread which suggest commenting out the ListenAddress
line in the sshd_config file. Basically it says if the ListenAddress is used it may try to get the IP before it has been assigned by the DCHP and so start up fails. Which makes sense. But I need that parameter to make my port forwarding work over my home network.
This documentation under the entry for ListenAddress says that
The default is to listen on all local addresses.
So maybe I don't need it to make port forwarding work?
I followed the previous thread to this bug report which is also quite old, but suggests to use network manager to restart ssh every time netmanager gets a new IP.
echo /etc/init.d/ssh restart > /etc/NetworkManager/dispatcher.d/10ssh
chmod 755 /etc/NetworkManager/dispatcher.d/10ssh
That is a lot more than this noobie can evaluate at this point. Is that safe? Is that the way to do this? Has something else been worked out in the last 7 years?
Any advice would be great. Thanks.
Solution 1:
Have you tried just simply setting
sudo systemctl enable ssh
?
That's how I have my ssh set to run at start up.
****I'm leaving in the above part of the answer in case it's helpful for others who come across this post****
Quoting my comment from above:
How do you have your port forwarding setup? I don't use the ListenAddress at all and things work fine on my home server. My router forwards anything that comes in on a port I specify (22 if you want the standard port, but you can use anything as long as it doesn't conflict with other services on your intl. network) to the computer I want. For example anything that comes in on port 12345 gets forwarded to port 22 on my CentOS server. Anything that comes in on 12346 gets forwarded to port 22 of my headless RasPi. Anything that comes in on 12347 goes to port 6697 for the IRC bouncer on my RasPi
The ListenAddress has nothing to do with port forwading, the ListenAdress can be used for a server that is configured with multiple IP addresses over one or more NICs. Quoting from here (the whole page is a good explanation): https://www.cyberciti.biz/tips/howto-openssh-sshd-listen-multiple-ip-address.html
Let us say you have total 8 public IP address and one private IP address. You >would like to bind sshd to one selected public IP (ex 70.5.xx.xx) and private >IP (10.1.5.1) only.
Luckily there is an easy way to achieve this using ListenAddress option. It >specifies the local addresses sshd should listen on. If this directive is >skipped from configuration file sshd will bind or list on all available IP >address.
It's to allow one some of your multiple IPs to accept ssh connections. I'm guessing you set the ListenAddress to either your public IP or your router's internal IP - I'm guessing if you set that to your server's local IP it would have worked fine. Regardless, now you know how ListenAddress works and you'll be ready if you have to configure a more complicated server. Glad to hear you got things working.
Solution 2:
As the suggested solution did not work for me, I eventually found that additional command is needed to start SSH on boot:
sudo update-rc.d ssh defaults
sudo systemctl enable ssh.socket
Some mentioned that this command:
sudo systemctl enable ssh.service
should be executed instead of this command:
sudo systemctl enable ssh