How To Change The SSH Port.
This tutorial explains how to change the default SSH port in Linux. We will also show you how to configure your firewall to allow access to the new SSH port.
By default, SSH listens on port 22. Changing the default SSH port adds an extra layer of security to your server by reducing the risk of automated attacks.
Changing the SSH Port
Changing the SSH port of an image is a simple task. All you need to do is to edit the SSH configuration file and restart the service.
The following sections explain how to change the SSH Port on a Linux system.
1.Choosing a New Port Number
In Linux, port numbers below 1024 are reserved for well-known services and can only be bound to by root. Although you can use a port within a 1-1024 range for the SSH service to avoid issues with port allocation in the future, it is recommended to choose a port above 1024.
In this example will change the SSH port to 5522, you can choose any port you want.
2. Adjusting Firewall
Before changing the SSH port, you’ll need to adjust your firewall to allow traffic on the new SSH port.
If you are using UFW, the default firewall configuration tool for Ubuntu, run the following command to open the new SSH port:
In CentOS, the default firewall management tool is FirewallD. To open the new port run:
CentOS users also need to adjust the SELinux rules:
If you are using iptables as your firewall, to open the new port, run:
3. Configuring SSH
Open the SSH configuration file /etc/ssh/sshd_config with your text editor:
Search for the line starting with Port 22. In most cases, this line starts with a hash (#) character. Remove the hash # and enter the new SSH port number:
Be extra careful when modifying the SSH configuration file. The incorrect configuration may cause the SSH service to fail to start.
Once done, save the file and restart the SSH service to apply the changes:
In CentOS the ssh service is named sshd:
To verify that SSH daemon is listening on the new port 5522, type:
The output should look something like this:
Using the New SSH Port
To specify the port, invoke the ssh command followed by the -p <port_number> option:
If you are regularly connecting to multiple systems, you can simplify your workflow by defining all of your connections in the SSH config file .
In this tutorial, you have learned how to change the SSH port on a Linux server. You should also set up an SSH key-based authentication and connect to your Linux servers without entering a password.