Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture network packets per PID?

Anyone know an easy way to ask Linux to "display every internet packet to/from google chrome" or "display every internet packet to/from telnet process with PID 10275"?

The telnet example is not too useful, since I can just use wireshark or tcpdump to see all TCP conversations involving port 23. That and nobody uses telnet anymore. But sniffing all packets to/from complex applications which use many ports seems like a useful thing.

I found some related answers exploring different ways to corroborate ports and PIDs (or programs names) and such, but nothing about packets

  • How to tie a network connection to a PID without using lsof or netstat?
  • How I can get ports associated to the application that opened them?
  • How to do like "netstat -p", but faster?

Looks like someone might have been willing to pay for this answer a while back:

  • http://www.freelancer.com/projects/Perl-CGI-C-C/tcpdump-PID-relation.html

NetHogs is useful for quickly seeing what programs are creating traffic over an interface, but it doesn't have a way to capture packets.

like image 276
Adam Monsen Avatar asked Oct 20 '11 21:10

Adam Monsen


People also ask

How do I capture a network packet?

To capture network traffic, you can use tcpdump. This is a packet sniffer that can observe and record network traffic on an interface. In the following example, we capture 1,000 packets using tcpdump. An easier way to analyze network traffic is to use an actual network traffic analyzer, such as Wireshark.

How do I capture network traffic between two systems?

Using the command lineWinDump will automatically capture traffic on the first network interface it finds. If you need to select a different interface, use "windump -D" to see all your system's network adapters and then use the name with the "-i" switch. In all cases, you can interrupt the capture with Control-C.

How do I capture network packets in Linux?

Steps to capture network traffic in Linux:Launch terminal. Identify the network interface that you want to capture the network traffic packets. Install tcpdump for your Linux distribution if it's not already installed. Run tcpdump against the network interface that you've selected.


1 Answers

Not directly a tcpdump, but can give you info about the network traffic, check https://bytefreaks.net/gnulinux/how-to-capture-all-network-traffic-of-a-single-process

strace -f -e trace=network -s 10000 <PROCESS WITH ARGUMENTS>; 

If the process is already started and you know its PID you can use the following 1

strace -f -e trace=network -s 10000 -p <PID>; 

Another alternative is more complex, using network namespaces, check the above link or use the tool nsntrace, but either can only work on new process, you can not change existent process network namespaces (AFAIK)

UPDATE: you can also install the tool bpfcc-tools (search for your distro, many have it included already) and use the command sudo tcptracer-bpfcc -v -p (PID) While this will not show the packets, it will list network connections for that pid. You can remove the -p (PID) to list all connections per process. This may be useful for those tracking short live connections, not the network payload.

like image 65
higuita Avatar answered Oct 08 '22 17:10

higuita