When working with remote Ubuntu servers, verifying port accessibility is crucial for various operations like SSH access, database connections, or web service deployments. Here's how to properly test port availability from your local machine to the remote server.
The most common tools for this task include:
# Basic connectivity check
ping remote-server.com
# Telnet (if available)
telnet remote-server.com 22
# Netcat (more reliable)
nc -zv remote-server.com 22
# Nmap (for comprehensive scanning)
nmap -Pn -p 22 remote-server.com
For SSH specifically (port 22), you can use these methods:
# Using SSH client with verbose output
ssh -vvv user@remote-server.com
# Quick timeout test
timeout 5 bash -c "</dev/tcp/remote-server.com/22" && echo "Port open" || echo "Port closed"
# Python alternative
python3 -c "import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); print(s.connect_ex(('remote-server.com',22)))"
For more detailed analysis of multiple ports:
# Scan common ports quickly
nmap -Pn -F remote-server.com
# Scan specific port range
nmap -Pn -p 20-100 remote-server.com
# UDP port scanning
nmap -Pn -sU -p 53,123 remote-server.com
Create a simple bash script to monitor critical ports:
#!/bin/bash
PORTS=(22 80 443 3306)
SERVER="remote-server.com"
for port in "${PORTS[@]}"
do
nc -zv $SERVER $port >/dev/null 2>&1 && \
echo "Port $port: OPEN" || \
echo "Port $port: CLOSED"
done
If ports appear closed but should be open:
- Check remote firewall:
sudo ufw status
- Verify service is running:
sudo ss -tulnp | grep :22
- Inspect network ACLs and security groups (AWS/GCP)
- Test from different networks to rule out local firewall
When working with Ubuntu servers in distributed environments, verifying port accessibility is a fundamental network troubleshooting task. This guide covers multiple methods to check if a specific port is open and reachable from your local machine to a remote Ubuntu system.
The simplest way to test port connectivity:
telnet remote_server_ip port_number
Example for checking SSH (port 22):
telnet 192.168.1.100 22
If the port is open, you'll see a response like:
Trying 192.168.1.100... Connected to 192.168.1.100. Escape character is '^]'. SSH-2.0-OpenSSH_8.9p1 Ubuntu-3
A more flexible alternative:
nc -zv remote_server_ip port_number
Example for checking MySQL port (3306):
nc -zv 192.168.1.100 3306
Successful output:
Connection to 192.168.1.100 3306 port [tcp/mysql] succeeded!
For comprehensive port analysis:
nmap -p port_number remote_server_ip
Example scanning HTTP/HTTPS ports (80, 443):
nmap -p 80,443 192.168.1.100
Sample output:
Starting Nmap 7.80 ( https://nmap.org ) at 2023-11-15 10:00 UTC Nmap scan report for server.example.com (192.168.1.100) PORT STATE SERVICE 80/tcp open http 443/tcp open https
If ports appear closed despite services running:
sudo ufw status sudo iptables -L -n
To allow a specific port through UFW:
sudo ufw allow port_number/tcp
When port checks fail:
- Verify the service is running:
sudo systemctl status service_name
- Check local firewall:
sudo ufw status
- Test with
ping
to confirm basic connectivity - Use
traceroute
to identify network path issues
Create a reusable script:
#!/bin/bash SERVER="192.168.1.100" PORTS=(22 80 443 3306) for PORT in "${PORTS[@]}"; do nc -zv $SERVER $PORT &> /dev/null if [ $? -eq 0 ]; then echo "Port $PORT is OPEN on $SERVER" else echo "Port $PORT is CLOSED on $SERVER" fi done