首先根据前人的介绍,安装两个软件包uml-utilities和bridge-utils,用apt-get还是新立得自己选吧。
根据前人提供的脚本文件,内容如下:
代码: 全选
#!/bin/sh
# vim:set ft=sh:
## 2007-09 windwiny vboxbridgedrun.sh
## 使用前自行修改 id,ip,gw,eth?,br?,tap? 等相关参数,
# VirtualBox Bridging VirtualBox 实现桥接模式
## 参考 http://www.oceanboo.cn/read.php?55
# Ubuntu 里安装软件包
## sudo apt-get install uml-utilities bridge-utils
## ---------------------------------------------------------------------
#function
usage()
{
echo -e "$RED Usage:
$GREEN sudo `basename "$0"` {start|stop} $WHITE\n"
}
if [ `id -u` -ne 0 ]; then
echo " must root can run this "
usage
exit 1
fi
. /lib/lsb/init-functions
[ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true
if [ -f ~/MYIP ] ; then
source ~/MYIP
else
IP=192.168.1.243 && NETMASK=255.255.255.0 && GW=192.168.1.1
fi
#function
start() {
# echo -e "$GREEN `basename "$0"` START $WHITE"
# log_begin_msg "$GREEN `basename "$0"` START $WHITE"
log_begin_msg "`basename "$0"` START"
log_end_msg 0
# Create a tap device with permission for the user running vbox
# 建立一个使用者(user)有权限的设备tap?,-u 参数为自己用户名 或 id。这里的4个lv是我启动VBox的用户名,你应改成你自己的用户名。这里一气整了4个虚拟网卡tap0~tap3,你可以同时运行4台虚拟机。
tunctl -t tap0 -u lv # 不能用 `id -u`,因为使用sudo 执行时id为0,
tunctl -t tap1 -u lv
tunctl -t tap2 -u lv
tunctl -t tap3 -u lv
chmod 0666 /dev/net/tun
# Bring up eth? and tap? in promiscuous mode
# 将eth?和tap?网卡界面设为混杂模式(Promiscuous)
ifconfig eth0 0.0.0.0 promisc
ifconfig tap0 0.0.0.0 promisc
ifconfig tap1 0.0.0.0 promisc
ifconfig tap2 0.0.0.0 promisc
ifconfig tap3 0.0.0.0 promisc
# Create a new bridge and add the interfaces to the bridge.
# 建立新的桥接界面(bridge),並把 eth?, tap? 加入bridge
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 tap0
brctl addif br0 tap1
brctl addif br0 tap2
brctl addif br0 tap3
# 下面是两种获取IP的方式,可以自由选择,把不需要的注释掉就好了。
# 将bridge设成静态IP。IP、子网掩码、网关,这些在第34行设置。
#ifconfig br0 $IP netmask $NETMASK up
# route add default gw $GW
# 将bridge设成动态DHCP分配IP。我的宽带路由能自己分地址,所以我选择DHCP
dhclient br0
}
#function
stop() {
# echo -e "$GREEN `basename "$0"` STOP $WHITE"
# log_begin_msg "$GREEN `basename "$0"` STOP $WHITE"
log_begin_msg "`basename "$0"` STOP"
log_end_msg 0
## 刪除 tap?
tunctl -d tap0
tunctl -d tap1
tunctl -d tap2
tunctl -d tap3
##
## 停止 br?
ifconfig br0 down
## 将 tap?, eth? 移出bridge
brctl delif br0 eth0
brctl delif br0 tap0
brctl delif br0 tap1
brctl delif br0 tap2
brctl delif br0 tap3
## 刪除 br?
brctl delbr br0
##
## 自定义恢复IP地址,默认网关
ifconfig eth0 $IP netmask $NETMASK up
route add default gw $GW
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
exit 1
;;
esac
exit 0
在保存vnet.sh文件的目录下,根据需要执行下面的命令
启动虚拟网卡
sudo ./vnet.sh start
停止虚拟网卡
sudo ./vnet.sh stop
重启虚拟网卡
sudo ./vnet.sh restart
在VBox中设置,虚拟机1(win2000)网卡设成Host interface,主机界面tap0,见图。虚拟机2(winxp)网卡设成Host interface,主机界面tap1,见图。
启动虚拟机1和虚拟机2,在虚拟机里分别设置其网卡获取IP方式为DHCP,设置工作为WORKGROUP,可以查看虚拟机获取的IP地址,见图。在虚拟机的网上邻居中就可以找到相邻的计算机,见图。设置共享资源,即可。