Secure a New Ubuntu Server from Scratch
Take a freshly provisioned Ubuntu server from password-and-root to key-only access with a firewall, automatic security patches, and a verified way back in.
Prerequisites
- A fresh Ubuntu server you can reach as root
- An SSH key on your own machine, from the SSH Keys From Scratch guide
- A second terminal window, kept open throughout
Update Ubuntu system packages
First command on any new server. Everything after this assumes current packages.
Create new sudo user
Daily work should never be done as root. This is the account you will use from now on.
Copy SSH Key to Remote Server
Copy your key to the new user, not to root. Run it from your own machine, not from the server.
Do not skip this. Everything after it can lock you out, and this is the only step that proves you have a way back.
Verify a Second Login Before Closing the First
Disable Root SSH Login
Root is the account every bot tries first. Removing it as a target ends most of the noise in your logs.
The point of no return. Your key is now the only way in, so confirm the previous verification step passed.
Disable SSH Password Authentication
Verify a Second Login Before Closing the First
Again, from a new terminal. This is the moment a mistake is still cheap to fix.
Allow SSH before enabling the firewall. Enabling first and allowing second cuts your own connection.
Enable UFW firewall with SSH
List All Listening Ports
Look at what is actually listening. Anything exposed that you did not intend is the next thing to close.
Install and Configure Fail2ban
Bans addresses that keep failing to authenticate. With passwords already off this is a second layer, not the first.
Enable Automatic Security Updates
The step that keeps working after you forget this server exists.
Set Server Timezone
Set it now so logs and scheduled jobs read in your own time when you are debugging at 2am.