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): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2 Request timeout for icmp_seq 3 4 packets transmitted, 0 packets received, 100.0% packet loss

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

ifconfig vmnet8
vmnet8: flags
=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether 00:50:56:c0:00:08 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) 56(84) bytes of data. 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=10.9 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=12.4 ms 64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=11.5 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms rtt min/avg/max/mdev = 10.960/11.660/12.451/0.618 ms

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

ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.192.200  netmask 255.255.255.248  broadcast 172.16.192.207
        inet6 fe80::adf:d6e7:6f20:e33  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:72:84:80  txqueuelen 1000  (Ethernet)
        RX packets 734  bytes 64157 (62.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 554  bytes 59560 (58.1 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 40  bytes 3208 (3.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3208 (3.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  这下问题明确了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="29"
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="24"
GATEWAY="172.16.192.2"
DNS1="172.16.192.2"
IPV6_PRIVACY="no"

  第二步:重启网络

systemctl restart network

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

ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.192.200  netmask 255.255.255.0  broadcast 172.16.192.255
        inet6 fe80::adf:d6e7:6f20:e33  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:72:84:80  txqueuelen 1000  (Ethernet)
        RX packets 1622  bytes 141206 (137.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1076  bytes 121838 (118.9 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 40  bytes 3208 (3.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3208 (3.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  哈哈! 还真是这个。

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

ping 172.16.192.200

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

 

总结

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

  主义的来一发。

 

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

 

我的个人站点

  www.sqlpy.com

 

posted on 2019-01-16 11:45  蒋乐兴的技术随笔  阅读(5641)  评论(0编辑  收藏  举报

导航