In the “But, how do I actually…” series, I try to explain how to do computer things in a way that previous me would have appreciated (and future me can reference).
This is a written checklist for me, borrowed and slightly adapted for my personal needs from an awesome video by CJ on the Syntax YouTube Channel. The video is included at the end of this post.
Keep in mind that I’m still learning, so if you have more tips for me when it comes to setting up a VPS as efficient and securely as possible, please reach out!
Setup and secure a new VPS provisioned on Hetzner
- Go to the Hetzner Cloud Dashboard and provision a new Ubuntu server (don’t overthink this too much, you can easily rescale these servers later – the only thing you can’t downsize is the disk storage)
- Add your public SSH Key during setup
- Connect to the server via your terminal
ssh root@<serverip> - Accept the fingerprint (if this ever comes up again, be aware that you might be the target of a man-in-the-middle attack)
- Update package lists:
apt update - Upgrade packages:
apt upgrade - Check, if a reboot is required:
ls /var/run/reboot-required. If no such file or directory exists, you’re good. If it exists, reboot the server via the Hetzner dashboard. Alternatively, typerebootinto the terminal. - Run
apt upgradeagain. If a package has been kept back, try upgrading it manually withapt upgrade <package-name>. - Optionally add a secondary user (see below)
- Disable password login:
vim /etc/ssh/sshd_config, search forPasswordAuthenticationand set it tono - Restart ssh service:
service ssh restart - Add a firewall in Hetzner, a good default is to just open the ports 22, 80 and 443
- Enable auto-updates:
apt install unattended-upgrades, thendpkg-reconfigure unattended-upgrades - Open the config file:
vim /etc/apt/apt.conf.d/50unattended-upgrades - Uncomment this line to enable regular updates as well (not just security ones):
// "$${distro\_codename}-updates"; - Verify with
systemctl status unattended-upgrades(you should see a green dot) - Done, your server is ready to be used now!
Create a separate user (optional)
- While logged in as root, run
adduser <username> - Set a secure password
- Add your data like full name etc. (optional)
- Give the new user the possibility to run sudo commands:
usermod -aG sudo <username> - Verify with
groups <username> - Create the .ssh directory in your
home/<username>folder (you can get your current directory by runningpwd):mkdir .ssh - Add your public key to the server:
vim .ssh/authorized_keys
Video
Here’s the video by CJ, that goes over many more details if you’re interested:
Just a fun little command
Run tail -n 10 -f /var/log/auth.log on your server to see all login attempts that happened on your server. The better secured your server is, the less login attempts not coming from you, you should see.