Back to blogs

November 2, 2026

How to Secure SSH on a Linux VPS

#Secure SSH on Linux VPS#Change default SSH port#Linux SSH security#SSH#Linux#SSH hardening best practices
How to Secure SSH on a Linux VPS

If you manage a remote Linux VPS, SSH (Secure Shell) is probably one of the tools you use most frequently. It allows you to connect to your server, execute commands, manage applications and perform administrative tasks securely over a network.

However, there is something important that many beginners overlook, having SSH enabled does not automatically mean your server is properly secured.

By default, most Linux distributions use port 22 for SSH connections. Since this is a well known default port, automated bots and attackers frequently scan internet facing servers for accessible SSH services. They may attempt brute force attacks using commonly used usernames and passwords.

Fortunately, there are several practical steps you can take to reduce these risks.

In this blog, we will learn how to secure a remote Linux VPS by changing the default SSH port, configuring SSH key-based authentication, disabling password authentication, restricting root access and implementing additional security practices.

We will use Ubuntu/Debian commands in this blog, but most of the concepts also apply to other Linux distributions.

Understand What is SSH?

SSH stands for Secure Shell. It is a network protocol that allows users to securely access and manage remote systems.

For example, you can connect to your VPS using

ssh username@your-server-ip

By default, SSH listens on port 22. If your server has the IP address 192.168.1.100, your connection command would look like this

ssh username@192.168.1.100

The connection is encrypted, meaning data exchanged between your local machine and the remote server is protected during transmission.

However, encryption alone does not protect your server from weak passwords, unauthorized login attempts or misconfigured access permissions. That is why SSH hardening is important.

Update Your Linux VPS

Before modifying SSH configurations, update your server packages. Connect to your VPS and execute

sudo apt update
sudo apt upgrade -y

This ensures that your system has the latest available security updates and package fixes.

You should also verify that the SSH server is running

sudo systemctl status ssh

If SSH is not installed, install the OpenSSH server

sudo apt install openssh-server -y

Change the Default SSH Port

One of the first SSH hardening steps is changing the default SSH port from 22 to a different port. For this blog, we will use port 2222. Changing the default port does not provide strong authentication or replace other security measures. However, it can reduce noise from automated scans targeting the default SSH port.

Step 1: Open the SSH Configuration File

The main SSH server configuration file is located at 

/etc/ssh/sshd_config

Open it using vim

sudo vim /etc/ssh/sshd_config

Find the following configuration

#Port 22

Uncomment it and change the port

Port 2222

If the Port directive is not present, you can add it to the configuration. Save the file and exit.

Step 2: Allow the New Port Through the Firewall

If you are using UFW (Uncomplicated Firewall), allow the new SSH port

sudo ufw allow 2222/tcp

Check your firewall rules

sudo ufw status

Important: Do not remove or close port 22 yet. Keep your existing SSH session open and verify that the new port works before removing access to the old port.

If you use a cloud provider such as AWS, remember to update the VPS security group or network firewall rules as well.

For AWS EC2 add an inbound rule

  • Type: Custom TCP
  • Port: 2222
  • Source: Your trusted public IP address
  • Protocol: TCP

Note: Avoid exposing SSH to 0.0.0.0/0 when you can restrict access to your own IP address.

Step 3: Validate and Restart SSH

Before restarting SSH, validate the configuration

sudo sshd -t

If there is no output, the configuration syntax is valid. Restart the SSH service

sudo systemctl restart ssh

Check whether SSH is listening on the new port

sudo ss -tlnp | grep ssh

You should see SSH listening on port 2222.

Step 4: Test the New SSH Connection

Open a new terminal on your local machine and connect using

ssh -p 2222 username@your-server-ip

For example

ssh -p 2222 ubuntu@192.168.1.100

If the connection works, your new SSH port is configured successfully. Only after confirming that the new connection works should you remove the old firewall rule

sudo ufw delete allow 22/tcp

If you use a cloud security group, remove the old port 22 inbound rule there too.

Configure SSH Key-Based Authentication

Changing the SSH port is not enough to secure your VPS. One of the most important security improvements is replacing password-based SSH authentication with SSH key-based authentication.

SSH keys use a pair of cryptographic keys

  • Private Key: Stored securely on your local computer. Never share it.

  • Public Key: Added to the remote server to authorize your login.

This approach provides stronger authentication than relying on passwords alone.

Step 1: Generate an SSH Key Pair

On your local machine, execute

ssh-keygen -t ed25519 -C "your-email@example.com"

Ed25519 is a modern public-key algorithm supported by OpenSSH. You will be asked where to save the key.

The default location is

~/.ssh/id_ed25519

Note: You can also set a passphrase to add another layer of protection to your private key.

Step 2: Copy the Public Key to Your VPS

If your SSH server still permits password authentication, you can use

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 username@your-server-ip

This command copies your public key to the remote server. If ssh-copy-id is unavailable, you can manually add the public key to the remote user's

~/.ssh/authorized_keys

Ensure the SSH directory and file permissions are correct

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Step 3: Test SSH Key Authentication

Open a new terminal and connect

ssh -i ~/.ssh/id_ed25519 -p 2222 username@your-server-ip

If everything is configured correctly, you should be able to log in using your SSH key. Keep your existing session open until you have successfully tested this connection.

Disable Password-Based SSH Authentication

Once you have confirmed that SSH key authentication works, you can disable password-based authentication.

Open the SSH configuration file

sudo nano /etc/ssh/sshd_config

Find or configure the following directives

PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no

These settings enable public-key authentication and disable password and keyboard-interactive authentication.

Important: If your server uses PAM or another authentication mechanism, review its configuration before making changes.

Validate the configuration

sudo sshd -t

Restart SSH

sudo systemctl restart ssh

Open another terminal and verify that you can still log in using your private key. Never close your existing working SSH session until the new authentication method has been tested.

Disable Direct Root Login

The root account has unrestricted administrative privileges on a Linux server. Allowing direct root SSH login increases the impact of compromised credentials. Instead, create a regular user account and grant it sudo privileges.

Step 1: Create a New User

For example

sudo adduser devuser

Step 2: Grant Sudo Privileges

sudo usermod -aG sudo devuser

Step 3: Configure SSH Access

Make sure the new user has a working SSH public key in

/home/devuser/.ssh/authorized_keys

Set the correct ownership and permissions

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

Test the new account in a separate terminal

ssh -i ~/.ssh/id_ed25519 -p 2222 devuser@your-server-ip

Step 4: Disable Root Login

Open the SSH configuration

sudo vim /etc/ssh/sshd_config

Set

PermitRootLogin no

Validate and restart

sudo sshd -t
sudo systemctl restart ssh

You should now manage your server using your regular account and sudo instead of logging in directly as root.

Apply Additional SSH Security Best Practices

Beyond changing the SSH port and disabling password authentication, consider the following security improvements.

Limit Authentication Attempts

Configure

MaxAuthTries 3

This limits the number of authentication attempts allowed per connection.

Set an Idle Connection Timeout

Configure

ClientAliveInterval 300
ClientAliveCountMax 2

This allows SSH to disconnect clients that do not respond to periodic keepalive messages.

Restrict SSH Access to Specific Users

You can restrict which Linux users are permitted to connect through SSH

AllowUsers devuser

If multiple users need access, list them

AllowUsers devuser deploy admin

Only add accounts that genuinely require SSH access.

Use a Firewall

A firewall helps control which network connections can reach your VPS. For UFW

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp
sudo ufw enable

Warning: Before enabling UFW remotely, make sure you have allowed your active SSH port. Otherwise, you could lock yourself out of the server.

If your VPS hosts a website, remember to allow the required application ports, such as HTTP (80) and HTTPS (443).

Final SSH Configuration

After completing the previous steps, your SSH configuration should include the following essential settings

Port 2222

PermitRootLogin no

PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no

MaxAuthTries 3

ClientAliveInterval 300
ClientAliveCountMax 2

AllowUsers devuser

These are example settings for a single user VPS. Review any existing configuration before applying them.

Also check whether additional configuration files under /etc/ssh/sshd_config.d/ override your settings.

You can inspect the effective SSH configuration with

sudo sshd -T | grep -E '^(port|permitrootlogin|pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|maxauthtries|clientaliveinterval|clientalivecountmax|allowusers)'

This helps verify what the SSH server will actually use.

Common SSH Security Mistakes to Avoid

Even after implementing these improvements, avoid the following mistakes

1. Closing the Existing SSH Session Too Early: Always keep an existing working session open while testing SSH configuration changes.

2. Disabling Password Authentication Before Testing SSH Keys: Make sure key-based authentication works before disabling password authentication.

3. Forgetting Firewall Rules: Changing the SSH port without updating UFW or your cloud firewall can make your server inaccessible.

4. Using Weak Private Key Protection: Protect your private key with a passphrase and never upload it to GitHub or share it with anyone.

5. Ignoring Security Updates: Regularly update your operating system and OpenSSH packages to receive security fixes.

6. Assuming a Non-Standard Port Is Complete Protection: Changing port 22 reduces exposure to some automated scans, but it does not replace strong authentication, firewall restrictions and regular security maintenance.

Useful Commands Reference

TaskCommand
Check SSH statussudo systemctl status ssh
Edit SSH configurationsudo vim /etc/ssh/sshd_config
Validate SSH configurationsudo sshd -t
Restart SSHsudo systemctl restart ssh
Check listening portssudo ss -tlnp
Check firewall rulessudo ufw status
Connect using custom portssh -p 2222 username@server-ip
Generate SSH keyssh-keygen -t ed25519
Copy public keyssh-copy-id -p 2222 username@server-ip

Conclusion

Securing SSH is one of the first things you should do when managing a publicly accessible Linux VPS. In this blog, we learned how to change the default SSH port, configure SSH key-based authentication, disable password authentication, prevent direct root login and apply additional security practices.

These changes help reduce unauthorized access risks and make your remote server administration more secure.

Remember, server security is not a one time task. Keep your system updated, review SSH access regularly, protect your private keys and monitor authentication logs.