[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedSeptember 7, 2023

How to Change the SSH Port.

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:

sudo ufw allow 5522/tcp

In CentOS, the default firewall management tool is FirewallD. To open the new port run:

sudo firewall-cmd --permanent --zone=public --add-port=5522/tcpsudo firewall-cmd --reload

CentOS users also need to adjust the SELinux rules:

sudo semanage port -a -t ssh_port_t -p tcp 5522

If you are using iptables as your firewall, to open the new port, run:

sudo iptables -A INPUT -p tcp --dport 5522 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

 

3. Configuring SSH

Open the SSH configuration file /etc/ssh/sshd_config with your text editor:

sudo vim /etc/ssh/sshd_config

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:

Port 5522

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:

sudo systemctl restart ssh

In CentOS the ssh service is named sshd:

sudo systemctl restart sshd

To verify that SSH daemon is listening on the new port 5522, type:

ss -an | grep 5522

The output should look something like this:

tcp   LISTEN      0        128            0.0.0.0:5522           0.0.0.0:*
tcp   ESTAB       0        0      XX.XX.XX.XX:5522     XX.XX.XX.XX:57638
tcp   LISTEN      0        128               [::]:5522              [::]:*

 

Using the New SSH Port

To specify the port, invoke the ssh command followed by the -p <port_number> option:

ssh -p 5522 [root-test-liviu]

If you are regularly connecting to multiple systems, you can simplify your workflow by defining all of your connections in the SSH config file .

 

Conclusion

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.

Keep reading
How to use who command.
August 14, 2023

How to use who command.

How To Use Who Command. In this article, we explain the who command that is bundled in GNU coreutils package. who is a command-line utility that prints a list of currently logged in users. It can also show the current run level, time of the last system boot, and more. How to Use the…

Read article
How to use basename command.
August 14, 2023

How to use basename command.

How To Use Basename Command. basename is a command-line utility that strips directory and trailing suffix from given file names. Using the basename Command The basename command supports two syntax formats: basename NAME [SUFFIX] basename OPTION… NAME… basename takes a filename and prints the last…

Read article
How to use chattr command in Linux.
August 14, 2023

How to use chattr command in Linux.

How To Use Chattr Command In Linux. This article explains how to use the chattr command to change file attributes on Linux file systems. In Linux, file attributes are meta-data properties that describe the file’s behavior. For example, an attribute can indicate whether a file is compressed or…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00