May 28, 2023

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

Currently I'm working as an Implementation Engineer, Started my career as an System Administrator - Linux. Additionally he loves to explore new technologies and research about new open-source software that ease the development cycle.

View all posts by Vedant Singh →

Leave a Reply

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


Ad Blocker Detected!

We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.