How to change what port SSH listens on

On most Linux servers you can find the SSH configuration file at /etc/ssh/sshd_config

Note: Upon updating SSH ports, be sure to add the port number to the firewall; otherwise, users will be locked out of the server.

Change Specific Parameters within sshd_config

To change specific parameters within sshd_config:

  1. Log into your server as the root user.
  2. Uncomment the desired line by removing the number-sign (#) and changing the value for the line.

    For example, the default SSH port appears in a line like this:

    #Port 22

    To change the SSH port to 456, you will need to make the line appear like this:

    Port 456


Update IPTables and Firewall

Note: Any changes made via GUI/TUI firewall will also change your server's iptables and may overwrite any manual changes made directly to the iptables file itself. This external link is to a discussion of the interactions between firewall programs and iptables (and will open in a new tab).

After you are finished configuring SSH, you will need to add the port to iptables as well as your installed firewall.


To update iptables, please enter the following in SSH:

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 456 -j ACCEPT


When using the command above, be sure to substitute 456 with the actual port number you are adding.


Reset SSH Daemon

After making the necessary changes to iptables and your computer's firewall, you will need to restart the SSH daemon. You can do so by issuing the following command:

/etc/init.d/sshd restart

Log out of your server and log in again using the proper user, IP address and port number you specified in sshd_config.

  • 3 Users Found This Useful
Was this answer helpful?

Related Articles

CentOS server fails to boot (after yum upgrade)

Issue After creating a CentOS 7 VM, running "yum update" and rebooting causes the VM to fail to...

Support

Colocation & Bandwidth Services  Phone: 888-328-2102 / 719-387-0000Email (ticket):...