SSH runs on port 22 by default, which makes it a primary target for automated brute-force attacks. Changing this port reduces your server’s exposure to these attacks by removing it from the most commonly scanned ports.
You will update the SSH configuration file, select a new port number and restart the SSH service. This prevents most automated scanning tools from finding your SSH service while maintaining secure remote access for legitimate users.
The SSH daemon reads its configuration from /etc/ssh/sshd_config. You will edit this file to specify a new port number, which tells SSH where to listen for incoming connections.
cd /etc/ssh to move into the directory containing the SSH configuration files.nano sshd_config to open the file in the nano text editor. You can use vim or vi if you prefer.Ctrl+W to open the search function in nano. Type Port and press Enter. This locates the line that controls which port SSH uses.22 with a port number between 1025 and 65535. Ports below 1024 are reserved for system services. Choose a number that does not conflict with other services running on your server. If you use specific applications that require SSH access, check their documentation for port requirements.Ctrl+X to exit nano. Press Y when prompted to save changes, then press Enter to confirm the filename.Your SSH configuration now specifies the new port. The change takes effect after you restart the SSH service.
Restarting the SSH service applies your configuration changes. The service will stop listening on port 22 and start listening on your new port instead.
systemctl restart sshd to restart the service. This applies your port change immediately.systemctl status sshd to check the service status. You should see active (running) in the output, which confirms SSH restarted correctly.ssh -p [your-port] user@your-server-ip, replacing [your-port] with your new port number, user with your username and your-server-ip with your server’s IP address. If the connection succeeds, your port change worked correctly.Keep your original SSH session open until you confirm the new port works. This prevents you from being locked out if something goes wrong.
Your firewall needs to allow traffic on the new SSH port. Without this rule, connections to your new port will be blocked even though SSH is listening.
firewall-cmd --permanent --add-port=[your-port]/tcp, replacing [your-port] with your new port number. This allows TCP connections on your chosen port.firewall-cmd --reload to apply the new rule. The firewall now permits SSH connections on your new port.firewall-cmd --permanent --remove-service=ssh to stop allowing connections on port 22. Run firewall-cmd --reload again to apply this change.Your firewall configuration now matches your SSH configuration. Port 22 is closed and your new port accepts SSH connections.
Your AlmaLinux server now runs SSH on a custom port instead of the default port 22. You updated the SSH configuration, restarted the service and configured your firewall to allow connections on the new port. This reduces your exposure to automated brute-force attacks that target the default SSH port.
Update any SSH clients, deployment scripts or monitoring tools to use your new port. Store your port number securely so you can access your server when needed. Our guide on securing your VPS covers additional hardening steps including key-based authentication and fail2ban configuration.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting