简述

在linux与windows之间共享目录使用smbfs. 但是在linux与linux之间共享目录要用nfs(Network File System网络文件系统). 服务端用nfs提供共享目录服务,客户端通过mount将共享目录挂载到本地.

以fedora 20设置共享目录为例

  • 安装nfs
fedora 20要安装以下两个包
[wyq@localhost ~]$ rpm -qa |grep nfs
libnfsidmap-0.25-8.fc20.x86_64
nfs-utils-1.3.0-2.1.fc20.x86_64
  • 配置共享目录
在/etc/exports文件中添加以下内容
/home/wyq/share  192.* (insecure,rw,sync,no_root_squash)
  • 设置防火墙,允许nfs服务
输入sudo firewall-config打开防火墙配置界面,选中nfs服务
  • 启动nfs服务
sudo service nfs start
  • 挂载目录
sudo mount -t nfs 127.0.0.1:/home/wyq/share /home/wyq/mount
  • 取消挂载
umount /home/wyq/mount
 
 
 
exports配置文件说明
/home/wyq/share 共享的目录
192.*           允许访问的主机IP, 如果不能确定 ,请使用 *(在fedora 20实验中表示任意IP时,不用加星号,否则nfs会无法启动) 
insecure        一个安全选项, 如果nfs服务端口号小于1024则可以不添加这个选项, 否则不添加的话, 是无法访问的.其他主机访问的话就会被拒绝.
rw              共享目录的权限,rw 是可读写的权限,只读的权限是ro.
sync            同步的选项, 可选的还有 async. sync是不使用缓存,随时写入同步, async是使用缓存的.
no_root_squash  NFS服务共享的目录的属性, 如果用户是root, 那么对这个目录就有root的权限.
 
启动nfs失败示例
  • 启动nfs时提示失败
[wyq@localhost workspace]$ sudo service nfs start
Redirecting to /bin/systemctl start  nfs.service
Job for nfs-server.service failed. See 'systemctl status nfs-server.service' and 'journalctl -xn' for details.
  • 根据提示查看错误信息
[wyq@localhost workspace]$ sudo systemctl status nfs-server.service
nfs-server.service - NFS Server
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled)
   Active: failed (Result: exit-code) since 四 2014-06-19 22:20:37 CST; 5s ago
  Process: 4984 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
  Process: 4980 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 3778 ExecStartPost=/usr/libexec/nfs-utils/scripts/nfs-server.postconfig (code=exited, status=0/SUCCESS)
  Process: 3761 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS $RPCNFSDCOUNT (code=exited, status=0/SUCCESS)
  Process: 5371 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=1/FAILURE)
  Process: 5369 ExecStartPre=/usr/libexec/nfs-utils/scripts/nfs-server.preconfig (code=exited, status=0/SUCCESS)
 Main PID: 3761 (code=exited, status=0/SUCCESS)

6月 19 22:20:37 localhost.localdomain systemd[1]: Starting NFS Server...
6月 19 22:20:37 localhost.localdomain exportfs[5371]: exportfs: No options for /home/wyq/share *: suggest *(sync) to avoid warning
6月 19 22:20:37 localhost.localdomain exportfs[5371]: exportfs: No host name given with /home/wyq/share (insecure,rw,sync,no_root_squash), suggest *(insecure,rw,sync,no_root_squash) to avoid warning
6月 19 22:20:37 localhost.localdomain exportfs[5371]: exportfs: incompatible duplicated export entries:
6月 19 22:20:37 localhost.localdomain exportfs[5371]: exportfs:         *:/home/wyq/share (0x422) [IGNORED]
6月 19 22:20:37 localhost.localdomain exportfs[5371]: exportfs:         *:/home/wyq/share (0x425)
6月 19 22:20:37 localhost.localdomain systemd[1]: nfs-server.service: control process exited, code=exited status=1
6月 19 22:20:37 localhost.localdomain systemd[1]: Failed to start NFS Server.
6月 19 22:20:37 localhost.localdomain systemd[1]: Unit nfs-server.service entered failed state.
注意到信息中"exportfs: incompatible duplicated export entries:" 意思是有重复配置
  • 查看配置文件/etc/exports, 内容如下
/home/wyq/share  *  (insecure,rw,sync,no_root_squash)
  • 猜测是配置错误导致nfs无法启动
将配置更改为以下几种,逐一尝试
第一种
/home/wyq/share  192.*  (insecure,rw,sync,no_root_squash)
第二种
/home/wyq/share  (insecure,rw,sync,no_root_squash)
 
发现使用以上两种方式的配置,nfs均能正常启动并且被挂载.
  • 结论
在fedora 20的nfs配置文件/etc/exports中,如果想让任意主机访问共享目录,不需要用*表示,不填即表示任意主机.