基本概述
1 2 |
NFS是Network File System的缩写及网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。 NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS |
原理
1 2 3 4 5 6 7 |
1.用户进程访问NFS客户端目录 2.NFS将请求转化成函数 3.NFS客户端与服务端建立TCP\IP连接 4.NFS服务端接收请求,会调用portmap进行端口映射 5.服务端rpc.nfsd进程进行判断NFS客户端是否可以连接 6.如果可以连接,rpc.mount进程会对客户端进行判断允许的操作 7.都通过则可以对服务端磁盘进行操作 |
NFS使用
1.挂载
1)查看挂载点
1 2 3 |
[root@web01 ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /data 172.16.1.0/24 |
2)挂载命令
1 2 3 4 5 6 7 |
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /data mount #挂载命令 -t #指定挂载的文件类型 nfs #挂载类型是NFS 172.16.1.31 #远端挂载的主机IP :/data #远端挂载的主机目录 /data #本地要挂载的目录 |
2.卸载
1)卸载命令
1 2 |
[root@web01 ~]# umount 172.16.1.31:/data [root@web01 ~]# umount /data |
2)卸载时需要注意
1 2 3 4 5 6 |
#卸载时,不要在要卸载的目录里面进行卸载 [root@web01 /data]# umount /data umount.nfs4: /data: device is busy #强制卸载 [root@web01 /data]# umount -lf /data |
注意:
1 2 |
1.卸载时,不要在要卸载的目录里面进行卸载 2.执行挂载目录时,本地要挂载的目录尽量不要有数据,如果有数据,不会丢失只是被盖住,取消挂载后仍然存在 |
NFS配置详解
参数作用 | |
---|---|
rw(常用) | 读写权限 |
ro(不常用) | 只读权限 |
root_squash(不常用) | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 |
no_root_squash(不常用) | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 |
all_squash(常用) | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 |
no_all_squash(不常用) | 无论NFS客户端使用什么账户访问,都不进行压缩 |
sync(常用) | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
async(不常用) | 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 |
anonuid(常用) | 配置all_squash使用,指定NFS的用户UID,必须存在系统 |
anongid(常用) |
NFS实践
IP | 角色 | |
---|---|---|
nfs | 172.16.1.31 | 服务端 |
web01 | 172.16.1.7 |
服务端
1)关闭防火墙
1 2 |
[root@nfs ~]# systemctl stop firewalld [root@nfs ~]# systemctl disable firewalld |
2)关闭selinux
1 2 3 |
[root@nfs ~]# setenforce 0 [root@nfs ~]# vim /etc/selinux/config SELINUX=disabled |
3)安装NFS和rpcbind
1 2 3 |
[root@nfs ~]# yum install -y nfs-utils rpcbind #如果是centos6需要单独安装rpcbind,centos7可以不单独安装 |
4)配置
1 2 |
[root@nfs ~]# vim /etc/exports /data 172.16.1.0/24(rw,sync,all_squash) |
5)创建数据目录
1 |
[root@nfs ~]# mkdir /data |
6)启动NFS
1 2 3 4 5 6 |
[root@nfs ~]# systemctl start rpcbind nfs-server #如果是centos6,启动时必须先启动rpcbind,centos7会自动启动 #验证启动 [root@nfs ~]# ps -ef | grep nfs |
7)验证NFS配置
1 2 |
[root@nfs ~]# cat /var/lib/nfs/etab /data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash) |
客户端
1)关闭防火墙和selinux
1 2 |
[root@nfs ~]# systemctl stop firewalld [root@nfs ~]# systemctl disable firewalld |
2)安装NFS和rpcbind
1 |
[root@nfs ~]# yum install -y nfs-utils rpcbind |
3)查看挂载点
1 2 3 |
[root@web01 ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /data 172.16.1.0/24 |
4)启动rpcbind
1 |
[root@web01 ~]# systemctl start rpcbind |
5)挂载
1 2 3 4 5 6 |
[root@web01 ~]# mkdir /data [root@web01 ~]# mount -t nfs 172.16.1.31:/data /data #验证 [root@web01 ~]# df -h 172.16.1.31:/data 98G 1.2G 97G 2% /data |
6)写入文件测试
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@web01 ~]# cd /data [root@web01 /data]# mkdir dir mkdir: cannot create direct‘dir’: Permission denied #权限不足,服务端data目录权限是root #服务端修改权限 [root@nfs ~]# chown -R nfsnobody.nfsnobody /data/ #客户端再次写入数据 [root@web01 /data]# mkdir dir [root@web01 /data]# ll total 0 drwxr-xr-x 2 nfsnobody nfsnobody 6 Aug 13 23:43 dir |