如何通过重启保留 ethtool 设置

Gus*_*s E 21 centos ethtool linux-networking

我想在 CentOS5 服务器上关闭 tcp 分段卸载。使用 ethtool 命令是ethtool -K eth0 tso off但是,此设置仅在此会话中持续存在。我怎样才能让它通过重新启动持续存在?

Gus*_*s E 16

从这个网页

您可以/etc/rc.local在当前运行级别完成后运行命令的(或您的发行版的等效版本)中输入 ethtool 命令,但这并不理想。网络服务可能在运行级别期间启动,并且 ethtool 命令往往会中断网络流量。更可取的是在界面启动时应用命令。

CentOS 中的网络服务有能力做到这一点。该脚本/etc/sysconfig/network-scripts/ifup-post检查是否存在/sbin/ifup-local,并且如果存在的话,与所述接口名称作为参数运行时,它(如:/sbin/ifup-local eth0

我们可以使用 touch 创建此文件,/sbin/ifup-local使其可执行并chmod +x /sbin/ifup-local设置其 SELinux 上下文,chcon --reference /sbin/ifup /sbin/ifup-local然后在编辑器中打开它。

将相同设置应用于所有接口的简单脚本类似于

#!/bin/bash
if [ -n "$1" ]; then
/sbin/ethtool -G $1 rx 4096 tx 4096
/sbin/ethtool -K $1 tso on gso on
fi
Run Code Online (Sandbox Code Playgroud)

请记住,这将尝试将设置应用于所有接口,甚至是环回。

如果我们有不同的接口,我们想应用不同的设置,或者想跳过环回,我们可以做一个 case 语句

#!/bin/bash
case "$1" in
eth0)
/sbin/ethtool -G $1 rx 16384 tx 16384
/sbin/ethtool -K $1 gso on gro on
;;
eth1)
/sbin/ethtool -G $1 rx 64 tx 64
/sbin/ethtool -K $1 tso on gso on
/sbin/ip link set $1 txqueuelen 0
;;
esac
exit 0
Run Code Online (Sandbox Code Playgroud)

现在 ethtool 设置会在接口启动时应用于接口,所有可能的网络通信中断都会在接口启动时完成,您的服务器可以继续以完整的网络功能启动。


小智 16

对于 /etc/sysconfig/network-scripts/ifcfg-* 中的 RHEL7,您可能有:

ETHTOOL_OPTS="-K ${DEVICE} gso off gro off tso off"

如果有更多选项,则使用类似

ETHTOOL_OPTS="-K ${DEVICE} gso off;-K ${DEVICE} gro off;-K ${DEVICE} tso off"

您必须在 ifcfg 文件中自然定义 DEVICE。

不需要 rc.local 或额外的 ifup 脚本。在通用部署系统中易于使用。


use*_*814 13

如果您使用RHEL7(或类似)并使用网络管理器而不是/etc/init.d/network来控制您的接口,则建议的答案将不起作用,因为/sbin/ifup-local(以及ifdown-pre-localifdown-local ) 永远不会被执行。

而是将您的脚本放入/etc/NetworkManager/dispatcher.d/并确保启用NetworkManager-dispatcher服务

systemctl enable NetworkManager-dispatcher
Run Code Online (Sandbox Code Playgroud)

提示:调度器只会在 NetworkManager 对接口进行更改时启动,它不需要运行或任何东西,所以如果状态读取

Active: inactive (dead)
Run Code Online (Sandbox Code Playgroud)

完全没问题!

还要确保您的脚本是:

  1. 可执行文件 (chmod +x)
  2. 由 root 拥有(chown root:root)
  3. 只能由 root 写入 (chmod 755)

现在 NetworkManager 会将两 (2) 个变量传递给调度程序:

$1是接口(eno16777984eth0ppp0等...)

$2保持状态(向上向下

它允许链接脚本(就像 /etc/rc...)来控制调度程序执行它们的顺序:

10-1、20-秒等等...

顺序将在连接时升序

if [$2 = "up"] 它的 10-first 后跟 20-second

并在断开连接时下降

如果 [$2 = "down"] 你得到 20 秒,然后是 10-first

等等。

因此,要完成 OP 正在寻找的内容,您可以输入以下内容:

#!/bin/bash
if [ "$1" = "eth0" && "$2" = "up" ]; then
  /sbin/ethtool --offload eth0 tso off
fi
Run Code Online (Sandbox Code Playgroud)

/etc/NetworkManager/dispatcher.d/20-ethtool

收工了。

干杯

  • bash 脚本有错误,应该是:`if [ "$1" = "eth0" ] && [ "$2" = "up" ]; 然后` (4认同)
  • 在 Google 上搜索“ifup-local”,您会得到第 6 次点击。没有什么我会认为“旧” (3认同)
  • 这无疑为 CentOS 7 添加了一些东西。在 CentOS 7 中没有其他解决方案对我有用。 (2认同)

Pad*_*dyD 5

我遇到了接受答案的问题(我赶紧补充,我发现这很有帮助),因为我使用的是绑定接口。

我花了一段时间才发现发生了什么,但我发现在启动绑定时,甚至在单独启动绑定从属时,ifup-local不会为从属接口调用脚本。我认为这是因为从接口没有分配任何 IP 地址。

为了解决这个问题,我修改了我ifup-local的解析/proc/bonding/bondX接口的内容,如果它是一个绑定,获取从接口名称,然后用它们做必要的事情。

最后我的ifup-local样子如下:

#!/bin/bash

if [ -n "$1" ]
then
  IFACE="$1"

  # If interface is physical
  if [[ $IFACE =~ ^eth[0-9]+$ ]]
  then
    # Do whatever you need for a physical interface here
    # example below
    /sbin/ethtool -K $IFACE rx off    

  # Else if it's a bond
  elif [[ $IFACE =~ ^bond[0-9]+$ ]]
  then
    # Do whatever you need for the bond here
    # example below
    /sbin/ethtool -K $IFACE gso off

    # Now deal with slaves
    # Pull out slave interface names from /proc/net/bonding/bondX
    SLAVES=$(/bin/grep -oP "Slave Interface: \K(eth[0-9]+)" /proc/net/bonding/$IFACE)
    for SLAVE in $SLAVES
    do
      # Do whatever you need with the slave here
      # example below
      /sbin/ethtool -K $SLAVE tso off gso off
    done
  fi
fi
Run Code Online (Sandbox Code Playgroud)

注意:不同版本的 RedHat/Fedora/CentOS 的 /proc/net/bonding/bondX 的内容可能与我编写脚本时使用的不同,所以提取从接口名称的命令可能不起作用.