NFS介绍

NFS是network file system(网络文件系统)的缩写,早期的NFS由Sun公司开发,分为2、3、4三个版本,4.0开始由Netapp主导开发,最新版本为4.1 NFS数据传输基于RPC协议(Remote Procedure call) NFS应用于多个服务器需要访问一个共享资源,如web服务器的网站内容,其共享的数据是一致的,一个NFS数据可供多台服务器挂载访问,服务器端访问NFS上数据内容是相同的 NFS基于RPC协议来进行数据传输

NFS服务端安装配置

服务器端安装nfs和rpcbind,安装nfs时不指定rpcbind也会把rpcbind安装上,在服务端和客户端都安装NFS

[root@aaa ~]# yum install -y nfs-utils rpcbind
已加载插件:fastestmirror
base | 3.6 kB 00:00:00     
epel/x86_64/metalink 
---------------------省略过程
已安装:
  nfs-utils.x86_64 1:1.3.0-0.54.el7 rpcbind.x86_64 0:0.2.0-44.el7                                  
作为依赖被安装:
  gssproxy.x86_64 0:0.7.0-17.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-29.el7      
  libcollection.x86_64 0:0.7.0-29.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.1-29.el7        
  libnfsidmap.x86_64 0:0.25-19.el7 libpath_utils.x86_64 0:0.2.1-29.el7 libref_array.x86_64 0:0.1.5-29.el7         
  libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-17.el7                 
  quota-nls.noarch 1:4.01-17.el7 tcp_wrappers.x86_64 0:7.6-77.el7            
更新完毕:
  selinux-policy.noarch 0:3.13.1-192.el7_5.6                                                                                         
作为依赖被升级:
  policycoreutils.x86_64 0:2.5-22.el7 selinux-policy-targeted.noarch 0:3.13.1-192.el7_5.6                      
完毕!

服务器端安装nfs的配置,在配置文件中写入nfs的分享目录的配置,并创建分享的目录,这两类分享的是web网站目录,为了便捷访问,把其权限设置为777

[root@aaa ~]# cat /etc/exports
/data/wwwroot/aaa/ 192.168.1.234/24(rw,sync,all_squash,anonuid=100,anongid=100)
[root@aaa ~]# ll -d /data/wwwroot/aaa/
drwxrwxrwx 4 root root 185 8月 15 23:48 /data/wwwroot/aaa/

NFS的分享享权限

rw  读写
ro   只读
rync   同步模式,内存数据实时写入磁盘
async  非实时同步模式
no_root_squash   客户端挂载NFS共享目录后,root用户不受权限约束,对分享的目录相当于用root权限来操作
root_squash   客户端挂载使用分享的目录会受到约束,其权限会被限制为服务器端的一个普通用户
all_squash   客户端上所有用户在使用NFS分享的目录时都会被限制为一个普通用户
anonuid/anongid   和root_squash、all_squash配合使用,指定限制用户的uid和gid

在服务器端启动nfs和rpcbind,并把NFS和rpcbind加入开机启动当中 [root@aaa ~]# systemctl start rpcbind [root@aaa ~]# systemctl start nfs [root@aaa ~]# systemctl enable rpcbind [root@aaa ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. 查看rpcbind监听的端口,rpcbind监听111端口

[root@aaa ~]# ps -aux |grep rpcbind
rpc 17079 0.0 0.1 65008 1424 ? Ss 01:33 0:00 /sbin/rpcbind -w
root 17193 0.0 0.0 112724 976 pts/1 R+ 01:39 0:00 grep --color=auto rpcbind
[root@aaa ~]# netstat -ntlp |grep rpcbind
tcp    0    0 0.0.0.0:111      0.0.0.0:*         LISTEN      17079/rpcbind 
tcp6   0    0 :::111           :::*              LISTEN      17079/rpcbind     

查看NFS启动进程,NFS并不监听端口,是通过rpcbind监听端口来提供服务的,所以NFS依赖rpc这个协议来对外提供访问

[root@aaa ~]# ps -axu |grep nfs
root 17135 0.0 0.0 0 0 ? S< 01:33 0:00 [nfsd4_callbacks]
root 17141 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17142 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17143 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17144 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17145 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17146 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17147 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17148 0.0 0.0 0 0 ? S 01:33 0:00 [nfsd]
root 17196 0.0 0.0 112724 976 pts/1 R+ 01:40 0:00 grep --color=auto nfs

NFS配置选项

在服务端查看nfs分享目录的配置,使用vim编辑/etc/exports这个文件,把分享的目录设定访问权限

[root@aaa ~]# cat /etc/exports
/data/wwwroot/aaa/ 192.168.1.0/24(rw,sync,all_squash,anonuid=100,anongid=100)
使用showmount可以在客户端查看NFS分享的目录信息   showmount -e
[root@www ~]# showmount -e 192.168.1.234
Export list for 192.168.1.234:
/data/wwwroot/aaa 192.168.1.234/24

NFS分享目录的权限配置

rw 读写
ro 只读
rync 同步模式,内存数据实时写入磁盘
async 非实时同步模式
no_root_squash 客户端挂载NFS共享目录后,root用户不受权限约束,对分享的目录相当于用root权限来操作
root_squash 客户端挂载使用分享的目录会受到约束,其权限会被限制为服务器端的一个普通用户
all_squash 客户端上所有用户在使用NFS分享的目录时都会被限制为一个普通用户
anonuid/anongid 和root_squash、all_squash配合使用,指定限制用户的uid和gid

挂载nfs分享的目录,使用df -h可以看到客户端上挂载的网络目录路径,卸载使用umount

[root@www ~]# mount -t nfs 192.168.1.234:/data/wwwroot/aaa /mnt
[root@www ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 15G 3.3G 82% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 38M 459M 8% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 197M 108M 89M 55% /boot
tmpfs 100M 0 100M 0% /run/user/0
192.168.1.234:/data/wwwroot/aaa 18G 6.5G 12G 37% /mnt

exportfs命令

NFS在添加新的共享路径时需要重启服务,但是在重启服务的时候客户端可能会正在读写文件,如果这时候发生重启,可能会导致使用nfs的某些进程被挂起,使用ps查看是D符号表示,挂起进程不仅会停止正常服务,还有可能会造成系统崩溃,所以在nfs增加新配置时,我们需要平滑重启加载配置的操作 exportfs常用操作 -a  全部挂载或全部卸载 -r  重新挂载 -u  卸载某一目录 -v  显示共享目录 在服务器端新添加一个共享目录

[root@aaa ~]# cat /etc/exports
/data/wwwroot/aaa/ 192.168.1.0/24(rw,sync,all_squash,anonuid=100,anongid=100)
/data/wwwroot/b/ 192.168.1.223(rw,sync,no_root_squash)

exportfs重新加载配置

[root@aaa ~]# exportfs -arv
exporting 192.168.1.223:/data/wwwroot/b
exporting 192.168.1.0/24:/data/wwwroot/aaa

在客户端使用showmount查看加载的配置,第一次为在服务器端未重新加载nfs的配置,第二次为加载配置后的showmount

[root@www ~]# showmount -e 192.168.1.234
Export list for 192.168.1.234:
/data/wwwroot/aaa 192.168.1.0/24
[root@www ~]# showmount -e 192.168.1.234
Export list for 192.168.1.234:
/data/wwwroot/aaa 192.168.1.0/24
/data/wwwroot/b 192.168.1.223

测试;在客户端挂载nfs的新共享目录

[root@www /]# mount -t nfs 192.168.1.234:/data/wwwroot/b /media/
[root@www /]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 15G 3.3G 82% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 38M 459M 8% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 197M 108M 89M 55% /boot
tmpfs 100M 0 100M 0% /run/user/0
192.168.1.234:/data/wwwroot/aaa 18G 6.5G 12G 37% /mnt
192.168.1.234:/data/wwwroot/b 18G 6.5G 12G 37% /media

在客户端创建文件,查看nfs是否限制root权限读写 挂载的客户端创建一个文件

[root@www /]# cd /media/
[root@www media]# ls
index.html index.php sleep.php
[root@www media]# touch 11.html
[root@www media]# ll -h 11.html 
-rw-r--r-- 1 root root 0 8月 18 2018 11.html

服务器端查看这个新创建的文件是什么权限

[root@aaa ~]# cd /data/wwwroot/b/
[root@aaa b]# ll -h 11.html 
-rw-r--r-- 1 root root 0 8月 18 03:59 11.html

在nfs共享中,使用no_root_squash配置比较多