How do I change the default SSH port

Avatar Linode 4 months ago

I'm trying to change the default SSH port. How do I do that?

4 Replies

Avatar dsmith 4 months ago Linode Staff

You can change the SSH port by editing your sshd_config. That file is located here:


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.

Do not forget to allow your new ssh port in iptables before you change it.

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 sshportt -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


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct