Linux NFS服务安装及配置

葛大爷 互联网 2016-04-05 3317
  1. NFS服务简介
            NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
            NFS在文件传送或信息传送过程中依赖于RPC协议。RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。
  2. NFS服务安装
    • 查看系统是否已安装NFS所需的软件包(CentOS5检查portmap,CentOS6检查rpcbind)
      [@JCluster01 ~]# rpm -qa|grep nfs
      nfs-utils-1.2.3-64.el6.x86_64
      nfs-utils-lib-1.1.5-11.el6.x86_64
      [@JCluster01 ~]# rpm -qa|grep rpcbind
      rpcbind-0.2.0-11.el6.x86_64
      系统默认已安装了nfs-utils、rpcbind两个软件包。
    • 如果当前系统没有安装FNS所需的软件包,需要手动安装。
    • 在NFS服务器上配置/etc/exports
      NFS主要有3类选项:
      访问权限选项
      设置输出目录只读:ro
      设置输出目录读写:rw
      用户映射选项
      all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);
      no_all_squash:与all_squash取反(默认设置);
      root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);
      no_root_squash:与rootsquash取反;
      anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
      anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);
      其它选项
      secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
      insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
      sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
      async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
      wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
      no_wdelay:若有写操作则立即执行,应与sync配合使用;
      subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
      no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;
      [@JCluster01 ~]# vi /etc/exports
      /opt/uploadfiles 10.2.176.134(rw,sync,no_root_squash)
      /opt/uploadfiles 10.2.176.135(rw,sync,no_root_squash)
      /opt/uploadfiles 10.2.176.136(rw,sync,no_root_squash)
      [@JCluster01 ~]# exportfs -rf
    • 启动NFS服务
      为了使NFS服务能正常工作,需要启动rpcbind和nfs两个服务,并且rpcbind一定要先于nfs启动。
      [@JCluster01 ~]# service rpcbind start
      Starting rpcbind: [ OK ]
      [@JCluster01 ~]# service nfs start
      Starting NFS services: [ OK ]
      Starting NFS quotas: [ OK ]
      Starting NFS mountd: rpc.mountd: svc_tli_create: could not open connection for udp6
      rpc.mountd: svc_tli_create: could not open connection for tcp6
      rpc.mountd: svc_tli_create: could not open connection for udp6
      rpc.mountd: svc_tli_create: could not open connection for tcp6
      rpc.mountd: svc_tli_create: could not open connection for udp6
      rpc.mountd: svc_tli_create: could not open connection for tcp6
      [ OK ]
      Starting NFS daemon: rpc.nfsd: address family inet6 not supported by protocol TCP
      [ OK ]
      Starting RPC idmapd:
    • 查看NFS服务状态
      [@JCluster01 ~]# service rpcbind status
      rpcbind (pid 3028) is running...
      [@JCluster01 ~]# service nfs status
      rpc.svcgssd is stopped
      rpc.mountd (pid 93356) is running...
      nfsd (pid 93376 93375 93374 93373 93372 93371 93370 93369) is running...
      rpc.rquotad (pid 93339) is running...
    • 停步NFS服务
      [@JCluster01 ~]# service rpcbind stop
      Stopping rpcbind: [ OK ]
      [@JCluster01 ~]# service nfs stop
      Shutting down NFS daemon: [ OK ]
      Shutting down NFS mountd: [ OK ]
      Shutting down NFS quotas: [ OK ]
      Shutting down NFS services: [ OK ]
      Shutting down RPC idmapd: [ OK ]
    • 设置NFS服务自动启动
      对于实际的应用系统,每次启动LINUX系统后都手工启动nfs服务器是不现实的,需要设置系统在指定的运行级别自动启动rpcbind和nfs服务。
      [@JCluster01 ~]# chkconfig --list rpcbind
      rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
      [@JCluster01 ~]# chkconfig --list nfs
      nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
      设置rpcbind和nfs服务在系统运行级别3和5自动启动。
      [@JCluster01 ~]# chkconfig --level 35 rpcbind on
      [@JCluster01 ~]# chkconfig --level 35 nfs on
    • 服务器端使用showmount命令查询NFS的共享状态
      [@JCluster01 ~]# showmount -e
      Export list for JCluster01:
      /opt/uploadfiles 10.2.176.136,10.2.176.135,10.2.176.134
    • 客户服务器挂载
      [@JCluster02 ~]mkdir /opt/uploadfiles
      [@JCluster02 ~]# mount -t nfs 10.2.176.133:/opt/uploadfiles/ /opt/uploadfiles/
      [@JCluster02 ~]# mount |grep nfs
      10.2.176.133:/opt/uploadfiles/ on /opt/uploadfiles type nfs (rw,vers=4,addr=10.2.176.133,clientaddr=10.2.176.134)
      启动自动挂载nfs文件系统
      [@JCluster02 ~]# vi /etc/fstab 
      10.2.176.133:/opt/uploadfiles /opt/uploadfiles nfs defaults 0 0
      [@JCluster02 ~]# mount -a
      [@JCluster02 ~]# reboot
      如果重启机器后,未挂载成功,参考下面修改方式,添加标黄的脚本
      [@JCluster02 ~]# vi /etc/rc.d/rc.local
      #!/bin/sh
      #
      # This script will be executed *after* all the other init scripts.
      # You can put your own initialization stuff in here if you don't
      # want to do the full Sys V style init stuff.

      touch /var/lock/subsys/local
      /opt/monitor/sec/bin/monitor > /dev/null 2>&1
      mount -t nfs 10.2.176.133:/opt/uploadfiles /opt/uploadfiles
      [@JCluster02 ~]# reboot