How To Change The Default SSH Port On Linux Server

How To Change The Default SSH Port On Linux Server

The default port for SSH, which stands for Secure Shell, is port 22. It is not a major security issue if you leave this as is, but it is recommended that you change it as an added security measure. A non-default SSH port will help you stop many automated attacks and make it a little harder for hackers to guess which SSH to access from.

How To Change Default SSH Port

Connect to your server via SSH and type in the following command to edit the sshd configuration file.

vi /etc/ssh/sshd_config

Look for line that read as follow:

Port 22

Replace 22 with a port between 1024 and 65536

Note: Make sure the new SSH port does not conflict with any known or blocked ports. If you do not know which port is safe to use, just pick from dynamic and/or private port, which are ports from 49152 through 65535. So if you want to change SSH port to 49456, it will looks like the following:

Port 49456

Save and close the file. Now you need to restart the sshd service by running one of following commands, depending on what Linux distribution you are using:

How to Restart SSH Service for CentOS / RHEL / Fedora / Redhat

/etc/init.d/sshd restart
OR
service sshd restart

How to Restart SSH Service for Debian / Ubuntu

/etc/init.d/ssh restart
OR
service ssh restart
OR
sudo service ssh restart
OR
sudo systemctl restart ssh

How to Restart SSH Service for FreeBSD

/etc/rc.d/sshd restart

How to Restart SSH Service for OpenBSD

/etc/rc.d/sshd restart

You have now successfully changed the SSH port. Now try to connect to the SSH with the newly changed port to verify that it works.

You May Also Like