In this tutorial, we will show you how to change the default ssh port on the CentOS system. For those of you who didn’t know, SSH is a program and protocol for securely connecting to remote machines across a network. It allows you to run programs, and do a variety of tasks as if you were sitting at the machine. SSH is very similar to telnet except it is with encryption to protect the transferred information and authentication.
The Secure Shell (SSH) Protocol by default uses port 22. Accepting this value does not make your system insecure, nor will changing the port provide a significant variance in security. However, changing the default SSH port will stop many automated attacks and a bit harder to guess which port SSH is accessible from
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The tutorial is quite simple. I will show you the step-by-step change of the default SSH port on the CentOS system.
Prerequisites
- A server running one of the following operating systems: CentOS or RHEL-based.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- Before changing the SSH port, it’s always a good idea to take a backup of the SSH configuration file in case something goes wrong.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, you can harm your system if you’re not careful when acting as the root.
Change SSH Port in CentOS
Step 1. Log into your server as the root user.
ssh root@hostname/IP
Step 2. Open our SSH configuration.
Open the sshd_config
file using a text editor. The sshd_config file is the main configuration file for the OpenSSH server:
nano /etc/ssh/sshd_config
Find the line that reads “#Port 22” and remove the “#” at the beginning of the line to uncomment it.
# What ports, IPs and protocols we listen for Port 1080
Change the value “22” to a port number of your choice. We recommend using a number between 1024 and 65535 that is not currently being used by any other service.
Save and close the file, then restart the SSH service to apply the changes:
sudo systemctl restart sshd
Step 3. Configuring Firewall.
You need to configure the firewall to allow traffic to the new port. If you’re using firewalld, you can use the following commands to add a new rule to allow traffic on the new SSH port:
sudo firewall-cmd --permanent --zone=public --add-port=1080/tcp sudo firewall-cmd --reload
If you’re using iptables, you can add a new rule to allow traffic on the new SSH port using the following command:
sudo iptables -A INPUT -p tcp --dport 1080 -j ACCEPT
Step 4. Testing the New SSH Port.
To test the new SSH port, you can use the SSH command with the -p option to specify the new port number. For example, if you changed the SSH port to 1080, you can use the following command to establish an SSH connection:
ssh username@hostname.com -p 1080
Congratulations! You have successfully changed the default SSH port. Thanks for using this tutorial to change the OpenSSH port number on CentOS systems. For additional help or useful information, we recommend you check the official CentOS website.