How to Change the SSH Port in Linux

 

 

 

 

“SSH” is an acronym for “Secure Shell”. This protocol makes it possible for a secure connection to be established with a remote server or system. In the sense of data transfer between the host and client, SSH is secure in that it encrypts the data. To simplify a bit, the client input is transferred to the host and the host output is relayed back to the client. The SSH protocol runs on port 22 of TCP/IP. Here, we will demonstrate how to change the SSH port in this tutorial, So let’s begin.

 

 

 

 

 

 

Step 1: Getting your Linux system up and running

 

 

 

 

 

VPSie offers a wide range of Linux distributions that you can choose according to your requirements. If you already have one, you can move on to Step 2.

 

  1. Sign in to your system or register a newly created one by logging in to your VPSie account
  2. Connect by SSH using the credentials we emailed you.
  3. Once you have logged into your Debian instance, run these commands to update your system.

 

 

 

 

 

Step 2: Select the New Port

 

 

 

 

 

Typically, Linux systems reserve port numbers below 1024 for their services. SSH can also be used on ports 1-1024, but it will be more effective if you choose a higher port than 1024. SSH service allows up to 65535 ports to be selected. 2244 is the port we are going to use for SSH. You may choose another port according to your preference.

 

 

 

 

 

 

Step 3: Configuring the firewall

 

 

 

 

 

 

Before changing your SSH port, you need to adjust the server’s firewall to allow the new port. It will then let traffic through the new port.

 

 

 

  • On Ubuntu systems: UFW is the default firewall tool. Run the following command to allow connections using the new SSH port:
# sudo ufw allow 2244

 

 

  • You can open a port by executing the below command on a Linux server with iptables installed as a firewall:

 

 

# sudo iptables -A INPUT -p tcp --dport 2244 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

 

  • CentOS and RHEL: Use the following command to open a new port on machines using FirewallD, the default firewall management tool.

 

 

 

# sudo firewall-cmd --permanent --zone=public --add-port=2244/tcp

# sudo firewall-cmd --reload

 

 

  • Other requirements for Linux-based CentOS and RHEL distributions include adjusting the SELinux rules to allow the new SSH port. Refer to the instructions below:

 

# sudo semanage port -a -t ssh_port_t -p tcp 2244
 
 

 

Step 4: Configuring SSH

 

 

 

 

 

 

SSH default port are stored in /etc/ssh/sshd_config file. You need to open the main SSH configuration file for editing.

 

 

 

# sudo nano /etc/ssh/sshd_config

 

 

Within the file, search for a line starting with Port 22. You should enter your new SSH port after removing the hash #. It should look like this.

 

 

Port 2244

 

 

 

Restart the SSH service,

 

 

# sudo systemctl restart ssh

 

 

 

In CentOS or RHEL use the following command is required to restart it,

 

 

# sudo systemctl restart sshd

 

 

 

Run command to see if our new port is being heard by the SSH daemon,


 

# ss -an | grep 2244

 

 

 

 


 

Step 5: Using the new port to connect to SSH

 

 

 




When you use the SSH command with a new port, you have to specify the port number -p,

 

 


# ssh -p 2244 [email protected]_address

 

 

 


 


We have successfully changed the SSH port on your Linux. Hopefully, you have found this article useful and gained some useful information from it. Thank you for reading.

 

 

 

 

 

 

Try VPSie for free today!