Start SSH automatically on boot

132,810

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
Share:
132,810

Related videos on Youtube

Rothrock
Author by

Rothrock

Updated on September 18, 2022

Comments

  • Rothrock
    Rothrock over 1 year

    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.

    • user535733
      user535733 about 7 years
      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.
    • Rothrock
      Rothrock about 7 years
      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?
    • muru
      muru about 7 years
      For all this, you haven't provided any information about SSH's status when you first boot up. What does systemctl status ssh output?
    • steeldriver
      steeldriver about 7 years
      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
    • RobertRSeattle
      RobertRSeattle about 7 years
      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.
    • Rothrock
      Rothrock about 7 years
      I commented out the ListenAddress in my sshd_config file and now it works just fine. Want to put that in a answer so I can mark it as correct? Can you explain why it is even in there if it isn't needed and only seems to break the ssh service?
    • RobertRSeattle
      RobertRSeattle about 7 years
      Updated my answer below. Glad to hear things are working for you now.
  • Sam Gleske
    Sam Gleske about 7 years
    +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.
  • Rothrock
    Rothrock about 7 years
    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?
  • Timo
    Timo about 6 years
    After enabling, a new file should be created, check with ls -l /etc/systemd/system/sshd.service
  • deepdebugging
    deepdebugging over 4 years
    Ubuntu 18, none worked. Requiring me a login from the GUI to be able to ssh from another machine.
  • oarfish
    oarfish about 4 years
    The second line did the trick on elementaryOS Hera (18.04)
  • niftylettuce
    niftylettuce about 4 years
    @deepdebugging if you have to log in from the GUI, it's because you need to follow the instructions here <askubuntu.com/a/882379/275969>