Virtualbox支持客户机和宿主机之间共享目录,但是根据版本不同,会有些问题。例如对于3.1.x版本,windows XP host和Debian guest组合下,VBox自己的共享目录(写入)性能极差甚至报错(本人遇到)。其实,在客户机和宿主机之间共享文件的还有一些其他方式,例如可以通过ftp或samba,二者都支持所有操作系统下的共享,但是相比之下samba更具优点,因为在Windows和Linux下都可以把Samba共享目录映射为本地磁盘目录。

先来看Linux访问Windows共享目录的情况。这比较容易,因为Windows上共享文件十分方便,可以通过点击右键实现文件夹共享。在Linux客户端,只要使用smbclient或者smbmount命令即可以实现远程共享目录的访问和映射。具体步骤包括:
1. 在Windows机器上选择文件夹,点击属性-》共享,指定共享名称和权限(是否可写)
2. 在Linux客户机上安装smbclient,然后执行smbclient -L ip_of_win_server来查看服务器所有共享资源。
3. 链接到共享文件夹,smbclient //ip_of_win_server/sharename -U username来连接,如果成功则会出现smb提示符可以使用samba client的命令操作远程目录。(默认需要Windows用户密码)或者使用sudo smbmount //ip_of_win_server/sharename local_path -o uid=your_user_name gid=your_group_name 来映射磁盘目录。

接下来看一看Linux做Samba服务器的情况。和上面的主要不同是,Linux需要安装和配置Samba server,而不是像Windows那样简便。首先安装服务器软件apt-get install samba gadmin-samba,后者是一个可视化Samba配置工具,没有它也是可以的,主要就是编写/etc/samba/smb.conf文件。由于Samba配置比较复杂,参数众多,所以建议使用图形化工具。这里只提几点配置要注意的问题,首先是创建一个section,例如[test],在这个section下面指定所需属性,属性的关键词可以查询Samba手册。如果使用图形工具就不用记太多名称了,多半就是点点鼠标而已。这里列出我机器上smb.conf中的部分内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
[global]
netbios name = vboxsmb
server string = Samba file and print server
workgroup = Workgroup
security = user
hosts allow = 127. 192.168.0. 192.168.1.
interfaces = 127.0.0.1/8 192.168.0.0/24 192.168.1.0/24
bind interfaces only = yes
remote announce = 192.168.0.255 192.168.1.4 192.168.1.3
remote browse sync = 192.168.0.255
printcap name = cups
load printers = yes
cups options = raw
printing = cups
guest account = smbguest
log file = /var/log/samba/samba.log
max log size = 1000
null passwords = no
username level = 6
password level = 6
encrypt passwords = yes
unix password sync = yes
socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
local master = no
domain master = no
preferred master = no
domain logons = no
os level = 33
logon drive = m:
logon home = \\%L\homes\%u
logon path = \\%L\profiles\%u
logon script = %G.bat
time server = no
name resolve order = wins lmhosts bcast
wins support = no
wins proxy = no
dns proxy = no
preserve case = yes
short preserve case = yes
client use spnego = no
client signing = no
client schannel = no
server signing = no
server schannel = no
nt pipe support = yes
nt status support = yes
allow trusted domains = no
obey pam restrictions = yes
enable spoolss = yes
client plaintext auth = no
disable netbios = no
follow symlinks = no
update encrypted = yes
pam password change = no
passwd chat timeout = 120
hostname lookups = no
username map = /etc/samba/smbusers
smb passwd file = /etc/samba/smbpasswd
passwd program = /usr/bin/passwd '%u'
passwd chat = *New*password* %n\n *ReType*new*password* %n\n *passwd*changed*\n
add user script = /usr/sbin/useradd -d /dev/null -c 'Samba User Account' -s /dev/null '%u'
add user to group script = /usr/sbin/useradd -d /dev/null -c 'Samba User Account' -s /dev/null -g '%g' '%u'
add group script = /usr/sbin/groupadd '%g'
delete user script = /usr/sbin/userdel '%u'
delete user from group script = /usr/sbin/userdel '%u' '%g'
delete group script = /usr/sbin/groupdel '%g'
add machine script = /usr/sbin/useradd -d /dev/null -g sambamachines -c 'Samba Machine Account' -s /dev/null -M '%u'
machine password timeout = 120
idmap uid = 16777216-33554431
idmap gid = 16777216-33554431
template shell = /dev/null
winbind use default domain = yes
winbind separator = @
winbind cache time = 360
winbind trusted domains only = yes
winbind nested groups = no
winbind nss info = no
winbind refresh tickets = no
winbind offline logon = no

[lit]
path = /home/lit
comment = Ting's home folder
valid users = lit
write list = lit
admin users = lit
directory mask = 0777
create mode = 0777
read only = no
available = yes
browseable = yes
writable = yes
guest ok = no
public = no
printable = no
share modes = no
locking = no

注意的是,由于是在本机使用VBox,所以没有对共享目录权限做严格设置,如果需要在网络上共享的话,需要小心设置。
修改完配置文件之后,执行/etc/init.d/samba restart重启服务。然后在Linux使用上面相似的smbclient和smbmount命令测试连接。在Windows客户机上,我们使用net view \\linux_server_ip来查看共享目录,使用net use win_driver: \\linux_server_ip\sharename来把远程共享目录映射为一个新的本地驱动器win_driver。映射以后无论在客户机还是host上都可以像使用本地目录一样读写同一个文件夹,十分方便。

最后提一句VBox的几种网络模式的异同:NAT最简单,客户机透过宿主直接访问外网,客户机和宿主机间的通讯无法干预和控制(因此不能用于构建Samba客户和服务器);bridged同样允许客户机访问外网,但是客户机和宿主机在同一内网,相互独立(IP)并可以通讯(可用户Samba相互共享);Internal模式允许客户机之间相对独立且可以通讯但对主机透明,客户机不能访问外网;host only模式只允许一个客户机节点访问外网其它客户机形成内网。在实际使用Bridged模式时,我的路由器似乎不能把客户机DNS请求发送出去,很是奇怪。网上有人认为是VBox的bug,也有的认为是其它软件在作祟。不过想到一个简单的解决办法,就是自己给客户机设定DNS。例如可以使用免费好用的google DNS 8.8.8.8 或 8.8.4.4 .