6.1.8 Lab: View Open Ports With Netstat

7 min read

Introduction

Understanding which network ports are open on a system is a fundamental skill for anyone studying computer networking, system administration, or cybersecurity. In Lab 6.1.8 – View Open Ports with netstat, students learn to use the classic netstat command to enumerate active connections, listening services, and the associated processes. Mastering this lab not only prepares you for real‑world troubleshooting but also builds a solid foundation for later topics such as firewall configuration, intrusion detection, and packet analysis.


What is netstat?

netstat (short for network statistics) is a command‑line utility available on most Unix‑like operating systems (Linux, macOS, BSD) and on Windows. It reports a snapshot of the kernel’s network stack, revealing:

  • Active TCP/UDP connections – source and destination IP addresses, ports, and connection state.
  • Listening sockets – services waiting for incoming connections.
  • Routing table – how packets are forwarded.
  • Interface statistics – packets transmitted/received per network interface.

Because netstat reads directly from the kernel, its output reflects the exact state of the system at the moment of execution, making it an indispensable diagnostic tool Surprisingly effective..


Lab Objectives

  1. Identify all listening ports on the host machine.
  2. Correlate each open port with the owning process (PID/Program name).
  3. Filter output to focus on specific protocols (TCP vs. UDP) or address families (IPv4 vs. IPv6).
  4. Interpret connection states such as ESTABLISHED, LISTEN, TIME_WAIT, and understand their security implications.
  5. Document findings in a concise report that can be used for further analysis or for creating firewall rules.

Prerequisites

  • A Linux distribution (Ubuntu, CentOS, Debian, etc.) or Windows 10/11 with administrative privileges.
  • Basic familiarity with the terminal or Command Prompt.
  • Optional: lsof or ss installed for cross‑checking results.

Step‑by‑Step Procedure

1. Open a Terminal with Sufficient Privileges

  • Linux/macOS:
    sudo -i   # or prefix each netstat call with sudo
    
  • Windows:
    Right‑click Command PromptRun as administrator.

Why root/administrator?
Without elevated rights, netstat cannot display the program name or PID for sockets owned by other users.

2. Display All Listening Ports

netstat -tuln
Flag Meaning
-t Show TCP sockets only
-u Show UDP sockets only
-l Display listening sockets
-n Show numeric addresses (skip DNS resolution)

The output resembles:

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
udp        0      0 127.0.0.1:323           0.0.0.0:*

3. Include Process Information

netstat -tulnp

The extra p flag adds the PID/Program name column, e.g.:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1234/sshd
tcp6       0      0 :::80                   :::*                    LISTEN      5678/apache2

Tip: On some systems netstat -p may be restricted; use sudo or consider ss -lptu as an alternative Simple, but easy to overlook..

4. Filter by Protocol or Port

  • Only TCP: netstat -tnlp
  • Only UDP: netstat -unlp
  • Specific port (e.g., 443):
    netstat -tnlp | grep ':443'
    

5. Show IPv4 and IPv6 Separately

netstat -4 -tnlp   # IPv4 only
netstat -6 -tnlp   # IPv6 only

6. Examine Established Connections

netstat -tnp | grep ESTABLISHED

This reveals active client‑server sessions, useful for spotting unexpected outbound traffic.

7. Export Results for Reporting

netstat -tulnp > lab6_1_8_open_ports.txt

The generated text file can be attached to the lab report or parsed by scripts for further automation Worth knowing..


Scientific Explanation: How netstat Retrieves Data

When you invoke netstat, the utility interacts with the operating system’s socket API. Under the hood:

  1. Kernel Data Structures – The OS maintains a socket table (often a hash table) where each entry represents an endpoint (IP, port, protocol).
  2. Procfs (Linux)netstat reads from /proc/net/tcp, /proc/net/udp, and their IPv6 counterparts. These pseudo‑files expose the raw binary socket entries in a human‑readable format.
  3. System Calls – On BSD/macOS, netstat uses sysctl to query the kernel. On Windows, it calls the IP Helper API (GetExtendedTcpTable).
  4. Process Mapping – To associate a socket with a PID, netstat cross‑references the socket’s inode (Linux) with the /proc/<pid>/fd directory, where each file descriptor points to a socket inode.

Understanding this pipeline helps you appreciate why certain sockets may be invisible without root privileges: the kernel protects process ownership information to prevent information leakage.


Security Implications of Open Ports

Port Common Service Typical Risk Mitigation
22 SSH Brute‑force login attempts Use key‑based auth, change default port, enable fail2ban
80/443 HTTP/HTTPS Web‑application vulnerabilities Keep servers patched, use WAF
3306 MySQL Remote database exposure Bind to localhost, firewall rule
23 Telnet Clear‑text credentials Disable or replace with SSH
3389 RDP Unauthorized remote desktop access Network‑level authentication, VPN only

During the lab, you may discover services you did not intentionally start (e.Day to day, , avahi-daemon on port 5353). On the flip side, g. Recognizing these “expected” services prevents false alarms and informs the creation of least‑privilege firewall policies Simple, but easy to overlook. And it works..


Frequently Asked Questions

Q1. Why does netstat sometimes show 0.0.0.0:* as the foreign address?
A: The asterisk indicates that the socket is listening and therefore not yet connected to a remote endpoint. It will accept connections from any IP address.

Q2. My output contains many entries with TIME_WAIT. Should I be concerned?
A: TIME_WAIT is a normal TCP state that persists for a short period (typically 2 × MSL, about 2‑4 minutes) after a connection closure. A large number of such entries may indicate a high‑traffic server, but it is usually harmless.

Q3. On Windows, netstat -p returns “Could not obtain ownership information.” What’s wrong?
A: Administrative rights are required. Ensure you run Command Prompt as Administrator. If the problem persists, the system may have disabled the IP Helper API; consider using PowerShell’s Get-NetTCPConnection.

Q4. How does netstat differ from ss?
A: ss (socket statistics) is a newer utility that queries kernel data structures directly, offering faster performance and richer filtering options. Still, netstat remains widely taught because of its cross‑platform availability and familiar syntax.

Q5. Can I monitor open ports continuously?
A: Yes. Combine watch with netstat on Linux: watch -n 2 'netstat -tulnp'. On Windows, use a loop in PowerShell: while ($true) { netstat -anob; Start-Sleep -Seconds 2 }.


Practical Tips for Lab Success

  1. Use the -c flag (Linux) to continuously refresh the display, similar to top:
    netstat -tulnpc 5
    
  2. Redirect errors when you only need numeric output:
    netstat -tulnp 2>/dev/null
    
  3. Cross‑verify with lsof -i if you suspect missing entries; discrepancies often stem from permission issues.
  4. Document the date, time, and host name in your report header; network configurations can change rapidly.
  5. Take screenshots of unusual findings (e.g., an unexpected service on port 111) for visual evidence.

Conclusion

Lab 6.1.8 equips you with the ability to view and interpret open ports using netstat, a skill that bridges theoretical networking concepts with hands‑on system administration. By mastering the command options, understanding the underlying kernel mechanisms, and appreciating the security ramifications of each listening service, you lay the groundwork for effective firewall design, incident response, and performance tuning.

Remember, an open port is not inherently dangerous—context matters. Always correlate the port number with the intended service, verify that the owning process aligns with your system’s purpose, and apply the principle of least privilege when configuring network access. With these practices, you’ll be ready to tackle more advanced labs, such as packet capture with tcpdump or intrusion detection with Snort, confident that you can first see exactly what’s listening on your machine Nothing fancy..

Just Published

Latest from Us

Neighboring Topics

Good Reads Nearby

Thank you for reading about 6.1.8 Lab: View Open Ports With Netstat. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home