NFS文件系统使用

NFS文件系统

NFS:Network File System网络文件系统,基于内核的文件系统,Sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(远程过程调用)实现
RPC:采用C/S模式,客户机请求程序调用进程发送一个有进程参数的调用信息到服务进程,然后等待响应信息。在服务器端,进程保持睡眠状态直到调用信息到达位置。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后客户端调用进程收答复信息,获得进程结果,然后调用执行继续进行
NFS优势:节省本地存储空间,将常用的数据如home目录存放在NFS服务器上且可以通过网络访问,本地终端将可减少自身空间的使用

NFS安装

yum install nfs-utils -y

NFS启动时需要启动两个服务nfs和rpcbind。
在centos7上只需要启动nfs服务,systemd会自动将所依赖的rpcbind启动起来

systemctl start nfs

在centos6上需要分别启动nfs和rpcbind服务

qservice rpcbind start
service nfs start

NFS基本用法

共享目录

NFS服务器端

建立共享文件目录

[root@localhost ~]# mkdir /data/nfs1
[root@localhost ~]# mkdir /data/nfs2
[root@localhost ~]# touch /data/nfs2/nfs1.txt
[root@localhost ~]# touch /data/nfs2/nfs2.txt

修改nfs配置文件

[root@localhost ~]# vim /etc/exports
/data/nfs1 *    #格式:需要共享的目录        共享的规则   

重启服务

[root@localhost ~]# systemctl restart nfs

客户端

查看NFS服务器共享出的目录有哪些

[root@localhost ~]# showmount -e 192.168.73.110
Export list for 192.168.73.110:
/data/nfs1 *

挂载后测试读写

[root@localhost ~]# mount 192.168.73.110:/data/nfs1 /data/nfs1/
[root@localhost ~]# ls /data/nfs1/
nfs1.txt                    # 可以读取
[root@localhost ~]# touch /data/nfs1/123
touch: cannot touch ‘/data/nfs1/123’: Read-only file system
                            #不可以写入

共享目录的读写权限

服务器端配置

1.修改配置文件

[root@localhost ~]# vim /etc/exports
/data/nfs1 *(rw)
2.重启服务
```bash
[root@localhost ~]# systemctl restart nfs

客户端操作

1.对目录下的nfs1.txt写入内容

[root@localhost ~]# echo '1234' > /data/nfs1/nfs1.txt 
-bash: /data/nfs1/nfs1.txt: Permission denied

无法写入,对文件写入需要对文件有写权限

匿名用户所映射的用户

服务器端配置

1.给与目录读写执行权限

[root@localhost data]# chmod 777 nfs1

客户端操作

创建一个文件

[root@localhost ~]# touch /data/nfs1/122

服务器端查看文件属性

[root@localhost data]# ll nfs1/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 19 04:04 122
-rw-r--r-- 1 root      root      0 May 19 03:37 nfs1.txt

nfs将匿名用户映射成nfsnobody用户,所以只需要给予nfsnobody读写执行就可以了,无需给予目录最大权限

root压榨(root_squire)

远程用户为root时,映射为服务器上的nfsnobody,此是应为用到了root_squire选项,可以使用no_root_squire取消压榨

[root@localhost nfs1]# exportfs -v
/data/nfs1      <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)

服务器操作

修改配置文件,重启服务

[root@localhost data]# vim /etc/exports
/data/nfs1 *(rw,no_root_squash)
[root@localhost data]# systemctl restart nfs

客户端操作

创建一个文件

[root@localhost nfs1]# touch 123

服务器端查看文件权限

[root@localhost nfs1]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 19 04:04 122
-rw-r--r-- 1 root      root      0 May 19 04:16 123     
-rw-r--r-- 1 root      root      0 May 19 03:37 nfs1.txt

此时所创建出的文件属主数组均为root

针对不同的地址给予不同的权限

服务器配置

修改配置文件重启服务,让73网段内111主机可读写其余不可写

[root@localhost nfs1]# vim /etc/exports
/data/nfs1 192.168.73.0/24(ro) 192.168.73.111(rw)
[root@localhost nfs1]# systemctl restart nfs

给予nfsnobody用户acl权限

[root@localhost nfs1]# setfacl -m u:nfsnobody:rwx /data/nfs1

客户端操作

73.111主机可读可写

[root@111 nfs1]# touch 111
[root@111 nfs1]# ls
111  122  123  nfs1.txt

73.132主机可读不可写

[root@132 nfs1]# touch 132
touch: cannot touch `132': Read-only file system

普通用户创建的文件属性

客户端操作

使用wang用户创建文件

[root@111 nfs1]# su wang
[wang@111 nfs1]$ touch wang
[wang@111 nfs1]$ ll
-rw-rw-r-- 1 wang wang 0 May 19 05:29 wang

服务器操作

查看wang用户所创建的文件

[root@localhost nfs1]# ll
total 0
-rw-rw-r-- 1 1001 1001 0 May 19 05:29 wang

显示的为uid,远程用户如果为普通用户,则不会和root一样压榨成nfsnobody,会保留id号,客户端的UID会映射成服务器端的UID的用户

NFS的永久挂载

客户端操作

将挂载写入/etc/fstab文件

[root@localhost nfs1]# vim /etc/fstab 
192.168.73.110:/data/nfs1       /data/nfs1      nfs     defaults        0 0
[root@localhost nfs1]# mount -a

异步读写

数据变化后不立即写入磁盘,性能高,添加async选项

服务器操作

[root@localhost nfs1]# vim /etc/exports
/data/nfs1 192.168.73.0/24(ro) 192.168.73.111(rw,async)
[root@localhost nfs1]# exportfs -r      #让配置生效
[root@localhost nfs1]# exportfs -v      #查看配置
/data/nfs1      192.168.73.111(async,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
/data/nfs1      192.168.73.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)

全部压榨

all_squash可以将所有用户都压榨成nfsnobody,当存在no_root_squash时哪个生效

服务器操作

修改配置文件

[root@localhost nfs1]# vim /etc/exports
/data/nfs1 192.168.73.0/24(ro) 192.168.73.111(rw,async,no_root_squash,all_squash)
[root@localhost nfs1]# exportfs -r      #生效
[root@localhost nfs1]# exportfs -v
/data/nfs1      192.168.73.111(async,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,all_squash)

客户端测试

在客户端创建文件

[root@111 nfs1]# touch aaa
[root@111 nfs1]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 May 19 05:59 aaa

依旧被压榨为nfsnobody用户

被压榨时映射为其他用户

压榨时不映射为nfsnobody用户,而更换为其他用户,可以将其映射成某服务的用户比如(apahce),使用anonuid,anongid分别指定所要压榨为用户的id

服务器操作

1.新建一个用户

[root@localhost nfs1]# useradd -r -s /sbin/nologin nfs
[root@localhost nfs1]# id nfs
uid=998(nfs) gid=996(nfs) groups=996(nfs)

2.修改配置文件

[root@localhost nfs1]# vim /etc/exports
/data/nfs1 192.168.73.111(rw,async,no_root_squash,all_squash,anonuid=998,anongid=996)
[root@localhost nfs1]# exportfs -r
[root@localhost nfs1]# exportfs -v
/data/nfs1      192.168.73.111(async,wdelay,hide,no_subtree_check,anonuid=998,anongid=996,sec=sys,rw,secure,no_root_squash,all_squash)

客户端操作

创建文件

[root@localhost nfs1]# ll
-rw-r--r-- 1 nfs       nfs       0 May 19 06:08 123

压榨root为nfs用户

nfs挂载共享家目录

每台主机上都有一个/home/wang,要求每台主机上的所有wang家目录内的东西都相同,可以考虑将wang的家目录共享出去来实现,方便集中数据的管理,节约磁盘的空间

服务器操作

1.创建配置文件并生效

[root@localhost nfs1]# mkdir /data/wanghome
[root@localhost nfs1]# vim /etc/exports.d/wang.exports
/data/wanghome *(rw)
[root@localhost nfs1]# exportfs -r
[root@localhost nfs1]# exportfs -v
/data/nfs1      192.168.73.111(async,wdelay,hide,no_subtree_check,anonuid=998,anongid=996,sec=sys,rw,secure,no_root_squash,all_squash)
/data/wanghome  <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)

2.复制模板文件至家目录

[root@localhost ~]# cp -r /etc/skel/. /data/wanghome/
[root@localhost ~]# chown -R wang.wang /data/wanghome/

客户端操作

查看服务器端的共享目录

[root@111 ~]# showmount -e 192.168.73.110
Export list for 192.168.73.110:
/data/wanghome *
/data/nfs1     192.168.73.111
挂载wanghome目录至/home/wang,切换用户查看

[root@111 ~]# mount 192.168.73.110:/data/wanghome /home/wang/
[root@111 ~]# su - wang             #
Last login: Sun May 19 06:26:12 CST 2019 on pts/0
[wang@111 ~]$ ls

NFS工具

1.停止本机所有挂载

[root@localhost nfs1]# exportfs -au
[root@localhost nfs1]# exportfs -v
[root@localhost nfs1]# 

2.恢复本机所有挂载

[root@localhost nfs1]# exportfs -v
/data/nfs1      192.168.73.111(async,wdelay,hide,no_subtree_check,anonuid=998,anongid=996,sec=sys,rw,secure,no_root_squash,all_squash)

3.查看远程主机注册的rpc端口号

[root@111 nfs1]# rpcinfo -s 192.168.73.110
   program version(s) netid(s)                         service     owner
    100000  2,3,4     local,udp,tcp,udp6,tcp6          portmapper  superuser
    100024  1         tcp6,udp6,tcp,udp                status      29
    100005  3,2,1     tcp6,udp6,tcp,udp                mountd      superuser
    100003  4,3       udp6,tcp6,udp,tcp                nfs         superuser
    100227  3         udp6,tcp6,udp,tcp                nfs_acl     superuser
    100021  4,3,1     tcp6,udp6,tcp,udp                nlockmgr    superuser

4.查看本机注册的rpc端口号

[root@111 nfs1]# rpcinfo -s
   program version(s) netid(s)                         service     owner
    100000  2,3,4     local,udp,tcp,udp6,tcp6          portmapper  superuser
    100024  1         tcp6,udp6,tcp,udp                status      29

猜你喜欢

转载自blog.51cto.com/11886307/2397515