How to change the SSH server port number
This article describes how to change the port number for an SSH server. You may want to do this for security reasons, for example, or if you use a third-party service that requires a specific SSH port number.
The following procedure only applies to semi-managed accounts. Shared and reseller hosting accounts do not have root access to the server and cannot change the SSH port number.
Changing the port for the SSH server
To change the port for the SSH server, follow these steps:
- Log in to the server as root using SSH.
- Open the /etc/ssh/sshd_config file in your preferred text editor (nano, vi, etc.).
- Locate the following line:
Remember that for security reasons, A2 Hosting uses port 7822 for SSH connections instead of the default port 22.
- Change 7822 to the new port number that you want to use.
- Save the changes to the /etc/ssh/sshd_config file, and then exit the text editor.
Restart the SSH service using the appropriate command for your Linux distribution:
While still logged in as root, in a new terminal window try to log in using the new SSH port number. If the login fails, check your settings. Do not exit your open root session until you are able to log in using the new configuration.
If you have a firewall configured for your server, you must also update the firewall rules to allow access on the new SSH port:
- For information about how to update a firewall using iptables, please see this article.
- For information about how to update a firewall using Advanced Policy Firewall (APF), please see this article.
- Using SSH (Secure Shell)
Secure Shell (SSH) provides a secure way for you to access your account from the command line. Read this article to learn how to set up and use an SSH client on a variety of operating systems.
- Using SSH keys
After you have set up and used an SSH client, you can optionally set up SSH keys. You can use SSH keys to set up automated SSH logins, or two-factor authentication for increased security.
- Disabling SSH logins for root
For security reasons, one of the first things you should do with a new VPS or dedicated server is disable SSH (Secure Shell) logins for the root account. This article shows you how.