How to change your SSH port

Step 1 – Connect to your server using SSH. If you are using Windows you can use the program Putty or if you are using iOS simply use the terminal.

vi /etc/ssh/sshd_config

Step 2 – Now find the below line in the sshd_config file:

#Port 22

This line indicates which port you are using for SSH.

Step 3 – Remove the # symbol to uncomment the line. This can be done by hitting the X key whilst the cursor is over the # symbol. Edit the number “22” to your desired port value, the valid range for the port is 0 to 65535. Press ESC and type :wq, this exits you out from edit mode.
W stands for write and Q for quit. In this example i have chosen to use port 57343.

Port 57343

Step 4 – Now we need to update the firewall rules. Enter the command below.

iptables –I INPUT 1 –p tcp –-dport 57343 –j ACCEPT

Step 5 – Save the new settings using the command below

/etc/init.d/iptables save

Step 6 – We need to test whether our new SSH port working or not. But before that disable the firewall temporarily.

service iptables stop
service ip6tables stop

Step 7 – Type the following command to reload SSHD service:

service sshd reload

Step 8 – You can verify the new port settings using the command below.

netstat -tulpn | grep sshd

Step 9 – If we see that the new port is working fine, we will start firewall using the commands:

service iptables start
service ip6tables start

Step 10 – Without closing this session, try and connect using your new port. If you are successful then please enter the below command to drop port 22 from the SSH access list.

iptables –A INPUT –j DROP –p tcp –dport 22

Other Knowledge Base Topics

"