Linux下IP設置腳本的實例及遇到問題解決辦法
Linux下IP設置腳本的實例及遇到問題解決辦法
背景
一個Java web項目有一個功能是IP修改,Linux的IP修改腳本如下:
#!/bin/bash #useing parameter ip:netmask:gateway:dns1 #system version:centos6,7/redhat6,7 #read parameter ipaddr=`echo $1|cut -d ":" -f 1` netmask=`echo $1|cut -d ":" -f 2` gateway=`echo $1|cut -d ":" -f 3` dns1=`echo $1|cut -d ":" -f 4` dns2=`echo $1|cut -d ":" -f 5` targetDevice=$2 #get device(active) name -- centos6 and centos7 DEVICE=`dmesg|grep Link|grep Up|cut -d ":" -f 3,4|cut -d " " -f 2|cut -d ":" -f 1|uniq|head -n 1` #if parameter has device name,use it ,else use first alive device if [ "$targetDevice" != "" ]; then DEVICE=$targetDevice echo 'device use target device :' $DEVICE fi #backup net_file mv /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE'.bak' -f #config(net_file) cat <<end>> /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE DEVICE=$DEVICE ONBOOT=yes TYPE=Ethernet BOOTPROTO=none IPV6INIT=no DEFROUTE=yes IPADDR=$ipaddr GATEWAY=$gateway NETMASK=$netmask DNS1=$dns1 DNS2=$dns2 end #update /etc/hosts cat /sensor/bin/standardhosts>/etc/hosts echo '127.0.0.1 '$(hostname)>>/etc/hosts #grant the net_file chmod 755 /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE #restart network /etc/init.d/network restart
實際在這個shell腳本執(zhí)行過程中,碰到幾個問題,是目標服務器的網(wǎng)絡服務相關的配置問題。有四種不同的錯誤,整理如下,以備后用。腳本的本質(zhì)就是將目的IP信息寫入到網(wǎng)卡文件ifcfg-eth2文件中。(eth2是測試機器上的通訊網(wǎng)卡名稱),作為參數(shù)需要傳遞給腳本的。
問題一
在虛擬機上測試,執(zhí)行這個腳本過程中遇到的問題。本地安裝的是VirtualBox,復制了5個虛擬機的節(jié)點?;谶@個背景,那么在執(zhí)行腳本完成后,重啟網(wǎng)卡服務service network restart命令,總是遭遇失敗,失敗信息提示:
Error:No suitable device found: no device found for connection "System eth0"
這是因為虛擬機在復制過程中復制了網(wǎng)卡,但實際上ifconfig-a只顯示一個網(wǎng)卡。解決辦法:
第一步,執(zhí)行ifconfig –a 查看當前系統(tǒng)的網(wǎng)卡名稱,我的當前虛擬機節(jié)點是eth2,lo.
第二步,保留/etc/sysconfig/network-scripts中ifcfg-eth2,ifcfg-lo,其他不存在的網(wǎng)卡文件需要刪除。復制過來的是eth0,rm –rf ifcfg-eth0文件。
第三步,ifcfg-eth2文件內(nèi)容,保證DEVICE=eth2參數(shù)名稱跟文件名稱一致。
第四步,修改網(wǎng)卡策略文件vi /etc/udev/rules.d/70-persistent-net.rules找到eth2網(wǎng)卡的配置,修正MAC地址和eth2的MAC地址一致。
第五步,驗證:執(zhí)行service network restart驗證網(wǎng)卡重啟成功。
問題二
nm_object_array_demarshal: couldn't create object for /org/freedesktop/NetworkManager/ActiveConnection/39 Error: Obtaining active connection for'/org/freedesktop/NetworkManager/ActiveConnection/39' failed. [FAILED]
問題三
See ‘systemctlstatus network.service‘ and ‘journalctl-xn‘ for details
這個問題,執(zhí)行journalctl–xe> /home/journalctl.log
查看日志文件中,找到真正的錯誤是它:
Bringing up interface enp4s0f0: RTNETLINK answers: File exists
問題四
Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization.
centos下出現(xiàn)該故障的二、三、四三種問題的根源一樣,就是啟動網(wǎng)絡的兩個服務有沖突:/etc/init.d/network和/etc/init.d/NetworkManager這兩個服務有沖突。
解決辦法:關掉NetworkManager服務,通過命令service NetworkManager stop完成。再執(zhí)行service network restart驗證能夠正常重啟網(wǎng)卡服務。
結論
驗證IP設置腳本,提供兩個參數(shù)一個是IP信息和網(wǎng)卡名稱執(zhí)行如下命令:
sh reset_ip.sh 192.168.10.170:255.255.255.0:192.168.10.1:10.0.1.90 eth2可以修改虛擬機IP地址。一個簡單的IP設置腳本,執(zhí)行過程中幾乎把網(wǎng)上跟網(wǎng)卡相關的問題,都遭遇了。各種嘗試終于找到原因了,主要根源就是NetworkManage這個服務的問題。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Linux crontab定時任務執(zhí)行失敗處理方案
這篇文章主要介紹了Linux crontab定時任務執(zhí)行失敗處理方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06centos6.5配置ssh免秘鑰登陸執(zhí)行pssh命令的講解
今天小編就為大家分享一篇關于centos6.5配置ssh免秘鑰登陸執(zhí)行pssh命令的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02