Linux下NFS搭建步骤


一、NFS简介

NFS是Network File System的缩写,即网络文件系统。一种使用于分散式文件协定,有SUN公司开发。功能是通过网络让不同的机器、不同的操作系统能够分享个人数据,让应用程序通过网络可以访问位于服务器磁盘中的数据。

NFS在文件传送或信息传送的过过程中,依赖于RPC协议。RPC,远程过程调用(Remote Procedure Call),是使客户端能够执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,就是因为NFS使用了RPC提供的传输协议,可以说NFS就是使用PRC的一个程序。

NFS服务端、RPC协议、客户端三者可以理解为房源、中介、租客之间的关系:

二、系统环境

      CentOS release 6.7 (Final)  2.6.32-573.el6.i686
        NFS IP:172.16.1.31
        web IP : 172.16.1.8
        /etc/init.d/iptables status
        iptables:未运行防火墙
        SElinux  :getenforce  Permissive
三、开始搭建
1)软件安装,NFS只需要安装两个软件,在通常情况下是作为系统默认软件安装的
    【rpcbind】centos 下面RPC主程序
    【nfs-utils】NFS服务主程序,包括NFS的基本命令和监控程序

[root@nfs01 ~]# yum install rpcbind nfs-utils
 
 2)开启RCP服务
[root@nfs01 ~]# /etc/init.d/rpcbind start
查看rpcbind服务端口


[root@nfs01 ~]# netstat -antlp|grep rpcbind
tcp        0      0 0.0.0.0:111                0.0.0.0:*                  LISTEN      1368/rpcbind
 
查看此时rpc服务上面是否有端口注册


[root@nfs01 ~]# rpcinfo -p localhost
  program vers proto  port  service
    100000    4  tcp    111  portmapper
    100000    3  tcp    111  portmapper
    100000    2  tcp    111  portmapper
    100000    4  udp    111  portmapper
    100000    3  udp    111  portmapper
    100000    2  udp    111  portmapper
 
3)开启NFS服务


[root@nfs01 ~]# /etc/init.d/nfs start
现在rpc上应该能看到好多新被注册的nfs端口了


[root@nfs01 ~]# rpcinfo -p localhost
    100003    2  tcp  2049  nfs
    100003    3  tcp  2049  nfs
    100003    4  tcp  2049  nfs
    100227    2  tcp  2049  nfs_acl
    100227    3  tcp  2049  nfs_acl
 
设置两个服务开机自启动

[root@nfs01 ~]# chkconfig rpcbind on
[root@nfs01 ~]# chkconfig nfs on
 
4)服务端配置共享目录(/data)

配置前确认rpcbind、nfs服务进程正常

[root@nfs01 ~]# /etc/init.d/nfs status
[root@nfs01 ~]# /etc/init.d/rpcbind status
[root@nfs01 ~]# ps -ef|egrep "rpc|nfs"
rpc      1368    1  0 00:14 ?        00:00:00 rpcbind
rpcuser  1391    1  0 00:14 ?        00:00:00 rpc.statd
root      1440    2  0 00:14 ?        00:00:00 [rpciod/0]
root      1449    1  0 00:14 ?        00:00:00 rpc.rquotad
root      1454    1  0 00:14 ?        00:00:00 rpc.mountd
root      1461    2  0 00:14 ?        00:00:00 [nfsd4]
root      1462    2  0 00:14 ?        00:00:00 [nfsd4_callbacks]
root      1463    2  0 00:14 ?        00:00:00 [nfsd]
root      1464    2  0 00:14 ?        00:00:00 [nfsd]
root      1465    2  0 00:14 ?        00:00:00 [nfsd]
root      1466    2  0 00:14 ?        00:00:00 [nfsd]
 
创建共享目录并授权("nfsnobody")

 #nfsnobody 用户是开启rpc、nfs进程后系统自动创建的

[root@nfs01 ~]# mkdir /data
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data 
 
修改服务端配置文件(/etc/exports)


[root@nfs01 ~]# vim /etc/exports


#share /data by oldboy for bingbing at 20160524
/data 172.16.1.0/24(rw,sync)
####
/data1 172.16.1.0/24(rw,sync,all_squash,anonuid=65534,anongid=65534)
 
注意!
#此时可以修改“anonuid”值来修改NFS默认虚拟用户,前提是用户在系统中存在,可以指定“-s /sbin/nologin”
查看系统加载的配置

[root@nfs01 ~]# cat /var/lib/nfs/etab
/data  172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
 
重新平滑加载服务

 

[root@nfs01 ~]# /etc/init.d/nfs reload
 
确认服务、目录等配置正确,共享成功


[root@nfs01 ~]# showmount -e
Export list for nfs01:
/data  172.16.1.0/24
 
5)客户端配置

客户端只需要安装rpcbind程序,并确认服务正常

[root@web01 ~]# /etc/init.d/rpcbind status
rpcbind (pid  1361) 正在运行...
 
挂载nfs共享目录

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
Filesystem        Size Used Avail Use% Mounted on
/dev/sda3          6.9G 1.3G  5.2G  20% /
tmpfs              503M    0 503M  0% /dev/shm
/dev/sda1          190M  33M  147M  19% /boot
172.16.1.31:/data  6.9G 1.3G  5.2G  20% /mnt
 
开机自动挂载

echo "mount -t nfs172.16.1.31:/data /mnt">>/etc/rc.local

另外,实现NFS共享自动挂载也可以在/ets/fstab里实现,不过此时要系统开启netfs服务
因为有可能在系统在重启过程中,先实现挂载,后启动网络,此时出现挂载不成功问题

开启netfs服务
[root@web01 ~]# chkconfig netfs on

NFS文件系统详解 

Ubuntu 12.04安装NFS server

NFS服务器安装配置实现Ubuntu 12.04与ARM文件共享

Ubuntu搭建nfs服务器

文件服务器NFS配置详解

Ubuntu下搭建NFS网络文件系统服务器

Heartbeat_ldirector+LB+NFS实现HA及LB、文件共享

CentOS 5.5配置NFS服务器教程

Ubuntu 12.10下NFS的安装使用

本文永久更新链接地址

相关内容