VMware workstation --虚拟机静态ip设置

时间:2023-01-07 14:35:32

背景介绍

我在本机win10上安装VMware workstation软件,新建两台centos7虚拟机,最近在配服务,每天虚拟机重启后,ip总会变,服务配置文件又要修改,很麻烦,便需要将其ip由dhcp获取变为静态ip。虚拟机的适配器我选用NAT模式。

静态ip设置

在虚拟机命令行查看网关

# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.20.2 0.0.0.0 UG 0 0 0 eno16777736

查看ip对应的网卡名

#ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.20.135 netmask 255.255.255.0 broadcast 192.168.20.255
inet6 fe80::20c:29ff:fe58:9b72 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:9b:72 txqueuelen 1000 (Ethernet)
RX packets 22052 bytes 7155344 (6.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14787 bytes 4995513 (4.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

修改网卡配置

#vim /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
IPADDR=192.168.20.135
NETMASK=255.255.255.0
GATEWAY=192.168.20.2 #以上查看的网关
BOOTPROTO=static
NAME=eno16777736
UUID=cf02cae1-778c-47a1-9ec2-6559a6bc3f61 #此uuid各不一样,不删此行即可
DEVICE=eno16777736
ONBOOT=yes

重启网络服务

systemctl restart network

测试网络是否正常

# ping 192.168.20.2
# ping www.baidu.com

外网内网都没问题了。

如果内网能ping通,外网ping不通,排错思路:

网关是否正确

配置DNS服务器:echo "nameserver 114.114.114.114" >>/etc/resolv.conf

查看网卡uuid

nmcli con show