Skip to content

How to Secure SSH Access on Your Linux VPS

This guide shows you how to protect your VPS from attackers by setting up SSH keys, disabling root login and changing the SSH port.

Warning

Follow the steps in order. If you disable password authentication before adding your SSH key, you will lock yourself out of the server.

1. Create SSH key

First, create an SSH key pair on your local PC.

Windows (PowerShell)

powershell
ssh-keygen -t ed25519
ssh-keygen -t ed25519

Linux / macOS (Terminal)

bash
ssh-keygen -t ed25519
ssh-keygen -t ed25519

Confirm the default path with Enter and optionally set a passphrase for additional protection.

Two files are created:

  • ~/.ssh/id_ed25519 — your private key (never share this!)
  • ~/.ssh/id_ed25519.pub — your public key (this goes on the server)

2. Upload public key to the server

Linux / macOS

bash
ssh-copy-id root@YOUR_SERVER_IP
ssh-copy-id root@YOUR_SERVER_IP

Windows (PowerShell)

powershell
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh root@YOUR_SERVER_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh root@YOUR_SERVER_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Test

Test the connection before continuing:

bash
ssh root@YOUR_SERVER_IP
ssh root@YOUR_SERVER_IP

You should be able to log in without entering a password.

3. Create a new user (optional)

Note

This step is optional. If you do not want to create a separate user, you can skip this step and continue working as root.

Optionally, you can create a separate user instead of logging in as root.

bash
adduser myuser
adduser myuser

Grant the user sudo rights:

bash
usermod -aG sudo myuser
usermod -aG sudo myuser

Copy the SSH key to the new user:

bash
mkdir -p /home/myuser/.ssh
cp ~/.ssh/authorized_keys /home/myuser/.ssh/authorized_keys
chown -R myuser:myuser /home/myuser/.ssh
chmod 700 /home/myuser/.ssh
chmod 600 /home/myuser/.ssh/authorized_keys
mkdir -p /home/myuser/.ssh
cp ~/.ssh/authorized_keys /home/myuser/.ssh/authorized_keys
chown -R myuser:myuser /home/myuser/.ssh
chmod 700 /home/myuser/.ssh
chmod 600 /home/myuser/.ssh/authorized_keys

Test

Test the login with the new user before continuing:

bash
ssh myuser@YOUR_SERVER_IP
ssh myuser@YOUR_SERVER_IP

4. Disable root login and password authentication

Open the SSH configuration:

bash
sudo nano /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config

Find and change the following line:

PasswordAuthentication no
PasswordAuthentication no

If you created a separate user in step 3, you can also disable root login:

PermitRootLogin no
PermitRootLogin no

Important

Make sure your SSH key is working and you can log in via SSH key before you make these changes. Otherwise you will lock yourself out!

Save with Ctrl + O, close with Ctrl + X and restart the SSH service:

bash
sudo systemctl restart sshd
sudo systemctl restart sshd

5. Change SSH port

By default SSH runs on port 22. Changing this reduces automated brute-force attacks.

Open the SSH configuration:

bash
sudo nano /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config

Find the line #Port 22 and change it to:

Port 2222
Port 2222

Note

Choose a port between 1024 and 65535 that is not already in use. In this example we use 2222.

If you are using UFW, allow the new port before restarting:

bash
sudo ufw allow 2222/tcp
sudo ufw allow 2222/tcp

Restart the SSH service:

bash
sudo systemctl restart sshd
sudo systemctl restart sshd

From now on, connect with:

bash
ssh -p 2222 root@YOUR_SERVER_IP
ssh -p 2222 root@YOUR_SERVER_IP

Important

Do not close your current SSH session until you have successfully connected in a new terminal using the new port!

Summary

MeasureEffect
SSH keySecure authentication without passwords
New userNo direct root access
Disable root loginBlocks root login attempts
Disable password authPrevents brute-force attacks
Change SSH portReduces automated attacks

Further security

For additional protection, also set up Fail2Ban and UFW.