SSH Port Number: How to Check and Change It in Linux Print

  • 1

Linux Administration ⏱ 4–5 min read

SSH Port Number: How to Check and Change It in Linux

Learn how SSH port 22 works, check your server's SSH configuration, and change the port using simple Linux commands.

⚡ Quick answer: The default SSH port is 22/TCP. SSH enables secure remote access to Linux servers, including VPS and dedicated servers.

What Is an SSH Port Number?

Secure Shell (SSH) is an encrypted protocol that allows administrators to log in to remote servers, execute commands, and transfer files securely. By default, SSH listens on TCP port 22, unless the server administrator configures a different port.

1. How to Check the SSH Port

To display the effective SSH port configuration, run:

Check configured port
sudo sshd -T | grep '^port'

To check which TCP ports are listening for SSH:

Check listening sockets
sudo ss -tlnp | grep sshd

2. How to Change the SSH Port

If you want SSH to use a custom port, follow these steps carefully.

Step 1Back Up the Configuration

Create backup
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

Step 2Edit the SSH Configuration

Open configuration file
sudo nano /etc/ssh/sshd_config

Find the existing Port directive. Set it to an available port, for example:

Example configuration
Port 5522

Save the file and exit the editor.

Step 3Allow the Port Through the Firewall

For UFW-based systems:

Ubuntu / UFW
sudo ufw allow 5522/tcp

For systems using FirewallD:

AlmaLinux / Rocky Linux / FirewallD
sudo firewall-cmd --permanent --add-port=5522/tcp
sudo firewall-cmd --reload

Step 4Validate the Configuration

Check for configuration errors
sudo sshd -t

Step 5Restart the SSH Service

Ubuntu and Debian:

Ubuntu / Debian
sudo systemctl restart ssh

AlmaLinux, Rocky Linux, and CentOS:

RHEL-based distributions
sudo systemctl restart sshd

Step 6Connect Using the New Port

Connect to your server
ssh -p 5522 username@server_ip
⚠ Important security tip: Before restarting SSH, allow the new port through the server firewall, provider firewall, and any network security groups. On SELinux-enabled systems, configure the port policy if required. Keep your current session open until you verify that a new connection works. Systems using SSH socket activation may require additional configuration.

3. Common SSH Connection Problems

❌ Connection timed out
Check firewall rules, network connectivity, and provider-level port filtering.
Connection refused
Check whether the SSH service is running and listening on the configured port.
Permission denied
Verify the username, SSH key or other authentication method, and account permissions.

Conclusion

SSH port 22 is the default for secure remote Linux administration. You can check or change the port using standard Linux commands, provided the SSH configuration and firewall rules are updated correctly.

For stronger security: use SSH keys, restrict access to trusted IP addresses where practical, and keep your server software updated. Changing the port alone does not replace these security measures.

Was this answer helpful?

« Back