一、配置IP
1.自动获取IP
- 命令:
dhclient
通过DHCP服务自动获取一个IP地址
注意:无法重复运行dhclient
,再次运行需要先使用命令dhclient -r
来释放IP
2.手动设置IP地址
首先打开VMnet(NAT模式)的NAT设置界面,查看子网IP为192.168.89.0,子网掩码为255.255.255.0,网关IP为192.168.89.2。
image.png通过编辑网卡的配置文件来修改IP地址,输入以下命令:
vi /etc/sysconfig/network-scripts/ifcfg-ens33
将BOOTPROTO=dhcp修改成BOOTPROTO=static,将ONBOOT=no改为ONBOOT=yes,并增加:
IPADDR=192.168.89.130
GATEWAY=192.168.89.2
NETMASK=255.255.255.0
BROADCAST=192.168.89.255
DNS1=119.29.29.29
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #static表示设置静态IP地址
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=c0f01c3c-028a-4219-86db-bc442037da9b
DEVICE=ens33
ONBOOT=yes #设置yes使网卡随系统一起启动
IPADDR=192.168.89.130 #指定IP地址
GATEWAY=192.168.89.2 #NAT设置里的网关地址
NETMASK=255.255.255.0
BROADCAST=192.168.89.255
DNS1=119.29.29.29
推出配置文档后需要重启网络服务,运行以下命令:
# systemctl restart network.service
3.查看IP地址
- 命令1:
ip addr
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:01:25 brd ff:ff:ff:ff:ff:ff
inet 192.168.89.130/16 brd 192.168.89.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe46:125/64 scope link
valid_lft forever preferred_lft forever
- 命令2:
ifconfig
由于系统是最小化安装,执行ifconfig命令会报命令未找到。
运行如下命令安装相关软件包:
# yum install net-tools
[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.89.130 netmask 255.255.0.0 broadcast 192.168.89.255
inet6 fe80::20c:29ff:fe46:125 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:46:01:25 txqueuelen 1000 (Ethernet)
RX packets 1332 bytes 430202 (420.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 724 bytes 90731 (88.6 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
4.测试网络联通性
- 命令:
ping
[root@localhost ~]# ping -c 4 www.aminglinux.com
PING www.aminglinux.com (182.254.152.182) 56(84) bytes of data.
64 bytes from 182.254.152.182 (182.254.152.182): icmp_seq=1 ttl=128 time=97.5 ms
64 bytes from 182.254.152.182 (182.254.152.182): icmp_seq=2 ttl=128 time=103 ms
64 bytes from 182.254.152.182 (182.254.152.182): icmp_seq=3 ttl=128 time=90.9 ms
64 bytes from 182.254.152.182 (182.254.152.182): icmp_seq=4 ttl=128 time=102 ms
--- www.aminglinux.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 11341ms
rtt min/avg/max/mdev = 90.986/98.713/103.441/5.028 ms
二、网络问题排查
- 使用桥接模式
image.png
然后使用dhclient
命令获取IP,获取到的IP与主机IP同一网段。
image.png
使用
ping
命令测试网络连通性,发现网络不通。进一步使用命令route - n
查看路由信息,看到没有网关地址。image.png
继续编辑网卡配置文件,修改IP信息为对应网段即可连接网络。
image.png
最后将网络模式再次改回到NAT模式,此时需要将虚拟机IP网段改到VMnet8分配的子网网段一致,网关一致,网络就可以联通。
网友评论