Since Ubuntu 16.04, most distributions have switched to systemd as the default init system. The traditional
/etc/init.d/sshscript 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 sshExpected 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 agoFor systemd-based systems (Ubuntu 16.04+):
sudo systemctl restart sshFor legacy systems using init.d (Ubuntu 14.04 and earlier):
sudo service ssh restart # OR sudo /etc/init.d/ssh restartIf you get "command not found" errors, first install OpenSSH server:
sudo apt update sudo apt install openssh-serverFor debugging connection issues after restart:
sudo journalctl -u ssh --no-pager -n 50To avoid dropping existing connections when changing configs:
sudo systemctl reload sshThis applies changes from
/etc/ssh/sshd_configwithout interrupting active sessions.Enable automatic startup on boot:
sudo systemctl enable sshVerify 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/sshapproach 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 theopenssh-serverpackage.First, verify if SSH is installed and running:
sudo systemctl status sshThis 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 sshFor 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 restartIf you get "command not found" errors, install openssh-server:
sudo apt update sudo apt install openssh-serverAfter restarting, check the SSH configuration syntax:
sudo sshd -tTo apply configuration changes without full restart (preserving connections):
sudo systemctl reload sshIf SSH fails to restart, check logs:
journalctl -u ssh -b # Or for older systems: tail -f /var/log/auth.logCheck for port conflicts:
sudo netstat -tulnp | grep :22When restarting SSH:
# Consider changing default port in /etc/ssh/sshd_config # Then reload: sudo systemctl reload sshAlways test SSH access from another session before closing your current connection!
How to Properly Restart SSH/SSHD Service on Ubuntu (Systemd vs Init.d Methods)
27 views