linux下的网卡启动后,如何自动加载脚本文件?

  • 需要在网卡每次启动后,自动加载一些配置文件脚本。求问这个文件该放在什么地方?

  • 需要加入的配置是ip route add ...

  • 已经尝试将文件放在了/etc/network/if-up.d//etc/network/interfaces.d/下但每次重启网卡后都没有自动运行我的脚本,同时也已经将我自己写的脚本改成了和目录下其他脚本一样的可读可写可执行

阅读 8.7k
2 个回答

直接在/etc/sysconfig/network-scripts/ifcfg-<name> 的最后写上你要执行的脚本即可
这似乎并不能保证在网卡成功启动后才能执行,也就是只要你尝试启动网卡,这里的命令都会执行,因此你可以尝试写入判断网卡已经成功启动的脚本

我的是fedora发行版本,提供参考:

sudo touch /etc/rc.d/rc.local
sudo vim /etc/rc.d/rc.local

在/etc/rc.d/rc.local文件中写入

#!/bin/bash
# 在这个文件中写入开机启动需要执行的命令

然后使用:wq命令 保存并退出.

赋予可执行权限:
sudo chmod+x /etc/rc.d/rc.local

设置开机启动:
sudo systemctl enable rc-local.service
如果出现以下错误提示:

[root@dev-zhanghua ~]# systemctl enable rc-local.service
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).


sudo vim /usr/lib/systemd/system/rc-local.service
在rc-local.service文件末尾加入:
[Install]
WantedBy=multi-user.target
并重新设置开机启动:
sudo systemctl enable rc-local.service


查看状态, 如果出现以下提示, 设置成功!
[zhanghua@dev-zhanghua ~]$ systemctl status rc-local.service
rc-local.service - /etc/rc.d/rc.local Compatibility
Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled)
Active: active (running) since Wed 2015-04-15 11:37:40 CST; 1h 44min ago
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题