首先安装NFS服务

yum groupinstall "NFS file server"

默认就给安装rpcbind(c6system)了

查询NFS服务向RPC服务注册的端口信息

[root@linux-chen ~]# 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
    100024    1   udp  55908  status
    100024    1   tcp  53125  status

因为NFS还没起动,所以,现在没太多注册的端口映射信息。请留意和后面NFS启动后这里的对比



查询下现在NFS服务运行状态

[root@linux-chen ~]# /etc/init.d/nfs status
rpc.svcgssd 已停
rpc.mountd 已停
nfsd 已停
rpc.rquotad 已停


上边显示没有启动,我们现在启动

[root@linux-chen ~]# /etc/init.d/nfs start
启动 NFS 服务:                                            [确定]
关掉 NFS 配额:                                            [确定]
启动 NFS mountd:                                          [确定]
启动 NFS 守护进程:                                        [确定]
正在启动 RPC idmapd:                                      [确定]

如第一段所说我们在查询下NFS在RPC注册的端口数

[root@linux-chen ~]# 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
    100024    1   udp  55908  status
    100024    1   tcp  53125  status
    100011    1   udp    875  rquotad
    100011    2   udp    875  rquotad
    100011    1   tcp    875  rquotad
    100011    2   tcp    875  rquotad
    100005    1   udp  51537  mountd
    100005    1   tcp  49708  mountd
    100005    2   udp  46369  mountd
    100005    2   tcp  49553  mountd
    100005    3   udp  56355  mountd
    100005    3   tcp  39360  mountd
    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
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    2   udp   2049  nfs_acl
    100227    3   udp   2049  nfs_acl
    100021    1   udp  60653  nlockmgr
    100021    3   udp  60653  nlockmgr
    100021    4   udp  60653  nlockmgr
    100021    1   tcp  56339  nlockmgr
    100021    3   tcp  56339  nlockmgr
    100021    4   tcp  56339  nlockmgr

果然多了很多


查询下NFS运行状态

[root@linux-chen ~]# /etc/init.d/nfs status
rpc.svcgssd 已停
rpc.mountd (pid 1572) 正在运行...
nfsd (pid 1588 1587 1586 1585 1584 1583 1582 1581) 正在运行...
rpc.rquotad (pid 1567) 正在运行...

设置成开机自动启动

[root@linux-chen ~]# chkconfig nfs on
[root@linux-chen ~]# chkconfig rpcbind on
[root@linux-chen ~]# chkconfig --list nfs
nfs            0:off1:off2:on3:on4:on5:on6:off
[root@linux-chen ~]# chkconfig --list rpcbind
rpcbind        0:off1:off2:on3:on4:on5:on6:off



分别more下 rpcbind and nfs server 看看哪个先启动 哪个后启动

[root@linux-chen ~]# more /etc/init.d/rpcbind

#! /bin/sh

#

# rpcbind       Start/Stop RPCbind

#

# chkconfig: 2345 13 87

# description: The rpcbind utility is a server that converts RPC program \

#              numbers into universal addresses. It must be running on the \

#              host to be able to make RPC calls on a server on that machine.

#

# processname: rpcbind

# probe: true

# config: /etc/sysconfig/rpcbind

#!/bin/sh
#
# nfs           This shell script takes care of starting and stopping
#               the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file sharing across networks.
#              This service provides NFS server functionality, which is \
#              configured via the /etc/exports file.
# probe: true
# config: /etc/sysconfig/nfs
### BEGIN INIT INFO
# Provides: nfs
# Required-Start: $local_fs $network $syslog $rpcbind
# Required-Stop: $local_fs $network $syslog $rpcbind
# Default-Stop: 0 1 6
# Short-Description: Start up the NFS server sevice
# Description: NFS is a popular protocol for file sharing across networks \
#              This service provides NFS server functionality, \
#              which is configured via the /etc/exports file.
### END INIT INFO

很明显:rpc先启动nfs后启动,nfs先关闭。rpc后关闭

 

#####################################################################################

客户端配置  

不用多说,看完上边的肯定看的懂下边的。只启动rpc即可。毕竟咱们是一般的client

[root@localhost ~]# service rpcbind start
[root@localhost ~]# /etc/init.d/rpcbind status
rpcbind (pid  2009) is running...
[root@localhost ~]# chkconfig rpcbind on
[root@localhost ~]# chkconfig --list rpcbind
rpcbind        0:off1:off2:on3:on4:on5:on6:off


##############################################################################

server端配置服务

配置文件位置 

[root@linux-chen ~]# ll /etc/exports

-rw-r--r--. 1 root root 0 Jan 12  2010 /etc/exports



开始共享目录data

mkdir /data

复制一些内容到新建目录

cp -r /tmp/temp /data

[root@linux-chen temp]# ls

a.du  b.du  b.sh  leran.log  miao.xx  test1.xx


vi /etc/exports
#shared data for NFS CLIENT at 20160128
/data  10.10.10.1/24(rw,sync)

"rw" 读写权限 “ro”只读权限 “sync”允许写入磁盘权限

重启服务

/etc/init.d/nfs reload 

(reload=平滑重启即等连接的服务完成以后再退出)


 检查下能否共享:

[root@linux-chen ~]# showmount -e localhost

Export list for localhost:

/data 10.10.10.1/24


##########################################################

CLIENT host


检查下能否共享

[root@localhost ~]# showmount -e 10.10.10.16
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

这个错误是因为server host的防火墙没有关闭

所以要在SERVER-HOST操做

[root@linux-chen ~]# service iptables stop
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:   
CLIENT host 重新检查
回到client

[root@localhost ~]# showmount -e 10.10.10.16

Export list for 10.10.10.16:

/data 10.10.10.1/24

即可~!


好!我们现在挂载server的 /data

[root@localhost temp]# mount -t nfs 10.10.10.16:/data /nethost
[root@localhost ~]# cd /nethost/temp
[root@localhost temp]# ls
a.du  b.du  b.sh  leran.log  miao.xx  test1.xx


注意:SERVERhost /data 的目录其他人没有写入权限,所以 客户不能写入。修改后即可

chmod -t nfs 777 /data


附录:图片示意

wKioL1aqACaQsQHtAADqogZQHwQ435.png

补充:

[root@linux-chen ~]# cat /var/lib/nfs/etab
/data10.10.10.1/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)

找到anonuid=65534,anongid=65534

[root@linux-chen ~]# cat /etc/passwd|grep 65534
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

默认用户名字:nfsnobody


############################################################

NFS进程

[root@linux-chen ~]# ps -ef|grep -E "nfs|rpc"

rpc       1299     1  0 00:33 ?        00:00:00 rpcbind  --- rpc服务进程
rpcuser   1321     1  0 00:33 ?        00:00:00 rpc.statd
root      1370     2  0 00:33 ?        00:00:00 [rpciod/0]
root      1379     1  0 00:33 ?        00:00:00 rpc.rquotad
root      1384     1  0 00:33 ?        00:00:00 rpc.mountd ----权限管理进程
root      1391     2  0 00:33 ?        00:00:00 [nfsd4]
root      1392     2  0 00:33 ?        00:00:00 [nfsd4_callbacks]
root      1393     2  0 00:33 ?        00:00:00 [nfsd]
root      1394     2  0 00:33 ?        00:00:00 [nfsd]
root      1395     2  0 00:33 ?        00:00:00 [nfsd]
root      1396     2  0 00:33 ?        00:00:00 [nfsd]
root      1397     2  0 00:33 ?        00:00:00 [nfsd]
root      1398     2  0 00:33 ?        00:00:00 [nfsd]
root      1399     2  0 00:33 ?        00:00:00 [nfsd]
root      1400     2  0 00:33 ?        00:00:00 [nfsd]--nfs的主进程,管理客户否可以登录
root      1429     1  0 00:33 ?        00:00:00 rpc.idmapd
root      1569  1485  0 01:57 pts/0    00:00:00 grep --color=auto -E nfs|rpc

工作场景案例:

实现多台客户端同时挂载一台nfs server时,无论从哪一个客户端写入数据,其他客户端同样也可以读写

,即让所有nfs客户端写入到nfs server 的文件或者目录在nfs server上的用户和组都是同一个名称nethost


#NFS 优点

1,简单,容易上手

2,部署简单,维护简单

3,数据可靠,耐用


缺点:

1,单点故障,如果nfs server宕机,所有客户端无法连接

2,高并发场合,NFS效率低(2千万的PV/日网站不是瓶颈,除非网站架构性能太差)

3,客户端认证时候基于ip和主机名,权限是根绝ID识别,安全性一般,(放置于内网比较好)

4,NFS数据传输是明文,对数据完整性不做完整

5,多台机器挂载NFS时候,连接管理麻烦,尤其服务器端出问题,所有NFS 客户端都是挂掉状态


生产环境配置NFS生产重要技巧

1,确保所有服务器对NFS共享目录具备相同的权限

  a,加all_squash参数 把所有客户端都压缩成匿名用户

  b,就是anonuid,anongid指定的UID和GID的用户

2,所有的客户端和服务端都需要一个相同的UID和GID的用户,即nfsnobody(UID必须相同)


针对客户端挂载nfs 共享目录时候,优化的参数有以下(mount):

1,*******async 锁涉及到文件系统i/O的操作都是异步处理,即不会同步写到磁盘,此参数会提高性能,但是会降低数据安全

一般生产环境,不推荐使用,除非性能要求高,数据可靠性不高的场合

2,atime 在每一次数据访问是,同步更新每次的访问的inode的时间,默认是atime,在高并发的情况下,建议加上noatime来取消默认这个选项,以达到IO,优化目的3,auto 能够被自动挂载

4,******defaults 这是缺省值,rw,suid,dev,exec,auto,nouser,async 

5,exec  运行可以执行二进制文件,取消这个参数会提升系统安全性

6,nodiratime 不更新文件爱你系统上的directory inode访问时间,高并发时候建议使用此选型

7,noauto  不自动挂载

8,noexec  即使设置了noexec shell php 程序还是可以执行

9.nosuid 禁止已给普通用户挂载文件系统,  


10.rsize wsize  如果内存足够大,可以将rsize wsize 设置大点

查看客户端挂载

[root@localhost ~]# grep application /proc/mounts 

192.168.0.221:/application/ /mnt nfs4 rw,relatime,vers=4,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.222,minorversion=0,local_lock=none,addr=192.168.0.221 0 0

mount -t nfs noatime,nodiratime 192.168.0.221:/application /mnt  效率比较好,节省磁盘读写次数,一般规模的站点够用

NFS 优化总结

1,硬件: sas/ssd 磁盘,买多块,raid0/raid0.网卡好

2.服务器端/data 10.10.10.16/24(rw,async,all_squash,anonuid=55,anongid=555)

3.客户端:rsize,wsize,noatime,nodirtime,nosuid,noexec,soff(halt,intr)建议加前四个

4.NFS防火墙优化

一般NFS在内网环境,一般是关闭iptablles :service iptables stop

(1)仅允许IP段访问

iptables -A INPUT -s 10.10.10.1/24 -j ACCEPT

(2)允许IP加端口访问

iptables -a input -i eth1 -p tcp -s 10.10.10.1/24 --dport 11 -j ACCEPT



   借鉴出处:http://douya.blog.51cto.com/6173221/1681449