第14章 NFS服务搭建与配置
14.1 NFS介绍
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。NFS的数据传输基于RPC(remote procedure call)协议。
应用场景
A,B,C三台机器上需要被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。
14.2 NFS服务端安装配置
准备两台虚拟机,一台作为服务端,一台作为客户端。
服务端192.168.230.135
[root@cham002 ~]# yum install -y nfs-utils rpcbind
[root@cham002 ~]# vim /etc/exports
/home/nfstestdir  192.168.230.145/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
#指定要进行分享的目录;指定要共享该目录的机器,ip可以ip段,也可以写ip。
:wq              
创建分享目录并制定权限:
[root@cham002 ~]# mkdir /home/nfstestdir
[root@cham002 ~]# chmod 777 /home/nfstestdir  
安装完rpcbind之后服务会自动启动
[root@cham002 ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      11547/rpcbind       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6019/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1913/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2498/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6019/nginx: master  
tcp6       0      0 :::3306                 :::*                    LISTEN      9831/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      11547/rpcbind       
tcp6       0      0 :::22                   :::*                    LISTEN      1913/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2498/master      
[root@cham002 ~]# ps aux |grep rpc
rpc      11547  0.0  0.1  64964  1044 ?        Ss   22:57   0:00 /sbin/rpcbind -w
root     11700  0.0  0.0 112680   976 pts/2    S+   23:16   0:00 grep --color=auto rpc
                                                              
启动NFS服务
[root@cham002 ~]# systemctl start nfs
[root@cham002 ~]# ps aux |grep nfs
root     11740  0.0  0.0      0     0 ?        S<   23:17   0:00 [nfsd4_callbacks]
root     11746  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11747  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11748  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11749  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11750  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11751  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11752  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11753  0.0  0.0      0     0 ?        S    23:17   0:00 [nfsd]
root     11770  0.0  0.0 112680   972 pts/2    S+   23:17   0:00 grep --color=auto nfs
[root@cham002 ~]# ps aux |grep rpc
rpc      11547  0.0  0.1  64964  1304 ?        Ss   22:57   0:00 /sbin/rpcbind -w
root     11723  0.0  0.0      0     0 ?        S<   23:17   0:00 [rpciod]
root     11728  0.0  0.0  43824   544 ?        Ss   23:17   0:00 /usr/sbin/rpc.idmapd
root     11729  0.0  0.0  42564   944 ?        Ss   23:17   0:00 /usr/sbin/rpc.mountd
root     11772  0.0  0.0 112680   972 pts/2    S+   23:18   0:00 grep --color=auto rpc
将NFS服务加入开机启动项: 
[root@cham002 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
客户端192.168.230.145
[root@test ~]# yum install -y nfs-utils
[root@test ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      38651/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1337/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      38908/mysqld        
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1337/sshd           
[root@test ~]#  ps aux |grep rpc
rpc      61559  0.0  0.1  64964  1048 ?        Ss   22:58   0:00 /sbin/rpcbind -w
root     61727  0.0  0.0 112684   980 pts/0    R+   23:16   0:00 grep --color=auto rpc
检查客户端是否有权限访问服务端文件:
[root@test ~]# showmount -e 192.168.230.135
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
报错: 无法连接到服务端机器(网络不通)!
解决办法:
检查服务端NFS服务是否开启(监听111端口)
如果确认服务端NFS服务已经开启,那么检查防火墙状态,关闭服务端和客户端firewalld和SELinux防火墙
192.168.230.145
[root@test ~]# systemctl stop firewalld
[root@test ~]# getenforce
Enforcing
[root@test ~]# setenforce 0
[root@test ~]# getenforce
Permissive
192.168.230.135
[root@cham002 ~]# systemctl stop firewalld
[root@cham002 ~]# systemctl stop firewalld
[root@cham002 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@cham002 ~]# systemctl stop iptables
解决完上述错误后再次执行命令:
[root@test ~]# showmount -e 192.168.230.135
Export list for 192.168.230.135:
/home/nfstestdir 192.168.230.0/24
即,客户端可以正常访问服务端机器,也可以看到共享的目录了。
开始挂载
[root@test ~]# mount -t nfs 192.168.230.135:/home/nfstestdir /mnt/
[root@test ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          17G  8.5G  7.7G   53% /
devtmpfs                          483M     0  483M    0% /dev
tmpfs                             493M     0  493M    0% /dev/shm
tmpfs                             493M  6.8M  486M    2% /run
tmpfs                             493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                         197M  109M   88M   56% /boot
tmpfs                              99M     0   99M    0% /run/user/0
192.168.230.135:/home/nfstestdir   17G  7.0G  9.2G   44% /mnt
[root@test ~]# 
192.168.230.145
[root@test ~]# cd /mnt/
[root@test mnt]# ls
testdir
[root@test mnt]# touch champinlinux.111
[root@test mnt]# ls -l
总用量 0
-rw-r--r-- 1 cham cham 0 1月  17 00:03 champinlinux.111
drwxr-xr-x 2 root root 6 1月  17 00:01 testdir
[root@test mnt]# id cham
uid=1000(cham) gid=1000(cham) 组=1000(cham),1007(user5)
192.168.230.135
[root@cham002 ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 user1 user1 0 1月  17 00:03 champinlinux.111
drwxr-xr-x 2 root  root  6 1月  17 00:01 testdir
[root@cham002 ~]# id user1
uid=1000(user1) gid=1000(user1) 组=1000(user1)
NFS配置选项
• rw 读写
• ro 只读
• sync 同步模式,内存数据实时写入磁盘
• async 非同步模式
• no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
• root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
• all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
• anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid
14.4 exportfs命令
exportfs命令用来管理当前NFS共享的文件系统列表。
• exportfs -arv //不用重启nfs服务,配置文件就会生效
192.168.230.145把它先卸载掉然后再重启nfs, 如果挂载了几十台机器每一台都要去卸载么?
[root@test ~]# umount /mnt
umount.nfs4: /mnt: device is busy
[root@test ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          17G  8.5G  7.7G   53% /
devtmpfs                          483M     0  483M    0% /dev
tmpfs                             493M     0  493M    0% /dev/shm
tmpfs                             493M  6.8M  486M    2% /run
tmpfs                             493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                         197M  109M   88M   56% /boot
tmpfs                              99M     0   99M    0% /run/user/0
192.168.230.135:/home/nfstestdir   17G  7.0G  9.2G   44% /mnt
[root@test ~]# umount -l /mnt
[root@test ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        17G  8.5G  7.7G   53% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.8M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M   56% /boot
tmpfs            99M     0   99M    0% /run/user/0
•常用选项
• -a 全部挂载或者全部卸载
• -r 重新挂载
• -u 卸载某一个目录
• -v 显示共享目录
• 以下操作在服务端上
•vim /etc/exports //增加
/tmp/ 192.168.230.0/24(rw,sync,no_root_squash)
192.168.230.135
[root@cham002 ~]# exportfs -arv
exporting 192.168.230.0/24:/home/nfstestdir
验证一下
[root@cham002 ~]# vim /etc/exports
/home/nfstestdir  192.168.230.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp 192.168.230.145 (rw,sync,no_root_squash)
[root@cham002 ~]# exportfs -arv
exportfs: No options for /tmp 192.168.230.145: suggest 192.168.230.145(sync) to avoid warning
exportfs: No host name given with /tmp (rw,sync,no_root_squash), suggest *(rw,sync,no_root_squash) to avoid warning
exporting 192.168.230.145:/tmp
exporting 192.168.230.0/24:/home/nfstestdir
exporting *:/tmp
192.168.230.145
[root@test ~]# showmount -e 192.168.230.135
Export list for 192.168.230.135:
/home/nfstestdir 192.168.230.0/24
/tmp             (everyone)
[root@test ~]# mount -t nfs 192.168.230.135:/tmp/ /mnt/
[root@test ~]# df -h
文件系统              容量  已用  可用 已用% 挂载点
/dev/sda3              17G  8.5G  7.7G   53% /
devtmpfs              483M     0  483M    0% /dev
tmpfs                 493M     0  493M    0% /dev/shm
tmpfs                 493M  6.8M  486M    2% /run
tmpfs                 493M     0  493M    0% /sys/fs/cgroup
/dev/sda1             197M  109M   88M   56% /boot
tmpfs                  99M     0   99M    0% /run/user/0
192.168.230.135:/tmp   17G  7.0G  9.2G   44% /mnt
[root@test ~]# ls /mnt
champ.sock                                                                test.com.log-20180108
mysql2.sql                                                                test.com.log-20180109
mysql_all.sql                                                             test.com.log-20180110
mysqlbak180116.sql                                                        test.com.log-20180112
mysql.sock                                                                test.com.log-20180115
php-fcgi.sock                                                             test.com.log-20180116
systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt  user.sql
test.com.log
[root@test ~]# vim 123.txt
123
[root@test ~]# ls -l /mnt/
总用量 1964
-rw-r--r-- 1 root  root       16 1月  18 08:09 1212.txt
srw-rw-rw- 1 root  root        0 1月  16 23:48 champ.sock
-rw-r--r-- 1 root  root    30848 1月  16 00:33 mysql2.sql
-rw-r--r-- 1 root  root  1306193 1月  16 00:29 mysql_all.sql
-rw-r--r-- 1 root  root   653300 1月  16 00:14 mysqlbak180116.sql
srwxrwxrwx 1 user5 user5       0 1月  16 23:48 mysql.sock
srw-rw-rw- 1 root  root        0 1月  16 23:48 php-fcgi.sock
drwx------ 3 root  root       17 1月  16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt
-rw-r--r-- 1 root  root        0 1月  17 00:00 test.com.log
-rw-r--r-- 1 root  root        0 1月   5 00:00 test.com.log-20180108
-rw-r--r-- 1 root  root     1583 1月   9 02:21 test.com.log-20180109
-rw-r--r-- 1 root  root        0 1月  10 00:00 test.com.log-20180110
-rw-r--r-- 1 root  root        0 1月  11 00:00 test.com.log-20180112
-rw-r--r-- 1 root  root        0 1月  13 00:00 test.com.log-20180115
-rw-r--r-- 1 root  root        0 1月  16 00:00 test.com.log-20180116
-rw-r--r-- 1 root  root     7002 1月  16 00:22 user.sql
192.168.230.135
[root@cham002 ~]# ls -l /mnt/
总用量 4
-rw-r--r-- 1 root root 16 11月  6 14:12 23.txt
[root@cham002 ~]# ls -l /tmp/
总用量 1964
-rw-r--r-- 1 root  root       16 1月  18 08:09 1212.txt
srw-rw-rw- 1 root  root        0 1月  16 23:48 champ.sock
-rw-r--r-- 1 root  root    30848 1月  16 00:33 mysql2.sql
-rw-r--r-- 1 root  root  1306193 1月  16 00:29 mysql_all.sql
-rw-r--r-- 1 root  root   653300 1月  16 00:14 mysqlbak180116.sql
srwxrwxrwx 1 mysql mysql       0 1月  16 23:48 mysql.sock
srw-rw-rw- 1 root  root        0 1月  16 23:48 php-fcgi.sock
drwx------ 3 root  root       17 1月  16 23:48 systemd-private-4045549bb7d44cbd9149ab7506b1c43c-vmtoolsd.service-iF9vYt
-rw-r--r-- 1 root  root        0 1月  17 00:00 test.com.log
-rw-r--r-- 1 root  root        0 1月   5 00:00 test.com.log-20180108
-rw-r--r-- 1 root  root     1583 1月   9 02:21 test.com.log-20180109
-rw-r--r-- 1 root  root        0 1月  10 00:00 test.com.log-20180110
-rw-r--r-- 1 root  root        0 1月  11 00:00 test.com.log-20180112
-rw-r--r-- 1 root  root        0 1月  13 00:00 test.com.log-20180115
-rw-r--r-- 1 root  root        0 1月  16 00:00 test.com.log-20180116
-rw-r--r-- 1 root  root     7002 1月  16 00:22 user.sql
说明:通常情况下,不限制root的情况多,就是no_root_squash
14.5 NFS客户端问题
针对NFS4版本在centos6中应用存在如下问题:
客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、数组为nobody。
解决方法:
- 方法1:在客户端进行挂载时加上选项-o nfsvers=3 - [root@test ~]# mount -t nfs -o nfsvers=3 192.168.230.135:/tmp/ /mnt/ 如果目录已经挂载,而又不想卸载,执行如下命令: [root@test ~]# mount -t nfs -oremount,nfsvers=3 192.168.230.135:/tmp/ /mnt/ 
- 方法2:客户端和服务端都需要 
•客户端和服务端都需要
• vim /etc/idmapd.conf //
• 把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务(cenos7 是没有这个服务的,重启rpcbind就行)

 
  
  
  
 
 
  
 