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
- Connect to your server with SSHLog in from Linux, macOS or Windows, fix the usual SSH errors and move from the root password to an SSH key.
- Use the console when SSH does not workA text console in your browser that works even when SSH or the network is down: how to open it, log in and what its limits are.
- Install your own system from an ISOBoot a new server from a Linux installer of your choice, finish the installation on a graphical console in your browser and encrypt the disk with LUKS.
- Restart, power off and reinstallWhat each power button does, when a reboot is not enough and exactly what a reinstall erases.
