How to Safely Change SSH Port in AlmaLinux (e.g., to 2022)
In this guide
- Step 1: Choose a New SSH Port
- Step 2: Edit SSH Configuration File
- Step 3: Open Port 2022 in Your Firewall
- ✅ If you have CSF Firewall (common in cPanel servers):
- ✅ If you have Imunify360 installed:
- ✅ If you use firewalld (the AlmaLinux default):
- ✅ If you don’t have CSF or Imunify360 (iptables only):
- Step 4: Restart SSH Service
- Step 5: Test the New Port
- Step 6 (Optional): Remove Old Port 22
- ✅ Done!
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 use firewalld (the AlmaLinux default):
Run these commands as root:
firewall-cmd --permanent --add-port=2022/tcp firewall-cmd --reload
✅ 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:
dnf install iptables-services -y systemctl enable iptables systemctl start iptables
SELinux (enabled by default on AlmaLinux): allow the new port before you restart SSH, otherwise sshd cannot start on it:
semanage port -a -t ssh_port_t -p tcp 2022
If the semanage command is not found, install it first with dnf install -y policycoreutils-python-utils.
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
Keep reading
Counting .tar.gz Files in a Directory on AlmaLinux
Three simple command-line ways to count .tar.gz files in a directory on AlmaLinux: ls with wc, find with wc, and grep.
How to Install Imunify360 with a 15-Day Trial on AlmaLinux?
Imunify360 is a comprehensive security solution designed to protect Linux servers from various threats. This guide will take you through obtaining a trial license,...
How to Install JetBackup 5 using terminal on AlmaLinux?
JetBackup 5 is a powerful backup solution for your AlmaLinux VPS or dedicated server. This guide will walk you through the installation and configuration process.