Changing an SSH port requires changes to the listener and the network access rules. This example uses a service-managed RHEL-family server and an example new port of 2222.
Check the server first
Use an administrator account, keep the current session open and confirm access to a recovery console. Check that the proposed port is unused:
sudo ss -ltnpThis procedure assumes sshd.service manages SSH directly. It does not describe Ubuntu’s socket-activated setup.
Allow the new port
If firewalld is the active firewall, add the port to the zone handling your administration connection, replacing public where necessary:
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --add-port=2222/tcp
sudo firewall-cmd --permanent --zone=public --add-port=2222/tcpThe first add changes the running rules; the second preserves the allowance across a firewall reload. Update any provider firewall too. Do not enable firewalld alongside a different firewall manager just for this procedure.
On an SELinux-enforcing server, inspect existing port assignments:
sudo semanage port -lIf the new port is unassigned, add the SSH assignment:
sudo semanage port -a -t ssh_port_t -p tcp 2222If it already belongs to another type, investigate the conflict rather than reassigning it blindly. The semanage command requires the distribution’s policy management package.
Configure the listener
-
Back up
/etc/ssh/sshd_configand inspect its included files. -
Edit the applicable configuration to retain the existing port temporarily and add the new one:
Port 22 Port 2222 -
Check any
ListenAddressdirectives for an explicit conflicting port. -
Validate and reload:
sudo /usr/sbin/sshd -t sudo systemctl reload sshd sudo systemctl status sshd
Do not reload if validation reports errors.
Verify a new connection
From another terminal:
ssh -p 2222 [email protected]Use the real server details. Confirm login and administrator access before removing the old port from the configuration and firewalls. Validate and test again after cleanup while keeping the recovery route available.