Centos-7.x 下子网掩码的配置

背景

  今天在自己的虚拟机上安装上了centos-7.6操作系统,应该是安装的过程中大意了;安装完成后虚拟机可以正常访问外网但是

  我的笔记本连接不上虚拟机。

  笔记本的IP地址:172.16.192.1 虚拟机地址:172.16.192.200 虚拟机网关:172.16.192.2

  

笔记本ping虚拟机

ping 172.16.192.200

PING 172.16.192.200 (172.16.192.200):  data bytes
Request timeout for icmp_seq
Request timeout for icmp_seq
Request timeout for icmp_seq
Request timeout for icmp_seq
packets transmitted, packets received, 100.0% packet loss

  由上面的结果可以看出笔记本ping不能虚拟机、笔记本上对应网卡的信息如下:

ifconfig vmnet8
vmnet8: flags=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu
ether :::c0::
inet 172.16.192.1 netmask 0xffffff00 broadcast 172.16.192.255

  以上内容可以提取出一个非常重要的信息,笔记本上的172.16.192.1对就的子网掩码为255.255.255.0

虚拟机可以正常上外网

ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) () bytes of data.
bytes from 14.215.177.38 (14.215.177.38): icmp_seq= ttl= time=10.9 ms
bytes from 14.215.177.38 (14.215.177.38): icmp_seq= ttl= time=12.4 ms
bytes from 14.215.177.38 (14.215.177.38): icmp_seq= ttl= time=11.5 ms --- www.a.shifen.com ping statistics ---
packets transmitted, received, % packet loss, time 2004ms
rtt min/avg/max/mdev = 10.960/11.660/12.451/0.618 ms

  看到上面的信息第一感觉是有鬼了,但是我并没有放弃还是决定debug一下,首先想到的是看一下虚拟机上网卡的状态

ifconfig

ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST>  mtu
inet 172.16.192.200 netmask 255.255.255.248 broadcast 172.16.192.207
inet6 fe80::adf:d6e7:6f20:e33 prefixlen scopeid 0x20<link>
ether :0c:::: txqueuelen (Ethernet)
RX packets bytes (62.6 KiB)
RX errors dropped overruns frame
TX packets bytes (58.1 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (3.1 KiB)
RX errors dropped overruns frame
TX packets bytes (3.1 KiB)
TX errors dropped overruns carrier collisions

  这下问题明确了172.16.192.200 对应的子网掩码是255.255.255.248;我这里就直接采用简单粗暴的方式把虚拟机加入到255.255.255.0这个子网。

解决问题

cat /etc/sysconfig/network-scripts/ifcfg-ens33 

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
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="58d375dc-2655-456d-b56d-a52837177e08"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="172.16.192.200"
PREFIX=""
GATEWAY="172.16.192.2"
DNS1="172.16.192.2"
IPV6_PRIVACY="no"

  从网卡的配置文件上没有看到有netmask呀! 老DBA了我决定用一点点经验来解决问题(反正这个虚拟机是刚创建出来的我输的起),仔细分析

  后发现PREFIX这个项最有可能是用来设置掩码的;255.255.255.0对应的就是24个1嘛,决定把它从29改到24然后重启一下网络

  第一步:改PREFIX为24

cat /etc/sysconfig/network-scripts/ifcfg-ens33  

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
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="58d375dc-2655-456d-b56d-a52837177e08"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="172.16.192.200"
PREFIX=""
GATEWAY="172.16.192.2"
DNS1="172.16.192.2"
IPV6_PRIVACY="no"

  第二步:重启网络

systemctl restart network

  第三步:查看虚拟机的网卡信息

ifconfig

ens33: flags=<UP,BROADCAST,RUNNING,MULTICAST>  mtu
inet 172.16.192.200 netmask 255.255.255.0 broadcast 172.16.192.255
inet6 fe80::adf:d6e7:6f20:e33 prefixlen scopeid 0x20<link>
ether :0c:::: txqueuelen (Ethernet)
RX packets bytes (137.8 KiB)
RX errors dropped overruns frame
TX packets bytes (118.9 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (3.1 KiB)
RX errors dropped overruns frame
TX packets bytes (3.1 KiB)
TX errors dropped overruns carrier collisions

  哈哈! 还真是这个。

  第四步:在笔记本上是否可能连接到虚拟机

ping 172.16.192.200

PING 172.16.192.200 (172.16.192.200):  data bytes
bytes from 172.16.192.200: icmp_seq= ttl= time=0.304 ms
bytes from 172.16.192.200: icmp_seq= ttl= time=0.475 ms
bytes from 172.16.192.200: icmp_seq= ttl= time=0.540 ms
^C
--- 172.16.192.200 ping statistics ---
packets transmitted, packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.304/0.440/0.540/0.100 ms

总结

  人(我自己吧)在某一领域内有一定的经验后,在面对一些第一次遇到的问题的时;有一重倾向,就是不看日志也没有做特别深入的分析就直接,经验

  主义的来一发。

  通常有那么一引起问题可以被这样低成本的解决,不过方法论上是否正确还有待商榷。

我的个人站点

  www.sqlpy.com

上一篇:gulp + gulp-better-rollup + rollup 构建 ES6 开发环境


下一篇:URL编码 utf-8 gb2312的区别