Prerequisites
Before changing SSH port, make sure you have:
- SSH access to your VPS
- Root or sudo privileges
- A firewall (UFW) configured to allow the new port
- Your new port number (between 1024 and 65535, avoid common ports)
⚠️ Keep your current SSH session open while testing the new port! If something goes wrong, you can revert from the existing session.
Step 1: Choose a New Port Number
Recommended ports to avoid:
- 22 (SSH - too common, bots scan this)
- 80 (HTTP), 443 (HTTPS), 21 (FTP), 25 (SMTP)
Good port choices:
- 2222, 22222, 222222
- 2022, 3022, 4022, 5022
- Random high port like 54321, 49152-65535
Step 2: Connect to Your VPS
ssh hxroot@YOUR_SERVER_IP -p 22
Step 3: Update Firewall (If Using UFW)
First, allow the new port:
sudo ufw allow 2222/tcp
DO NOT close port 22 yet until you test the new port.
Step 4: Edit SSH Configuration
sudo nano /etc/ssh/sshd_config
Find the line:
#Port 22
Change it to (remove # and change port number):
Port 2222
You can also keep both ports active temporarily:
Port 22
Port 2222
Step 5: Save and Exit
In nano: Ctrl+X, then Y, then Enter.
Step 6: Restart SSH Service
sudo systemctl restart sshd
If using both ports, restart with:
sudo systemctl restart ssh
Step 7: Test New Port (Without Closing Old Session)
Open a new terminal window and test:
ssh hxroot@YOUR_SERVER_IP -p 2222
If successful, you can now remove port 22.
Step 8: Remove Old Port (Optional)
Once confirmed new port works:
sudo nano /etc/ssh/sshd_config
Remove or comment out the Port 22 line:
#Port 22
Port 2222
sudo systemctl restart sshd
Step 9: Update Firewall to Remove Port 22
sudo ufw delete allow 22/tcp
Verify firewall rules:
sudo ufw status
Step 10: Update Your SSH Client Configuration
On your local machine, update or create SSH config:
nano ~/.ssh/config
Add:
Host hostxpeed-vps
HostName YOUR_SERVER_IP
Port 2222
User hxroot
Now you can connect with:
ssh hostxpeed-vps
For CentOS/Rocky/AlmaLinux (SELinux)
If SELinux is enabled, also run:
sudo semanage port -a -t ssh_port_t -p tcp 2222
If You Get Locked Out
If new port doesn't work:
- Use your original SSH session that is still open
- Revert changes in /etc/ssh/sshd_config
- Restart SSH
If no session open, use Hostxpeed portal console:
- Log in to Hostxpeed Client Area
- Go to My Services → Your VPS
- Use "Console Access" to connect
- Revert SSH config changes
Check What Port SSH is Listening On
sudo netstat -tulpn | grep sshd
Or:
sudo ss -tulpn | grep sshd
Example output:
tcp LISTEN 0 128 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=1234,fd=3))
Benefits of Changing SSH Port
- Reduces automated brute force attacks
- Decreases log noise from failed login attempts
- Adds security through obscurity (but not a complete solution)
✅ Your SSH port has been changed. Remember to always specify -p PORTNUMBER when connecting or update your SSH config file.