Skip to content

Harden SSH on a new server

Log in as your own user, turn off root and password logins, and put a firewall in front of SSH on Ubuntu or Debian.

A new server lets us log in as root with the SSH keys we picked when we deployed it. That works, but every server on the internet gets constant login attempts, so it’s worth locking SSH down before we put anything on it. It takes about ten minutes on Ubuntu or Debian.

Take a snapshot first

A mistake here can lock us out of SSH. A snapshot lets us undo everything in one step.

Create our own user#

We log in as root and create a user who can use sudo. Replace alice with your name:

adduser alice
usermod -aG sudo alice

adduser asks for a password. We won’t use it to log in over SSH, but sudo asks for it.

Give the user our SSH keys#

We copy root’s authorized keys to the new user, so the keys we deployed with work for them too:

mkdir -p /home/alice/.ssh
cp /root/.ssh/authorized_keys /home/alice/.ssh/
chown -R alice:alice /home/alice/.ssh
chmod 700 /home/alice/.ssh
chmod 600 /home/alice/.ssh/authorized_keys

Before going on, we open a second terminal and check that we can log in as the new user and use sudo:

ssh alice@203.0.113.10
sudo whoami
root

We keep that session open until we’ve finished. If a later step goes wrong, we can fix it from there.

Turn off root and password logins#

We put our settings in a file of their own in /etc/ssh/sshd_config.d/. SSH uses the first value it reads for each setting and reads these files in name order, so starting the name with 00- makes sure ours win over others that might be there, like 50-cloud-init.conf:

/etc/ssh/sshd_config.d/00-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

We check the configuration, then reload SSH. The service is called ssh on both Ubuntu and Debian:

sudo sshd -t && sudo systemctl reload ssh

sshd -t prints nothing when the configuration is fine. Reloading keeps our open sessions connected.

From another terminal, we check that root and passwords are refused, and that our user still gets in:

ssh root@203.0.113.10
root@203.0.113.10: Permission denied (publickey).
ssh -o PubkeyAuthentication=no alice@203.0.113.10
alice@203.0.113.10: Permission denied (publickey).
ssh alice@203.0.113.10
Welcome to Ubuntu 24.04 LTS

Put a firewall in front#

ufw is installed on Ubuntu; on Debian, we install it with sudo apt install ufw. We allow SSH before turning it on, or we’d cut off our own connection:

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

From now on, we open only the ports we need. For a web server, for example:

sudo ufw allow 80,443/tcp

Our firewall guide goes further, including how Docker gets around ufw.

Slow down login attempts (optional)#

With passwords off, nobody can guess their way in, but the attempts still fill our logs. fail2ban bans addresses that keep failing to log in, and on Ubuntu and Debian it watches SSH as soon as it’s installed:

sudo apt install fail2ban
sudo fail2ban-client status sshd
On Debian 12 or newer? fail2ban may not start

Login attempts go to the systemd journal there, not /var/log/auth.log, so we tell fail2ban to read from the journal:

/etc/fail2ban/jail.d/sshd.local
[sshd]
backend = systemd

Then sudo systemctl restart fail2ban and check the status again.

If we lock ourselves out#

If the session we kept open is still connected, we use it to fix or delete /etc/ssh/sshd_config.d/00-hardening.conf, or run sudo ufw disable, then try again. If it isn’t, we restore the snapshot we took at the start.

Next steps#