On the internet, attacks start within hours of a flaw becoming public, while people often take weeks to react. Automatic updates and strong SSH access matter far more than tricks such as moving SSH to another port. Several of these changes can lock you out if you do them in the wrong order, so read the warnings.
0 of 6 habits in place
Your progress is saved only in this browser. Nothing is sent to us.
Essential
Do this first. It stops the most common attacks.
Recommended
Worth doing once the essentials are in place.
Advanced
Extra protection for people facing higher risk.
Keep automatic security updates on
Essential
Leave unattended-upgrades enabled and choose a time for automatic reboots.
Why it matters
Unpatched software is now the most common way into cloud servers. Ubuntu's automatic updates install security fixes every day, but packages from third-party repositories are only included if you add them.
How to do it
Check that unattended-upgrades is installed and enabled.
Set Unattended-Upgrade::Automatic-Reboot-Time to a quiet hour so that kernel fixes take effect.
Add third-party repositories, such as PHP or Node.js sources, to the allowed origins if you want them updated too.
If a surprise service restart would hurt, exclude that service from needrestart.
Use SSH keys and turn off password and root login
Essential
Add an SSH key, test it, then switch off password and root logins.
Why it matters
Password logins invite endless guessing attacks, and root login hands an attacker everything at once. Keys are far stronger, but careless SSH changes are the most common way people lock themselves out of their own servers.
How to do it
Add your public key, then confirm in a new terminal that you can log in with it, keeping the old session open.
Put your settings in a file under /etc/ssh/sshd_config.d/, because files there override the main configuration.
Set PasswordAuthentication no and PermitRootLogin no.
Run sudo sshd -t to check the configuration before you restart SSH.
Watch out: Keep your current session open until a fresh login with your key works. If the test fails, you can still undo the change.
Enable the firewall in the right order
Essential
Allow SSH before you turn on ufw, and limit SSH to your own IP address if you can.
Why it matters
Ubuntu's ufw firewall is off by default. Turning it on before allowing SSH cuts off your own connection.
How to do it
Run sudo ufw allow OpenSSH (or allow your SSH port) first.
Allow only the other ports you need, such as 80 and 443 for a website.
Then run sudo ufw enable and check the result with sudo ufw status.
Where possible, allow SSH only from your own IP address.
Watch out: Never run sudo ufw enable on a remote server before the SSH rule is in place.
Know your provider's emergency console
Recommended
Learn how to reach your hosting provider's web or serial console before you need it.
Why it matters
If a firewall or SSH change locks you out, the provider's console is often the only way back in.
How to do it
Find the console option in your hosting provider's dashboard.
Try logging in through it once, while everything still works.
Check what your provider requires to log in there, such as a local user with a password.
Write the steps down in your notes.
Run a security audit every quarter
Advanced
Use a tool such as Lynis to check the server's configuration, and work through its suggestions.
Why it matters
Audits catch settings that drift over time. Lynis runs on the server itself, so it is less disruptive than network scanners.
How to do it
Install Lynis from your distribution's packages or the official site.
Run sudo lynis audit system.
Work through the warnings first, then the suggestions.
Repeat every quarter and compare the results.
Read your update reports
Recommended
Turn on email reports for automatic updates and look at the update log.
Why it matters
Updates can fail without anyone noticing. An update that failed is the same as no update at all.
How to do it
Set Unattended-Upgrade::Mail to your address and Unattended-Upgrade::MailReport to on-change.
Make sure the server can actually send email.
Look at /var/log/unattended-upgrades/ from time to time.