问题描述:只要VM虚拟机重启,那么对应的linux的ip地址就会改变

我们想要将该ip地址固定,方法如下。

1、解决:

  • 主要是配置的ip网络使用的是dhcp动态地址

  • 使用vim /etc/sysconfig/network-scripts/ifcfg-ens33命令进入编辑

改为以下内容

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"   #需要将这里的dhcp改为static
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="d3df3122-c090-4ea7-8895-9a5d8c43d8c5"
DEVICE="ens33"
ONBOOT="yes"        #这里更改为yes
IPADDR="192.168.193.137"     #这里更改为自己想要固定的ip地址
NETMASK="255.255.255.0"       #子网掩码
GATEWAY="192.168.193.2"       #使用route -n命令获取,默认为第一条   默认网关
DNS="192.168.193.2"           #DNS保持和上面的一样,也可以是114.114.114.114或者8.8.8.8

2、验证:

1、输入命令 service network restart 重启网卡

[root@df01 ~]# service network restart
Restarting network (via systemctl):                        [  确定  ]

2、使用ifconfig进行查看ip地址是否改变


[root@df01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.193.137  netmask 255.255.255.0  broadcast 192.168.193.255
        inet6 fe80::20c:29ff:fe4c:9d62  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4c:9d:62  txqueuelen 1000  (Ethernet)
        RX packets 1572  bytes 962307 (939.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 993  bytes 154060 (150.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:e2:9b:de  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3、使用ping www.baidu.com 查看是否由网络(使用Ctrl+C来进行中止ping命令)

[root@df01 ~]# ping www.baidu.com
PING www.a.shifen.com (36.152.44.96) 56(84) bytes of data.
64 bytes from localhost (36.152.44.96): icmp_seq=1 ttl=128 time=22.5 ms
64 bytes from localhost (36.152.44.96): icmp_seq=2 ttl=128 time=23.5 ms
64 bytes from localhost (36.152.44.96): icmp_seq=3 ttl=128 time=23.5 ms
64 bytes from localhost (36.152.44.96): icmp_seq=4 ttl=128 time=22.4 ms
64 bytes from localhost (36.152.44.96): icmp_seq=5 ttl=128 time=24.7 ms
64 bytes from localhost (36.152.44.96): icmp_seq=6 ttl=128 time=23.3 ms
64 bytes from localhost (36.152.44.96): icmp_seq=7 ttl=128 time=23.6 ms
^C
--- www.a.shifen.com ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6013ms
rtt min/avg/max/mdev = 22.499/23.421/24.749/0.726 ms
[root@df01 ~]# 

本文章使用limfx的vscode插件快速发布