I run the following command in a terminal.
sudo tcpdump -c 2 -w /tmp/z.pcap icmp
Then run the following command in a terminal.
ping 8.8.8.8
The file generated belongs to the user tcpdump instead of root.
$ stat /tmp/z.pcap
File: /tmp/z.pcap
Size: 158 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 4068722 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 115/ tcpdump) Gid: ( 120/ tcpdump)
Access: 2021-02-25 10:05:52.910772287 -0500
Modify: 2021-02-25 10:06:00.102859691 -0500
Change: 2021-02-25 10:06:00.102859691 -0500
Birth: 2021-02-25 10:05:52.910772287 -0500
The command tcpdump belongs to the root. Why the file generated does not belong to the root?
$ ls -l $(which tcpdump)
-rwxr-xr-x 1 root root 1261512 2021/01/15-17:41:47 /usr/bin/tcpdump
See
man tcpdump
. It's generally good practice to NOT run as root if it's not necessary, so the developers added:In other words: tcpdump, once spawned, does not need to keep root permissions, so it sheds them.
Because
tcpdump
will will spawn a subprocess that is owned bytcpdump
:It drops privileges as it doesn't need it anymore. Use
-Z
option to change that behavior. See this answer for details.