NFS网络文件系统配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NFS网络文件系统配置相关的知识,希望对你有一定的参考价值。

NFS网络文件系统配置 



NFS简介


NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。


 

nfs安装包:nfs-utils

 

nfs配置文件:/etc/exports


语法:   文件系统  客户端(导出选项)  客户端(选项)


客户端处可填写:IP、FQDN或DOMAIN、NETWORK、*


 


NFS主要有3类选项:


访问权限选项


    设置输出目录只读:ro

    设置输出目录读写:rw


用户映射选项


    all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);

    no_all_squash:与all_squash取反(默认设置);

    root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);

    no_root_squash:与root_squash取反;以root身份创建目录和文件,此时共享目录权限(755);针对于其它用户向共享目录写入目录或文件,需要给共享目录权限(757)此时普通用户将普通用户自己身份写入目录和文件。

    anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);

    anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);


其它选项


    secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);

    insecure:允许客户端从大于1024的tcp/ip端口连接服务器;

    sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;

    async:将数据先保存在内存缓冲区中,必要时才写入磁盘;

    wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);

    no_wdelay:若有写操作则立即执行,应与sync配合使用;

    subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);

    no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;



 


查看NFS服务器端共享的文件系统


    showmount –e SERVER_IP


 


客户端使用NFS文件系统


    mount –t nfs SERVER:/path/to/sharedfs /path/to


 

  # exportfs [-aruv]


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

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

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

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


    具体例子: 

         exportfs:维护exports文件导出的文件系统表的专用工具


    exportfs –rv:重新导出所有的文件系统


    exportfs –au:关闭导出的所有文件系统(卸载所有共享目录)



 


开机自动挂载nfs:


SERVER:/PATH/TO/EXPORTED_FS /mount_point   nfs     defaults,_netdev   0 0


 _netdev:说明此共享是一个网络文件系统,如果联系不上,就会跳过此文件系统


示例:


172.16.4.100:/share/nfs   /mnt       nfs     defaults,_netdev 0 0





NFS使用演示


示例:将本机的/share/nfs使用nfs共享出去,让其他客户端可以访问


[[email protected] ~]# mkdir -p /share/nfs

[[email protected] ~]# vim /etc/exports

/share/nfs 172.16.4.136(rw)

[[email protected] ~]# exportfs -v

/share/nfs      172.16.4.136(rw)

/export  10.1.1.0/255.255.255.0(rw,fsid=0,no_root_squash,async)


验证nfs是否成功共享


[[email protected] ~]# showmount -e 172.16.4.100

Export list for 172.16.4.100:

/share/nfs 172.16.4.136


客户端挂载使用


[[email protected] ~]# mount -t nfs 172.16.4.100:/share/nfs /mnt/

[[email protected] ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg0-root   20G 299M   19G   2% /

tmpfs                 491M     0 491M   0% /dev/shm

/dev/sda1             190M   28M 153M  16% /boot

/dev/mapper/vg0-usr   9.8G 2.0G  7.4G  21% /usr

/dev/mapper/vg0-var    20G 267M   19G   2% /var

172.16.4.100:/share/nfs

                       20G  312M  19G   2% /mnt


这个时候nfs就可以正常使用了


 


由于服务器默认将root用户映射为了来宾用户,如果以root用户访问nfs服务器是没有写权限的


[[email protected] ~]# cp /etc/fstab /mnt/

cp: cannot create regular file `/mnt/fstab‘:Read-only file system


服务器端设置允许root用户以root权限访问这个共享服务器


[[email protected] ~]# vim /etc/exports

/share/nfs 172.16.4.136(rw,no_root_squash)


设置完成之后重启服务器,在使用root用户复制文件就有权限了


[[email protected] ~]# cp /etc/fstab /mnt/

[[email protected] ~]# ll /mnt/

total 4

-rw-r--r-- 1 nobody nobody 921 Apr 23 18:51 fstab

验证:nfs是通过用户id来认证用户,而不是用户名


创建用户gentoo使用户对/share/nfs有读写执行权限,然后在里面创建一个文件

[[email protected] ~]# useradd -u 600 gentoo

[[email protected] ~]# setfacl -m u:gentoo:rwx /share/nfs/

[[email protected] ~]# su - gentoo

[[email protected] ~]$ cd /share/nfs/

[[email protected] nfs]$ touch a.gentoo

[[email protected] nfs]$ ll

total 4

-rw-rw-r-- 1 gentoo gentoo   0 Apr 23 18:55 a.gentoo


客户端操作:


创建相同id,然后不同的用户名,进行写入

[[email protected] ~]# useradd -u 600 fedora

[[email protected] ~]# su - fedora

[[email protected] ~]$ cd /mnt/

[[email protected] mnt]$ ll

total 4

-rw-rw-r-- 1 nobody nobody   0 Apr23 18:55 a.gentoo

[[email protected] mnt]$ touch a.fedora

[[email protected] mnt]$ ll

total 4

-rw-rw-r-- 1 nobody nobody   0 Apr 23 18:59 a.fedora

-rw-rw-r-- 1 nobody nobody   0 Apr 23 18:55 a.gentoo


创建不同id,相同用户名,进行写入

[[email protected] ~]# useradd gentoo

[[email protected] ~]# id gentoo

uid=602(gentoo) gid=602(gentoo) groups=602(gentoo)

[[email protected] ~]# su - gentoo

[[email protected] ~]$ cd /mnt/

[[email protected] mnt]$ ll

total 4

-rw-rw-r-- 1 nobody nobody   0 Apr 24 2015 a.fedora

-rw-rw-r-- 1 nobody nobody   0 Apr 24 2015 a.gentoo

[[email protected] mnt]$ touch b.gentoo

touch: cannot touch `b.gentoo‘: Permission denied


结论:相同用户名,无法成功写入,相同id可以成功写入。如果相同的用户名,相同id也是可以成功写入的。



关于权限的分析


  1. 客户端连接时候,对普通用户的检查


    a. 如果明确设定了普通用户被压缩的身份,那么此时客户端用户的身份转换为指定用户;


    b. 如果NFS server上面有同ID用户,那么此时客户端登录账户的身份转换为NFS server上面的同ID用户;


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

        


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


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


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


    c. 如果没有明确指定,此时root用户被压缩为nfsnobody;其它用户对照上述1中的b、c来显示所有者和所属组;


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



示例一:

NFS Server:

[[email protected] ~]# mkdir /Data

[[email protected] ~]# chmod o+w /Data/

[[email protected] ~]# cat /etc/exports 

/Data  10.10.172.0/255.255.255.0(rw,no_root_squash,async)

[[email protected] ~]# service nfs restart

Shutting down NFS daemon: [  OK  ]

Shutting down NFS mountd: [  OK  ]

Shutting down NFS quotas: [  OK  ]

Shutting down NFS services:  [  OK  ]

Shutting down RPC idmapd: [  OK  ]

Starting NFS services:  [  OK  ]

Starting NFS quotas: [  OK  ]

Starting NFS mountd: [  OK  ]

Starting NFS daemon: [  OK  ]

Starting RPC idmapd: [  OK  ]

[[email protected] ~]# tail /etc/passwd

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

centos:x:500:500:centos:/home/centos:/bin/bash

test:x:501:501::/home/test:/bin/bash

[[email protected] ~]# 


NFS Client:

[[email protected] ~]# tail /etc/passwd

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

centos:x:500:500:centos:/home/centos:/bin/bash

gentoo:x:501:501::/home/gentoo:/bin/bash

test:x:502:502::/home/test:/bin/bash

[[email protected] ~]# mount -t nfs 10.10.172.82:/Data /mnt

[[email protected] ~]# cd /mnt/

[[email protected] mnt]# touch root.txt

[[email protected] mnt]# ll

total 0

-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt

[[email protected] ~]# su centos

[[email protected] mnt]$ touch centos.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos centos 0 Sep 18  2016 centos.txt

-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt

[[email protected] mnt]$ su gentoo

Password: 

[[email protected] mnt]$ touch gentoo.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos centos 0 Sep 18 16:55 centos.txt

-rw-rw-r--. 1 test   test   0 Sep 18  2016 gentoo.txt

-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt

[[email protected] mnt]$ su test

Password: 

[[email protected] mnt]$ touch test.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos centos 0 Sep 18 16:55 centos.txt

-rw-rw-r--. 1 test   test   0 Sep 18 16:55 gentoo.txt

-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt

-rw-rw-r--. 1 nobody nobody 0 Sep 18 16:56 test.txt

[[email protected] mnt]$ 



示例二:

NFS Server:

[[email protected] ~]# tail /etc/passwd

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

centos:x:500:500:centos:/home/centos:/bin/bash

test:x:501:501::/home/test:/bin/bash

[[email protected] ~]# cat /etc/exports 

/Data  10.10.172.0/255.255.255.0(rw)

[[email protected] ~]# 


NFS Client:

[[email protected] ~]# tail /etc/passwd

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

centos:x:500:500:centos:/home/centos:/bin/bash

gentoo:x:501:501::/home/gentoo:/bin/bash

test:x:502:502::/home/test:/bin/bash

[[email protected] ~]# mount -t nfs 10.10.172.82:/Data /mnt

[[email protected] ~]# cd /mnt/

[[email protected] mnt]# touch root.txt

[[email protected] mnt]# ll

total 0

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18  2016 root.txt

[[email protected] mnt]# su centos

[[email protected] mnt]$ touch centos.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos    centos    0 Sep 18  2016 centos.txt

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18  2016 root.txt

[[email protected] mnt]$ su test

Password: 

[[email protected] mnt]$ touch test.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos    centos    0 Sep 18 17:16 centos.txt

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18 17:16 root.txt

-rw-rw-r--. 1 nobody    nobody    0 Sep 18  2016 test.txt

[[email protected] mnt]$ su gentoo

Password: 

[[email protected] mnt]$ touch gentoo.txt

[[email protected] mnt]$ ll

total 0

-rw-rw-r--. 1 centos    centos    0 Sep 18 17:16 centos.txt

-rw-rw-r--. 1 test      test      0 Sep 18  2016 gentoo.txt

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18 17:16 root.txt

-rw-rw-r--. 1 nobody    nobody    0 Sep 18 17:17 test.txt

[[email protected] mnt]$ 



本文出自 “眼眸刻着你的微笑” 博客,请务必保留此出处http://dengaosky.blog.51cto.com/9215128/1853686

以上是关于NFS网络文件系统配置的主要内容,如果未能解决你的问题,请参考以下文章

NFS介绍 NFS服务端安装配置 NFS配置选项

NFS网络文件系统服务配置验证及错误解决

linux网络文件系统NFS

NFS网络存储文件系统配置流程

nfs 网络共享服务器的搭建和配置

nfs 网络共享服务器的搭建和配置