How to Check if a Port is Open or in Use on Ubuntu 8.04 using Command Line


1 views

The netstat command is one of the most straightforward ways to check port availability:


netstat -tuln | grep ':80'

This checks if port 80 is in use. Flags explanation:

  • -t shows TCP ports
  • -u shows UDP ports
  • -l shows listening ports
  • -n shows numeric addresses

For more detailed information about which process is using a port:


sudo lsof -i :22

Sample output might show:


COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
sshd    1234 root    3u  IPv4  12345       TCP *:22 (LISTEN)

To test if a port is open and accepting connections:


telnet localhost 80

If the port is available but no service is listening, you'll see:


Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Use nmap to scan ports on remote systems:


nmap -p 80 example.com

Sample output:


PORT   STATE SERVICE
80/tcp open  http

Here's a bash script to check multiple ports:


#!/bin/bash
ports=(80 443 22 3306)

for port in "${ports[@]}"
do
    if netstat -tuln | grep ":$port " >/dev/null; then
        echo "Port $port is in use"
    else
        echo "Port $port is available"
    fi
done

Remember that even if a port isn't in use locally, firewall rules might block it:


sudo iptables -L -n -v | grep 80

When working with network applications or services on Ubuntu 8.04, it's essential to verify whether a specific port is available before binding a service to it. This prevents conflicts and ensures smooth operation. Below, we'll explore several methods to check port availability from the command line.

The netstat command is a classic tool for network statistics. To check if a port (e.g., port 80) is in use:

netstat -tuln | grep :80

If the port is in use, you'll see output like:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN

No output means the port is available.

On modern systems, ss is faster than netstat. For Ubuntu 8.04:

ss -tuln | grep :80

This provides similar output to netstat but with better performance.

The lsof command lists open files and can identify processes using specific ports:

sudo lsof -i :80

Example output:

COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
apache2   123   root    3u  IPv4   1234      0t0  TCP *:http (LISTEN)

To test if a port is open and accepting connections:

telnet localhost 80

If the port is open, you'll see:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

A "Connection refused" message indicates the port is not in use.

For scripting purposes, this Bash function checks port availability:

is_port_available() {
    local port=$1
    if netstat -tuln | grep -q ":$port "; then
        echo "Port $port is in use"
        return 1
    else
        echo "Port $port is available"
        return 0
    fi
}

Usage:

is_port_available 80

To verify if a remote port is open (useful for testing services):

nc -zv remote_host 80

Successful output looks like:

Connection to remote_host 80 port [tcp/http] succeeded!

Remember that even if a port isn't in use locally, firewall rules might block external access. Check Ubuntu's firewall with:

sudo iptables -L -n