ifup_ethernets.sh
方法一
#!/bin/bash
# 过滤网卡名称
ethList=(`ifconfig -a | grep ^[a-z] | awk -F: '{print $1}'`)
for ((i=0; i<${#ethList[@]}; i++))
do
echo 启动网卡 ${ethList[$i]} ...
ifconfig ${ethList[$i]} up
done
方法二
#!/bin/bash
# 等待udev完成设备初始化
udevadm settle --timeout=5
# 获取所有物理网卡
interfaces=()
while IFS= read -r line; do
interfaces+=("$line")
done < <(find /sys/class/net -type l -name 'e*' -printf '%f\n')
# 遍历启动所有网卡
for iface in "${interfaces[@]}"; do
# 跳过虚拟网卡
[[ -e "/sys/devices/virtual/net/$iface" ]] && continue
# 启动物理网卡
echo "Activating interface: $iface"
ip link set "$iface" up
# 等待网卡实际激活
timeout=5
while [[ $timeout -gt 0 ]]; do
state=$(cat "/sys/class/net/$iface/operstate")
[[ "$state" == "up" || "$state" == "unknown" ]] && break
sleep 1
((timeout--))
done
done
用法: ./ifup_ethernets.sh。
设置系统开机自启:
- 把文件
ifup_ethernets.sh放在目录/lib/ifupdown/下,chmod +x ifup_ethernets.sh赋予可执行权限。 - 创建服务文件
/lib/systemd/system/ifup_ethernets.service
方法一
[Unit]
Description=ifup all ethernets
DefaultDependencies=no
After=network.target
[Service]
Type=simple
KillMode=none
ExecStart=/lib/ifupdown/ifup_ethernets.sh
[Install]
WantedBy=network.target
方法二
[Unit]
Description=Bring up all ethernet interfaces
After=systemd-udevd.service
Before=network.target
Wants=systemd-udevd.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/lib/ifupdown/ifup_ethernets.sh
TimeoutStartSec=30
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
- 启动服务
systemctl enable ifup_ethernets
systemctl daemon-reload
systemctl start ifup_ethernets
该bash脚本用于遍历并启动所有网卡接口,通过ifconfig命令获取接口名并执行ifconfig[接口名]up。将脚本置于/lib/ifupdown/目录下,赋予可执行权限,并创建systemd服务文件。服务文件定义在/lib/systemd/system/ifup_ethernets.service中,设置为在network.target之后启动。最后,通过systemctl启用服务并使其在开机时自动运行。
885

被折叠的 条评论
为什么被折叠?



