October 14, 2019

How to Change SSH Port in Linux

SSH stands for secure shell is a cryptographic network protocol to access remote server securely over an unsecured network. The applications include remote command-line login and remote command execution.

SSH (secure shell) uses the public to authenticate the remote computer and allows it to authenticate the user, if necessary. There are many ways to use SSH, one is to use automatically generated public-private key pairs to simply encrypt a network connection and then use password authentication to log on.

By default the SSH port number is 22/TCP. So, therefore, it is recommended to change your default port because many hackers and bots continuously targeted for vulnerabilities.

To change the SSH port first you will have login to your server and open main SSH configuration with your favourite text editor.

Use the following command to open your SSH configuration.

# vi /etc/ssh/sshd_config

In this configuration file search for word Port 22 and comment the line using # and add the desired port number

In this example we have just used a random port, in your case you can choose your own

#Port 22 
Port 4567

After making the changes in the file restart the SSH daemon and netstat inorder to listen to the port that you have defined.

# systemctl restart ssh
# netstat -tlpn| grep ssh
# ss -tlpn| grep ssh

In centos 7 or RHEL distributions install policycoreutils package to relax the selinux policy

# yum install policycoreutils
# semanage port -a -t ssh_port_t -p tcp 4567
# semanage port -m -t ssh_port_t -p tcp 4567
# systemctl restart sshd
# netstat -tlpn| grep ssh
# ss -tlpn| grep ssh

After if the firewall is enabled on your system then update your firewall rules by the following command.

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

Vedant Singh

Linux System Administrator | Entrepreneur | Founder Techmion. I have started my career as a Linux system Administrator after that quit my job to start something new that excites me and started a Tech Company in order to provide the best technology to my clients. My Company website link: Technology at its best For Bulk, SMS-Marketing follows the link : https://www.pingsms.in/

View all posts by Vedant Singh →

Leave a Reply

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