Securing a Linux Droplet
A few simple best practices to make your Linux VMs deployed in the cloud more secure.
A few simple best practices to make your Linux VMs deployed in the cloud more secure.
Created 2025-11-27
Setting up a Linux Droplet on DigitalOcean is straightforward. However, as an IaaS offering, it also puts responsibility on you in terms of security. You are responsible for securing the VM to prevent attacks. In this tutorial, I'll outline my minimal approach to securing a Linux VM for development and learning. For production systems, even stricter security measures are required.
I won't cover how to set up an SSH key on DigitalOcean, but I personally use 1Password Developer to manage my SSH keys. It's very convenient and my keys are synced accross all my devices. I never have to worry about backing them up. See the resources at the end of this article for how to get started with 1Password for SSH.
After your first successful SSH login, you should start by updating the package repository and upgrading the installed packages:
apt update && apt upgrade -y
Running everything as sudo is risky. Create a regular admin user and add it to the sudo group:
adduser username
usermod -aG sudo username
If you create a new non-root user, SSH key authentication won't work until you set up their ~/.ssh/authorized_keys properly.
mkdir -p /home/username/.ssh
chmod 700 /home/username/.ssh
cp /root/.ssh/authorized_keys /home/username/.ssh/
chmod 600 /home/username/.ssh/authorized_keys
chown -R username:username /home/username/.ssh
ssh username@your_droplet_ip
Now, since the new admin user can SSH into the Linux VM and is part of the sudo group, we can lock down root SSH access.
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
sudo systemctl restart ssh
Open another terminal session and try logging in as root. It shouldn't work aynmore!
DigitalOcean Droplets usually come with ufw pre-installed by default, but you can also install the package manually:
sudo apt install ufw
This removes all rules, disables UFW, and restores the defaults. You'll see a warning that all rules will be deleted. Confirm with y:
sudo ufw reset
This should show Status: inactive with no rules:
sudo ufw status verbose
This blocks all incoming connections except ones you explicitly allow. Don't worry, you won't be kicked out of your current SSH session, ufw is smart enough:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https