Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

SSH hardening checklist for a fresh Ubuntu 24.04 or Debian 12 VPS

A new VPS with a public IP gets its first SSH password guesses within minutes. Nothing about that is personal: bots scan the whole IPv4 space all day. The good news is that an hour of work, done in the right order, remov

A new VPS with a public IP gets its first SSH password guesses within minutes. Nothing about that is personal: bots scan the whole IPv4 space all day. The good news is that an hour of work, done in the right order, removes almost all of that risk.

This checklist is written for Ubuntu 24.04 and Debian 12. Both have a couple of details that older guides get wrong (socket-activated SSH on Ubuntu, no auth.log on Debian), so those are called out where they matter.

Golden rule: keep your current SSH session open the whole time, and test every change from a second terminal before you close the first one.

1. Update first

apt update && apt full-upgrade -y
# reboot if a new kernel was installed
[ -f /var/run/reboot-required ] && reboot

2. Create a normal user with sudo

Working as root all day turns every typo into an outage.

adduser deploy
usermod -aG sudo deploy

3. Use SSH keys

On your own machine (not the server):

ssh-keygen -t ed25519 -a 100 -C "laptop-2026"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@YOUR_SERVER_IP

Then test it from a second terminal:

ssh -i ~/.ssh/id_ed25519 deploy@YOUR_SERVER_IP
sudo -v   # confirm sudo works for this user

Do not continue until this login works without a password prompt (a passphrase prompt for the key itself is fine and recommended).

4. Harden sshd with a drop-in that actually wins

Both distributions read /etc/ssh/sshd_config.d/*.conf at the top of the main config. For most sshd options the first value read wins, not the last. Many cloud images ship /etc/ssh/sshd_config.d/50-cloud-init.conf containing PasswordAuthentication yes, which silently overrides anything you write further down in sshd_config.

So give your file a name that sorts first:

cat > /etc/ssh/sshd_config.d/00-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deploy
EOF

Check the syntax, then check what sshd will really use:

sshd -t && echo "config OK"
sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|allowusers)'

If sshd -T still shows passwordauthentication yes, another file is being read before yours. Find it with grep -ri passwordauthentication /etc/ssh/.

Apply the change:

systemctl reload ssh

Now open a new terminal and log in as deploy again. Also confirm that root and passwords are refused:

ssh root@YOUR_SERVER_IP                       # should fail
ssh -o PubkeyAuthentication=no deploy@YOUR_SERVER_IP   # should fail

Changing the SSH port (optional)

A non-standard port does not add real security, but it cuts log noise a lot. On Ubuntu 24.04, SSH is started by ssh.socket, so the port in sshd_config alone is not enough after a change; the socket must be regenerated:

echo "Port 2222" >> /etc/ssh/sshd_config.d/00-hardening.conf
sshd -t
systemctl daemon-reload
systemctl restart ssh.socket
ss -tlnp | grep 2222

On Debian 12 a plain systemctl restart ssh is enough. Whichever you use, open the new port in the firewall (next step) before you restart, and remember to replace 22 with your port in the commands below.

5. Firewall with ufw

Allow SSH first, then turn the firewall on. limit rate-limits new connections (6 in 30 seconds per source IP), which slows brute force down further.

apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw limit 22/tcp
# add what your server actually serves, for example:
# ufw allow 80/tcp
# ufw allow 443/tcp
ufw enable
ufw status verbose

A side effect of limit worth knowing: if you open many SSH connections quickly (scripts, Ansible, retry loops), you will lock yourself out for a short while. Batch your commands or use SSH connection multiplexing (ControlMaster auto in ~/.ssh/config).

6. fail2ban, with the backend that works on both systems

Debian 12 does not install rsyslog by default, so there is no /var/log/auth.log and fail2ban's default sshd jail can fail to start. Reading the systemd journal works on both distributions:

apt install -y fail2ban python3-systemd

cat > /etc/fail2ban/jail.local <<'EOF'
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
# bantime.increment = true   # optional: longer bans for repeat offenders

[sshd]
enabled = true
backend = systemd
port    = ssh
EOF

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

If you moved SSH to another port, set port = 2222 in the jail. To unban yourself: fail2ban-client set sshd unbanip YOUR_IP.

7. Automatic security updates

apt install -y unattended-upgrades apt-listchanges
dpkg-reconfigure -plow unattended-upgrades   # answer "Yes"
cat /etc/apt/apt.conf.d/20auto-upgrades

The last command should show:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Kernel and libc updates need a reboot to take effect. If an unplanned reboot at night is acceptable for this server, put your choice in a local file so package upgrades never overwrite it:

cat > /etc/apt/apt.conf.d/52unattended-upgrades-local <<'EOF'
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
EOF

unattended-upgrade --dry-run --debug 2>&1 | tail -n 20

8. Final checks

ss -tulpn                      # only the ports you expect should listen
ufw status numbered
fail2ban-client status sshd
journalctl -u ssh --since "1 hour ago" | tail

Write down how you get in if SSH breaks: most hosts offer a browser or VNC console. Find it now, while everything works.

The 30-second version

If you set up servers often, the same steps are wrapped in a short, readable open-source script: vps-first-steps. It installs updates and unattended-upgrades, creates a sudo user and copies your keys, sets up ufw with rate-limited SSH and fail2ban, and turns off password login only if an SSH key is already installed. It tests the sshd config before reloading and reverts its own change if the test fails.

curl -fsSLO https://raw.githubusercontent.com/phantomvps/vps-first-steps/main/vps-first-steps.sh
less vps-first-steps.sh      # read it first, it is short
sudo bash vps-first-steps.sh --user deploy --allow 80,443

Disclosure: the script is maintained by the team behind PhantomVPS, where I work. We sell VPS and dedicated servers with root access; sign-up needs an email address and a password, no ID documents and no phone number, and you can pay with Monero, Bitcoin, Litecoin, USDT, USDC, SOL or TRX. The checklist above works the same on any Ubuntu or Debian server, wherever it runs.

Checklist

  • [ ] System updated, rebooted if needed
  • [ ] Non-root sudo user
  • [ ] Ed25519 key login tested from a second terminal
  • [ ] 00-hardening.conf in sshd_config.d, verified with sshd -T
  • [ ] ufw on, SSH rate-limited, only needed ports open
  • [ ] fail2ban sshd jail running with backend = systemd
  • [ ] unattended-upgrades enabled, reboot policy decided
  • [ ] Emergency console location known
📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.