---
title: "虚机没法上网"
author: "sbl2255"
date: "2014-07-08T15:01:31Z"
tags: []
categories: ["操作系统"]
slug: "untitled"
draft: false
lastmod: "2015-06-17T14:31:20Z"
id: 2089537
url: "https://sants.iteye.com/blog/2089537"
---

开机后，和往常一样打开虚机，可是怪问题出现了，之前一直用的是HOST-ONLY，不用外网，也就没注意。

打包程序时，需要一个程序包，又懒得下载到Window,然后在拷贝到虚机里，干脆直接在虚机里用NAT下载得了，修改NAT还是很简单的，只要将虚机的网卡连接方式修改，然后在虚机里修改下网络DHCP就可以了；

可是ping的半天，貌似没有成功……

 汗~

 

```
[root@localhost webmin]# ping www.baidu.com
connect: Network is unreachable
```

 

测试了下本地的IP和我另一个虚机的IP

 

```
[root@localhost webmin]# ping 192.168.28.1
PING 192.168.28.1 (192.168.28.1) 56(84) bytes of data.
64 bytes from 192.168.28.1: icmp_seq=1 ttl=64 time=0.795 ms
64 bytes from 192.168.28.1: icmp_seq=2 ttl=64 time=0.336 ms

--- 192.168.28.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.336/0.565/0.795/0.230 ms
[root@localhost webmin]# ping 192.168.28.130
PING 192.168.28.130 (192.168.28.130) 56(84) bytes of data.
64 bytes from 192.168.28.130: icmp_seq=1 ttl=64 time=22.2 ms
64 bytes from 192.168.28.130: icmp_seq=2 ttl=64 time=0.062 ms
```

 

不是IP的问题，可以上局域网，没法外网，是不是路由表出问题了；

 

```
[root@localhost webmin]# nslookup www.baidu.com
Server:         192.168.28.2
Address:        192.168.28.2#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 61.135.169.125
Name:   www.a.shifen.com
Address: 61.135.169.105
```

 

```
[root@localhost webmin]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.28.0    *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
```

 

果然，192.168.28.2 没在里边；

 

 

```
[root@localhost webmin]# route add default gw 192.168.28.2
[root@localhost webmin]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.28.0    *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
default         192.168.28.2    0.0.0.0         UG    0      0        0 eth0
[root@localhost webmin]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.105) 56(84) bytes of data.
64 bytes from 61.135.169.105: icmp_seq=1 ttl=128 time=8.66 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.667/8.667/8.667/0.000 ms
```

 加上之后重启网卡；成功

 

 CentOS 7 设定

上网方式还是通过网络共享给VMnet1，然后网卡模式选择host-only; 这样保证能与主机联通后，虚机就可以共享上网。

 

遇到的问题：

1.网卡没有ip

 

![](http://dl2.iteye.com/upload/attachment/0109/5732/4f10278b-c25f-3787-b8ea-9b0aaaa86b1c.jpg)  
 

主要是onboot,ipaddr等参数配置

 

2.可以ping主机，但无法上外网

 

 设置ip之后，可以和主机联通，但无法上网，判断应该是路由的问题，按上图配置DNS后，可以ping通外网。

![](http://dl2.iteye.com/upload/attachment/0109/5736/5c8716fa-9db6-3537-ac74-e387431214f7.jpg)