天天看點

tcpdump 逾時包_為何無法在sudo下運作tcpdump逾時?

我想以10秒的時間限制運作tcpdump指令.

timeout 10 sudo tcpdump -i eth0 -nn 'host 192.168.1.176'

它并沒有停止.為什麼逾時指令在這裡對tcpdump無效?

解決方法:

問題在于逾時會以您的使用者權限運作. sudo程序将特權更新為root(或另一個使用者),是以不允許逾時将SIGTERM發送給子程序.可以用strace來顯示(我以#開頭的注釋,以及為了可讀性而使用的空行):

user$strace timeout 1 sudo sleep 5

# lots of irrelevant stuff

# here, timeout sets up the timer to get a signal when the child should be terminated

rt_sigprocmask(SIG_UNBLOCK, [ALRM], NULL, 8) = 0

timer_create(CLOCK_REALTIME, {sigev_value={sival_int=1889673072, sival_ptr=0x560c70a21f70}, sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}, [0]) = 0

timer_settime(0, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=1, tv_nsec=0}}, NULL) = 0

wait4(12320, 0x7ffdfeb0ef0c, 0, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)

# the signal arrives

--- SIGALRM {si_signo=SIGALRM, si_code=SI_TIMER, si_timerid=0, si_overrun=0, si_value={int=1889673072, ptr=0x560c70a21f70}} ---

# timeout tries to kill the child

kill(12320, SIGTERM) = -1 EPERM (Operation not permitted)

# and gets EPERM!

解決方法是也以root特權運作逾時.以下将按預期工作:

user$sudo timeout 1 sleep 5

當然,如果您已經是root使用者,則在指令行中的sudo之前或之後放置逾時1都沒有關系.

root$sudo timeout 1 sleep 5

root$timeout 1 sudo sleep 5

标簽:bash,tcpdump,linux

來源: https://codeday.me/bug/20191026/1935350.html