How to Use the Linux tshark Command for Headless Network Packet Capture Analysis

The Headless Server Dilemma

When a complex network issue arises—such as a database randomly dropping API requests, or a load balancer forwarding corrupted packets—network engineers rely on packet capture software like Wireshark to intercept and analyze the exact binary data flowing across the network card.

The problem is that Wireshark is a heavy, graphical application. The vast majority of enterprise Linux servers (running Ubuntu Server, RHEL, or CentOS) are completely headless; they have no graphical user interface (GUI) installed. You cannot launch Wireshark over a standard SSH connection.

To execute deep packet inspection on production servers, Linux engineers use tshark (Terminal Wireshark). tshark is the command-line equivalent of the Wireshark GUI. It utilizes the exact same highly advanced libpcap capture libraries and the exact same display filter syntax. By running tshark in the terminal, administrators can surgically intercept network traffic on headless servers, output the data to a standardized .pcap file, and then securely transfer that file to their local workstation for graphical analysis in the full Wireshark application.

Step 1: Installation and Security Permissions

tshark is not installed by default on minimal Linux distributions. It is bundled within the main Wireshark package.

sudo apt update
sudo apt install tshark -y

During installation, Debian/Ubuntu-based systems will present a critical full-screen prompt asking: “Should non-superusers be able to capture packets?”

For strict security, select No. This ensures that only users with sudo (root) privileges can eavesdrop on network traffic. If a low-level service account is compromised, the attacker will be blocked from running tshark to steal unencrypted HTTP passwords.

Step 2: Identifying the Capture Interface

Before you begin capturing, you must tell tshark which physical network card to listen to. A server might have multiple interfaces (e.g., one for public web traffic, one for a private backend database connection).

List all available interfaces using the -D (Discover) flag:

sudo tshark -D

The output will list the interfaces numerically, such as 1. eth0, 2. lo (loopback), and 3. docker0. Make a note of the specific interface you wish to monitor (e.g., eth0).

Step 3: Executing a Surgical Capture Filter

If you simply run sudo tshark -i eth0, the command will intercept every single packet on the server and print a continuous, unreadable blur of thousands of lines of text to your SSH terminal. You must constrain the capture using Capture Filters (BPF – Berkeley Packet Filter syntax).

Capture filters are executed before the packet is written to memory, making them incredibly CPU-efficient.

Suppose you are troubleshooting a connection issue between your web server and a specific PostgreSQL database located at IP address 10.0.5.50 on port 5432.

sudo tshark -i eth0 -f "host 10.0.5.50 and port 5432"

Decoding the Flags:

  • -i eth0: Specifies the interface.
  • -f "...": The BPF Capture Filter string. The kernel will instantly discard any packet that does not match this exact IP and port combination, ensuring your terminal only displays the relevant database traffic.

Step 4: Writing to a PCAP File

While watching text scroll by in the terminal is useful for a quick check, complex forensic analysis (like tracking TCP retransmissions or analyzing TLS handshakes) requires writing the raw binary data to a file.

You use the -w (write) flag to output a PCAP (Packet Capture) file.

sudo tshark -i eth0 -f "host 10.0.5.50" -w /tmp/database_capture.pcap

Because writing to a file hides the terminal output, you have no idea how much data you are capturing. To prevent filling up the server’s hard drive, always use the -c (count) flag to forcefully terminate the capture after a specific number of packets.

sudo tshark -i eth0 -c 5000 -w /tmp/database_capture.pcap

This command captures exactly 5,000 packets and then autonomously exits, returning you to the command prompt.

Step 5: Applying Display Filters to Read PCAP Files

Once you have captured the .pcap file, you can securely copy it (using scp or sftp) to your Windows or Mac workstation and open it in the graphical Wireshark application.

However, you can also analyze the file directly on the Linux server using tshark. You use the -r (read) flag.

tshark -r /tmp/database_capture.pcap

Because the capture is already saved, you can now apply highly complex Wireshark Display Filters using the -Y flag to extract specific forensic data.

Suppose you want to read the PCAP file, but only display packets where the TCP connection was forcefully reset (a classic sign of a firewall aggressively dropping connections):

tshark -r /tmp/database_capture.pcap -Y "tcp.flags.reset == 1"

If you want to extract and display only the HTTP GET requests from a massive capture file:

tshark -r /tmp/web_capture.pcap -Y "http.request.method == GET"

Conclusion

The inability to launch graphical applications on enterprise Linux servers should never prevent deep network analysis. By mastering the tshark command, utilizing BPF capture filters to minimize CPU overhead, and writing standardized PCAP files, system administrators can execute surgical packet captures on critical headless infrastructure, providing the irrefutable evidence required to solve the most complex network routing and application layer failures.

RELATED POSTS

  • How to Use the Linux journalctl Command to Read systemd Logs
  • How to Use the Linux tty Command to Identify the Current Terminal Session
  • How to Use the Linux netstat Command to Check Open Ports
  • How to Use the patch Command to Apply Code Changes in Linux
  • How to Prevent a Linux System from Sleeping via Terminal
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.