NFS服务搭建与配置

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上的一致

总结:NFC服务需要借助RPC协议实现通信。


NFS服务端安装配置

实验需要2台机器,一台作为服务端,一台作为客户端。

服务端,安装2个包nfs-utils和rpcbind

  [root@zyshanlinux-001 ~]# yum install -y nfs-utils rpcbind
  ​
  Installed:
    nfs-utils.x86_64 1:1.3.0-0.54.el7                               rpcbind.x86_64 0:0.2.0-44.el7 
客户端,安装包nfs-utils

  [root@zyshanlinux-02 ~]# yum install -y nfs-utils
  ​
  Installed:
    nfs-utils.x86_64 1:1.3.0-0.54.el7 
配置文件,允许共享主机IP

  [root@zyshanlinux-001 ~]# vim /etc/exports
配置内容,就一行

  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
保存配置文件后,执行如下准备操作

首先要创建分享的目录,给创建的目录赋予777的权限。

  [root@zyshanlinux-001 ~]# mkdir /home/nfstestdir
  [root@zyshanlinux-001 ~]# chmod 777 /home/nfstestdir
服务端启动rpcbind前后监听端口情况

  [root@zyshanlinux-001 ~]# 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:80              0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1086/sshd           
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1325/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp6       0      0 :::22                   :::*                    LISTEN      1086/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1325/master         
  tcp6       0      0 :::3306                 :::*                    LISTEN      1447/mysqld         
  [root@zyshanlinux-001 ~]# systemctl start rpcbind
  [root@zyshanlinux-001 ~]# 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      46926/rpcbind       
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1086/sshd           
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1325/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp6       0      0 :::111                  :::*                    LISTEN      46926/rpcbind       
  tcp6       0      0 :::22                   :::*                    LISTEN      1086/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1325/master         
  tcp6       0      0 :::3306                 :::*                    LISTEN      1447/mysqld 
客户端启动rpcbind前后监听端口情况

  [root@zyshanlinux-02 ~]# 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:22              0.0.0.0:*               LISTEN      878/sshd            
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1113/master         
  tcp6       0      0 :::22                   :::*                    LISTEN      878/sshd            
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1113/master         
  [root@zyshanlinux-02 ~]# ps aux |grep rpc
  root     21597  0.0  0.0 112660   964 pts/0    R+   20:38   0:00 grep --color=auto rpc
  [root@zyshanlinux-02 ~]# systemctl start rpcbind
  [root@zyshanlinux-02 ~]# 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      22898/rpcbind       
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      878/sshd            
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1113/master         
  tcp6       0      0 :::111                  :::*                    LISTEN      22898/rpcbind       
  tcp6       0      0 :::22                   :::*                    LISTEN      878/sshd            
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1113/master         
  [root@zyshanlinux-02 ~]# ps aux |grep rpc
  rpc      22898  0.0  0.0  64956  1048 ?        Ss   20:39   0:00 /sbin/rpcbind -w
  root     23120  0.0  0.0 112660   968 pts/0    R+   20:39   0:00 grep --color=auto rpc
  ​
启动NFS

  [root@zyshanlinux-001 ~]# systemctl start nfs
  [root@zyshanlinux-001 ~]# ps aux |grep nfs
  root     51317  0.0  0.0      0     0 ?        S<   20:42   0:00 [nfsd4_callbacks]
  root     51323  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51324  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51325  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51326  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51327  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51328  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51329  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51330  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51574  0.0  0.0 112704   960 pts/0    R+   20:42   0:00 grep --color=auto nfs
  [root@zyshanlinux-001 ~]# ps aux |grep rpc
  rpc      46926  0.0  0.0  69220  1428 ?        Ss   20:38   0:00 /sbin/rpcbind -w
  root     51289  0.0  0.0      0     0 ?        S<   20:42   0:00 [rpciod]
  rpcuser  51290  0.0  0.0  42420  1752 ?        Ss   20:42   0:00 /usr/sbin/rpc.statd
  root     51307  0.0  0.0  42608   940 ?        Ss   20:42   0:00 /usr/sbin/rpc.mountd
  root     51308  0.0  0.0  45924   540 ?        Ss   20:42   0:00 /usr/sbin/rpc.idmapd
  root     52778  0.0  0.0 112704   956 pts/0    R+   20:43   0:00 grep --color=auto rpc
  ​
服务端开机启动NFS

  [root@zyshanlinux-001 ~]# systemctl enable nfs
  Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

NFS配置选项

  [root@zyshanlinux-001 ~]# cat /etc/exports
  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
rw 读写 ro 只读 sync 同步模式,内存数据实时写入磁盘 async 非同步模式 no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大 root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户 all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户 anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

##客户端连接服务端的IP

  [root@zyshanlinux-02 ~]# showmount -e 192.168.106.128
  clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
报错,排除是网络不通的问题,那就需要关闭防火墙,服务端客户端都关闭。

服务端:

1、关闭防火墙 2、关闭SELinux,但服务端已经关闭。

  [root@zyshanlinux-001 ~]# systemctl stop firewalld
  [root@zyshanlinux-001 ~]# setenforce 0
  setenforce: SELinux is disabled
客户端:

1、关闭防火墙 2、关闭SELinux

  [root@zyshanlinux-02 ~]# systemctl stop firewalld
  [root@zyshanlinux-02 ~]# getenforce
  Enforcing
  [root@zyshanlinux-02 ~]# setenforce 0
连接服务端IP成功

  [root@zyshanlinux-02 ~]# showmount -e 192.168.106.128
  Export list for 192.168.106.128:
  /home/nfstestdir 192.168.106.0/24
挂载服务端共享的目录,用df -h测试,挂载成功

  [root@zyshanlinux-02 ~]# mount -t nfs 192.168.106.128:/home/nfstestdir /mnt
  [root@zyshanlinux-02 ~]# df -h
  Filesystem                        Size  Used Avail Use% Mounted on
  /dev/sda3                          28G 1011M   27G   4% /
  devtmpfs                          907M     0  907M   0% /dev
  tmpfs                             916M     0  916M   0% /dev/shm
  tmpfs                             916M  8.8M  908M   1% /run
  tmpfs                             916M     0  916M   0% /sys/fs/cgroup
  /dev/sda1                         197M  113M   85M  58% /boot
  tmpfs                             184M     0  184M   0% /run/user/0
  192.168.106.128:/home/nfstestdir   28G  7.3G   21G  27% /mnt
由于配置文件上设置了属主和属组,可以看到文件的属主和属组都为1000

客户端:创建文件,创建的文件的属主和属组都为1000,由于没有该用户,都用1000代替

  [root@zyshanlinux-02 mnt]# touch zyshanlinux.111
  [root@zyshanlinux-02 mnt]# ls -l
  total 0
  -rw-r--r--. 1 1000 1000 0 Jul 15 21:20 zyshanlinux.111
  [root@zyshanlinux-02 mnt]# id 1000
  id: 1000: no such user
服务端:客户端创建的文件在服务端查看,属主是user1,属组是1000

  [root@zyshanlinux-001 ~]# ls -l /home/nfstestdir
  total 0
  -rw-r--r-- 1 user1 1000 0 Jul 15 21:20 zyshanlinux.111
  [root@zyshanlinux-001 ~]# id user1
  uid=1000(user1) gid=1001(user1) groups=1001(user1)

exportfs命令

如果要服务端要关闭或重启NFS,需要先把客户端挂载服务端的目录先卸载

  [root@zyshanlinux-02 mnt]# umount /mnt
  umount.nfs4: /mnt: device is busy
  [root@zyshanlinux-02 mnt]# cd 
  [root@zyshanlinux-02 ~]# umount /mnt
  [root@zyshanlinux-02 ~]# 
由于服务端不能随意关闭或重启nfs,会导致客户端正在挂载的目录读写会出现问题。引入了exportfs命令,不许重启NFS服务,配置文件也会生效。

修改配置文件

  [root@zyshanlinux-001 ~]# !vi
  vim /etc/exports
新增配置内容,记得IP是允许的客户端IP

  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  /tmp 192.168.106.130(rw,sync,no_root_squash)
服务端,全部共享目录重新挂载并显示,不用重启nfs服务,配置文件就会生效

  [root@zyshanlinux-001 ~]# exportfs -arv
  exporting 192.168.106.130:/tmp
  exporting 192.168.106.0/24:/home/nfstestdir
客户端也生效了。

  [root@zyshanlinux-02 ~]# !showm
  showmount -e 192.168.106.128
  Export list for 192.168.106.128:
  /home/nfstestdir 192.168.106.0/24
  /tmp             192.168.106.130
客户端挂载

  [root@zyshanlinux-02 ~]# mount -t nfs 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# cd /mnt
  [root@zyshanlinux-02 mnt]# ls
  mysql2.sql               systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-chronyd.service-Tpa4NU
  mysql_all.sql            systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-vgauthd.service-iHfxUd
  mysql.sock               systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-vmtoolsd.service-mSuqtC
  mysql.sql                test.com.log
  pear                     test.com.log-20180704
  php_errors.log-20180704  user.sql
  php-fcgi.sock
由于配置文件中写了no_root_squash,所以客户端和服务端属主属组的差异

  [root@zyshanlinux-02 mnt]# vi 1212.txt
  [root@zyshanlinux-02 mnt]# ls -l 1212.txt
  -rw-r--r--. 1 root root 28 Jul 15 22:21 1212.txt
  ​
  [root@zyshanlinux-001 tmp]# ls -l 1212.txt
  -rw-r--r-- 1 root root 28 Jul 15 22:21 1212.txt

NFS客户端问题

NFS 4版本会有该问题 客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、属组为nobody 客户端挂载时加上 -o nfsvers=3 客户端和服务端都需要 vim /etc/idmapd.conf // 把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务


先在挂载前

mount -t nfs 192.168.106.128:/tmp/ /mnt/

再挂载

mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/

  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  mount.nfs: an incorrect mount option was specified
  [root@zyshanlinux-02 ~]# cd
  [root@zyshanlinux-02 ~]# umount /mnt/
  umount: /mnt/: not mounted
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  mount.nfs: an incorrect mount option was specified
  [root@zyshanlinux-02 ~]# mount -t nfs -o nfsvers=3 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# df -h
  Filesystem             Size  Used Avail Use% Mounted on
  /dev/sda3               28G  1.1G   27G   4% /
  devtmpfs               907M     0  907M   0% /dev
  tmpfs                  916M     0  916M   0% /dev/shm
  tmpfs                  916M  8.7M  908M   1% /run
  tmpfs                  916M     0  916M   0% /sys/fs/cgroup
  /dev/sda1              197M  113M   85M  58% /boot
  tmpfs                  184M     0  184M   0% /run/user/0
  192.168.106.128:/tmp/   28G  7.3G   21G  27% /mnt
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
--------------------- 

NFS exports参数说明

https://blog.csdn.net/nirenxiaoxiao/article/details/17711969

rw 可读写的权限 
ro 只读的权限 
no_root_squash 登入NFS主机,使用该共享目录时相当于该目录的拥有者,如果是root的话,那么对于这个共享的目录来说,他就具有root的权 
               限,这个参数『极不安全』,不建议使用

root_squash 登入NFS主机,使用该共享目录时相当于该目录的拥有者。但是如果是以root身份使用这个共享目录的时候,那么这个使用者(root)
             的权限将被压缩成为匿名使用者,即通常他的UID与GID都会变成nobody那个身份

all_squash 不论登入NFS的使用者身份为何,他的身份都会被压缩成为匿名使用者,通常也就是nobody
anonuid 可以自行设定这个UID的值,这个UID必需要存在于你的/etc/passwd当中
anongid 同anonuid,但是变成groupID就是了 
sync 资料同步写入到内存与硬盘当中 
async 资料会先暂存于内存当中,而非直接写入硬盘 
insecure 允许从这台机器过来的非授权访问

/etc/exports 部分实例
1./tmp *(rw,no_root_squash) //*号表示所有的IP都可以访问

2./tmp *(rw)

   /home/public 192.168.0.*(rw) *(ro) //下面两行作用一样

   /home/public 192.168.0.0/24(rw) *(ro)

3./home/test 192.168.0.100(rw) //只对某部机器设置权限

4./home/linux *.linux.org(rw,all_squash,anonuid=40,anongid=40)当*.linux.org 登陆此NFS主机,并且在/home/linux下面写入文件时,该文件的所有人与所有组,就会变 
 成/etc/passwd里面对应的UID为40的那个身份的使用者了。

注意:修改了/etc/exports后,并不需要重启nfs服务,只要用exportfs重新扫描一次/etc/exports,并且重新加载即可
exportfs[-aruv] 
参数 含义 
a 全部挂载(或卸载)/etc/exports档案内的设定 
r 重新挂载/etc/exports 里面的设定,也同步的更新/etc/exports和/var/lib/nfs/xtab里面的内容 
u 卸载某一目录 
v 在export的时候,将分享的目录显示到荧屏上 
#exportfs -rv//重新export一次 
#exportfs -au//全部卸载 
举例
node1:~ # df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             9.6G  2.4G  7.2G  25% /
tmpfs                 126M  8.0K  126M   1% /dev/shm
172.16.12.89:/home/test
                      9.6G  2.4G  7.2G  26% /home/nfs
node2:~ # exportfs -av
exporting *:/home/test
node1:~ # umount /home/nfs/
node1:~ # df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             9.6G  2.4G  7.2G  25% /
tmpfs                 126M  8.0K  126M   1% /dev/shm

showmount命令 
语法:showmount [-aed] [hostname] 
-a:显示目前以及连上主机的client机器的使用目录的状态 
-e:显示hostname的/etc/exports里面共享的目录 
-d:只显示被client机器挂载的目录

NFS故障解决 
1、can't contactportmapper: RPC:Remotesystem error-Connection refused: 
   出现这个错误信息是由于SEVER端的PORTMAP没有启动。

2、mountclntudp_create: RPC: Programnotregistered:NFS没有启动起来,可以 
   用showmout-ehost命令来检查NFSSERVER是否正常启动起来。

3、mount: localhost:/home/test failed, reason given by server: Permission denied:
   这个提示是当client要mountnfsserver时可能出现的提示,意思是说本机没有权限去mount nfsserver上的目录。解决方法当然是去修改NFSSERVER咯。

4、被防火墙搞掉 
   这个原因很多人都忽视了,在有严格要求的网络环境中,我们一般会关闭linux上的所有端口,当需要使用哪个端口的时候才会去打开。而NFS默认是使用111端口,所以我们先 
   要检测是否打开了这个端口,另外也要检查TCP_Wrappers的设定。

   重新设置防火墙,包括iptables与TCP_Wrappers,因为激活了portmap,所以端口111必须提供出去.因此在iptablesrules中,要增加: 
   iptables-AINPUT-pTCP --dport111-jACCEPT 
   iptables-AINPUT-pUDP --dport111-jACCEPT 
   如果还不行,那就是TCP_Wrappers的问题,检查/etc/hosts.deny,如果有一行是: 
   ALL:ALL: deny 
   那就必须在/etc/hosts.allow中增加: 
   portmap:ALL:allow

注意:由于NFS使用的这个RPC在client端连上主机时,那么你的主机想要关机,那可就会成为『不可能的任务』。即如果你的Server上面还有Client在联机,那么你要关机,可能 
      得要等到数个钟头才能够正常的关机成功!所以,建议在NFSServer要关机之前,要先『关掉portmap与nfs』这两个东西。如果无法正确的将这两个daemons关掉,那么先以    
      netstat -utlp找出PID,然后kill掉。

NFS客户端设置
为了担心会不小心将NFS端挂进来的具有SUID权限档案的程序执行,root可以将NFS所分享的目录以较为安全的情况挂载进来,可以 
#mount-t nfs -o nosuid,ro hostname:/directory/mountponit

mountnfs的其它可选参数:
HARD:在后台,NFSCLIENT会不断的尝试与SERVER的连接,直到mount上
SOFT:会在前台尝试与SERVER的连接,是默认的连接方式。当收到错误信息后终止mount尝试,并给出相关信息。 
例如:mount-F nfs -o hard192.168.0.10:/nfs /nfs

timeo=n:设置超时时间,当数据传输遇到问题时,会根据这个参数尝试进行重新传输
intr 允许通知中断一个NFS调用。当服务器没有应答需要放弃的时候有用处。
retry=n:设定当网络传输出现故障的时候,尝试重新连接多少时间后不再尝试。默认的数值是10000minutes 
同时使用多个参数的方法:mount -t nfs -o timeo=3,udp,hard 192.168.0.30:/tmp/nfs

注意:NFS客户机和服务器的选项并不一定完全相同,而且有的时候会有冲突。比如说服务器以只读的方式导出,客户端却以可写的方式mount,虽然可以成功mount上,但尝 
     试写入的时候就会发生错误。一般服务器和客户端配置冲突的时候,会以服务器的配置为准。

-----------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值