Skip to content
Dhruvgiri Goswami
All writing

Hardening a fresh Ubuntu server in 15 minutes

Dhruvgiri Goswami2 min readSecurityDevOps

A new server with a public IP starts getting login attempts within minutes. Most are automated scripts trying common passwords, and almost all of them fail against a server set up with a few basic habits. These are the steps I run on every fresh Ubuntu machine, in this order, before installing anything else.

Keep your first SSH session open the whole time. If you lock yourself out, that open session is how you get back in.

1. Update everything

sudo apt update && sudo apt full-upgrade -y
sudo reboot   # if a new kernel was installed

2. Create your own user

Do day-to-day work as a normal user with sudo, not as root.

adduser dhruv
usermod -aG sudo dhruv

3. Log in with SSH keys, not passwords

On your own computer, create a key if you don’t have one, and copy it to the server:

ssh-keygen -t ed25519 -C "dhruv@laptop"
ssh-copy-id dhruv@your.server.ip

Open a new terminal and check you can log in as that user without a password. Only then turn password logins off.

On recent Ubuntu versions, the main SSH config includes every file in /etc/ssh/sshd_config.d/, in alphabetical order, and the first value it finds for a setting wins. Cloud images often ship a 50-cloud-init.conf that turns passwords back on. So put your settings in a file that sorts first:

sudo tee /etc/ssh/sshd_config.d/01-hardening.conf > /dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOF

sudo sshd -t                  # check the config; no output means OK
sudo systemctl restart ssh

Test again from a new terminal. Try ssh root@your.server.ip too: it should be refused.

4. Turn on the firewall

Allow SSH first, then web traffic, then switch it on. Doing it in the other order cuts off your own session.

sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

The Docker gotcha: ports that Docker publishes skip ufw entirely, because Docker adds its own firewall rules. A container started with -p 27017:27017 is open to the internet even though ufw status says otherwise. Publish ports on 127.0.0.1 only (-p 127.0.0.1:3000:3000) and keep databases on Docker’s internal network with no published ports at all. My Docker Compose setup shows this in practice.

5. Ban repeated failed logins

fail2ban watches login attempts and blocks IPs that keep failing.

sudo apt install fail2ban -y

sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
EOF

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

If the sshd jail won’t start on a release that only logs to the systemd journal, add backend = systemd under [sshd].

With password logins off, attackers can’t get in anyway, but fail2ban keeps the logs readable and cuts noise.

6. Install security updates automatically

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

This installs security patches daily. It does not reboot by default. To allow a reboot at a quiet hour when a kernel update needs one, set these in /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";

7. Small things that help later

sudo timedatectl set-timezone Asia/Kolkata   # logs in your own time zone
sudo hostnamectl set-hostname acme-prod       # know which server you're on

Add swap on small servers (1 to 2 GB RAM) so a memory spike slows things down instead of killing the database:

sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

The checklist

  • System fully updated
  • Personal user with sudo
  • SSH key login works, then passwords and root login turned off
  • Firewall on with only SSH, 80 and 443 open
  • Docker ports bound to 127.0.0.1, databases not published
  • fail2ban running
  • Automatic security updates on
  • Time zone, hostname and swap set

None of this makes a server unbreakable. It does take you out of the large pool of easy targets, which is where almost all real-world break-ins on small servers happen. After this, the next most important thing is backups you have actually tested.