How to Change SSH Port?

Share your love by sharing

SSH is a network protocol that allows connecting to remote computers or servers remotely. SSH is used primarily for connection because it offers strong password and public key based authentication. Since it encrypts the data between two devices, it is the most secure protocol. Here in this tutorial we will see how to change SSH port.

By default, SSH use port 22 to connect to any other server or computer. Since everyone know this, anyone might try to access your server using the same port. Change this port to something else adds a good amount of security to your setup.

How-to-change-ssh-port
How-to-change-ssh-port

Prerequisites

  • A system with Linux installed
  • A user will root privileges
  • Terminal access

Change SSH port

We have 65536 ports in total, out of which we can use ports from 0-65535 as 1 port is reserved for system itself. 0-1023 ports are called as well-known ports because these ports are used by various services and applications that keep our systems runnings. Although, we can change SSH to any port between 0-1023 but it might cause conflict with other sevrvices. So we will change SSH port to any between 1024-65535 as they are free ports.

Step 1 – Connect to remote server

To connect to your remote server, use the syntax below

    ssh username@ip_address

For example

    # ssh ashish@192.168.2.4

Step 2 – Change SSH Configuration

SSH configuration file is located in /etc/ssh/sshd_config. Open it using the editor of your choice. Here we are using nano

    # sudo nano /etc/ssh/sshd_config

Search for the line that says #Port 22

Change

    #Port 22

To

    Port 1818
Note: Change 1818 to your desired port

Save the file and exit

Step 3 – Update Filewall Rules

We will have to update the firewall rules so that our firewall allows the connection through our port. Depending upon the firewall you are using, command syntax will vary.

If you are using UFW firewall then the syntax will be

    # sudo ufw allow 1818/tcp

If you are using firewalld then the syntax will be

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

Step 4 – Restart SSH Service

Last step is to restart SSH service so that the new settings will be applied

    # sudo service sshd restart

Step 5 – Testing New Port

Now, lets test if the new port is working or not.

    # ssh -p 1818 username@ip_address

For example

    # ssh -p 1818 ashish@192.168.2.4

If everything was done in right way, you should be able to connect to your server.

Share your love by sharing
Sourabh Verma
Sourabh Verma

I.T Engineer by profession, tech lover. Passionate for reading and writing technical stuffs. Loves to share knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *