14.1 NFS介绍
NFS是Network File System的缩写。
NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本。
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。
NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。
NFS服务需借助于RPC服务实现文件传输,在centos6之前的版本中,RPC服务是由portmap提供,centos6之后的版本改用rpcbind提供。
14.2 NFS服务端客户端安装配置
服务端:yum install -y nfs-utils rpcbind
客户端:yum install -y nfs-utils
安装完成后在服务端做如下配置:
1、编辑 vim /etc/exports
/home/nfstestdir 服务端分享的目录;
192.168.37.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 指定分享的客户端及权限属性。
2、创建分享目录更改权限。
[root@liang-00 ~]# mkdir /home/nfstestdir [root@liang-00 ~]# chmod 777 !$ chmod 777 /home/nfstestdir [root@liang-00 ~]#
3、启动服务。
sytemctl start rpcbind 启动rpcbind服务。
sytemctl start nfs 启动nfs。
[root@liang-00 ~]# ps aux |grep nfs root 1974 0.0 0.0 0 0 ? S< 10:40 0:00 [nfsd4_callbacks] root 1988 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1989 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1990 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1991 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1992 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1993 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1994 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 1995 0.0 0.0 0 0 ? S 10:40 0:00 [nfsd] root 2040 0.0 0.0 112704 972 pts/0 S+ 10:41 0:00 grep --color=auto nfs [root@liang-00 ~]# ps aux |grep rpc rpc 1931 0.0 0.1 69220 1536 ? Ss 10:40 0:00 /sbin/rpcbind -w rpcuser 1955 0.0 0.1 42420 1752 ? Ss 10:40 0:00 /usr/sbin/rpc.statd root 1962 0.0 0.0 0 0 ? S< 10:40 0:00 [rpciod] root 1966 0.0 0.0 45924 536 ? Ss 10:40 0:00 /usr/sbin/rpc.idmapd root 1969 0.0 0.0 42608 948 ? Ss 10:40 0:00 /usr/sbin/rpc.mountd root 2050 0.0 0.0 112704 972 pts/0 S+ 10:42 0:00 grep --color=auto rpc [root@liang-00 ~]#
设置开机启动。
systemctl enable rpcbind
systemctl enable nfs
[root@liang-00 ~]# systemctl is-enabled rpcbind enabled [root@liang-00 ~]# systemctl is-enabled nfs enabled [root@liang-00 ~]#
14.3 NFS配置选项
1、/etc/exports中参数的含义:
- rw 读写
- ro 只读
- sync 同步模式,内存数据实时写入磁盘,相应会降低磁盘效率
- async 非同步模式,保证磁盘效率,缺点是断电情况向数据会丢失一部分
- no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
- root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
- all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
- anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
2、客户端挂载共享目录。
在挂载前检查服务端nfs是否能通信:
[root@liang-03 ~]# showmount -e 192.168.37.200 Hclnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) [root@liang-03 ~]#
RPC: Port mapper failure 说明rpc不能到达服务端,这时我们就要检查服务端和客户端是否开启nfs,两边的防火墙是否关闭。
[root@liang-03 ~]# systemctl stop firewalld [root@liang-03 ~]# getenforce Enforcing [root@liang-03 ~]# setenforce 0 [root@liang-03 ~]# showmount -e 192.168.37.200 Export list for 192.168.37.200: /home/nfstestdir 192.168.133.0/24 [root@liang-03 ~]#
挂载共享目录。
mount -t nfs 192.168.37.200:/home/nfstestdir /mnt/
[root@liang-03 ~]# mount -t nfs 192.168.37.200:/home/nfstestdir /mnt/ [root@liang-03 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 28G 4.9G 23G 18% / devtmpfs 480M 0 480M 0% /dev tmpfs 492M 0 492M 0% /dev/shm tmpfs 492M 7.6M 485M 2% /run tmpfs 492M 0 492M 0% /sys/fs/cgroup /dev/sda1 197M 108M 90M 55% /boot tmpfs 99M 0 99M 0% /run/user/0 192.168.37.200:/home/nfstestdir 28G 7.6G 21G 28% /mnt [root@liang-03 ~]#
在客户端 /mnt 下创建文件。
[root@liang-03 mnt]# touch liang.txt [root@liang-03 mnt]# ll total 0 -rw-r--r--. 1 mysql mysql 0 Dec 22 11:30 liang.txt [root@liang-03 mnt]#
服务端的显示:
[root@liang-00 ~]# ll /home/nfstestdir/ total 0 -rw-r--r-- 1 liang liang 0 Dec 22 11:30 liang.txt [root@liang-00 ~]#
可以看到,在客户端和服务端中创建的文件属主属组不同。这时因为在/etc/exports中配置了:anonuid=1000,anongid=1000。
在服务端中liang的属主属组是:1000,在客户端mysql的属主属组是:1000
14.4 exportfs命令
NFS服务在远程客户端正在使用的时候,我们不能直接重启nfs服务,会导致远程客户端服务挂起。严重的会导致服务器异常。
exportfs命令
常用选项:
- -a 全部挂载或者全部卸载
- -r 重新挂载
- -u 卸载某一个目录
- -v 显示共享目录
1、exportfs -arv 使新添加的共享目录重新生效。
[root@liang-00 ~]# exportfs -arv exporting 192.168.37.0/24:/home/nfstestdir [root@liang-00 ~]#
在服务端中:
/etc/exports 中添加目录:/tmp/ 192.168.37.0/24(rw,sync,no_root_squash)
no_root_squash:让root用户权限不变。
使其生效。
[root@liang-00 ~]# exportfs -arv exporting 192.168.37.203:/tmp exporting 192.168.37.0/24:/home/nfstestdir [root@liang-00 ~]#
在客户端中:
[root@liang-03 ~]# showmount -e 192.168.37.200 Export list for 192.168.37.200: /home/nfstestdir 192.168.37.0/24 /tmp 192.168.37.203 [root@liang-03 ~]# mount -t nfs 192.168.37.200:/tmp/ /mnt/ [root@liang-03 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 28G 4.9G 23G 18% / devtmpfs 480M 0 480M 0% /dev tmpfs 492M 0 492M 0% /dev/shm tmpfs 492M 7.5M 485M 2% /run tmpfs 492M 0 492M 0% /sys/fs/cgroup /dev/sda1 197M 108M 90M 55% /boot tmpfs 99M 0 99M 0% /run/user/0 192.168.37.200:/tmp 28G 7.6G 21G 28% /mnt [root@liang-03 ~]#
在挂载的共享目录中创建普通文件查看属主属组。
客户端:属主属组为root
[root@liang-03 ~]# vi /mnt/123.txt
[root@liang-03 ~]# ll /mnt/
total 1992
-rw-r--r--. 1 root root 20 Dec 22 12:02 123.txt
srw-rw-rw-. 1 root root 0 Dec 22 10:02 liang.sock
-rw-r--r--. 1 root root 1320318 Dec 20 14:58 mysql_all.sql
-rw-r--r--. 1 root root 660295 Dec 20 14:33 mysqlbak.sql
-rw-r--r--. 1 root root 30807 Dec 20 15:00 mysql_only_table.sql
drwxr-xr-x. 2 root root 6 Dec 18 18:49 pear
-rw-r--r--. 1 root root 57 Dec 20 19:36 percona-version-check
srw-rw-rw-. 1 root root 0 Dec 22 10:02 php-fcgi.sock
drwx------. 3 root root 17 Dec 22 10:02 systemd-private-295babf5336a44f29cda290a631fa806-chronyd.service-CGXLni
drwx------. 3 root root 17 Dec 19 10:31 systemd-private-b01e82040cbb4136a07fdbdcb2458d3a-chronyd.service-Quu2fl
-rw-r--r--. 1 root root 3076 Dec 17 10:30 test.com.log
-rw-r--r--. 1 root root 7465 Dec 20 14:55 user.sql
drwx------. 2 root root 6 Dec 18 08:46 vmware-root
[root@liang-03 ~]#
服务端:属主属组为root
[root@liang-00 ~]# ll /tmp/
total 1992
-rw-r--r-- 1 root root 20 Dec 22 12:02 123.txt
srw-rw-rw- 1 root root 0 Dec 22 10:02 liang.sock
-rw-r--r-- 1 root root 1320318 Dec 20 14:58 mysql_all.sql
-rw-r--r-- 1 root root 660295 Dec 20 14:33 mysqlbak.sql
-rw-r--r-- 1 root root 30807 Dec 20 15:00 mysql_only_table.sql
drwxr-xr-x 2 root root 6 Dec 18 18:49 pear
-rw-r--r-- 1 root root 57 Dec 20 19:36 percona-version-check
srw-rw-rw- 1 root root 0 Dec 22 10:02 php-fcgi.sock
drwx------ 3 root root 17 Dec 22 10:02 systemd-private-295babf5336a44f29cda290a631fa806-chronyd.service-CGXLni
drwx------ 3 root root 17 Dec 19 10:31 systemd-private-b01e82040cbb4136a07fdbdcb2458d3a-chronyd.service-Quu2fl
-rw-r--r-- 1 root root 3076 Dec 17 10:30 test.com.log
-rw-r--r-- 1 root root 7465 Dec 20 14:55 user.sql
drwx------ 2 root root 6 Dec 18 08:46 vmware-root
[root@liang-00 ~]#
14.5 NFS客户端问题
NFS 4版本会有该问题:
在客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、属组为nobody
解决方法1:
客户端挂载时加上 -o nfsvers=3,重新挂载共享目录。
[root@liang-03 ~]# umount /mnt/ [root@liang-03 ~]# mount -t nfs -o nfsvers=3 192.168.37.200:/tmp/ /mnt/ [root@liang-03 ~]# mount -t nfs -oremount,nfsvers=3 192.168.37.200:/tmp/ /mnt/ [root@liang-03 ~]#
解决方法2:
客户端和服务端都需要:
vim /etc/idmapd.conf //把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义),然后再重启rpcbind服务