14

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.

  • Gee, I simply installed the openssh-server package and configured it properly. My ssh server has automatically started properly every time since. No need for any extra fiddling at all. – user535733 Mar 13 '17 at 2:48
  • 1
    Are you using port forwarding and you didn't need to use the ListenAddress parameter? If so, can you share what "configured it properly" includes? – Rothrock Mar 13 '17 at 4:51
  • For all this, you haven't provided any information about SSH's status when you first boot up. What does systemctl status ssh output? – muru Mar 13 '17 at 5:41
  • Your issue sounds similar to this (apparently unresolved) SSH Daemon doesn't start normally way - can you please edit your question to include the output of systemctl status NetworkManager-wait-online.service – steeldriver Mar 13 '17 at 12:30
  • 1
    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. – RobertRSeattle Mar 13 '17 at 16:34
43

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.

  • 2
    +1 This is the correct answer. Ubuntu 16.04 started using systemd. The command in this answer is using systemd to start SSH on reboot. The other threads the OP linked only applied to upstart and not systemd or didn't apply to their problem at all. – Sam Gleske Mar 13 '17 at 3:28
  • 1
    I did systemctl list-unit-files –type=service and I see that ssh.service is enabled. So I don't think that is the problem. But thanks for the tip on systemctl/systemd. That gives me more places to look. Is there a way to tell systemd/systemctl to wait until the DCHP returns the IP address? – Rothrock Mar 13 '17 at 5:02
  • After enabling, a new file should be created, check with ls -l /etc/systemd/system/sshd.service – Timo Apr 10 '18 at 13:34
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.