Changing the default SSH port (22) helps protect your server from automated attacks. In this guide, we’ll show you how to change the SSH port safely and open the new port in your firewall — whether you use CSF, Imunify360, or just iptables.
Step 1: Choose a New SSH Port
Pick a port number between 1024 and 65535. In this example, we’ll use 2022.
Step 2: Edit SSH Configuration File
Log in to your server as root using SSH and run the following:
nano /etc/ssh/sshd_config
Find the line that says:
#Port 22
Change it to this:
Port 2022
Save and exit the file (press Ctrl + X, then Y, then Enter).
Step 3: Open Port 2022 in Your Firewall
Your server may be using different firewalls. Check below and follow the one that matches your setup:
✅ If you have CSF Firewall (common in cPanel servers):
You can do this either by terminal or from WHM panel.
Option 1: Using Terminal
nano /etc/csf/csf.conf
Find and update these two lines by adding 2022:
TCP_IN = "... ,22,2022,..." TCP_OUT = "... ,22,2022,..."
Then restart CSF:
csf -r
Option 2: Using WHM Dashboard (Beginner Friendly)
- Log in to WHM as root.
- Search for “ConfigServer Security & Firewall” and open it.
- Click “Firewall Configuration.”
- Find the fields named TCP_IN and TCP_OUT.
- Add 2022 to both, separated by commas.
- Scroll down and click “Change.”
- Click “Restart csf+lfd” to apply the changes.
✅ If you have Imunify360 installed:
Imunify360 usually works with CSF or iptables. If you’ve already added the port using CSF (as shown above), you’re good. But to make sure changes are loaded:
Option 1: Using Terminal
imunify360-agent firewall reload imunify360-agent firewall status
Option 2: Using WHM Panel
- Log in to WHM as root.
- Search for “Imunify360” and open it.
- Go to the “Firewall” tab.
- Look for any options to allow custom ports or confirm the port 2022 is active.
✅ If you don’t have CSF or Imunify360 (iptables only):
Run the following commands to allow port 2022:
iptables -I INPUT -p tcp --dport 2022 -j ACCEPT service iptables save
If that doesn’t work, install iptables-services:
yum install iptables-services -y systemctl enable iptables systemctl start iptables
Step 4: Restart SSH Service
Apply the new port by restarting the SSH service:
systemctl restart sshd
Step 5: Test the New Port
Do NOT close your current SSH session yet. Open a new terminal and test your new port:
ssh -p 2022 root@your_server_ip
If the login works, you’re good to go!
Step 6 (Optional): Remove Old Port 22
Once you confirm that port 2022 is working, you can remove port 22 to block unwanted access.
With CSF: (via terminal or WHM)
- Edit TCP_IN and TCP_OUT and remove 22.
- Then restart CSF again.
With iptables:
iptables -D INPUT -p tcp --dport 22 -j ACCEPT service iptables save
✅ Done!
You’ve successfully changed the SSH port and secured your server. Always test carefully and keep access to your hosting panel in case you need to roll back.
– Changed SSH config file to new port 2022
– Opened the port in firewall (CSF, Imunify360, or iptables)
– Restarted SSH service and tested access
– Optionally removed port 22 after verifying success