Changing the SSH Server Port Number
By default, SSH servers listen on port 22. For security reasons, or when required by third-party services, you may want to change this port number. This guide explains how to safely update the SSH port on your Server.
Steps to Change the SSH Port
Section titled “Steps to Change the SSH Port”Step 1: Log in as Root
Section titled “Step 1: Log in as Root”Connect to your server using SSH on the current port (default is 22):
ssh root@yourdomain.com -p 22Step 2: Edit the SSH Configuration File
Section titled “Step 2: Edit the SSH Configuration File”-
Open the SSH configuration file in a text editor:
nano /etc/ssh/sshd_config -
Locate the line:
Port 22 -
Change
22to your desired port number (e.g.,7822).Port 7822 -
Save and exit the file.
Step 3: Update Firewall Rules
Section titled “Step 3: Update Firewall Rules”If your server has a firewall enabled, you must allow the new port:
-
iptables example:
Terminal window iptables -A INPUT -p tcp --dport 7822 -j ACCEPTservice iptables save -
firewalld example (CentOS/AlmaLinux):
Terminal window firewall-cmd --permanent --add-port=7822/tcpfirewall-cmd --reload -
ufw example (Ubuntu/Debian):
Terminal window ufw allow 7822/tcpufw reload
Step 4: Restart the SSH Service
Section titled “Step 4: Restart the SSH Service”Restart SSH to apply the changes:
-
On AlmaLinux/Fedora:
Terminal window service sshd restart -
On Debian/Ubuntu:
Terminal window service ssh restart
Step 5: Test the New Port
Section titled “Step 5: Test the New Port”Open a new terminal window and try connecting with the new port:
ssh root@yourdomain.com -p 7822Do not close your original session until you confirm the new connection works.