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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值