Linux 下怎么才能不重启系统平滑重置 ifconfig 命令下的网卡流量统计清零 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
cndns
V2EX    Linux

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

  •  
  •   cndns 2023-10-12 13:33:20 +08:00 2583 次点击
    这是一个创建于 810 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    10 条回复    2023-10-16 18:29:04 +08:00
    iwishing
        1
    iwishing  
       2023-10-12 14:10:57 +08:00
    超一下人家的答案。。。
    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
        2
    bohai  
       2023-10-12 14:54:13 +08:00
    @iwishing 这种方法我之前试过,做不到平滑重置,操作后机器失联了 GG ~
    pheyx
        3
    pheyx  
       2023-10-12 16:22:37 +08:00
    @bohai tmux 里操作不会。除了 1 楼步骤最后还需重启 NetworkManager 或其他的网络管理程序。tmux 里操作的话,会断联一小会儿,之后就恢复了。
    julyclyde
        4
    julyclyde  
       2023-10-12 16:24:43 +08:00
    @bohai 把 down 和 up 写到一行就可以

    @iwishing 我是比较质疑居然还需要 rmmod 吗?按说一个模块有可能对应多个设备,计数器不应该在模块这边而应该在模块管理的设备数据那边啊
    bohai
        5
    bohai  
       2023-10-12 20:17:09 +08:00
    @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
        6
    bjzhush  
       2023-10-12 20:48:30 +08:00
    其实可以换个思路,比如走别的程序统计流量
    比如自己写个脚本,解析 ifconfig 的流量,这样不用重置了,自己计算就好
    iceheart
        7
    iceheart  
       2023-10-13 07:14:52 +08:00 via Android
    screen 保驾,重启网卡
    julyclyde
        8
    julyclyde  
       2023-10-13 10:55:00 +08:00
    @bohai 所以我是建议 OP 检讨一下
    就目前的行业成熟程度来说,如果某件事很难做出来,那应该都是需求有错误

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

    ifconfig up 和 ifup 可是两码事,不要混用
    iwishing
        9
    iwishing  
       2023-10-16 16:30:40 +08:00
    @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
        10
    julyclyde  
       2023-10-16 18:29:04 +08:00
    @iwishing 大概是丢了 4 个 ping
    (每秒一个那种 ping )
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5039 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 05:45 PVG 13:45 LAX 21:45 JFK 00:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86