How do I change the default SSH port
I'm trying to change the default SSH port. How do I do that?
4 Replies
You can change the SSH port by editing your sshd_config
. That file is located here:
/etc/ssh/sshd_config
You'll then need to edit the Port
line. If there is a #
in front of the line make sure to remove it.
Finally, run the following to restart SSH:
systemctl restart sshd
After that, you'll be able to connect to your Linode on the new port.
Also don't forget that after you change the SSH port, you will need to tell your SSH client to connect to the new port instead of the default port 22. For example, if your new SSH port is 4321, you would initiate a connection like so:
ssh username@123.456.78.999 -p 4321
In order to change from port 22 for ssh on CentOS, there are several additional steps you need to take due to SELinux. First, while you're still set to port 22, you'll want to back up your configuration:
$ cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
Go ahead and select the new port like you did before in /etc/ssh/sshd and save. Then you'll want to notify CentOS you are making the change and update the firewall rules to make sure the new port is open and not filtered:
$ yum install policycoreutils-python
$ semanage port -a -t ssh_port_t -p tcp $PORT
$ firewall-cmd --permanent --zone=public --add-port=$PORT/tcp
$ firewall-cmd --reload
$ systemctl restart sshd
Once this is complete, the new port is set. Check that everything saved properly:
$ ss -tnlp | grep ssh
You are looking for output like this:
LISTEN 0 128 *:$PORT *:* users:(("sshd",pid=$ID,fd=3))
LISTEN 0 128 :::$PORT :::* users:(("sshd",pid=$ID,fd=4))
You can verify this by checking the port from your local computer:
$ nmap -p $PORT $HOSTNAME