设为首页 收藏本站
查看: 636|回复: 0

[经验分享] CentOS7基于NFS服务的文件共享

[复制链接]

尚未签到

发表于 2018-4-21 10:39:19 | 显示全部楼层 |阅读模式
  1        NFS服务器安装与配置
  1.1   环境信息
  操作系统:centos7
  内核版本:3.10.0-327.el7.x86_64
  

  1.2   NFS安装与配置
  关闭selinux功能:
[root@10-12-35-251 ~]# setenforce 0

  

  查看selinux状态:
[root@10-12-35-251 ~]# sestatus

  SELinux status:                 disabled
  

  

  服务器NFS软件包安装:
[root@10-12-35-251 ~]# yum install -y nfs-utils

  

  

  配置export配置文件:
[root@10-12-35-251 ~]# cat /etc/exports

  

  /data/ *(rw,sync,fsid=0)
  

  注:*代表任何IP可以访问并挂载;rw表示可读写;sync表示同步写,fsid=0表示将/data找个目录包装成根目录
  

  新建/data共享目录:
[root@10-12-35-251 ~]# mkdir /data

  

  

  1.3   启动NFS服务
  先为rpcbind和nfs做开机启动:
  

[root@10-12-35-251 ~]#systemctl enable rpcbind.service

[root@10-12-35-251 ~]#systemctl enable nfs-server.service

  

  然后分别启动rpcbind和nfs服务:
[root@10-12-35-251 ~]#systemctl start rpcbind.service

[root@10-12-35-251 ~]#systemctl start nfs-server.service

  

  确认NFS服务器启动成功:
[root@10-12-35-251 ~]#rpcinfo -p

  

  检查 NFS 服务器是否开放挂载我们想共享的目录 /data:
[root@10-12-35-251 ~]#exportfs

  /data           <world>
  

  

  2        客户端进行NFS目录挂载
  启动rpcbind服务:
[root@localhost ~]# systemctl start rpcbind.service

  

  检查共享目录是否存在:
[root@localhost ~]# showmount -e 10.12.35.251

  Export list for 10.12.35.251:
  /data *
  

  挂载NFS共享目录:
  [root@localhost ~]# mount -t nfs 10.12.35.251:/data//mnt
  

  查看客户端mount信息:
  10.12.35.251:/data/ on /mnt type nfs(rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=10.12.35.251,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=10.12.35.251)
  

  3        NFS客户端文件写入测试
  3.1   客户端root用户写测试
[root@localhost ~]# cd /mnt

[root@localhost mnt]# touch test2

  touch: cannot touch test2Permissiondenied
  

  报错关键字:Permission denied
  出现这个错误是由于NFS服务器没有开放目录权限导致。
  

  

  查看NFS目录权限:
[root@10-12-35-251 ~]# ll /data/

  total 26688
  -rw-r--r-- 1 root root 27323419 Mar 2115:03 elasticsearch-1.7.1.noarch.rpm
  -rw-r--r-- 1 root root       30 Mar 21 15:19 test
  drwxr-xr-x 2 root root        6 Mar 21 15:03 test1
  注:可以看到此目录只有root用户才能写入;
  

  

  对NFS目录赋予777权限:
[root@10-12-35-251 ~]# chmod 777 -R/data

[root@10-12-35-251 ~]# ll /data/

  total 26688
  -rwxrwxrwx 1 root root 27323419 Mar 2115:03 elasticsearch-1.7.1.noarch.rpm
  -rwxrwxrwx 1 root root       30 Mar 21 15:19 test
  drwxrwxrwx 2 root root        6 Mar 21 15:03 test1
  

  在客户端即可进行文件写入:

[root@localhost mnt]# touch test2

[root@localhost mnt]# echo ooooo>> test2

[root@localhost mnt]# cat test2

  ooooo
  

  查看目录的文件内容:

[root@localhost mnt]# ll

  total 26692

  -rwxrwxrwx 1 root      root     27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm

  -rwxrwxrwx 1 root      root           30 Mar 21 15:19 test

  drwxrwxrwx 2 root      root             6 Mar 21 15:03 test1

  -rw-r--r-- 1nfsnobody nfsnobody        6 Mar 21 15:33test2

  注:用root 用户建立的文件test2,变成了nfsnobody 用户。

  

  NFS服务有很多默认的参数,通过NFS服务器的/var/lib/nfs/etab文件查看/data目录完整权限设定。
  

[root@10-12-35-251 ~]# cat/var/lib/nfs/etab

  

  /data  *(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,fsid=0,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
  

  默认就有sync,wdelay,hide 等等,no_root_squash 是让root保持权限,root_squash 是把root映射成nobody,no_all_squash不让所有用户保持在挂载目录中的权限。所以,root建立的文件所有者是nfsnobody。
  

  3.2   客户端使用普通用户测试
[root@localhost mnt]# groupadd test

[root@localhost mnt]# useradd test -gtest

[root@localhost mnt]# su test

[test@localhost mnt]$ ls

  elasticsearch-1.7.1.noarch.rpm  test test1  test2

  

  创建文件

[test@localhost mnt]$ touch testfile

  

  写入内容
[test@localhost mnt]$ echo"thisistestfileintheNFS" >>testfile

  

  查看文件内容
[test@localhost mnt]$ cat testfile

  thisistestfileintheNFS

  

  查看文件权限
[test@localhost mnt]$ ll

  total 26696
  -rwxrwxrwx 1 root      root     27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm
  -rwxrwxrwx 1 root      root            30 Mar 21 15:19 test
  drwxrwxrwx 2 root      root             6 Mar 21 15:03 test1
  -rw-r--r-- 1 nfsnobody nfsnobody        6 Mar 21 15:33 test2
  -rw-rw-r-- 1 test     test            23 Mar 21 15:40 testfile
  

  在客户端使用root用户对testfile写入内容:
[root@localhost mnt]# echo 'sdfdd'>> testfile

  -bash: testfile: Permission denied
  

  权限不够,普通用户写入文件时文件权限为用户名与组,这也就保证了服务器的安全性。
  

  3.3   针对客户端root用户权限的解决方法
  连接到NFS服务器,查看NFS目录的配置信息:
  

[root@10-12-35-251 ~]# exportfs -v

  /data           <world>(rw,wdelay,root_squash,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
  

  

  修改服务器端export配置文件:
[root@10-12-35-251 ~]# cat /etc/exports

  /data/ *(rw,sync,no_root_squash,fsid=0)
  

  注:添加no_root_squash参数,表明此时客户端root用户的身份等同于NFS server上面的root用户;
  

  再次载入服务器端NFS配置:
[root@10-12-35-251 ~]# exportfs -rv

  exporting *:/data
  

  查看现有NFS目录权限配置:
[root@10-12-35-251 ~]# exportfs -v

  

  /data           <world>(rw,wdelay,no_root_squash,no_subtree_check,fsid=0,sec=sys,rw,secure,no_root_squash,no_all_squash)
  

  

  在客户端使用root用户创建文件:
[root@localhost mnt]# echothisisrootuserfile >> rootfile

  

  查看root用户创建的文件权限:
[root@localhost mnt]# ll

  total 26700
  -rwxrwxrwx 1 root      root     27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm

  -rw-r--r-- 1root      root            19 Mar 21 16:02 rootfile

  -rwxrwxrwx 1 root      root            30 Mar 21 15:19 test

  drwxrwxrwx 2 root      root             6 Mar 21 15:03 test1

  -rw-r--r-- 1 nfsnobody nfsnobody        6 Mar 21 15:33 test2

  -rw-rw-r-- 1 test      test            23 Mar 21 15:40 testfile

  

  可以看到客户端root用户创建的文件已经升级为NFS服务器端的root用户权限。
  

  4        扩展阅读
  4.1   关于NFS权限扩展阅读
  1. 客户端连接时候,对普通用户的检查
  

  a. 如果明确设定了普通用户被压缩的身份,那么此时客户端用户的身份转换为指定用户;
  b. 如果NFS server上面有同名用户,那么此时客户端登录账户的身份转换为NFS server上面的同名用户;

  

  c. 如果没有明确指定,也没有同名用户,那么此时用户身份被压缩成nfsnobody;
  

  2. 客户端连接的时候,对root的检查
  

  a. 如果设置no_root_squash,那么此时root用户的身份被压缩为NFS server上面的root;
  

  b. 如果设置了all_squash、anonuid、anongid,此时root 身份被压缩为指定用户;
  

  c. 如果没有明确指定,此时root用户被压缩为nfsnobody;
  

  d. 如果同时指定no_root_squash与all_squash 用户将被压缩为 nfsnobody,如果设置了anonuid、anongid将被压缩到所指定的用户与组;
  

  

  4.2   NFS相关命令扩展
  1、exportfs
  

  如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:
  

  # exportfs [-aruv]
  

  -a 全部挂载或卸载 /etc/exports中的内容
  

  -r 重新读取/etc/exports 中的信息,并同步更新/etc/exports、/var/lib/nfs/xtab
  

  -u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
  

  -v 在export的时候,将详细的信息输出到屏幕上。
  

  

  具体例子:
  

  # exportfs -au 卸载所有共享目录
  # exportfs -rv 重新共享所有目录并输出详细信息

  

  2、nfsstat
  查看NFS的运行状态,对于调整NFS的运行有很大帮助。

  

  3、rpcinfo
  查看rpc执行信息,可以用于检测rpc运行情况的工具,利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些。

  4、showmount

  -a 显示已经于客户端连接上的目录信息

  -e IP或者hostname 显示此IP地址分享出来的目录

  

  5、netstat
  

  可以查看出nfs服务开启的端口,其中nfs 开启的是2049,portmap 开启的是111,其余则是rpc开启的。
  

  最后注意两点,虽然通过权限设置可以让普通用户访问,但是挂载的时候默认情况下只有root可以去挂载,普通用户可以执行sudo。
  

  NFS server 关机的时候一点要确保NFS服务关闭,没有客户端处于连接状态!通过showmount -a 可以查看,如果有的话用kill killall pkill 来结束,(-9 强制结束)
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-449950-1-1.html 上篇帖子: CentOS 6.0系列更新安全补丁 下篇帖子: centos 安装 jumpserver
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表