Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux Port And Query Method

In Linux, understanding how to query and interact with network ports is crucial for troubleshooting and monitoring network services. This tutorial will introduce you to various methods for querying and interacting with network ports in a Linux system.

  • Understanding Network Ports

A network port is a unique number assigned to a specific service or application to facilitate communication over a network. Port numbers range from 1 to 65535 and are divided into three ranges:

  • Well-known ports: 1-1023 (reserved for widely used services like HTTP, FTP, and SSH)
  • Registered ports: 1024-49151 (assigned by IANA for specific applications)
  • Dynamic or private ports: 49152-65535 (available for ephemeral connections)
  • netstat Command

netstat is a versatile command that provides information about network connections, routing tables, and interface statistics. To list all listening ports and associated services, use:

netstat -tuln
  • -t lists TCP connections
  • -u lists UDP connections
  • -l lists listening sockets
  • -n shows numerical addresses and port numbers
  • ss Command

ss is a more powerful and faster replacement for netstat. It displays socket statistics and can provide information about network connections, including listening ports. To list all listening ports, use:

ss -tuln
  • -t lists TCP connections
  • -u lists UDP connections
  • -l lists listening sockets
  • -n shows numerical addresses and port numbers
  • lsof Command

lsof (List Open Files) is another versatile command that can display information about open files, including network connections. To list all listening ports, use:

sudo lsof -i -P -n | grep LISTEN
  • -i lists all network connections
  • -P shows port numbers instead of service names
  • -n shows numerical addresses
  • grep LISTEN filters only the listening ports
  • nmap Command

nmap is a powerful network scanning tool that can discover open ports and services on a remote host. To install nmap, use:

sudo apt install nmap  # Debian/Ubuntu-based distributions
sudo yum install nmap  # CentOS/RHEL-based distributions
sudo dnf install nmap  # Fedora-based distributions

To scan a remote host for open ports, use:

nmap example.com

Replace example.com with the target hostname or IP address.

  • nc Command (netcat)

nc (netcat) is a versatile utility for reading and writing to network connections. You can use it to check if a specific port is open on a remote host by using:

nc -zv example.com 80

Replace example.com with the target hostname or IP address, and 80 with the port number you want to check.

In conclusion, Linux provides various tools for querying and interacting with network ports. Understanding how to use these tools is essential for troubleshooting and monitoring network services in your system.

  1. Port scanning tools for Linux: There are various port scanning tools for Linux, such as nmap. To install and use nmap for a basic port scan:

    sudo apt-get update
    sudo apt-get install nmap
    nmap target_ip
    
  2. List open ports in Linux command line: Use the netstat command to list open ports:

    netstat -lntu
    
  3. Checking port status in Linux: Check the status of a specific port using the netstat command:

    netstat -an | grep 8080
    
  4. Linux netstat command for querying ports: The netstat command provides information about network connections and open ports. To list all listening ports:

    netstat -l
    
  5. Firewall settings and port configuration in Linux: Use firewall tools like iptables or ufw to configure port settings. For example, to open port 80:

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    
  6. Port forwarding in Linux: Configure port forwarding using tools like iptables. To forward traffic from port 8080 to port 80:

    sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 80
    
  7. Linux lsof command for querying open ports: The lsof command provides information about open files and processes, including open ports. To list processes using a specific port (e.g., 8080):

    lsof -i :8080