How to Change the SSH Port on a Linux Server

By default, SSH runs on port 22, which is commonly targeted by automated bots and brute-force attacks. Changing the SSH port is a simple yet effective security hardening step for any Linux server.

Step 1: Edit the SSH Configuration File

Open the SSH daemon configuration file:

nano /etc/ssh/sshd_config

Look for the following line:

#Port 22

Uncomment it and change the port number (example: 2222):

Port 2222

Save and exit the file.

Step 2: Validate the SSH Configuration

Before restarting SSH, validate the configuration:

sshd -t

If there is no output, the configuration is valid.

Check exit status:

echo $?
 
Expected output:

0

Step 3: Restart the SSH Service

For AlmaLinux / Rocky Linux / CentOS / RHEL

systemctl restart sshd

For Ubuntu / Debian

systemctl restart ssh

 Step 4: Verify SSH Is Listening on the New Port

ss -tulpn | grep ssh
 
Expected output:
 
LISTEN 0 128 0.0.0.0:2222
 

If you still see port 22, the configuration was not applied correctly.

Step 5: Allow the New Port in Firewall

Raise ticket with us to allow any port in firewall.

Step 7: Test SSH Connection (Very Important)

Do not close your current SSH session yet.

Open a new terminal and connect using the new port:

ssh root@SERVER_IP -p 2222
 

If the login is successful, the new SSH port is working correctly.

 
  • linux, linux server, linux vps, Linux
  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

VPS Server - First time user

Hi All,This blog is for first time VPS users. Please follow the below steps to check your VPS....

Backup restoration

Since backups are saved at various nodes to avoid data loss, we manage the backup and...

How To allow SQL Express traffic in windows firewall

Please follow the below steps to allow SQL express communication in the firewall. Open SQL...

How to change SQL authentication in SQL Express

Please follow the below steps Login to SQL Express using SQL management studio. Right-click...

Set Up Sending of Email in Plesk

To Set Up Sending of Email in Plesk, You need to follow the below Points   1. Go to Tools...