When SSH connections fail, restarting the SSH service in Linux can re-establish secure access quickly. This guide will show you exactly how to restart ssh service in linux across different distributions, with clear commands and troubleshooting tips.
SSH is the backbone of remote server management. When it stops working, you need fast solutions. Let’s get your SSH service back up and running.
Understanding SSH Service In Linux
SSH (Secure Shell) allows secure remote logins and command execution. The service runs in the background, listening for connection requests. Sometimes it needs a restart after configuration changes or when it crashes.
Different Linux distributions use different service managers. The most common ones are systemd, SysVinit, and Upstart. Knowing which one your system uses is the first step.
Check Your Service Manager
Run this command to see what init system your Linux uses:
ps -p 1 -o comm=
If it returns systemd, you’re on a modern system. If it shows init or upstart, you’re using an older init system. This determines the exact commands you’ll use.
How To Restart Ssh Service In Linux
Here’s the core method for restarting SSH on most modern Linux systems. The exact command depends on your distribution and init system.
Using Systemd (Ubuntu 16.04+, Debian 8+, CentOS 7+, RHEL 7+)
Systemd is the default on most modern Linux distributions. Use these commands:
- Open a terminal or SSH session (if you still have access).
- Run:
sudo systemctl restart ssh(on Debian/Ubuntu) orsudo systemctl restart sshd(on CentOS/RHEL/Fedora). - Check the status:
sudo systemctl status sshorsudo systemctl status sshd.
That’s it. The service should restart instantly. If you see “active (running)” in green, you’re good.
Using SysVinit (Older Systems Like CentOS 6, Debian 7)
For older systems without systemd, use the service command:
- Run:
sudo service ssh restart(Debian/Ubuntu) orsudo service sshd restart(CentOS/RHEL). - Verify:
sudo service ssh statusorsudo service sshd status.
Some older systems also support /etc/init.d/ssh restart directly.
Using Upstart (Ubuntu 14.10 And Earlier)
Upstart was used before systemd became standard. Commands are similar:
- Run:
sudo initctl restart ssh. - Or:
sudo service ssh restart.
Upstart systems are rare now, but you might encounter them on legacy servers.
Common SSH Service Names By Distribution
One common confusion: the service name varies. Here’s a quick reference:
- Ubuntu/Debian: The service is usually called
ssh. - CentOS/RHEL/Fedora: The service is called
sshd. - OpenSUSE: Also uses
sshd. - Arch Linux: Uses
sshd.
If you’re unsure, try both names. Running sudo systemctl restart ssh on a system that expects sshd will just give an error. Then you know to use the other name.
Restarting SSH Without Losing Your Connection
This is a critical concern. When you restart the SSH service, your current session might drop. Here’s how to avoid that:
- Use
screenortmuxto keep your session alive. - Open a second SSH session before restarting, so you have a backup.
- Use
nohupif you need to run a restart command that might disconnect.
Example with screen:
- Run
screen -S sshrestart. - Execute your restart command.
- If you get disconnected, reconnect and run
screen -r sshrestart.
This is a lifesaver when working on remote servers.
When To Restart SSH Service
You don’t need to restart SSH often. But here are common scenarios:
- After config changes: You edited
/etc/ssh/sshd_configand need to apply changes. - Service crash: SSH stopped responding or shows errors.
- Security updates: After updating the SSH package, a restart ensures the new version runs.
- Port changes: You changed the SSH port from 22 to something else.
- Key updates: After adding or removing SSH keys.
In most cases, you can reload instead of restarting. Reloading applies config changes without dropping active connections.
Reload Vs Restart
Reload is gentler. It tells the SSH daemon to re-read its configuration without stopping the service. Active connections stay alive.
For systemd: sudo systemctl reload ssh or sudo systemctl reload sshd.
For SysVinit: sudo service ssh reload or sudo service sshd reload.
Use reload when you only changed config files. Use restart when the service is actually broken or unresponsive.
Troubleshooting SSH Restart Issues
Sometimes the restart command fails. Here’s what to check:
Permission Denied
You need root or sudo privileges. Always prefix with sudo.
If you get “user is not in the sudoers file,” log in as root directly or contact your system administrator.
Service Not Found
This usually means the service name is wrong. Try both ssh and sshd. Also check if SSH is installed:
which sshd or dpkg -l | grep ssh (Debian/Ubuntu) or rpm -qa | grep ssh (CentOS/RHEL).
Configuration Errors
If you made a mistake in sshd_config, the service might fail to start. Check the logs:
sudo journalctl -u ssh or sudo journalctl -u sshd (systemd).
sudo tail -f /var/log/auth.log (Debian/Ubuntu) or sudo tail -f /var/log/secure (CentOS/RHEL).
Look for lines like “error: Could not load host key” or “Bad configuration option.” Fix the config file and try again.
Port Already In Use
If another service is using port 22, SSH won’t start. Check with:
sudo netstat -tulpn | grep :22 or sudo ss -tulpn | grep :22.
If something else is on port 22, either stop that service or change SSH to a different port.
Advanced SSH Restart Commands
Here are some additional commands for specific situations:
Restart SSH On Specific Port
If you changed the port in config, restart normally. The service will use the new port.
Restart SSH With Debug Mode
For testing, run SSH in debug mode:
sudo sshd -d (runs in foreground, shows debug output).
Press Ctrl+C to stop. This helps identify config issues.
Restart SSH After Kernel Update
Kernel updates don’t affect SSH directly. But if you reboot the server, SSH will restart automatically. You can also restart manually after a reboot to ensure it’s running.
Automating SSH Service Restarts
You can set up automatic restarts if SSH crashes frequently. This is not ideal for production, but can be a temporary fix.
Using Cron
Add a cron job to check and restart SSH if it’s down:
- Edit crontab:
crontab -e. - Add:
*/5 * * * * /usr/bin/systemctl is-active sshd || /usr/bin/systemctl restart sshd.
This checks every 5 minutes and restarts if inactive.
Using Systemd Timer
Create a systemd service and timer for more control. But this is overkill for most users.
Security Considerations When Restarting SSH
Restarting SSH can temporarily expose your system if you’re not careful:
- Ensure your firewall allows SSH on the correct port.
- If you changed the port, update firewall rules before restarting.
- Test your new config with
sshd -tbefore restarting. This checks syntax without applying changes. - Keep a backup SSH session open in case the restart fails.
Command to test config: sudo sshd -t. If it returns no output, your config is valid. If it shows errors, fix them first.
Restarting SSH On Different Linux Distributions
Let’s cover specific commands for popular distros:
Ubuntu 20.04/22.04/24.04
sudo systemctl restart ssh
Debian 10/11/12
sudo systemctl restart ssh
CentOS 7/8, RHEL 7/8/9
sudo systemctl restart sshd
Fedora
sudo systemctl restart sshd
Arch Linux
sudo systemctl restart sshd
OpenSUSE
sudo systemctl restart sshd
Alpine Linux
Alpine uses OpenRC. Run: sudo rc-service sshd restart.
What To Do If SSH Restart Fails And You’re Locked Out
This is the worst-case scenario. You restarted SSH and now you can’t connect. Here’s how to recover:
Use Out-Of-Band Access
If your server provider offers a console (like iDRAC, IPMI, or a web-based VNC console), use that to log in locally. Then fix the SSH config.
Boot Into Rescue Mode
Most cloud providers have a rescue mode that boots a minimal Linux environment. Use it to mount your server’s filesystem and edit /etc/ssh/sshd_config.
Restore Default Config
If you messed up the config, restore from backup or reinstall the SSH package:
sudo apt-get install --reinstall openssh-server (Debian/Ubuntu).
sudo yum reinstall openssh-server (CentOS/RHEL).
This resets to default config files. You’ll lose custom settings, but regain access.
Common Mistakes When Restarting SSH
Avoid these pitfalls:
- Forgetting sudo: Most commands need root privileges.
- Using wrong service name: Double-check if it’s ssh or sshd.
- Not testing config first: Always run
sshd -tbefore restarting. - Closing all SSH sessions: Always keep at least one backup session open.
- Ignoring firewall rules: If you changed ports, update the firewall first.
Monitoring SSH Service Health
After restarting, monitor the service to ensure it stays up:
- Check status:
sudo systemctl status ssh. - View logs:
sudo journalctl -u ssh -f(follow mode). - Test connection from another terminal:
ssh localhost.
If the service keeps failing, investigate the logs for recurring errors.
Frequently Asked Questions
What Is The Command To Restart SSH Service In Linux?
On most modern systems, use sudo systemctl restart ssh (Ubuntu/Debian) or sudo systemctl restart sshd (CentOS/RHEL). For older systems, use sudo service ssh restart.
Does Restarting SSH Disconnect Current Sessions?
Yes, a restart will drop all active SSH connections. Use reload instead if you only changed config files. Or use screen/tmux to preserve sessions.
How Do I Restart SSH If I’m Already Locked Out?
Use out-of-band access like a server console, rescue mode, or ask your hosting provider to restart the service for you. Always keep a backup access method.
Why Does My SSH Restart Command Say “Failed To Restart Ssh.service: Unit Not Found”?
This means the service name is wrong or SSH is not installed. Try sshd instead of ssh. Check if SSH is installed with which sshd.
Can I Restart SSH Without Root Access?
No, restarting system services requires root or sudo privileges. If you don’t have sudo access, contact your system administrator.
Conclusion
Restarting the SSH service in Linux is a simple but essential skill. Whether you’re using systemd, SysVinit, or Upstart, the commands are straightforward once you know your distribution. Always test config changes with sshd -t before restarting, and keep a backup session open to avoid lockouts.
Remember the key commands: systemctl restart ssh for Debian-based systems, systemctl restart sshd for Red Hat-based ones. For older systems, fall back to service ssh restart. With these tools, you can quickly restore SSH access and keep your servers running smoothly.
If you encounter issues, check the logs and verify your config file. Most problems are easy to fix once you know where to look. Now you have the knowledge to handle SSH restarts confidently.