卡)
ifup_ethernets.sh方法一#!/bin/bash# 等待udev設(shè)備穩(wěn)定udevadm settle--timeout5# 遍歷啟動所有網(wǎng)卡forifacein/sys/class/net/e*;do# 提取網(wǎng)卡名ifaceName$(basename$iface)# 跳過虛擬網(wǎng)卡: loopback, bridge, docker等[[-e/sys/devices/virtual/net/$ifaceName]]continue# 狀態(tài)檢查(避免重復(fù)操作)state$(cat/sys/class/net/$ifaceName/operstate2/dev/null)[[$state!down]]continue# 啟動網(wǎng)卡echo啟動網(wǎng)卡$ifaceName...iplinkset$ifaceNameup2/dev/null||{echo網(wǎng)卡$ifaceName啟動失敗2}done方法二#!/bin/bash# 等待udev完成設(shè)備初始化udevadm settle--timeout2# 獲取所有物理網(wǎng)卡interfaces()whileIFSread-rline;dointerfaces($line)done(find/sys/class/net-typel-namee*-printf%f\n)# 遍歷啟動所有網(wǎng)卡forifacein${interfaces[]};do# 跳過虛擬網(wǎng)卡[[-e/sys/devices/virtual/net/$iface]]continue# 啟動物理網(wǎng)卡echoActivating interface:$ifaceiplinkset$ifaceup# 等待網(wǎng)卡實(shí)際激活timeout5while[[$timeout-gt0]];dostate$(cat/sys/class/net/$iface/operstate)[[$stateup||$stateunknown]]breaksleep1((timeout--))donedone用法./ifup_ethernets.sh。設(shè)置系統(tǒng)開機(jī)自啟把文件ifup_ethernets.sh放在目錄/lib/ifupdown/下chmod x ifup_ethernets.sh賦予可執(zhí)行權(quán)限。創(chuàng)建服務(wù)文件/lib/systemd/system/ifup_ethernets.service[Unit]Descriptionifup all ethernetsAftersystemd-udevd.service network-pre.target networking.serviceBeforenetwork.target[Service]TypeoneshotRemainAfterExityesExecStart/lib/ifupdown/ifup_ethernets.shTimeoutStartSec10StandardOutputjournalStandardErrorjournal[Install]WantedBynetwork.target啟動服務(wù)systemctlenableifup_ethernets systemctl daemon-reload systemctl start ifup_ethernets