How to Change the Default SSH Port for Security Print

  • SSH Port, ssh port, change ssh port, ssh, linux, vps
  • 0

Changing the default SSH port (22) is a simple and effective way to reduce automated attacks and improve server security. This guide shows you how to change your SSH port on a Linux VPS and connect using the new port.

 


 

Important Warning

 

Before you begin:

Make sure you have access to the Virtualizor panel at https://vps.hosteons.com in case you misconfigure SSH and lose access. You can use the built-in VNC console to recover.

 


 

Step 1: Choose a New Port Number

 

Pick a port between 1024–65535 that is not used by other services. Common options include 2222, 2200, or 2022.

 

Avoid ports like:

80, 443 (used by web servers)

21, 25, 3306 (used by FTP, email, and databases)

 


 

Step 2: Open the New Port in Your Firewall

 

If you’re using UFW (Ubuntu/Debian):

ufw allow 2222/tcp

If you’re using firewalld (CentOS/AlmaLinux):

firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

If you’re not sure, you can temporarily disable the firewall to avoid lockout during testing (not recommended for production).

 


 

Step 3: Edit the SSH Configuration File

 

Open the SSH server config:

nano /etc/ssh/sshd_config

Find this line:

#Port 22

Uncomment it and change it:

Port 2222

Save and exit (Ctrl + X, then Y, then Enter).

 


 

Step 4: Restart the SSH Service

 

On Ubuntu/Debian:

systemctl restart ssh

On CentOS/AlmaLinux:

systemctl restart sshd

 

 


 

Step 5: Test SSH Access Using the New Port

 

Open a new terminal window and connect like this:

ssh -p 2222 root@your_server_ip

Do not close your current SSH session until you confirm that the new port works.

 


 

Step 6: Disable Port 22 (Optional)

 

Once you’ve confirmed the new port works, remove access to port 22 for extra security.

 

With UFW:

ufw delete allow 22

With firewalld:

firewall-cmd --permanent --remove-port=22/tcp
firewall-cmd --reload

 

 


 

Troubleshooting

Can’t connect?

Use the VNC Console in Virtualizor at https://vps.hosteons.com to revert the changes.

Port already in use?

Pick a different port and try again.

Firewall blocking port?

Double-check that the new port is allowed in your firewall.

 


 

Conclusion

 

Changing the default SSH port adds an extra layer of security to your VPS. Always test new settings before applying them permanently, and ensure you have backup access via the Virtualizor control panel.

 

For help regaining access or setting up a custom SSH port, contact Hosteons Support.

 


Was this answer helpful?

« Back