To configure SSH on Linux, install the OpenSSH server, set up key-based login, put your settings (no root login, no passwords) in /etc/ssh/sshd_config.d/, test them with sudo sshd -t, reload the service, and allow SSH through the firewall. That takes about fifteen minutes and closes the door most attacks on internet-facing servers try first.

SSH (Secure Shell) is how you manage almost every Linux server: you open a terminal on your own computer and run commands on the server over an encrypted connection. This guide is about the server side: installing the SSH service, the configuration options that matter, firewall and Fail2ban rules, logs, and troubleshooting. If you haven’t created an SSH key yet, or want to know how keys work, read What Is SSH? A Beginner’s Guide to Secure Remote Login with SSH Keys first.

Step 1: Install the OpenSSH Server

Most cloud servers and VPS images come with SSH already running. On a desktop or a fresh install, add it:

# Debian, Ubuntu
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh

# Fedora, RHEL, Rocky, AlmaLinux
sudo dnf install openssh-server
sudo systemctl enable --now sshd

The service is called ssh on Debian and Ubuntu and sshd on Red Hat family systems. Check that it’s running and listening on port 22:

sudo systemctl status ssh
sudo ss -tlnp | grep ssh

Find the server’s IP address with ip -br addr, then connect from your own computer:

The first time you connect, SSH shows the server’s key fingerprint and asks you to confirm it. It saves the answer in ~/.ssh/known_hosts on your computer and warns you if the key ever changes.

Step 2: Set Up Key-Based Login

Passwords can be guessed; a modern SSH key effectively can’t. On your own computer (not the server), create a key if you don’t have one and copy it to the server:

ssh-keygen -t ed25519 -C "jane@laptop"
ssh-copy-id [email protected]

ssh-copy-id adds your public key to ~/.ssh/authorized_keys on the server. Open a new terminal and confirm you can log in without the account password (you may be asked for the key’s passphrase, which is fine).

SSH refuses keys if the permissions on the server are too open. If key login doesn’t work, fix them:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

For more on Linux permissions, see Linux User and Permission Management.

Step 3: Where the SSH Configuration Lives

FilePurpose
/etc/ssh/sshd_configMain server configuration
/etc/ssh/sshd_config.d/*.confExtra settings files, read from the top of the main file
/etc/ssh/ssh_host_*_keyThe server’s host keys (its identity)
~/.ssh/authorized_keysPublic keys allowed to log in as that user
/etc/ssh/ssh_config and ~/.ssh/configClient settings, for connections from this machine

Note the d in sshd_config: that’s the server. ssh_config without the d is the client.

On current Debian, Ubuntu, and Fedora releases, sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf. Putting your changes in your own file there is cleaner than editing the main file, and package updates won’t overwrite it.

One rule trips everyone up: for most options, sshd uses the first value it reads. Files in sshd_config.d/ are read in alphabetical order, before the rest of the main file. Some cloud images include a file such as 50-cloud-init.conf that sets PasswordAuthentication yes, which would beat a setting further down. Give your file a name that sorts first, like 00-hardening.conf.

Check that the Include line is there:

grep -i '^include' /etc/ssh/sshd_config

If it isn’t (on older releases), make the same changes directly in /etc/ssh/sshd_config.

Create your settings file:

sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

Paste this, replacing jane deploy with the users who should be able to log in:

# Log in with keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no

# No direct root login; use sudo instead
PermitRootLogin no

# Only these users may log in over SSH
AllowUsers jane deploy

# Slow down guessing and drop abandoned sessions
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2

# Turn off features most servers don't need
X11Forwarding no

What each setting does:

SettingEffect
PasswordAuthentication noOnly keys work, so password guessing is pointless
KbdInteractiveAuthentication noCloses the other password prompt path
PermitRootLogin noAttackers must guess a user name too, and admins use sudo, which is logged
AllowUsersEvery other account is refused, even with a valid key (AllowGroups sshusers works the same way for a group)
MaxAuthTries 3Disconnects after three failed attempts
LoginGraceTime 30Gives 30 seconds to finish logging in
ClientAliveInterval / ClientAliveCountMaxCloses connections that stop responding (after about 10 minutes here)
X11Forwarding noDisables forwarding graphical apps, rarely needed on a server

Before you reload, keep your current SSH session open. If something is wrong, you’ll still be logged in to fix it.

Test the configuration for errors. No output means it’s valid:

sudo sshd -t

Then reload the service (existing sessions stay connected):

sudo systemctl reload ssh      # Debian, Ubuntu
sudo systemctl reload sshd     # Fedora, RHEL

Open a new terminal and log in again. Only close the old session once that works.

To see the settings sshd is actually using after all files are combined:

sudo sshd -T | grep -Ei 'passwordauth|permitroot|allowusers|maxauth'

Step 5: Should You Change the SSH Port?

Moving SSH from port 22 to another port (say 2222) cuts the noise of automated login attempts in your logs. It doesn’t stop a determined attacker, who can scan every port, so treat it as tidying up, not as security. If you do it:

  1. Add Port 2222 to your settings file.
  2. Allow the new port in the firewall before reloading.
  3. On Ubuntu 22.10 and later, SSH is started by systemd socket activation, so also run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket.
  4. On SELinux systems (Fedora, RHEL), allow the port: sudo semanage port -a -t ssh_port_t -p tcp 2222.
  5. Connect with ssh -p 2222 [email protected].

Step 6: Allow SSH Through the Firewall

On Ubuntu with UFW:

sudo ufw allow OpenSSH          # or: sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status

sudo ufw limit OpenSSH instead of allow temporarily blocks an address that opens six or more connections in 30 seconds.

On Fedora and RHEL with firewalld:

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

If only a few places need SSH (your office, a VPN), allow just those addresses. With UFW: sudo ufw allow from 198.51.100.0/24 to any port 22 proto tcp. Your cloud provider’s firewall can do the same before traffic even reaches the server.

Step 7: Block Brute-Force Attempts with Fail2ban

Fail2ban reads the login logs and bans addresses that keep failing:

sudo apt install fail2ban       # Fedora/RHEL: sudo dnf install fail2ban

Create /etc/fail2ban/jail.local (never edit jail.conf, which updates overwrite):

[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
backend = systemd

Then start it and check the SSH jail:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

To unban an address (yours, for example): sudo fail2ban-client set sshd unbanip 198.51.100.25.

With password login turned off, Fail2ban mostly keeps logs clean and saves resources, but it’s still worth having.

Step 8: Watch the SSH Logs

# Debian, Ubuntu
sudo journalctl -u ssh --since today
# Fedora, RHEL
sudo journalctl -u sshd --since today

# Failed attempts
sudo journalctl -u ssh | grep -i "failed\|invalid user"

# Who is logged in right now
who

Ubuntu and Debian also write SSH events to /var/log/auth.log when rsyslog is installed; Red Hat family systems use /var/log/secure.

Make Connecting Easier: The Client Config File

On your own computer, ~/.ssh/config saves typing:

Host web1
    HostName 203.0.113.10
    User jane
    Port 22
    IdentityFile ~/.ssh/id_ed25519

Now ssh web1 is enough, and scp file.txt web1: and rsync use the same shortcut.

Useful Things SSH Can Do

  • Copy files: scp backup.tar.gz web1:/tmp/ or, for large folders, rsync -avz ./site/ web1:/var/www/site/.
  • Transfer files with a GUI: SFTP runs over SSH; see How to Set Up an SFTP Server on Linux.
  • Reach a service that isn’t public: ssh -L 5432:localhost:5432 web1 makes the server’s PostgreSQL available at localhost:5432 on your computer, without opening the database port to the internet.
  • Jump through a bastion host: ssh -J jump.example.com web1 connects to web1 through another server.

Troubleshooting Common SSH Errors

ErrorLikely causeFix
Connection refusedSSH isn’t running, or it’s on another portsudo systemctl status ssh; check Port and use ssh -p
Connection timed outA firewall is dropping the trafficCheck UFW/firewalld and your cloud provider’s firewall
Permission denied (publickey)The key isn’t in authorized_keys, or permissions are too openCheck the key, chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys
Too many authentication failuresYour agent offered too many keysAdd IdentitiesOnly yes and an IdentityFile for that host
REMOTE HOST IDENTIFICATION HAS CHANGEDThe server was rebuilt, or something is intercepting the connectionConfirm with the server’s admin, then ssh-keygen -R <host>
Locked out after a config changeA setting blocks your userUse your provider’s web console, fix the file, run sshd -t

To see exactly where a connection fails, add -v (or -vvv) on the client: ssh -v [email protected].

SSH Server Checklist

  • Key-based login works for every admin
  • PasswordAuthentication no and PermitRootLogin no
  • AllowUsers or AllowGroups limits who can log in
  • sudo sshd -t passes and sshd -T shows your settings
  • Firewall allows SSH only from where it’s needed
  • Fail2ban is running
  • The OpenSSH package is kept up to date with the rest of the system

Conclusion

A well-configured SSH server accepts keys only, refuses root, lets in only the users you name, and sits behind a firewall and Fail2ban. Keep your changes in a file under sshd_config.d/, always test with sshd -t, and keep a session open while you reload. SSH is the front door of your server, so it’s the first item in Linux Server Security: 10 Best Practices to Protect Your Server.

Frequently Asked Questions

What’s the difference between ssh and sshd?

ssh is the client you run on your own computer to connect. sshd is the server (daemon) that runs on the remote machine and accepts connections.

How do I restart SSH on Linux?

Run sudo systemctl restart ssh on Debian and Ubuntu, or sudo systemctl restart sshd on Fedora and RHEL. reload applies config changes without dropping existing sessions, so prefer it.

Is it safe to disable password authentication?

Yes, once key login works for every user who needs access. Test in a new session first, and keep your provider’s web console as a backup way in.

How do I check which SSH port a server uses?

On the server, run sudo ss -tlnp | grep ssh or sudo sshd -T | grep -i '^port'.

Can I use two-factor authentication with SSH?

Yes. PAM modules such as Google Authenticator’s (libpam-google-authenticator) add a one-time code, and hardware security keys work with ed25519-sk key types. Set it up on a test server first, since a mistake can lock you out. See Two-Factor Authentication (2FA) Explained.

Want to keep learning? These related guides pair well with this post: