Linux 下怎么才能不重启系统平滑重置 ifconfig 命令下的网卡流量统计清零

209 天前
 cndns

Linux 下怎么才能不重启系统平滑重置 ifconfig 命令下的网卡流量统计清零

1557 次点击
所在节点    Linux
10 条回复
iwishing
209 天前
超一下人家的答案。。。
https://askubuntu.com/questions/348038/how-to-reset-ifconfig-counters

Those counters are kept by the kernel, so your answer depends on how your network card driver is built. Two possible choices:

Kernel module
Inside the kernel
If it is second, you can not reset counters without restarting the operating system. If it is first, you can do it by unloading the module from the kernel and then loading it back again. If your NIC card use e1000 module, use following commands:

$ ifconfig eth0 down
$ modprobe -r e1000
$ modprobe e1000
$ ifconfig eth0 up
Use ethtool to find out your NIC module:

$ ethtool -i eth0
In front of driver you see your module name:

driver: e1000
version: 7.3.21-k8-NAPI
firmware-version:
...
Use apt-get to install ethtool as follow:

$ apt-get install ethtool
bohai
209 天前
@iwishing 这种方法我之前试过,做不到平滑重置,操作后机器失联了 GG ~
pheyx
208 天前
@bohai tmux 里操作不会。除了 1 楼步骤最后还需重启 NetworkManager 或其他的网络管理程序。tmux 里操作的话,会断联一小会儿,之后就恢复了。
julyclyde
208 天前
@bohai 把 down 和 up 写到一行就可以

@iwishing 我是比较质疑居然还需要 rmmod 吗?按说一个模块有可能对应多个设备,计数器不应该在模块这边而应该在模块管理的设备数据那边啊
bohai
208 天前
@julyclyde 我又去试了一下, down up 写在一行的场景可能还不适用所有的机器,比如我在 esxi 的虚拟机中执行 ifconfig eth0 down; ifconfig eth0 up 确实可以达到平滑重置的效果;但是 kvm 这种虚拟机 阿里云或者其他厂商开出来的机器这种方法真的会造成机器失联,开 vnc 上去执行 ifup eth0 后网络恢复正常。继续在 kvm 的机器中测试 ifconfig eth0 down; ifconfig eth0 up;ifup eth0 虽然不会导致机器失联,但是无法达到重置的效果。

再配合一楼的方法,执行 modprobe -r virtio_net;modprobe virtio_net ;ifup eth0 后,流量可以被重置,但是执行过程中会丢四个包。
bjzhush
208 天前
其实可以换个思路,比如走别的程序统计流量
比如自己写个脚本,解析 ifconfig 的流量,这样不用重置了,自己计算就好
iceheart
208 天前
screen 保驾,重启网卡
julyclyde
208 天前
@bohai 所以我是建议 OP 检讨一下
就目前的行业成熟程度来说,如果某件事很难做出来,那应该都是需求有错误

btw
一行里先 down 再 up 不应该会失联的。因为两句命令是一起送给 shell 执行的呀

ifconfig up 和 ifup 可是两码事,不要混用
iwishing
204 天前
@julyclyde #4 这是人家说的-_-b ,2 种情况,1 种就是计数器在内核模块里,一种是在内核里;在内核里没法处理,除了重启操作系统;在模块里就是移除模块再加入。
你用 SSH 操作虚拟机么肯定很容易失联:p. A better solution for remote access is writing these command to a bash script file and then run it with nohup, i.e: nohup bash restart_counters_script.sh
KVM 怎么处理,没查到,猜测是不是虚机是 DHCP 的,重启网卡后直接换 IP 了?地址租期应该没这么短吧。
丢包问题不知道。。你怎么知道丢了 4 个包-_-b ,是不是因为命令执行太慢了?写个脚本吧。
julyclyde
204 天前
@iwishing 大概是丢了 4 个 ping
(每秒一个那种 ping )

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/981293

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX