配置使VirtualBox下的linux可以宿主机互访并上网

1. 设置VirtualBox,选择桥接网卡

 

2. 配置linux的ifcfg-eth0

配置完成后,用service network restart重启网络。

3. 然后查后路由配置是否正确


如果没有 192.168.2.1 记录,使用 route add default gw 192.168.2.1,添加网关。

route add添加重启就会没有的了,这时需要在/etc/rc.d/rc.local文件添加 route add default gw 192.168.2.1 这条记录。

****************************** 迁移系统 *************************
后来需要换工作机,这样虚拟机也需要迁移了。一开始我把相关的都 copy 过去,然后新建一个同名的虚拟机,vdi选择 copy 过来的那个。这样确实可以重建到虚拟机,
但开机启动后,发现这是最初安装时的版本的。我那台虚拟机,做了两个备份,备份里安装了 nginx 和 larlel ,如果不恢复备份需要重新安装,又要花很多时间。

所以这个方法是行不通的。
于是所有 copy 都删除。搜索旧工作机C盘下面的 VirtualBox.xml 文件,复制红框的代码到新工作机相应的 xml 文件中。

copy 旧工作机的 F:\VirtualBox VMs\Centos6.5 整个文件夹,到新工作机。做完这步工作后,再修改新工作机的 VirtualBox.xml 配置文件。

<?xml version="1.0"?>
<!--
** DO NOT EDIT THIS FILE.
** If you make changes to this file while any VirtualBox related application
** is running, your changes will be overwritten later, without taking effect.
** Use VBoxManage or the VirtualBox Manager GUI to make changes.
-->
<VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-windows">
  <Global>
    <ExtraData>
      <ExtraDataItem name="GUI/DetailsPageBoxes" value="general,system,preview,display,storage,audio,network,usb,sharedFolders,description"/>
      <ExtraDataItem name="GUI/GroupDefinitions/" value="m=234ad341-890c-4a35-b9ae-a3558ee1a6f0"/>
      <ExtraDataItem name="GUI/LastItemSelected" value="m=Centos6.5"/>
      <ExtraDataItem name="GUI/LastWindowPosition" value="574,345,770,550"/>
      <ExtraDataItem name="GUI/RecentFolderHD" value="F:/VirtualBox VMs/Centos6.5"/>
      <ExtraDataItem name="GUI/RecentListHD" value="F:\VirtualBox VMs\Centos6.5\Centos6.5.vdi"/>
      <ExtraDataItem name="GUI/SplitterSizes" value="255,510"/>
      <ExtraDataItem name="GUI/SuppressMessages" value="confirmInputCapture"/>
      <ExtraDataItem name="GUI/UpdateCheckCount" value="5"/>
      <ExtraDataItem name="GUI/UpdateDate" value="1 d, 2016-07-17, stable, 5.0.24"/>
    </ExtraData>
    <MachineRegistry>
      <MachineEntry uuid="{234ad341-890c-4a35-b9ae-a3558ee1a6f0}" src="F:\VirtualBox VMs\Centos6.5\CentOS6.5.vbox"/>
    </MachineRegistry>
    <MediaRegistry>
      <HardDisks/>
      <DVDImages/>
      <FloppyImages/>
    </MediaRegistry>
    <NetserviceRegistry>
      <DHCPServers>
        <DHCPServer networkName="HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter" IPAddress="192.168.56.100" networkMask="255.255.255.0" lowerIP="192.168.56.101" upperIP="192.168.56.254" enabled="1"/>
      </DHCPServers>
    </NetserviceRegistry>
    <SystemProperties defaultMachineFolder="F:\VirtualBox VMs\" defaultHardDiskFormat="VDI" VRDEAuthLibrary="VBoxAuth" webServiceAuthLibrary="VBoxAuth" LogHistoryCount="3" exclusiveHwVirt="false"/>
    <USBDeviceFilters/>
  </Global>
</VirtualBox>

上面着色部分是相应要修改的部分。打开任务管理器,把 VirtualBox 的所有进程都关闭了。再重新启动 VirtualBox 。发现已出现我们要迁移的虚拟机。
打开系统后,发现上不了网,查看 ifconfig 配置又正常。ping 192.168.2.30 提示: Destination host unreachable,上网搜索所有人都说是网卡有问题。关闭虚拟机,
进入 CentOS6.5.vbox 配置文件,检查网卡部分。发现网卡还是旧工作机的网卡,于是更改新的后再启动。ping 192.168.2.30 还是提示: Destination host unreachable。
service network restart 重启网卡试试,再ping,终于正常了。


******************************************* 2016-07-26 ***********************************************
后来搬迁后,内网是可以 ping 通的,但就是上不了外网。提示“connect: 网络不可达”。上网查了很多资料了,试了很多方法,都是上不了网。后来看到一篇博客说"NETWORK=192.168.2.255"这个的问题,注释掉,重启网络,再ping。终于可以上网了。

多次仔细确认各个网络配置文件之后,发现在网卡配置文件中(ifcfg-eth0)有一个NETWORK配置项,对于这个配置项,本身作用不是很清楚,因此到官网查看了一下说明:

    NETWORK=
    whereis the network address. This directive is deprecated, as the value is calculated automatically with ifcalc.


这节奏是告诉我们这个参数不要设置嘛(deprecate),这个值将会由ifcalc自动计算。既然不用设置,自然去掉。

去掉之后,重启network服务之后,外网竟然神奇的能够访问了。

附:CentOS linux 配置好网络后无法访问外网 connect: Network is unreachable


******************************************* 2016-07-26 ***********************************************

用了一段时间后,发现之前固定分配的8G空间已经不够用了。需要在 cdm 命令行下,执行以下命令,将空间扩大到20G

VBoxManage modifymedium "F:\VirtualBox VMs\Centos6.5\Centos6.5.vdi" --resize 20000

 

posted @ 2014-03-14 17:16  chy1000  阅读(550)  评论(0编辑  收藏  举报