Find custom SSH port number
Introduction
Secure Shell (SSH) is the standard protocol used by system administrators and DevOps engineers to securely access and manage Linux servers remotely. By default, SSH listens on port 22. However, many organizations configure SSH to use a custom port to reduce exposure to automated attacks and enhance security.
There are situations where administrators need to identify the configured SSH port, such as troubleshooting connection issues, validating server configurations, performing security audits, or onboarding new team members. This guide explains multiple methods to find a custom SSH port number on Linux systems quickly and efficiently.
Why Use a Custom SSH Port?
Although changing the SSH port is not a replacement for proper security controls, it provides an additional layer of protection by reducing noise from automated bots scanning port 22.
Common reasons for using a custom SSH port include:
- Reducing automated brute-force attacks
- Improving security through obscurity
- Meeting organizational security requirements
- Separating administrative access from standard services
Prerequisites
Before proceeding, ensure you have:
- Access to the Linux server
- Sudo or root privileges (recommended)
- Basic knowledge of Linux command-line operations
Method 1: Check the SSH Configuration File
The most reliable method is to inspect the SSH daemon configuration file.
View the SSH Port Configuration
sudo grep -i "^Port" /etc/ssh/sshd_configExample output:
Port 2222This indicates that SSH is configured to listen on port 2222.
Check Additional Configuration Files
Some Linux distributions include additional SSH configuration files.
sudo grep -Ri "^Port" /etc/ssh/Example output:
/etc/ssh/sshd_config:Port 2222
/etc/ssh/sshd_config.d/custom.conf:Port 2200Review all matching entries to determine the active configuration.
Method 2: Verify Listening SSH Ports
You can identify the active SSH port by checking which ports the SSH service is listening on.
Using ss Command
sudo ss -tulpn | grep sshExample output:
tcp LISTEN 0 128 0.0.0.0:2222 0.0.0.0:* users:(("sshd",pid=1001))The server is listening on port 2222.
Using netstat
If available:
sudo netstat -tulpn | grep sshExample output:
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 1001/sshdMethod 3: Use lsof to Find SSH Listening Ports
The lsof utility can display open network sockets.
sudo lsof -i -P -n | grep sshdExample output:
sshd 1001 root 3u IPv4 12345 TCP *:2222 (LISTEN)This confirms SSH is listening on port 2222.
Method 4: Check Running SSHD Process
View the SSH daemon process details:
ps -ef | grep sshdAlthough this may not directly display the port number, it helps verify that the SSH daemon is running correctly.
Method 5: Verify with Systemctl
Check the status of the SSH service.
For Ubuntu/Debian:
sudo systemctl status sshFor RHEL/CentOS/Rocky Linux:
sudo systemctl status sshdYou can also inspect logs for startup information:
sudo journalctl -u sshdor
sudo journalctl -u sshMethod 6: Test SSH Connectivity Locally
If you suspect a custom port, test connectivity using:
ssh -p 2222 localhostReplace 2222 with the suspected port.
Successful authentication confirms the SSH service is listening on that port.
Method 7: Find SSH Port Remotely
If you do not have direct server access, you can scan for open ports.
Using Nmap
nmap -sV <server-ip>Example output:
PORT STATE SERVICE VERSION
2222/tcp open ssh OpenSSH 9.0For a focused scan:
nmap -p 1-65535 <server-ip> | grep sshThis identifies the SSH service running on a non-standard port.
Troubleshooting Tips
SSH Service Not Running
Verify service status:
sudo systemctl status sshdStart the service if needed:
sudo systemctl start sshdFirewall Blocking the Port
Check firewall rules:
For firewalld:
sudo firewall-cmd --list-allFor UFW:
sudo ufw statusEnsure the custom SSH port is allowed.
SELinux Restrictions
On SELinux-enabled systems:
sudo semanage port -l | grep sshVerify the configured port is permitted for SSH traffic.
Security Best Practices
When using a custom SSH port:
- Disable password authentication where possible
- Use SSH key-based authentication
- Restrict root login
- Enable multi-factor authentication
- Configure firewall rules to limit SSH access
- Monitor authentication logs regularly
- Keep OpenSSH packages updated
Example SSH hardening settings:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yesConclusion
Identifying a custom SSH port is a common task for Linux administrators, especially when troubleshooting connectivity issues or reviewing server security configurations. The most effective approach is to inspect the SSH configuration file, while tools such as ss, netstat, lsof, and nmap provide additional verification. By understanding these methods and following SSH security best practices, administrators can maintain secure and reliable remote access to their Linux servers.
Whether you’re managing a single server or a large enterprise environment, knowing how to quickly determine the active SSH port is an essential skill that improves operational efficiency and security.
