您的位置:首页 > 理论基础 > 计算机网络

为virtualbox 建立桥接网络

2011-11-21 10:23 274 查看
有的时候会使用多个虚拟机,比如 haprxoy + nginx 做负载均衡测试(1+2),所以在笔记本上的ubuntu 系统中安装了 virtualbox ,virtualbox 自带网络有两种类型,

1 nat 方式,可以很方便的共享宿主机网络资源,但是有个缺点 我无法通过宿主机ssh 到虚拟机中。

2 bridge方式,此方法可以桥接到宿主机的某一快物理网卡,然后能够获得这块网卡的ip 地址与 网络参数,这里有个问题,此块物理网卡如果不是是激活的(比如外出无网络可用),那么也就不能通信了!

3 折衷办法,自己虚拟出一块网卡来,然后virtualbox 在桥接到这块虚拟网卡上!

为virtualbox 配置桥接网络

参考
http://dngood.blog.51cto.com/446195/580540
步骤

1. 建立虚拟网卡与桥接网络

安装 bridge-utils

apt-get install uml-utilities bridge-utils

修改/etc/network/interfaces

增加以下内容:

auto vnet0
iface vnet0 inet static
address 172.16.0.1
netmask 255.255.255.0
bridge_ports none
bridge_maxwait 0
bridge_fd 1
up iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
down iptables -t nat -D POSTROUTING -s 172.16.0.0/24 -j MASQUERADE

up网卡

sudo ifup vnet0

2. 启用ip 转发

修改/etc/sysctl.conf 去掉这行的注释

# Uncomment the next line to enable packet forwarding for IPv4

net.ipv4.ip_forward=1

运行下面命令使其生效

sysctl -p

3. 搭建dhcp服务器

安装 dnsmasq

apt-get install dnsmasq

修改/etc/dnsmasq.conf

去掉下面的注释
# Include a another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
conf-dir=/etc/dnsmasq.d

在/etc/dnsmasq.d/目录下面增加一个名为 wifi-dhcp 的文件,内容为:
#######################################
interface=vnet0
dhcp-range=172.16.0.2,172.16.0.254,1h
dhcp-option=option:dns-server,172.16.0.1,208.67.222.222,208.67.220.220
dhcp-option=option:domain-name,precision-m65


结束

如果virtualbox 虚拟的 Linux 系统,设定 与 vnet0 相同网段的静态ip 地址,那么第3步可以省略!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息