Security
How to secure a new Linux server in 15 minutes
The first steps on any fresh server: updates, a non-root user, SSH keys, a firewall and automatic security patches.

A new server is scanned by bots within minutes of going online. None of that is personal, and none of it matters if you do six things before installing your software. The commands below are for Ubuntu and Debian; the differences for AlmaLinux and Rocky Linux are noted at the end.
1. Update everything
apt update && apt upgrade -y
Images are built on a given day; fixes have been released since. If the kernel was updated, reboot once with reboot and log back in.
2. Create a normal user
Working as root means every typo runs with full power. Create an account for daily use and give it sudo:
apt install -y sudo
adduser deploy
usermod -aG sudo deploy
No SSH key on this server yet? Set one up first: How to connect to a Linux server with SSH. Then copy your key to the new user, so you can log in as that user directly:
cp -r ~/.ssh /home/deploy/
chown -R deploy:deploy /home/deploy/.ssh
3. Lock down SSH
Open a second terminal and check that ssh deploy@203.0.113.10 logs you in with your key, without asking for a password. If it asks for one, the key was not copied: fix that first, because the next command turns passwords off. Then, on the server:
cat > /etc/ssh/sshd_config.d/10-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
EOF
sshd -t && systemctl restart ssh
Root can no longer log in from outside and passwords are no longer accepted. You log in as deploy with your key and use sudo when needed.
Do not close your original session until the new login works. It is your way back in if a line was mistyped.
Check what is in force: sshd -T | grep -Ei 'permitrootlogin|passwordauthentication' must answer no twice. If a line still says yes, another file of /etc/ssh/sshd_config.d/ is read before yours, and the first value wins: give yours a name that sorts first, such as 00-hardening.conf, and restart the service again.
The rest of this guide is run as root: keep using the session you already have open, or type sudo in front of each command when you work as deploy.
4. Turn on a firewall
Allow what you use, refuse the rest:
apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw enable
Running a website? Add ufw allow 80,443/tcp. Check the result at any time with ufw status.
5. Install security updates automatically
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
Answer “Yes”. Security patches are now applied daily without you. Major upgrades stay manual.
6. Slow down the guessers
With passwords disabled, brute-force attempts cannot succeed, but they still fill your logs. Fail2ban blocks addresses that keep trying:
apt install -y fail2ban python3-systemd
printf '[sshd]\nenabled = true\nbackend = systemd\n' > /etc/fail2ban/jail.local
systemctl restart fail2ban
The second line makes it read SSH attempts from the system journal, which works on every current Debian and Ubuntu release, with or without a log file. See what it is doing with fail2ban-client status sshd.
Check your work
List everything that listens for connections:
ss -tulpn
Each line is a door. If you do not recognize a service, find out what it is; if you do not need it, stop it. A database, in particular, should listen on 127.0.0.1 unless other machines must reach it.
And the thing nobody does until too late
None of the above protects you from deleting the wrong directory. Decide today where a second copy of your data lives: another server, a storage machine, anywhere that is not this disk. Tools such as restic or borg make encrypted, incremental backups to a remote host with one scheduled command.
AlmaLinux and Rocky Linux
The six steps are the same. These commands replace the ones above.
Steps 1 and 2: update, a password for the new user, and the group that gives sudo. Here adduser creates the account without asking for a password, and sudo will ask for one: set it now, or once SSH is locked down deploy will be able to log in and unable to become root.
dnf upgrade -y
adduser deploy
passwd deploy
usermod -aG wheel deploy
Step 3: the SSH service has another name.
sshd -t && systemctl restart sshd
Step 4: the firewall. SSH stays allowed; the second line is for a website.
dnf install -y firewalld && systemctl enable --now firewalld
firewall-cmd --permanent --add-service=http --add-service=https && firewall-cmd --reload
Step 5: automatic updates. The timer whose name ends in -install applies them; the plain one only downloads them.
dnf install -y dnf-automatic && systemctl enable --now dnf-automatic-install.timer
Step 6: Fail2ban comes from the EPEL repository, and no jail is on by default.
dnf install -y epel-release && dnf install -y fail2ban
printf '[sshd]\nenabled = true\n' > /etc/fail2ban/jail.local
systemctl enable --now fail2ban
The checklist
- System updated, and updating itself.
- A normal user with sudo; root login disabled.
- Key-only SSH.
- Firewall on, with only the ports you use.
- Fail2ban watching SSH.
- A backup destination chosen.
Sources and further reading
Read nextTwo guides to follow this one.
All security guides
SecurityHow to set up your own WireGuard VPN on a VPSBuild a private VPN on a small VPS: server and client keys, routing, firewall rules and a phone profile.
Privacy and policyDMCA takedowns explained, and what DMCA-ignored hosting meansHow a DMCA notice works, why hosts suspend first and ask later, and what “DMCA-ignored” hosting does and does not cover.
Your own server, in a few minutes.
Cloud VPS from $3.99 a month: no email, no ID, crypto payment, root access in minutes.
- No KYCNo email, no ID
- No DMCANotices not acted on
- Crypto friendlyBTC, ETH, XMR, USDT, SOL
- UnmeteredOn every plan