SSH is more than just a remote shell; it’s a cryptographic tunnel that can secure virtually any TCP/IP traffic.
Let’s see how sshd actually starts up and serves connections.
Imagine you just fired up a fresh Linux or macOS box, or maybe you’re setting up a new server. You want to connect to it from your laptop, but by default, the SSH server (sshd) might not be running or configured to listen for your incoming connections. This guide walks you through getting that sshd service up and running, so you can ditch that keyboard and monitor and work remotely.
Linux (Systemd-based distributions like Ubuntu, CentOS 7+, Fedora, Debian 8+)
Most modern Linux systems use systemd to manage services.
-
Check if
sshdis installed:dpkg -s openssh-server > /dev/null 2>&1 || echo "openssh-server not installed" rpm -q openssh-server > /dev/null 2>&1 || echo "openssh-server not installed"If it’s not installed, use your distribution’s package manager:
- Debian/Ubuntu:
sudo apt update && sudo apt install openssh-server - CentOS/Fedora/RHEL:
sudo yum install openssh-serverorsudo dnf install openssh-server
- Debian/Ubuntu:
-
Start the
sshdservice:sudo systemctl start sshdThis command tells
systemdto initiate the SSH daemon process. -
Enable
sshdto start on boot:sudo systemctl enable sshdThis creates a symbolic link so that
systemdautomatically startssshdevery time the system boots up. -
Check the status of the
sshdservice:sudo systemctl status sshdLook for
Active: active (running)in the output. -
Verify
sshdis listening on the default port (22):sudo ss -tlpn | grep ':22'You should see a line like
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*orLISTEN 0 128 [::]:22 [::]:*, indicatingsshdis bound to port 22 on all network interfaces. -
Configure the firewall (if necessary): If you have a firewall like
ufworfirewalldrunning, you need to allow SSH traffic.- ufw (Ubuntu/Debian):
sudo ufw allow ssh sudo ufw enable # If not already enabled sudo ufw statusufw allow sshis a shortcut forufw allow 22/tcp. - firewalld (CentOS/Fedora/RHEL):
This permanently adds thesudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload sudo firewall-cmd --list-allsshservice (which defaults to port 22) to the firewall’s configuration and reloads the rules.
- ufw (Ubuntu/Debian):
macOS
macOS comes with an SSH server, but it’s usually disabled by default.
-
Enable Remote Login:
- Go to System Settings (or System Preferences on older macOS versions).
- Navigate to General > Sharing.
- Toggle Remote Login on.
This action modifies the
launchdconfiguration forsshdand typically starts the service immediately. -
Verify
sshdis running: You can check this from the Terminal.sudo launchctl list | grep sshdYou should see an entry for
com.openssh.sshd. -
Check if
sshdis listening on port 22:sudo ss -tlpn | grep ':22'Similar to Linux, you’re looking for
LISTENon port 22. -
Firewall Considerations on macOS: macOS has an application-level firewall. Enabling Remote Login usually prompts you to allow incoming connections for
sshd. If you’ve previously denied it, you can manage it via System Settings > Network > Firewall > Options…. Ensuresshdor "Remote Login" is set to "Allow incoming connections."
Common sshd_config Settings
The behavior of sshd is controlled by /etc/ssh/sshd_config. After making changes, you must reload or restart the service for them to take effect.
- Reloading (preferred for config changes):
sudo systemctl reload sshd # Linux sudo launchctl kickstart -k system/com.openssh.sshd # macOS (might need restart if kickstart fails) - Restarting (if reload doesn’t work or for service status issues):
sudo systemctl restart sshd # Linux sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist && sudo launchctl load /System/Library/LaunchDaemons/ssh.plist # macOS
Key sshd_config directives:
Port 22: The portsshdlistens on. Changing this requires updating firewall rules and client connection commands.ListenAddress 0.0.0.0: Bindssshdto all available network interfaces. You can specify a single IP address if you only want it to listen on a particular interface.PermitRootLogin prohibit-password: Disallows direct root login with a password. This is a critical security setting. Considerprohibit-passwordornoif you need root access, usingsudoafter logging in as a regular user.PasswordAuthentication yes: Allows users to log in using their system passwords. For enhanced security, consider setting this tonoand using SSH key-based authentication.PubkeyAuthentication yes: Enables SSH key-based authentication. This is highly recommended for security.
Troubleshooting
If you can’t connect after setting up, check:
- Is
sshdrunning? (Usesystemctl status sshdorlaunchctl list | grep sshd) - Is
sshdlistening on the correct port? (Usess -tlpn | grep ':22') - Is the firewall blocking port 22? (Check
ufw status,firewall-cmd --list-all, or macOS Sharing/Firewall settings) - Are there any syntax errors in
sshd_config? Runsudo sshd -tto test the configuration file. - Are network routes correct? Ensure the server can be reached from your client.
- Check
sshdlogs:- Linux (systemd):
sudo journalctl -u sshd - macOS: Logs are typically found in
/var/log/system.logor can be viewed withlog stream --predicate 'senderImagePath contains "sshd"'.
- Linux (systemd):
The next error you’ll hit is "Permission denied (publickey,password)."