如何在 CentOS 上网络接口启动后自动运行启动脚本如何在 CentOS 上网络接口启动后自动运行启动脚本如何在 CentOS 上网络接口启动后自动运行启动脚本如何在 CentOS 上网络接口启动后自动运行启动脚本
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 CentOS 上网络接口启动后自动运行启动脚本

有时,您可能希望在给定网络接口(例如 eth0)启动后立即运行特定命令或自定义脚本。例如,您想要配置特定于接口的 iptables 规则或 QoS 设置。请遵循此指南来了解如何为 CentOS 上的特定网络接口设置启动脚本。

在 CentOS 上,网络接口相关脚本位于 /etc/sysconfig/network-scripts 中。其中包括ifup-post,它在任何网络接口上线后立即调用。在此脚本中,您将在末尾找到以下代码片段。

#!/bin/sh
if [ -x /sbin/ifup-local ]; then
    /sbin/ifup-local ${DEVICE}

该代码片段意味着,如果 /sbin 目录中存在名为 ifup-local 的脚本,则该脚本将使用接口名称参数执行。在普通的 CentOS 系统上,不存在这样的脚本。因此,为了在网络接口启动后自动运行启动脚本,请创建一个可执行脚本/sbin/ifup-local,并在其中放入您想要运行的任何命令或脚本。例如:

$ sudo vi /sbin/ifup-local
#!/bin/sh
if [[ "$1" == "eth0" ]]
then
    echo "this part will be executed right after eth0 is up."
    echo "so you can put any startup command for eth0 here."
else
    #DO_NOTHING
fi

最后,使脚本可执行。

$ sudo chmod +x /sbin/ifup-local
©2015-2025 艾丽卡 support@alaica.com