
If you manage a Linux VPS or Ubuntu server, changing the SSH port can be one step toward improving server security. By default, SSH listens on port 22, which makes it a common target for automated scans. Moving SSH to a different port may help reduce unwanted connection attempts. In this guide, we will walk you through the steps to change the SSH port on your Linux server safely.
Why Change the SSH Port?

When you use a Linux VPS, your server is exposed to the internet, and attackers regularly scan for open ports, especially port 22, which is the default SSH port. Changing the SSH port may reduce unwanted connection attempts from automated scans. However, it does not replace essential security measures such as SSH key authentication and firewall rules.
Prerequisites for Changing the SSH Port
Before changing the SSH port, make sure you have the following:
- Access to an Ubuntu server or Linux VPS.
- Root access or a user account with sudo privileges.
- Basic knowledge of using SSH to connect to your server.
Before making any changes, back up important configuration files and keep your existing SSH session open. This will help you maintain access to the server if something goes wrong during the configuration process.
Changing the default SSH port (22) on your Ubuntu server may help reduce unwanted connection attempts from automated scans. Let’s go through the steps to change the SSH port safely.
Step 1: Check the Current SSH Port
By default, SSH uses port 22. To confirm this, run:
sudo grep Port /etc/ssh/sshd_config
This will display the port your SSH service currently listens on.
Step 2: Confirm SSH Service Connectivity
Ensure the SSH service is running to prevent connection issues later:
sudo systemctl status ssh
This command verifies that the SSH service is active and working correctly on your Linux server.
Step 3: Connect to the Remote Server via SSH
Open your terminal and connect to your Ubuntu server or Linux VPS:
ssh user@your-server-ip
Make sure you have sudo privileges for the configuration changes ahead.
Step 4: Allow the New SSH Port Through the Firewall
Before changing the SSH configuration, allow the new port through the firewall:
sudo ufw allow 2222/tcp
sudo ufw reload
Verify that the new port is allowed:
sudo ufw status
Keep port 22 allowed until the new SSH connection has been tested successfully.
Step 5: Modify the SSH Port
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
Find the following line:
#Port 22
Remove the # symbol and change the port number to 2222:
Port 2222
Save the changes and exit the editor.
Important: Keep your existing SSH session open. Do not close your current connection until you have successfully tested the new SSH port.
Step 6: Test SSH Configuration and Restart the Service
Before restarting the SSH service, check the configuration for syntax errors:
sudo sshd -t
If the command returns no output, the configuration syntax check has passed. Next, restart the SSH service:
sudo systemctl restart ssh
Keep your existing SSH session open while restarting the service. Do not log out until you have successfully tested the new SSH port.
Note: On Ubuntu systems using SSH socket activation, check the ssh.socket configuration and update its listening port to 2222 before restarting the socket.
Step 7: Verify Port After Changes
Before closing your existing SSH session, open a new terminal window and connect to the server using the new SSH port:
ssh -p 2222 user@your-server-ip
Replace user with your server username and your-server-ip with your server’s IP address.
If the connection is successful, the new SSH port is working correctly. Keep your original SSH session open until you have confirmed that you can log in through port 2222.
Do not disable port 22 until the new connection has been tested successfully.
For Ubuntu systems using SSH socket activation:
Check whether SSH socket activation is enabled:
systemctl is-active ssh.socket
If the output is active, update the socket listening port:
sudo systemctl edit ssh.socket
Add the following configuration:
[Socket]
ListenStream=
ListenStream=2222
Save the configuration, then run:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
If socket activation is not active, use the SSH service restart command instead:
sudo systemctl restart ssh
Keep your existing SSH session open and test the new port from a separate terminal before closing it.
Additional Tips for Managing Your Ubuntu Server

- Disable Root Login: For better security, disable direct root login over SSH by setting
PermitRootLogin noin the/etc/ssh/sshd_configfile. Make sure you have another user account with sudo privileges before applying this change. - Use SSH Keys: Use SSH key-based authentication instead of password-based login to improve the security of your Linux server.
- Monitor Logs: Regularly check SSH authentication logs in
/var/log/auth.logon Ubuntu servers to monitor login attempts and identify suspicious activity.
Conclusion
Changing the SSH port on your Ubuntu server is a simple step that can help reduce unwanted connection attempts from automated scans. However, changing the default port alone does not guarantee security. Make sure to update your firewall rules, verify the SSH configuration, and test the new port before closing your existing session.
By following these steps, you can improve your server’s security while maintaining reliable remote access. For stronger protection, use SSH key authentication, keep your system updated, and restrict access to trusted IP addresses where possible.
