胡萝贝୧⍤⃝? 2023-08-14 15:02 采纳率: 46.7%
浏览 6

使用脚本进行配置 /etc/network/interfaces 网络配置文件,配置成静态IP,然后使用 ifconfig 查看IP地址依旧显示动态IP地址,也可以通过ssh连接上动态IP

使用脚本进行配置 /etc/network/interfaces 网络配置文件,配置成静态IP,然后使用 ifconfig 查看IP地址依旧显示动态IP地址,也可以通过ssh连接上动态IP,是为什么呢?


#!/bin/bash
  
My_address="192.168.31.250"
file=/etc/network/interfaces

LAN_IP=$(ifconfig eth0 | awk '{print $2}' | sed -n '/^192.168/p')

if [ "$LAN_IP" = "$My_address" ]; then

        sed -i '1,2!d' $file
        sed -i '$aauto lo' $file
        sed -i '$aiface lo inet loopback' $file
        sed -i '$aauto eth0' $file
        sed -i '$aauto eth0 inet dhcp' $file

else

        sed -i '1,2!d' $file
        sed -i '$aauto lo' $file
        sed -i '$aiface lo inet loopback' $file
        sed -i '$aauto eth0' $file
        sed -i '$aiface eth0 inet static' $file
        sed -i '$a address 192.168.31.250' $file
        sed -i '$a netmask 255.255.255.0' $file
        sed -i '$a gateway 192.168.31.1' $file

fi
sleep 1
sync
sleep 1
reboot

img

  • 写回答

1条回答 默认 最新

  • 少林and叔叔 2023-08-15 07:28
    关注

    重启虚拟机了吗?重启后试下

    评论

报告相同问题?

问题事件

  • 创建了问题 8月14日