Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Secure SSH Access with Key-Based Authentication

Use SSH keys, turn off passwords, add two-factor login, change the port and block attackers with Fail2Ban.

SSL and Security3 min read17 screenshots

Why use keys

Passwords can be guessed or stolen, and servers are attacked by bots all day. An SSH key pair is much stronger. The private key stays on your computer and the public key goes on the server. Nobody can log in without the private key.

Warning: Keep a second SSH session or the console open while you change SSH settings. A mistake can lock you out.

Step 1: Create a key pair on your computer

ssh-keygen -t ed25519 -C "you@example.com"
Generating the SSH keys

Press Enter to accept the default file. Set a passphrase for extra safety.

Step 2: Copy the public key to the server

ssh-copy-id user@your_server_ip
Copying the public key to the server

If ssh-copy-id is not available:

cat ~/.ssh/id_ed25519.pub | ssh user@your_server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Step 3: Test the key login

ssh user@your_server_ip
Testing the key login

It should log in without asking for the account password (it may ask for the key passphrase).

Step 4: Turn off password login

Only do this after the key login works.

sudo nano /etc/ssh/sshd_config

Set these lines (remove the # if there is one):

PasswordAuthentication no
PubkeyAuthentication yes
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
Disabling password authentication

Check the file and restart SSH:

sudo sshd -t
sudo systemctl restart sshd
Restarting sshd

(On Ubuntu the service can be called ssh.) Open a new session to test before you close the old one.

Step 5: Add two-factor authentication (optional)

Install Google Authenticator for PAM

sudo apt install libpam-google-authenticator
Installing the Google Authenticator package

Create your secret

Run as the user who logs in:

google-authenticator
Generating the 2FA secret

Scan the QR code with an authenticator app and answer the questions (answer y to the time-based tokens and to update the file).

Save the backup codes in a safe place.

Saving the backup codes

Tell PAM to ask for the code. Edit /etc/pam.d/sshd and add:

auth required pam_google_authenticator.so
Configuring PAM to require the code

Turn it on in SSH. In /etc/ssh/sshd_config set:

KbdInteractiveAuthentication yes
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive
Enabling 2FA in the SSH configuration

Restart SSH:

sudo systemctl restart sshd
Restarting sshd

Test the login. You must now use the key and the 6-digit code.

Testing the SSH login with 2FA

Step 6: Change the default port (optional)

A different port reduces the noise from bots. It does not replace the other steps.

sudo nano /etc/ssh/sshd_config
Port 2222
Changing the default SSH port

Open the new port in the firewall first (step 7), then restart:

sudo systemctl restart sshd
Restarting SSH

Connect with ssh -p 2222 user@your_server_ip.

Step 7: Firewall rules

With UFW:

sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status
Enabling the firewall rules

After you confirm that the new port works, remove the old rule with sudo ufw delete allow 22/tcp.

Step 8: Install Fail2Ban

Fail2Ban bans addresses that fail to log in many times.

sudo apt install fail2ban
Installing Fail2Ban

Create /etc/fail2ban/jail.local:

[sshd]
enabled = true
port = 2222
maxretry = 5
bantime = 1h

Start it:

sudo systemctl enable --now fail2ban
Enabling the Fail2Ban service

Check the bans with sudo fail2ban-client status sshd.

Quick recap

  • Use ed25519 keys, turn off passwords and root password login.
  • Add 2FA, a firewall and Fail2Ban for more safety.