How to Change the SSH Port on Dedicated and VPS
You can find the SSH configuration file at /etc/ssh/sshd_config
Change Specific Parameters within sshd_config
To change specific parameters within sshd_config:
- Log into your server as the root user.
- 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
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.