虚拟机Linux共享宿主机Windows资源详解

来源:互联网 发布:网络运营策划 工资 编辑:程序博客网 时间:2024/06/05 19:39

 

 

载之:http://hi.baidu.com/52hack/blog/item/5774c13dcdd5efcf9f3d62c4.html

 

 

虚拟机的使用的确给Linux的学习者提供了很大的方便。不过在Linux学习过程中,当涉及到应用软件的使用时,虽然可以直接从网上下载程序包或源码,但用惯了迅雷,对Linux中的下载速度简直无法忍受,且原有的很多资源本应该可以直接使用,没有必要重新下载。

因而在两个系统中共享信息成为亟待解决的问题。
在网上搜索了大量相关信息,介绍两个系统间信息共享的不少,但是提供虚拟机host-guest机不同系统之间资源共享解决方案的不多。在朋友的帮助下,经过多次尝试和摸索,终于有了一些搜获。现提供一套包括局域网配置在内的较为详细的解决方案,供初学者参考。
环境介绍:
虚拟机:VMware Workstation 5.5
Host机系统:Windows 2000 Server
Guest机系统:Red Hat Enterprise Linux 4

其实作为两个系统而言,要进行资源的共享,方法很多,最初我尝试了使用mount命令挂载文件系统。从命令本身来看,想要挂载一个Windows下的文件系统或驱动盘似乎没有什么问题。
首先在Linux系统/mnt空目录下,建立挂载点:#mkdir /mnt/mystudy
/mnt目录是专门用来当作挂载点的目录。mystudy是自定义的专用挂载点名称。

然后我们看一下mount命令的使用方法:
命令格式:  mount [-t vfstype] [-o options] device dir
主要参数:
① -t vfstype 指定文件系统的类型,通常不必指定。mount 会自动选择正确的类型,Windows NT ntfs文件系统:ntfs
② -o options 主要用来描述设备或档案的挂接方式。
③ device:要挂接(mount)的设备。Linux中,设备名通常存放在/dev中,设备名遵循一些规则,如:/dev/hda1中,hd指Hard Disk,a代表第一个硬盘设备,1代表第一个硬盘的第一主分区。
④ dir:设备在系统上的挂接点(mount point)。如,我们刚刚建立的挂载点:/mnt/mystudy

遵循此规则,希望挂载硬盘的第二个主分区,
键入命令: #mount -t ntfs /dev/hda2 /mnt/mystudy
返回信息: Mount:fs type ntfs not supported by kernel

竟然无法找到ntfs格式的分区?那么我的Windows系统跑到哪里去了?
查看一下查看一下分区情况:#fdisk –L
得到如下结果:

screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-2.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
发现根本没有找到Windows的分区。
这个时候才想起,我的Linux装在虚拟机里,虚拟机分配给Linux系统的设备仅仅是8G大小的虚拟硬盘分区,所以根本没有办法挂载Windows系统所在的分区以及其他的硬盘分区。

那么在虚拟机环境下,想要共享资源,mount命令基本上派不上用场了,只能从联机的方式来考虑了。
提供最常用的两种方案:
1. FTP访问
2. Samba服务

不管是FTP还是Samba,首先都需要分析虚拟机环境下的Host-Guest网络连接。基本上我们可以把Host机与Guest机之间当作局域网中的两台机器来实现互联。那么现在需要重新考虑关于《虚拟机的网络配置》一文中提到过的网络连接方式。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-1.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
当时我们选择以NAT模式进行网络连接,这是一种使用DHCP服务进行网络连接的方式,也就是说,Guest机并没有自己的固定IP地址,这个地址是在启动了系统之后,从Host机上取得的、一个暂时提供给Guest机使用的IP地址。
在这种网络连接方式下,设置Guest机中系统的IP获取方式为自动获取,不管Host机联网的方式如何,只要Host能上网,Guest机也可以上网。
虽然选择这种方式,外网连接是很方便,可是在Host-Guest之间的局域网连接真的让人伤透了脑筋,虚拟网卡VMnet8的IP与Host机的IP不在同一网段,折腾了一个晚上也没有找到好的解决办法,最后舍弃了NAT模式。将LINUX虚拟机的网连方式改为了Bridge。
Bridge模式将虚拟主机的虚拟网卡桥接到一个Host主机的物理网卡上,可以理解为Guest机和Host主机处于对等的地位,在网络关系上是平等的,对于我这种对路由的事情比较白痴的人来说,这是最易理解的一种局域网互联方式。
我的使用网络服务是小区LAN网,IP自然是自动获取的,不用花心思改动它,将Guest机的网络也设置成为自动获取IP:
应用程序-->系统设置-->网络-->编辑eth0设备
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-3.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
Host机的ipconfig结果:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-4.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
Guest机ifconfig结果为:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-5.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
看到了吧?Host和Guest在同一网段,基本上就像局域网里面的两台机器。
Host设置一下FTP, FTP算是实现对Host机资源的访问了,可文件的管理似乎不那么方便。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-6.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
另一种共享Windows资源的方法是很多Linux用户都非常青睐的Samba。Samba是一套让UNIX系统能够应用Microshoft网络通讯协议的软件。它使执行UNIX系统的计算机能与执行Windows系统的计算机分享驱动器与打印机。非常适用于Windows和Linux并存的网络。Red Hat AS 4.0内附Samba Server,但默认并不是在系统启动时自动运行的。可通过service命令查看和控制Samba服务:


  • 查看Samba服务运行状态:# service smb status
  • 启动Samba服务:# service smb start
  • 停止Samba服务:# service smb stop
  • 也可设置系统重启时,自动启动Samba服务:# chkconfig --level 345 smb on


首先我们在Windows系统中设置文件夹或驱动器Web共享,并对其设置别名,如:/study
然后在Linux中,以用户身份登录smb服务:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-7.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
常见的错误提示:
1、防火墙问题
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-8.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
使用Samba服务时,Host和Guest都应关闭防火墙。
Linux系统下关闭防火墙的方法:系统设置-->服务器设置-->服务-->iptable-->停止
2.登录用户名或密码错误
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-9.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
验证用户名密码即可。
成功登录smb
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-10.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
可用LS命令查看当前目录下的共享文件:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-11.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
用get命令可从服务器上下载某文件:
Smb:/> get file1 file2(从服务器上下载file1,以file2为名保存在本地——默认为root主目录)
Exit命令可退出smb服务器
如果仍然觉得文件的管理不方便,可以使用smbmount 命令,挂载Windows的共享文件夹或驱动器:
# smbmount //192.168.0.162/study /mnt/mystudy –o username=admin
其中,192.168.0.162为Host机IP;study为共享文件夹别名;/mnt/mystudy为挂载点,可由#mkdir /mnt/mystudy创建;admin为Host机登录用户名。
打开挂载目录:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www.pcdog.com/ArtImage/20070924/231910-12.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
这样看起来习惯多了吧?跟Windows里面的文件管理界面几乎相同了。
至此,虚拟机Linux共享宿主机Windows资源基本实现,完成时候觉得很简单,但是一路摸索过来的确遇到了不少问题。