Preparing your machine

If you are setting up a fresh machine, it is recommended that you secure it first. Assuming that you are running Ubuntu 18.04:

Do not allow password-based SSH login (keys only)

First make sure you are actually logging in to the server using keys and not via a password, otherwise this will lock you out. Many hosting providers support uploading a public key and automatically set up key-based root login on new machines for you.

Edit /etc/ssh/sshd_config and find PasswordAuthentication. Make sure it’s uncommented and set to no. If you made any changes, restart sshd:

Update system packages

apt update && apt upgrade -y

Install fail2ban so it blocks repeated login attempts

Edit /etc/fail2ban/jail.local and put this inside:

[DEFAULT]
destemail = your@email.here
sendername = Fail2Ban

[sshd]
enabled = true
port = 22

[sshd-ddos]
enabled = true
port = 22

Finally restart fail2ban:

Install a firewall and only whitelist SSH, HTTP and HTTPS ports

First, install iptables-persistent. During installation it will ask you if you want to keep current rules–decline.

Edit /etc/iptables/rules.v4 and put this inside:

With iptables-persistent, that configuration will be loaded at boot time. But since we are not rebooting right now, we need to load it manually for the first time:

Last updated

Was this helpful?