用VirtualBox 对Linux虚机进行网络配置

1. 需求:

  • 各个虚机(linux操作系统)之间可联网互访
  • 各虚机可以上英特网
  • 各虚机有静态ip地址,当主机的物理机器移动时(比如从公司将笔记本主机带到家或者咖啡馆)不影响已有网络配置
  • 主机可以访问各个虚机


2. 方案:


1.将虚机的network配置为adapter1用host-only ,adapter2用NAT

hostonly 用来配置主机到虚机的访问,具有静态IP

NAT用来配置虚机到INTERNET的访问,具有动态 IP (DHCP)

2.In VirtualBox > Preferences > Network, set up a host-only network.

比如,it is manually configured:
ip 192.168.126.1
netmask 255.255.255.0

不要dhcp 

-------ununtu:-------------------------------------

 a..启动虚机,进入shell,执行 

ls /sys/class/net
会看到三个(虚拟的)网络适配器,比如: eth1 , eth2 (and lo, the loopback interface)

b.在 shell 执行   sudo vi /etc/network/interfaces

将内容编辑为如下:

# The loopback network interface
auto lo
iface lo inet loopback

# Host-only interface
auto eth1
iface eth1 inet static
        address         192.168.126.101
        netmask         255.255.255.0
        network         192.168.126.0
        broadcast       192.168.126.255

# NAT interface
auto eth2
iface eth2 inet dhcp

在你的机器上根据你的需要配置ip,并改变eth1 或者eth2

如果是桌面版的,在菜单上选择network connection->edit connection,配置 Wired Connection(或 wireless connection) 和 Auto Ethnet。使得wired connection具有手动(manual)ipv4地址,而Auto Ethnet的ipv4为Automatic(DHCP);无需编辑/etc/network/interfaces


----------RPM based linux, such as Redhat /centOS

a. configure static IP for hostonly

#  vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
HWADDR=xx:xx:xx:xx:xx:xx # Don't change, match your MAC_ADDRESS in virtual box setting
TYPE=Ethernet
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Don't change
ONBOOT=yes # no -> yes
NM_CONTROLLED=no # yes -> no
BOOTPROTO=none # dhcp -> none
# add bellow
IPADDR=192.168.56.10
NETMASK=255.255.255.0

b.configure DHCP for NAT

# vi  /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
HWADDR=xx:xx:xx:xx:xx:xx # Don't change,match your MAC_ADDRESS in virtual box setting
TYPE=Ethernet
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Don't change
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp



3. start network service

service network restart 重启网络服务(也可以当作检测,如果有错误查看哪一步错误了)

ifconfig eth0 up 打开eth0 网卡

ifup etho up  打开网络接口


你可能感兴趣的:(用VirtualBox 对Linux虚机进行网络配置)