目的
使用网桥模式,将多台linux虚拟机/物理主机桥接。同时保证虚拟机和宿主机器及局域网内其他主机进行通信。最后能够使本虚拟机访问外网。
准备
1、确认你主机的IP,也就是虚拟机的宿机。
2、虚拟机IP写死,与宿机同一网段即可。
3、虚拟机网关,和宿机一致。
4、当然,你的虚拟机如果也准备好了,那么我们就开始吧。
我使用的Debian8x系统。
过程
1、设置桥接模式
在虚拟机窗口,选择【控制】-【设置】-【桥接网卡】 。 注意:选择桥接网卡名称的时候根据你当前的主机的网络连接情况而定,通过网线或无线需要选择不同的主机网卡。
OK。
2、在debian中设置网络
需要在debian中设置一个固定IP供debian联网使用。不同于RedHat 系的是,debian使用interfaces来控制网卡配置。下面,修改interfaces设置debian的IP地址、网关、子网掩码等。
root@debian:~# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
#开机激活eth0网卡,你可以使用eth1、2、3...
auto eth0
#eth0网卡静态
iface eth0 inet static
address 192.168.1.229
netmask 255.255.254.0
gateway 192.168.1.1
root@debian:~#
设置DNS,访问外网
root@debian:/# cat /etc/resolv.conf
# Generated by NetworkManager
#设置可用的DNS
nameserver 202.106.0.20
nameserver 219.239.26.42
接下来,重启网络。
root@debian:/# /etc/init.d/networking restart
[OK] Restarting networking (via systemctl): networking.service.
root@debian:/#
重启成功后,如果没有意外的话,你就可以让debian和同一局域网的其他主机进行互相通信了,我们测试一下。
root@debian:/# ping 192.168.0.233
PING 192.168.0.233 (192.168.0.233) 56(84) bytes of data.
64 bytes from 192.168.0.233: icmp_seq=1 ttl=64 time=72.8 ms
64 bytes from 192.168.0.233: icmp_seq=2 ttl=64 time=10.3 ms
64 bytes from 192.168.0.233: icmp_seq=3 ttl=64 time=8.49 ms
^C
--- 192.168.0.233 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 8.490/30.564/72.853/29.912 ms
root@debian:/# ping 192.168.0.236
PING 192.168.0.236 (192.168.0.236) 56(84) bytes of data.
64 bytes from 192.168.0.236: icmp_seq=1 ttl=64 time=52.7 ms
64 bytes from 192.168.0.236: icmp_seq=2 ttl=64 time=19.1 ms
64 bytes from 192.168.0.236: icmp_seq=3 ttl=64 time=9.56 ms
64 bytes from 192.168.0.236: icmp_seq=4 ttl=64 time=27.7 ms
64 bytes from 192.168.0.236: icmp_seq=5 ttl=64 time=10.9 ms
^C
--- 192.168.0.236 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 9.565/24.036/52.754/15.772 ms
root@debian:/# ping www.baidu.com
PING www.a.shifen.com (119.75.217.109) 56(84) bytes of data.
64 bytes from 119.75.217.109: icmp_seq=1 ttl=56 time=4.46 ms
64 bytes from 119.75.217.109: icmp_seq=2 ttl=56 time=17.3 ms
64 bytes from 119.75.217.109: icmp_seq=3 ttl=56 time=3.66 ms
64 bytes from 119.75.217.109: icmp_seq=4 ttl=56 time=5.56 ms
64 bytes from 119.75.217.109: icmp_seq=5 ttl=56 time=4.72 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 3.661/7.150/17.333/5.128 ms
OK。(www.baidu.com测试网络连接真的很好用 !!!