V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
cndns
V2EX  ›  Linux

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

  •  
  •   cndns · 198 天前 · 1531 次点击
    这是一个创建于 198 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    ifconfig up 和 ifup 可是两码事,不要混用
    iwishing
        9
    iwishing  
       194 天前
    @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  
       194 天前
    @iwishing 大概是丢了 4 个 ping
    (每秒一个那种 ping )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2400 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.