The first things I do when I get a Linux server
You get a new VPS: an IP, the root user and a password. As soon as it has a public IP, there are
bots trying passwords against its port 22.
Before installing the application, I spend a while getting it into a reasonable state. This is not
a complete hardening guide: it is the short list I always apply, in this order, on Debian and
Ubuntu.
1. Update
First, bring the system up to date:
apt update && apt full-upgrade -y
If the kernel has been updated, reboot now (reboot), before you start configuring anything.
2. A user with sudo
Working as root means any mistake runs with full permissions. I create a user and give it sudo:
adduser deploy
adduser deploy sudo
On Debian, if a root password was set during installation, sudo is not installed: run
apt install sudo first. Group membership is applied at login, so log in again as the new user for
it to take effect.
3. SSH keys
From your machine, not from the server, copy your public key to the new user. If you do not have
one yet, ssh-keygen -t ed25519 creates it:
ssh-copy-id deploy@203.0.113.10
ssh-copy-id adds the key to the user’s ~/.ssh/authorized_keys with the correct permissions.
Check that you can log in without being asked for the server’s password before carrying on:
ssh deploy@203.0.113.10
4. No passwords and no root over SSH
Now that you log in with a key, I close the two doors the bots use: password login and root
login.
On Debian and Ubuntu, /etc/ssh/sshd_config starts with
Include /etc/ssh/sshd_config.d/*.conf. I put my changes in a file in that directory instead of
editing the main one: they stay separate from the package’s values and it is easier to update.
# /etc/ssh/sshd_config.d/00-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PasswordAuthentication no: you can only log in with a key.KbdInteractiveAuthentication no: closes the other method that can end up asking for a password.PermitRootLogin no:rootcannot log in over SSH, not even with a key. You log in with your user and usesudo.
Before restarting the service, check the syntax and the value that actually applies. sshd -t
validates the configuration and sshd -T prints the effective configuration:
sudo sshd -t
sudo sshd -T | rg -i '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
sudo systemctl restart ssh
Do not close the session you are in. Open another terminal and log in with your user. If it
works, also check that ssh root@203.0.113.10 is rejected. If something fails, you still have the
first session open to undo it: restarting the service does not drop sessions that are already
established.
5. Firewall with ufw
ufw is the simplest way to manage the firewall on Debian and Ubuntu. On Ubuntu it comes installed
but disabled; on Debian, sudo apt install ufw. When enabled, it blocks all incoming traffic and
allows outgoing.
Allow SSH before enabling it, or you lock yourself out:
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose
OpenSSH is a profile installed by the SSH server package (sudo ufw app list shows them). If the
server is going to serve a website, open 80 and 443 too (sudo ufw allow 80,443/tcp).
6. fail2ban
With passwords disabled, bots can no longer get in, but they keep filling the log. fail2ban reads
the failed attempts and blocks the IPs that persist for a while:
sudo apt install fail2ban
sudo fail2ban-client status sshd
On Debian 13 and Ubuntu 24.04, the package already ships the sshd jail enabled and reading the
systemd journal, so nothing needs configuring to get started. If you want to change something (how
long the ban lasts, for example), do it in /etc/fail2ban/jail.local, not in jail.conf, which
belongs to the package.
7. Automatic security updates
Security patches should not wait until you remember. unattended-upgrades installs them on its
own:
- Ubuntu: it comes installed and enabled, and applies security updates once a day.
- Debian: depending on the installation, it may be missing or disabled.
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
sudo unattended-upgrade --dry-run -v
The default configuration installs security updates, not new versions. What it does each night is
logged in /var/log/unattended-upgrades/.
8. Time zone and synchronised clock
Logs, cron jobs and certificates depend on the time. I set the time zone and check that the clock is synchronised via NTP:
sudo timedatectl set-timezone Europe/Madrid
sudo timedatectl set-ntp true
timedatectl status
In the output, System clock synchronized: yes. timedatectl list-timezones lists the valid names.
Some people keep servers on UTC to avoid dealing with daylight saving changes; what matters is that
all of yours use the same one.
9. Machine name
A name that says what the machine is comes in handy when you have several terminals open:
sudo hostnamectl hostname web-01
On Debian, the installer adds a 127.0.1.1 line with the name to /etc/hosts. Change it too so it
matches the new name.
Summary
- Update, create a user with
sudoand log in with an SSH key. - Disable passwords and
rootin asshd_config.d/file that is read first, validate withsshd -tand test in a second session before closing the first. - Enable
ufwafter allowing OpenSSH. fail2banandunattended-upgradesfor bot noise and security patches.- Time zone, NTP and a machine name that says what it is.
References
- OpenSSH:
sshd_config(5) - OpenSSH:
sshd(8),-tand-Toptions - Ubuntu Server: OpenSSH
- Ubuntu Server: firewall with ufw
- Ubuntu:
ufw(8) - Ubuntu Server: automatic updates
- Debian Wiki: sudo
- Debian Wiki: UnattendedUpgrades
- Debian Wiki: date and time
- Debian: fail2ban package
- Debian Reference: the hostname
- systemd:
timedatectl - systemd:
hostnamectl