Updated 2 min read

Initial server setup on Ubuntu 24.04

Update the system, create a sudo user, switch to SSH keys, enable a firewall and automatic security updates on a fresh Ubuntu 24.04 server.

A new server on a public IP is scanned within minutes by bots that try root passwords everywhere. Do these steps right after your first login, in this order. Connect as root first (how).

1. Update the system#

apt update && apt full-upgrade -y
reboot

A new kernel only takes effect after the reboot. Reconnect afterwards.

2. Create a sudo user#

adduser deploy
usermod -aG sudo deploy

Pick your own username. Daily work as root turns small mistakes into big ones.

3. Add your SSH key to that user#

From your own computer:

ssh-copy-id [email protected]

Open a new terminal and confirm that ssh [email protected] works and that sudo -v accepts your password. Do not continue until it does.

4. Turn off passwords and root login#

Our image enables password login in /etc/ssh/sshd_config.d/01-ironbalkans.conf. Replace that file with strict settings:

sudo tee /etc/ssh/sshd_config.d/01-ironbalkans.conf >/dev/null <<'EOF'
PasswordAuthentication no
PermitRootLogin no
EOF
sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -E 'passwordauthentication|permitrootlogin'

The last command must print no twice. Keep your current session open and test a fresh login before you close it.

Important If you lock yourself out, the console still logs you in with the root password: it does not use SSH, so these settings do not affect it.

5. Enable the firewall#

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw enable

limit slows down repeated connection attempts. Open further ports only when you need them. More in the UFW guide.

6. Install security updates automatically#

sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Answer Yes. Security updates now install on their own; reboot when /var/run/reboot-required appears.

7. Optional: fail2ban#

sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban

With password login off, nobody can guess their way in, but fail2ban keeps logs and load down.

8. Plan your backups#

We do not back up your server and there are no snapshots. A disk failure can destroy your data for good. Set up encrypted backups with restic today, not after the first loss.

Related reading (in English): fail2ban vs CrowdSec, systemd service hardening.

More in this section