linux中 virtualbox 网络配置

使用virtualbox一定想要像vmware一样的nat配置,既可以让host访问guest,又可以让guest访问外部网络

 


重点:

如果你得vbox是源码编译安装的OSE版,那么还需做如下操作:

运行时只需加载模块:

#modprobe vboxdrv

如果要支持桥接,文件共享,还需

#modprobe vboxnetadp

#modprobe vboxnetflt

#modprobe vboxpci

这些模块均位于

virtualbox_OSE/out/linux.amd64/release/bin/src中

实现开机挂载:

把上面四条命令添加到

/etc/rc.local的exit 0之前

 


两种方法:

一、

vbox管理界面直接选择系统默认存在的:

设置-网络

连接方式选“Bridged Adapter”

界面名称选“virbr0”

虚机里的IP段可在物理机执行#ifconfig查看到,设置为同一段IP即可

 

二、

自定义一个桥
1、建立桥接网络
apt-get install bridge-utils

修改/etc/network/interfaces
增加以下内容:
####################################################
# VirtualBox NAT bridge
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服务器和dns服务器
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/目录下面增加一个名为virtualbox的文件,内容为:
#######################################
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
# mac地址 , 主机名, 分配的ip
dhcp-host=08:08:27:00:00:02,vmxp,172.16.0.20 # Windows XP
#######################################

4、启动dnsmasq

# /etc/init.d/dnsmasq start

如果报错:dnsmasq:failed to bind DHCP server socket:地址已在使用

原因:本机的dns服务已把53端口占用

解决:/etc/dnsmasq.conf去除如下的注释

   bind-interfaces

5、修改virtualbox 虚拟机配置
a. 打开虚拟机配置
b. 选择网络
c. 选择Attached to: “Bridged Adapter”
Name: “vnet0″
Advanced-> Mac Address:080827000002 #对应3步中virtualbox给虚拟机分配的mac地址,注意去掉冒号