How to Properly Restart SSH/SSHD Service on Ubuntu (Systemd vs Init.d Methods)


2 views



Since Ubuntu 16.04, most distributions have switched to systemd as the default init system. The traditional /etc/init.d/ssh script has been deprecated in favor of systemd's service management. Here's how to properly handle SSH service operations:

First verify if OpenSSH server is installed and running:

sudo systemctl status ssh

Expected output for running service:

● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2023-11-02 14:30:21 UTC; 1 day 2h ago

For systemd-based systems (Ubuntu 16.04+):

sudo systemctl restart ssh

For legacy systems using init.d (Ubuntu 14.04 and earlier):

sudo service ssh restart
# OR
sudo /etc/init.d/ssh restart

If you get "command not found" errors, first install OpenSSH server:

sudo apt update
sudo apt install openssh-server

For debugging connection issues after restart:

sudo journalctl -u ssh --no-pager -n 50

To avoid dropping existing connections when changing configs:

sudo systemctl reload ssh

This applies changes from /etc/ssh/sshd_config without interrupting active sessions.

Enable automatic startup on boot:

sudo systemctl enable ssh

Verify it's enabled:

systemctl is-enabled ssh


If you're running a modern Ubuntu release (16.04 or later), you'll notice the traditional /etc/init.d/ssh approach no longer works. This is because Ubuntu transitioned to systemd as its default init system. The SSH daemon is pre-installed in Ubuntu as part of the openssh-server package.

First, verify if SSH is installed and running:

sudo systemctl status ssh

This command will show whether the service is active (running) or inactive (stopped).

For Ubuntu 16.04+ systems using systemd:

# Restart SSH service
sudo systemctl restart ssh

# Stop SSH service
sudo systemctl stop ssh

# Start SSH service
sudo systemctl start ssh

# Enable SSH to start at boot
sudo systemctl enable ssh

For older Ubuntu versions (pre-16.04) using init.d:

# Traditional method (now deprecated)
sudo /etc/init.d/ssh restart
# Or using service wrapper
sudo service ssh restart

If you get "command not found" errors, install openssh-server:

sudo apt update
sudo apt install openssh-server

After restarting, check the SSH configuration syntax:

sudo sshd -t

To apply configuration changes without full restart (preserving connections):

sudo systemctl reload ssh

If SSH fails to restart, check logs:

journalctl -u ssh -b
# Or for older systems:
tail -f /var/log/auth.log

Check for port conflicts:

sudo netstat -tulnp | grep :22

When restarting SSH:

# Consider changing default port in /etc/ssh/sshd_config
# Then reload:
sudo systemctl reload ssh

Always test SSH access from another session before closing your current connection!