Security

How to Secure SSH on a Linux VPS: Keys, Root Login, fail2ban and 2FA

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

SSH is the front door of almost every Linux server, and it is the first thing automated scanners knock on. Within minutes of a new VPS getting a public IP address, its logs usually start filling with login attempts for users like root, admin and test. None of this is personal; bots simply try weak passwords on every address they can reach. The good news is that a few well-understood changes make SSH extremely hard to break into. This guide walks through them in a safe order, so you never lock yourself out along the way.

Why passwords are the weak point

A password can be guessed, reused from a leaked database or typed into a phishing page. An SSH key pair works differently: the private key stays on your computer, and the server only stores the matching public key. During login, your client proves it holds the private key without ever sending it. A modern key is far too long to brute-force, which is why disabling password logins removes the main target of those bots.

Step 1: Create a non-root user with sudo

Working as root all day means one mistake can damage the whole system. Create a normal user and give it sudo rights. On Ubuntu or Debian:

adduser deploy
usermod -aG sudo deploy

On AlmaLinux or Rocky Linux, use the wheel group instead of sudo.

Step 2: Generate an SSH key on your own computer

Run this on your laptop or desktop, not on the server. Ed25519 keys are short, fast and widely supported:

ssh-keygen -t ed25519 -C "laptop-2026"

Accept the default path (~/.ssh/id_ed25519) and set a passphrase. The passphrase encrypts the private key on disk, so a stolen laptop does not mean a stolen server. Windows 10 and 11 include the same OpenSSH client in PowerShell.

Step 3: Copy the public key to the server

The easiest way on Linux and macOS:

ssh-copy-id deploy@YOUR_SERVER_IP

If ssh-copy-id is not available, append the contents of id_ed25519.pub to /home/deploy/.ssh/authorized_keys on the server, then fix the permissions:

chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys
chown -R deploy:deploy /home/deploy/.ssh

Now open a new terminal and confirm that ssh deploy@YOUR_SERVER_IP logs you in with the key. Keep your current session open until everything below is tested.

Step 4: Harden sshd_config

Edit /etc/ssh/sshd_config, or better, create a drop-in file such as /etc/ssh/sshd_config.d/10-hardening.conf on systems that include that directory (recent Ubuntu and Debian do):

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers deploy

Check the syntax and reload the service:

sudo sshd -t
sudo systemctl reload ssh     # on RHEL-family systems: sshd

Test again from a new terminal before closing the old one. If something is wrong, the open session lets you fix it. As an extra safety net, a browser console like the one included with NileVPS servers lets you log in even if SSH itself is misconfigured.

Step 5: Add fail2ban

Even with passwords disabled, bots still consume log space and connections. fail2ban watches the logs and temporarily bans IP addresses that fail repeatedly:

sudo apt install fail2ban

Create /etc/fail2ban/jail.local:

[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h

Then run sudo systemctl enable --now fail2ban and check the status with sudo fail2ban-client status sshd.

Step 6: Changing the SSH port (optional)

Moving SSH from port 22 to, say, 2222 does not make it more secure in a cryptographic sense, but it cuts log noise sharply because most bots only try port 22. If you do it, set Port 2222 in the config, allow the new port in your firewall before reloading, and on SELinux systems run semanage port -a -t ssh_port_t -p tcp 2222. On Ubuntu 22.10 and later, SSH may be socket-activated, so the port is set in ssh.socket; check with systemctl status ssh.socket.

Step 7: Two-factor authentication (optional)

For servers that several people access, you can require a time-based code in addition to the key using the libpam-google-authenticator package. Each user runs google-authenticator to enroll, you add auth required pam_google_authenticator.so to /etc/pam.d/sshd, and set AuthenticationMethods publickey,keyboard-interactive with KbdInteractiveAuthentication yes. Test it carefully, because a mistake here is the most common way people lock themselves out.

Restrict who can reach SSH at all

The strongest step is network-level: if you always connect from a fixed IP or VPN, allow port 22 only from that address. A cloud firewall does this before traffic even reaches the server. Our guide firewall explained covers ufw, nftables and cloud firewall rules in detail.

Quick checklist

SettingRecommended value
Login methodSSH keys only (Ed25519)
Root loginDisabled
Password loginDisabled
Brute-force protectionfail2ban enabled
Network accessRestricted by firewall where possible
2FAFor shared or sensitive servers

Ready to practice on a fresh machine? You can deploy a Linux VPS in a few minutes and apply these steps from the first login.

FAQ

What happens if I lose my private SSH key?

You will not be able to log in with that key anymore. Use your server's browser console or another authorized key to log in, add a new public key to authorized_keys, and remove the old one. It is wise to register two keys, for example one on your laptop and one stored safely as a backup.

Is RSA still acceptable for SSH keys?

Yes, RSA keys of 3072 or 4096 bits remain secure. Ed25519 is simply preferred for new keys because it is shorter and faster. Avoid DSA keys and RSA keys shorter than 2048 bits, which modern OpenSSH versions reject or discourage.

Do I still need fail2ban if password login is disabled?

It is no longer essential for keeping attackers out, because there is no password to guess. It still helps by reducing log noise and wasted connections, and it can protect other services such as mail or web logins using additional jails.

Related guides