Basic Linux Server Security: SSH Keys, Firewall, Updates

Updated: 4 December 20264 min read57 viewsMarkdown

Once you have root access to a server, keeping it secure is your job. Doing these four basics on day one blocks most automated attacks: SSH keys, no password logins, a firewall and regular updates.

Illustration of a server protected by nested shields for keys, firewall and updates
Security works in layers: keys, firewall, updates.

Before you start

  • You need SSH access as root. Your IP address and password are in your activation email or in the service details under Client area > My services.
  • Commands are given separately for Ubuntu/Debian and AlmaLinux/Rocky Linux.
  • Keep your current SSH session open while you change settings, and test each change from a second terminal window.

1. Update the system

Start by patching known vulnerabilities in the installed packages.

# Ubuntu / Debian
apt update && apt upgrade -y

# AlmaLinux / Rocky Linux
dnf upgrade -y

To have security updates installed automatically:

# Ubuntu / Debian
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades

# AlmaLinux / Rocky Linux
dnf install -y dnf-automatic
systemctl enable --now dnf-automatic-install.timer

2. Create an SSH key and add it to the server

  1. Generate a key pair on your computer. In Windows PowerShell or a macOS/Linux terminal:
    ssh-keygen -t ed25519 -C "user@example.com"
    When prompted, we recommend setting a passphrase to protect the key.
  2. Copy the public key to the server. On macOS and Linux:
    ssh-copy-id root@203.0.113.10
    Windows has no ssh-copy-id, so use this instead:
    type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh root@203.0.113.10 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
  3. Check that key login works. Open a new terminal and run ssh root@203.0.113.10. You should get in without the server password (at most, you will be asked for your key passphrase).

3. Turn off password login

Once key login works, edit /etc/ssh/sshd_config (for example with nano /etc/ssh/sshd_config) and set:

PermitRootLogin prohibit-password
PasswordAuthentication no

On recent Ubuntu releases, files in /etc/ssh/sshd_config.d/ can override these settings. If one of them contains PasswordAuthentication yes, change that too. Then test the configuration and restart the service:

sshd -t
# Ubuntu / Debian
systemctl restart ssh
# AlmaLinux / Rocky Linux
systemctl restart sshd
Don't lock yourself out

After the restart, keep your open session and try connecting with your key from a second window. If it fails, undo the change from the session that is still open.

4. Enable a firewall

Only allow the ports your services actually use. Add the SSH (22) rule before you switch the firewall on.

# Ubuntu / Debian (ufw)
apt install -y ufw
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status

# AlmaLinux / Rocky Linux (firewalld)
dnf install -y firewalld
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

If you don't run a web server, skip the 80 and 443 lines.

Numbered diagram: create a key, disable password login, enable the firewall
1: create and add a key, 2: disable password login, 3: enable the firewall.

5. Stop brute-force attempts with fail2ban

fail2ban temporarily blocks IP addresses after repeated failed logins.

# Ubuntu / Debian
apt install -y fail2ban
# AlmaLinux / Rocky Linux (needs the EPEL repository)
dnf install -y epel-release && dnf install -y fail2ban

systemctl enable --now fail2ban
fail2ban-client status sshd

Troubleshooting

SymptomLikely causeFix
The server still asks for a password even with a keyWrong permissions on authorized_keysRun chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
I can't connect after enabling the firewallThe firewall went on before the SSH rule was addedIf you have no console access in the panel, open a support ticket.
My own IP got blockedfail2ban banned it after a few failed attemptsConnect from another network and run fail2ban-client set sshd unbanip IP.

Frequently asked questions

Should I change the SSH port?

A different port cuts down log noise from bots, but it is not real protection. Key-only login and fail2ban are what keep you safe. If you do change the port, allow the new one in your firewall first.

Should I use a separate user instead of root?

Yes, it is good practice. Create a user with adduser, add it to the sudo group (Ubuntu/Debian) or wheel group (AlmaLinux/Rocky), then disable root login entirely with PermitRootLogin no.

Does this make my server fully secure?

It gives you a solid baseline. Keeping your applications up to date, taking regular backups and watching your logs are also part of security.

Stuck somewhere? Open a ticket from the client area or contact us. For servers with full root access, see VDS servers.

Was this article helpful?
Thank you for your feedback.