take changes in file sshd_config file without server reboot

125,913

Solution 1

Simply restart the sshd service:

sudo service sshd restart

or:

sudo /etc/init.d/sshd restart

Just in case you are restarting remotely, the configuration should be checked first to make sure it will not fail to start:

sudo sshd -t

Solution 2

There's an even less intrusive way to do this, without restarting the SSH service.

From man sshd:

sshd rereads its configuration file when it receives a hangup signal, SIGHUP, by executing itself with the name and options it was started with, e.g. /usr/sbin/sshd.

So you can use a command like the following to send SIGHUP to the SSH server process:

sudo kill -SIGHUP $(pgrep -f "sshd -D")

The pgrep -f "sshd -D" part will return only the PID of the sshd daemon process that listens for new connections, since there are likely to be other PIDs for each active session that don't need the signal.

Solution 3

For Systemd Systems (Ubuntu default)

sudo systemctl reload sshd.service

or

 sudo systemctl reload sshd

or

 sudo /bin/systemctl reload sshd.service

For Sysvinit / Systemd (Linux from Scratch default and Unix systems)

sudo service sshd reload

or

sudo /etc/init.d/sshd reload

Ubuntu uses systemd: Here the service command passes the units: start, stop, status, and reload through to their systemctl/initctl equivalents.

Solution 4

sudo service ssh restart

will not do it. You need to restart sshd, not ssh:

sudo service sshd restart

Solution 5

As root check

service --status-all | grep ssh

I had no sshd service, but had ssh service on Ubuntu server. Then

service ssh restart
Share:
125,913

Related videos on Youtube

Maxim Yefremov
Author by

Maxim Yefremov

Updated on September 18, 2022

Comments

  • Maxim Yefremov
    Maxim Yefremov over 1 year

    I changed the configuration in:

    /etc/ssh/sshd_config
    

    but the changes were only applied after rebooting the server. How to apply changes without a reboot?

    • AzkerM
      AzkerM almost 10 years
      You may use sudo service ssh restart to restart the service to take effect. But remember if you're connected through SSH, your session will be terminated.
  • Anwar
    Anwar over 8 years
    What if I'm on an ssh connection?
  • schulwitz
    schulwitz over 8 years
    Depends on the system configuration. In Lubuntu 14.04 sudo service ssh restart works great, whereas sudo service sshd restart reports "sshd: unrecognized service". Not sure why this varies though...
  • Scott Stensland
    Scott Stensland over 7 years
    nothing ... bouncing sshd is smart enough to permit existing ssh connections to merrily continue unabated
  • Chris Cogdon
    Chris Cogdon over 7 years
    I do not recommend running anything in /etc/init.d directly. Use the "service" command.Most services won't care, but there are a few that do because they're impacted by environment variables. "service" ensures the environment is cleaned out. For example, running "sudo" leaves the HOME environment variable to your non-root home directory.
  • lucasart
    lucasart almost 5 years
    This is the safest (stribika.github.io/2015/01/04/secure-secure-shell.html) way to reload the config: "Be extremely careful when configuring SSH on a remote host. Always keep an active session, never restart sshd. Instead you can send the SIGHUP signal to reload the configuration without killing your session. You can be even more careful by starting a new sshd instance on a different port and testing that."
  • AdamKalisz
    AdamKalisz almost 5 years
    Is there any meaningful difference to sudo pkill -HUP sshd?
  • Steven K
    Steven K almost 5 years
    @Adam You only want to HUP the listening process, not the active clients which are also sshd processes. sudo pkill -HUP -f "sshd -D" is a tempting alternative, but then pkill signals its own sudo parent since its full command line matches its own search pattern. You can come up with a pattern that doesn't match itself like ... -f "sshd -[D], but that obfuscates things a bit. I think the pgrep method is easier for learners to wrap their heads around.
  • tarabyte
    tarabyte almost 4 years
    This doesn't work on Ubuntu 16.04 for me.
  • dlamblin
    dlamblin over 3 years
    @maximyefremov DONT; you can reload instead as the question wanted, which is given in the answer from abu_bua