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

[经验分享] NFS网络文件系统配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-19 08:53:12 | 显示全部楼层 |阅读模式
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共享出去,让其他客户端可以访问

[iyunv@nfs ~]# mkdir -p /share/nfs
[iyunv@nfs ~]# vim /etc/exports
/share/nfs 172.16.4.136(rw)
[iyunv@nfs ~]# 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是否成功共享

[iyunv@client ~]# showmount -e 172.16.4.100
Export list for 172.16.4.100:
/share/nfs 172.16.4.136

客户端挂载使用

[iyunv@client ~]# mount -t nfs 172.16.4.100:/share/nfs /mnt/
[iyunv@client ~]# 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服务器是没有写权限的

[iyunv@client ~]# cp /etc/fstab /mnt/
cp: cannot create regular file `/mnt/fstab':Read-only file system

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

[iyunv@nfs ~]# vim /etc/exports
/share/nfs 172.16.4.136(rw,no_root_squash)

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

[iyunv@client ~]# cp /etc/fstab /mnt/
[iyunv@client ~]# ll /mnt/
total 4
-rw-r--r-- 1 nobody nobody 921 Apr 23 18:51 fstab
验证:nfs是通过用户id来认证用户,而不是用户名

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

[iyunv@Server ~]# useradd -u 600 gentoo
[iyunv@Server ~]# setfacl -m u:gentoo:rwx /share/nfs/
[iyunv@Server ~]# su - gentoo
[gentoo@Server ~]$ cd /share/nfs/
[gentoo@Server nfs]$ touch a.gentoo
[gentoo@Server nfs]$ ll
total 4
-rw-rw-r-- 1 gentoo gentoo   0 Apr 23 18:55 a.gentoo

客户端操作:

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

[iyunv@client ~]# useradd -u 600 fedora
[iyunv@client ~]# su - fedora
[fedora@client ~]$ cd /mnt/
[fedora@client mnt]$ ll
total 4
-rw-rw-r-- 1 nobody nobody   0 Apr23 18:55 a.gentoo
[fedora@client mnt]$ touch a.fedora
[fedora@client 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,相同用户名,进行写入

[iyunv@client ~]# useradd gentoo
[iyunv@client ~]# id gentoo
uid=602(gentoo) gid=602(gentoo) groups=602(gentoo)
[iyunv@client ~]# su - gentoo
[gentoo@client ~]$ cd /mnt/
[gentoo@client 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
[gentoo@client 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:
[iyunv@NFS ~]# mkdir /Data
[iyunv@NFS ~]# chmod o+w /Data/
[iyunv@NFS ~]# cat /etc/exports
/Data  10.10.172.0/255.255.255.0(rw,no_root_squash,async)
[iyunv@NFS ~]# 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  ]

[iyunv@NFS ~]# 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
[iyunv@NFS ~]#

NFS Client:
[iyunv@Client ~]# 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
[iyunv@Client ~]# mount -t nfs 10.10.172.82:/Data /mnt
[iyunv@Client ~]# cd /mnt/
[iyunv@Client mnt]# touch root.txt
[iyunv@Client mnt]# ll
total 0
-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt
[iyunv@Client ~]# su centos
[centos@Client mnt]$ touch centos.txt
[centos@Client 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
[centos@Client mnt]$ su gentoo
Password:
[gentoo@Client mnt]$ touch gentoo.txt
[gentoo@Client 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
[gentoo@Client mnt]$ su test
Password:
[test@Client mnt]$ touch test.txt
[test@Client 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
[test@Client mnt]$


示例二:
NFS Server:
[iyunv@NFS ~]# 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
[iyunv@NFS ~]# cat /etc/exports
/Data  10.10.172.0/255.255.255.0(rw)
[iyunv@NFS ~]#

NFS Client:

[iyunv@Client ~]# 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
[iyunv@Client ~]# mount -t nfs 10.10.172.82:/Data /mnt
[iyunv@Client ~]# cd /mnt/
[iyunv@Client mnt]# touch root.txt
[iyunv@Client mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18  2016 root.txt
[iyunv@Client mnt]# su centos
[centos@Client mnt]$ touch centos.txt
[centos@Client 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
[centos@Client mnt]$ su test
Password:
[test@Client mnt]$ touch test.txt
[test@Client 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
[test@Client mnt]$ su gentoo
Password:
[gentoo@Client mnt]$ touch gentoo.txt
[gentoo@Client 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
[gentoo@Client mnt]$



运维网声明 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-274206-1-1.html 上篇帖子: CentOS解锁登录认证失败的用户 下篇帖子: Openldap配置TLS加密传输 网络
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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