Sep 27, 2023
Edgaras G. & Noviantika G.
How to Change the SSH Port on VPS, Common Ports and How to Choose the Right One
Secure Shell (SSH) is a network protocol that allows users to access the server remotely. It also encrypts the communication between a client and a private server, making it more secure.
However, using the standard Transmission Control Protocol (TCP) port 22 for SSH can pose risks, especially vulnerability to cyber threats like brute-force attacks that attempt to access encrypted sensitive data.
Therefore, changing your default port is one of the best ways to protect your SSH server. In this article, we will explain how to choose a new SSH port and walk you through changing it on your virtual private server (VPS).
How to Choose a New SSH Port
When choosing a new SSH port, note that port numbers 0-1023 are reserved for various services and can only be bound by root access.
Here is a list of the most common privileged services and their associated ports and functions:
|20||TCP||File Transfer Protocol (FTP) data|
|25||TCP||Simple Mail Transfer Protocol (SMTP)|
|53||TCP/UDP||Domain Name System (DNS)|
|67/68||UDP||Dynamic Host Configuration Protocol (DHCP)|
|69||UDP||Trivial FTP (TFTP)|
|80||TCP||Hypertext Transfer Protocol (HTTP)|
|110||TCP||Post Office Protocol 3 (POP3)|
|123||UDP||Network Time Protocol (NTP)|
|143||TCP||Internet Message Access Protocol (IMAP)|
|161/162||TCP/UDP||Simple Network Management Protocol (SNMP)|
|179||TCP||Border Gateway Protocol (BGP)|
|389||TCP/UDP||Lightweight Directory Access Protocol (LDAP)|
|443||TCP||HTTP over SSL/TLS (HTTPS)|
|636||TCP/UDP||LDAP over SSL/TLS (LDAPS)|
|989/990||TCP||FTP over SSL/TLS (FTPS)|
Although you can still use them, doing so will put your SSH connection at risk of network conflict.
Therefore, we recommend choosing from different ports ranging from 1024 to 65535 as they are non-privileged.
How to Change the Default SSH Port
In this section, we will explain how to change your default SSH port in four simple and easy steps.
1. Connect to Your Server via SSH
To do so, run the following command line on Terminal or PuTTy:
Next, you will be prompted to type the password or SSH keys. Once you’ve done so, the connection will be established.
Check out our article to learn how to set up SSH keys and manage them properly.
2. Change SSH Configurations
Start by executing the following command to access the SSH daemon (sshd):
sudo nano /etc/ssh/sshd_config
The SSH configuration file will open. Find the line that reads #Port 22. Next, delete the number and # and replace it with the new SSH port number you want to use.
Change the SSH port number to 1026:
Then save the changes.
If the contents of your sshd_config file look differently, simply add a new Port line, just like in the example above.
3. Configure Your Firewall
Start by making sure the newly selected port is not blocked. If this is a new VPS setup, all ports should be open by default.
Update your firewall settings to allow incoming connections to the new port. For this example, we will be using the UFW firewall. Run the following command:
sudo ufw allow 1026/tcp
Next, restart the SSH service using the following command:
Debian and Ubuntu:
sudo service ssh restart
Debian and Ubuntu with systemd:
sudo systemctl restart ssh
CentOS and Fedora
sudo service sshd restart
CentOS and Fedora with systems:
sudo systemctl restart sshd
Alternatively, you can take advantage of Hostinger VPS Firewall. Select your VPS and navigate to the Firewall section:
Click Create firewall configuration, give your firewall configuration a name, and click Create:
Then, select the Edit button:
Lastly, configure the rules you prefer and click Add rule:
Keep in mind that our VPS Firewall filters incoming traffic.
4. Test the New Default Port
To verify that the new SSH port is open, run either the ss or netstat command:
ss -tulpn | grep 1026
netstat -tulpn | grep 1026
Now you can try to log in to SSH using the following command:
ssh -p [port] user@server
ssh -p 1026 firstname.lastname@example.org
Test the SSH connection using a new Terminal window. Do not log out from the previous root session until you’ve confirmed that the new setup is working well.
As for Hostinger users, if you cannot connect to your VPS using the new port, you can always reset the SSH and firewall settings to default from your VPS dashboard on hPanel.
Using the default SSH port 22 can make you an easy target for hackers – they often look for open ports through which to intercept and extract sensitive data. Therefore, we recommend changing the SSH port to avoid potential cyber attacks and add extra protection to your Linux server.
Before choosing a new port for your SSH access, note that the ports numbered 0-1023 are reserved for privileged services. Hence, it is best to use a port ranging from 1024 to 65535.
Configuring a new specified port is relatively easy – you only need to update the number in the SSH daemon configuration file and firewall settings. Then, restart the SSH service and test the new port’s connection again.
We hope this article has helped you change your SSH port on VPS. If you have any questions or ideas, please leave them in the comments section below.