Changing the SSH port number on systems such as Debian, Ubuntu and Cent OS should be considered as an additional security measure to prevent unauthorized access.
When you buy a Linux VPS server, the VPS service provider provides it with the default SSH port 22 or with a dedicated port, so changing the SSH port to a non-standard port increases SSH security to the highest degree.
Here is a brief tutorial on how to change SSH port on Debian, Ubuntu and CentOS.
Here is a detailed step-by-step guide on how to change the SSH port on all Linux systems in the shortest possible time:
Choose the new port number which is free of use by any other service. Now, let’s take this into our context by using 6065 as the fresh SSH port.
Try to find out if the given port (6065 in our example) is not occupied by another application that is running.
sudo lsof -i -P -n | grep LISTEN
Make sure that the service that uses port 6065 has not already been in use.
Set firewall rules to allow incoming SSH connections on new port instead of the old one.
sudo iptables -I INPUT -p tcp --dport 6065 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
This command lets in the traffic of TCP on new port and does this process by the firewall.
Open the SSH server configuration file (sshd_config) and change the SSH port to the new one.
sudo nano /etc/ssh/sshd_config
Find the commented-out line with the SSH port set-up (usually port 22) and change it to read your new port.
Port 6065
If there is a # symbol at the beginning of the line, delete it. Then, use CTRL + x and enter y to save and exit.
Restart the SSH service by issuing the following commands so as to apply the changes made in the configuration file.
sudo systemctl restart sshd
You can now connect to Linux server through Putty or another ssh connection software using the new port.
This is the easiest way to change the port on Linux server, you can easily change the port of your Linux server for secure connection
The SSH port is, therefore, the main port that allows you to connect to a server through the SSH protocol using cryptographic algorithms. Without any manual interference, SSH servers are configured to connect on port 22. It is like the particular address of the server on the Internet: when you want to manage your server by connecting remotely, your SSH client will get the server through this port.
Changing the default SSH port from 22 to another number is often considered an Applicable Security practice. Why? You see, port 22 is very famous, and when these bots try to automatically break into the computers, it is often seen.
A selection of a non-standard port number for the SSH server, such as 2222, for example, should make it more difficult for the automatic scripts to find your SSH server.
Though port redirection isn’t a full solution security-wise, it’s not the be-all and end-all of security. However, it is the fundamental key. Some other important precautions include using passwords such as SSH keys for authentication, configuring the firewall to restrict access, and ensuring periodic software updates.
Normally, the SSH port change adds an additional layer of privacy and requires an attacker to perform more powerful attacks manually. However, port changing is effective only when applied together with other best security practices.
Changing the SSH port on a Linux system can help enhance security by reducing automated attacks. Here are tips for safely changing the SSH port:
You may also use different versions of the Linux server, that’s why we have separately provided the commands for each Linux operating system, CentOS, RHEL, Fedora, Ubuntu, etc.
Below is a detailed step-by-step tutorial on how to change the SSH port in CentOS, RHEL (Red Hat Enterprise Linux), or Fedora, including handling SELinux and firewall configurations:
First, check if SELinux is enabled on your system:
sudo status
If SELinux is enabled, you will see some output stating its status enabling or disabling. In this case, proceed with the following steps. If you think it is disabled, jump to Step 4 or directly proceed to Step 4.
Use semanage to introduce the new SSH port (e.g., 3333) to SELinux’s list of allowed SSH ports (ssh_port_t):, 3333) to SELinux’s list of allowed SSH ports (ssh_port_t):
sudo semanage port -a -t ssh_port_t -p tcp 3333
This command allows SSH connections on port 3333 through SELinux.
Subsequently, enable the firewall to allow incoming SSH port (3333). Assume that you are operating within the public zone:
sudo firewall-cmd --zone=public --add-port=3333/tcp --permanent
This command configures the firewall to open port #3333 to the public domain.
Reload the firewall to apply the new configuration changes
sudo firewall-cmd --reload
This command forces the firewall service to reload with the new configuration.
Now, go to the SSH server configuration file (sshd_config) to alter it and give the new SSH port as well.
sudo nano /etc/ssh/sshd_config
Find the line that mentions the SSH port (often commented with #Port 22) and change it to use the new port (3333).
Port 3333
Write the changes, press CTRL + O to save, press Enter to confirm, and press CTRL + X to exit the text editor.
Finally, test the SSH connection using the updated port (3333 in this example):
ssh username@server_ip -p 3333
Here’s a detailed step-by-step guide on how to change the SSH port in Ubuntu, Debian, or Kali Linux:
Specify a new port number that is not normally associated with other services. Let’s take 2222 as a new SSH port.
Allow incoming connections on the new SSH port (2222) using UFW:
ufw allow 2222
This command allows the firewall to accept TCP traffic on a particular port, for example, port 2222.
Modify the SSH server configuration file (sshd_config) to specify the new SSH port.
sudo nano /etc/ssh/sshd_config
Find the line that indicates the SSH port (Port 22 is already commented out with #Port 22) and edit it to use the new port (2222).
Port 2222
Save the settings and the quit text editor (CTRL + O to write changes, Enter to confirm, CTRL + X to exit).
Finally, test the SSH connection using the updated port (2222 in this example):
ssh username@server_ip -p 2222
Use your actual username and server_ip as variations, where the server_ip is either the IP address or the hostname of the server.
Here are some additional security measures and tips to further enhance the security of your Linux server:
SSH security is crucial for several reasons:
changing the SSH port on Linux systems like Debian, Ubuntu, and CentOS is a good decision for user security since it reduces the exposure of the server to automated attacks launched at the default SSH port (port 22).
The SSH could be setup to listen on an alternate port that is non-standard (ports like 1021 or 8310). In other words, by replacing the “2222” in the address with the star, you add an extra layer of secrecy that can stop unauthorized access attempts.
Nevertheless, you should pair port modification with other security practices such as the use of strong passwords or SSH keys, implementation of firewall rules, and constantly updating system software in order to have a robust security posture.