NFS配置

作者: admin 分类: Linux 发布时间: 2018-06-21 15:39 浏览:884 次    

NFS 网络文件系统(Network File System) 是由Sun公司1984年发布的分布式文件系统协议。它允许客户端上的用户像访问本地文件一样地访问网络上的文件。

环境:centos7.4         nfs       模式:C/S       端口:111 2049

1、安装nfs

[root@xuexi ~]# yum -y install nfs-utils rpcbind

2、配置NFS

[root@xuexi ~]# mkdir /data
[root@xuexi ~]# chmod -R 666 /data
[root@xuexi ~]# vim /etc/exports
/data 17.20.10.0/24(rw,no_root_squash,no_all_squash,sync) 
注:17.20.10.0/24(rw,no_root_squash,no_all_squash,sync) 允许网段与括号之间不能有空格,否则会报权限不够问题
  • # /data  *(rw)   /data 是要共享的目录,*为任意网段,也可写指定网段,(rw)为读写权限
  • # rw 读写   ro只读
  • # sysnc 数据会同步写入内存及硬盘 ,async先写入内存而不直接写入硬盘    (可选)
  • # no_root_squash开放客户端使用 root 身份来操作服务器的文件, root_squash 使用nfsnobody身份 (可选)
  • # all_squash 所有身份都会被压缩成为匿名用户也就是nobody(nfsnobody) (可选)
  • # /data  *(rw,anonuid=xx,anongid=xx,sync)   /data anonuid是指定用户id号,如root是0

3、启动服务

[root@xuexi ~]# systemctl enable rpcbind nfs --now
注:如果服务启动失败,请检查rpcbind服务是否已开启,nfs是基于rpc服务的111端口工作 RPC在centos5.x下名称为portmap,在Centos6.x以上版本名称为rpcbind。

4、查看是否已监听

[root@nfs ~]# ss -anl | grep 111
u_str LISTEN 0 100 private/trace 37111 * 0
udp UNCONN 0 0 *:111 *:*
udp UNCONN 0 0 :::111 :::*
tcp LISTEN 0 128 *:111 *:*
tcp LISTEN 0 128 :::111 :::*
[root@nfs ~]# netstat -anutp |grep 2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN

5、客户端配置

5.1、安装客户端

[root@bogon ~]# yum -y install nfs-utils

5.2创建挂载目录

[root@bogon ~]# mkdir /tot

5.3、查看共享目录信息

[root@bogon ~]# showmount -e 172.20.10.8
 Export list for 172.20.10.8:
 /data 172.20.10.0/24

5.4、挂载

[root@bogon ~]# mount 172.20.10.8:/data /tot      #临时挂载
[root@bogon ~]# vi /etc/fstab      #永久挂载  172.20.10.8:/data /tot nfs defaults 0 0

5.5、测试

[root@bogon ~]# touch /tot/a.txt       #写入测试
[root@bogon ~]# ls /tot
a.txt

常见错误:

错误1
[root@bogon ~]# mount 172.20.10.8:/data /tot
 mount.nfs: Stale NFS file handle

原因:客户端之前挂载的tot目录在没有卸载的情况下,服务器端删除了data目录,才会出现这样的错误提示

解决方法:

[root@bogon ~]# umount -lf  /tot    #卸载
[root@bogon ~]# mount 172.20.10.8:/data /tot      #重新挂载
错误2
[root@bogon ~]# mount 172.20.10.8:/data /tot
[root@bogon ~]# touch /tot/a.txt
touch: setting times of `/mnt': Permission denied

原因:服务端给的权限不够

解决方法:
1、直接在服务端添加权限(注意权限过大问题)
[root@nfs ~]# chmod 777 /data
2、修改服务端属主
[root@nfs ~]# chown nfsnobody.   /data

错误3

[root@bogon ~]# ls /tot
ls: 无法访问/tot/a.txt: 权限不够
a.txt
[root@bogon ~]# showmount -e 172.20.10.8
Export list for 172.20.10.8:
/data (everyone) #此处应该是允许网段,而不是everyone

原因:服务端nfs权限未生效,可能是配置文件中允许网段和权限之间有空格

%title插图%num
解决方法:
去掉多余的空格,使用以下命令检测并生效
[root@nfs ~]# exportfs -r 
[root@nfs ~]# systemctl restart rpcbind nfs nfs-server.service


温馨提示:如无特殊说明,本站文章均为作者原创,转载时请注明出处及相应链接!

发表评论