您的位置:首页 > 运维架构 > Linux

CentOS7基于NFS服务的文件共享

2017-03-21 16:14 204 查看

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 enablenfs-server.service

 

然后分别启动rpcbind和nfs服务:

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

[root@10-12-35-251 ~]#systemctl startnfs-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 鈥榯est鈥Permissiondenied

 

报错关键字: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:40testfile

 

在客户端使用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 强制结束)

 

5        参考

http://blog.dreamlu.net/blog/67
http://www.cnblogs.com/mchina/archive/2013/01/03/2840040.html http://www.server-world.info/en/note?os=CentOS_7&p=nfs
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CentOS7 nfs