NFS网络文件系统

查看linux支持哪些文件系统

/usr/lib/modules/$(uname -r)/kernel/fs

NFS 网络文件系统

http://nfs.sourceforge.net/nfs-howto/
https://moosefs.com/support/#documentation
https://github.com/donnie4w/wfs

NFS的历史介绍

第一个网络文件系统被称为File Access Listener 由Digital Equipment Corporation (DEC)在1976年开发
NFS是第一个构建于IP协议之上的现代网络文件系统。在20世纪80年代,它首先作为实验的文件系统,由 Sun Microsystems 在内部完成开发。NFS协议归属于Request for Comments (RFC)标准,并且随后演化为了NFSv2 作为一个标准,由于NFS与其他客户端和服务器的互操作能力很好而发展快速

NFS工作流程

在这里插入图片描述

RPC(Remote Procedure Call)

因为NFS支持的功能相当多,而不同的功能都会使用不同的程序来启动,每启动一个功能就会启用一些端口来传输数据,因此,NFS的功能所对应的端口无法固定,它会随机取用一些未被使用的端口来作为传输之用,其中Centos5.x的随机端口都小于1024,而Centos6.x的随机端口都是较大的

因为端口不固定,这样一来就会造成NFS客户端与NFS服务端的通信故障,因为NFS客户端必须要知道NFS服务器端的数据传输端口才能进行通信,才能交互数据

要解决上面的困扰,就需要通过远程过程调用RPC服务来帮忙了,NFS的RPC服务最主要的功能就是记录每个NFS功能所对应的端口号,并且在NFS客户端请求是将该端口和功能对应的信息传递给请求数据的NFS客户端,从而确保客户端可以连接到正确的NFS端口上去,达到实现数据传输交互数据目的。这个RPC服务类似NFS服务端和NFS客户端之间的一个中介

http://www.tldp.org/HOWTO/NFS-HOWTO/indes.html

http://www.citi.umich.edu/projects/nfsv4/linux/

http://www.vanemery.com/Linux/NFSv4/NFSv4-no-rpcsec.html

http://www.ibm.com/developerworks/cn/linux/l-network-filesystems/

NFS软件使用

NFS软件

nfs-utils portmap(C5.8) rpcbind

查看

rpm -qa nfs-utils portmap rpcbind

服务端安装

yum install nfs-utils rpcbind -y

yum groupinstall "NFS file server" -y

服务端启动

先启动rpc 再启动nfs

[root@nfsserver ~]# /etc/init.d/rpcbind start
正在启动 rpcbind:                                         [确定]
[root@nfsserver ~]# ps -ef |grep rpc
rpc       1751     1  0 23:23 ?        00:00:00 rpcbind
root      1755  1533  0 23:23 pts/0    00:00:00 grep rpc

端口

[root@nfs01 ~]# netstat -lntp |grep rpcbind 
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1842/rpcbind   

查看房源

[root@nfsserver ~]# 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
[root@nfsserver ~]# /etc/init.d/rpcbind stop
停止 rpcbind:                                             [确定]
[root@nfsserver ~]# rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused

再启动nfs

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

NFS默认端口 2049

C7启动

systemctl start rpcbind.service 
systemctl start nfs-server.service
[root@nfs01 ~]# netstat -lntp |grep rpc|grep -v :::
tcp        0      0 0.0.0.0:14661               0.0.0.0:*                   LISTEN      1899/rpc.mountd     
tcp        0      0 0.0.0.0:875                 0.0.0.0:*                   LISTEN      1894/rpc.rquotad    
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1842/rpcbind        
tcp        0      0 0.0.0.0:8151                0.0.0.0:*                   LISTEN      1899/rpc.mountd     
tcp        0      0 0.0.0.0:9692                0.0.0.0:*                   LISTEN      1899/rpc.mountd  
[root@nfsserver ~]# 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
    100011    1   udp    875  rquotad
    100011    2   udp    875  rquotad
    100011    1   tcp    875  rquotad
    100011    2   tcp    875  rquotad
    100005    1   udp  60919  mountd
    100005    1   tcp  46606  mountd
    100005    2   udp  41661  mountd
    100005    2   tcp  36397  mountd
    100005    3   udp  60203  mountd
    100005    3   tcp  53910  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  49919  nlockmgr
    100021    3   udp  49919  nlockmgr
    100021    4   udp  49919  nlockmgr
    100021    1   tcp  51681  nlockmgr
    100021    3   tcp  51681  nlockmgr
    100021    4   tcp  51681  nlockmgr

设置开机自启动并检查

[root@nfsserver ~]# chkconfig nfs on
[root@nfsserver ~]# chkconfig rpcbind on
[root@nfsserver ~]# chkconfig --list nfs
nfs             0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[root@nfsserver ~]# chkconfig --list rpcbind
rpcbind         0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

客户端安装rpcbind和 nfs-utils 启动rpcbind

[root@nfsclient ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@nfsclient ~]# /etc/init.d/rpcbind status
rpcbind (pid  2284) is running...

[root@nfsclient ~]# chkconfig rpcbind on
You have new mail in /var/spool/mail/root
[root@nfsclient ~]# chkconfig --list rpcbind
rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off

NFS进程说明

nfsd(rpc.nfsd)rpm.nfsd的主要功能是管理NFS客户端是否能够登入NFS服务器,其中还包含登入者的ID判别等
mountd(rpc.mountd)rpc.mountd的主要功能则是管理NFS文件系统。当NFS客户端顺利通过rpc.nfsd登入NFS服务端时,在使用NFS服务器提供数据之前,它会去读NFS的配置文件 /etc/exports 来比对NFS客户端的权限,通过这一关之后,还会经过NFS服务端本地文件系统使用权限(就是 owner、group、other权限)的认证程序。如果都通过了,NFS客户端就可以取得使用NFS服务器端文件的权限。注意,这个 /etc/exports 文件也是我们用来管理NFS共享目录的使用权限与安全设置的地方,特别强调,NFS本身设置的是网络共享权限,整个共享目录的权限和目录自身的系统权限有关
rpc.lockd(非必要)可用来锁定文件,用于多客户端同时写入
rpc.statd(非必要)检查文件的一致性,与rpc.lockd有关。两个服务需要客户端、服务器端同时开启才可以;rpc.statd 监听来自其它主机重启的通知,并且管理当本地系统重启时主机列表
rpc.idmapd名字映射后台进程

nfs配置文件

/etc/exports

[root@nfsserver ~]# cat /etc/exports 
#shared data
/data 10.0.0.0/24(rw,sync)

[root@nfsserver ~]# cat**/etc/exports**

/data  172.16.1.0/24(rw,sync,anonuid=888,anongid=888,all_squash)
/nfsdata *(rw,sync,anonuid=65534,anongid=65534,all_squash)
/data/users 10.127.36.0/24(rw,sync,no_root_squash) 10.127.37.0/24(rw,sync,no_root_squash)

/dockerfile_yaml 10.10.98.0/24(insecure,rw,sync,no_root_squash)
# sample /etc/exports file
/               master(rw) trusty(rw,no_root_squash)
/projects       proj*.local.domain(rw)
/usr            *.local.domain(ro) @trusted(rw)
/home/joe       pc001(rw,all_squash,anonuid=150,anongid=100)
/pub            *(ro,insecure,all_squash)
/srv/www        -sync,rw server @trusted @external(ro)
/foo            2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build          buildhost[0-9].local.domain(rw)

/usr/sbin/exportfs [-avi] [-o options,…] [client:/path …]

/usr/sbin/exportfs -o rw,sync,anonuid=888,anongid=888 172.16.1.0/24:/data 相当于修改 /etc/exports

nfs协议版本

RHEL6.5以NFSv3作为默认版本,NFSv3使用TCP、UDP协议(端口号是2049),默认是UDP;

RHEL7以NFSv4作为默认版本,NFSv4使用TCP协议(端口号是2049)

vim /etc/sysconfig/nfs

RPCNFSDARGS="-N 2 -N 3"

nfs服务器开启防火墙

需要开放如下端口

  • portmap 端口 111 udp/tcp;
  • nfsd 端口 2049 udp/tcp;
  • mountd 端口 “xxx” udp/tcp
    如果/etc/services中没有mountd固定端口则添加

vim /etc/services

在末尾添加

mountd 20048/udp
mountd 20048/tcp

防火墙添加规则

firewall-cmd --add-port={111,2049,20048}/tcp
firewall-cmd --permanent --add-port={111,2049,20048}/tcp
firewall-cmd --add-port={111,2049,20048}/udp
firewall-cmd --permanent --add-port={111,2049,20048}/udp
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload

平滑加载配置文件

/etc/init.d/nfs reload <=======> /usr/sbin/exportfs -r

另外一个配置文件

/etc/sysconfig/nfs

[root@localhost ~]# grep 90 /etc/sysconfig/nfs

#NFSD_V4_GRACE=90
#NFSD_V4_LEASE=90
#NLM_GRACE_PERIOD=90

检查nfs 是否有效

  1. 服务端检查
    服务端ip地址 10.0.0.31

客户端ip地址 10.0.0.32

[root@nfsserver ~]# showmount -e localhost
Export list for localhost:
/data 10.0.0.0/24
  1. 客户端检查
[root@nfsclient ~]# showmount -e 10.0.0.31
Export list for 10.0.0.31:
/data 10.0.0.0/24

排查故障

ping 10.0.0.31

telnet 10.0.0.31 111 (rpc服务监听111端口)

客户端挂载

[root@nfsclient ~]# mount -t nfs 10.0.0.31:/data /mnt
[root@nfsclient ~]# df -h
Filesystem       Size  Used Avail Use% Mounted on
/dev/sda3        7.7G  1.5G  5.8G  20% /
tmpfs            116M     0  116M   0% /dev/shm
/dev/sda1        190M   36M  145M  20% /boot
10.0.0.31:/data  7.7G  1.5G  5.8G  20% /mnt

C7

mount.nfs4 172.16.1.31:/data /mnt/

客户端挂载慢问题

客户端挂载慢的问题,/etc/hosts 文件中解析有问题

开机自动挂载

echo "mount -t nfs 172.16.1.31:/data /mnt" >>/etc/rc.local

卸载

umount  -lf  /mnt

nfs挂载默认参数

[root@nfsserver data]# cat /var/lib/nfs/etab 
/data   10.0.0.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@nfsserver data]# grep 65534 /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

修改权限

[root@nfsserver data]# chown -R nfsnobody.nfsnobody /data
[root@nfsserver data]# ll -d /data
drwxr-xr-x 3 nfsnobody nfsnobody 4096 5月   7 01:03 /data

创建NFS用户

[root@nfs01 data]# useradd -u 888 nfs -s /sbin/nologin -M
[root@nfs01 data]# id nfs
uid=888(nfs) gid=888(nfs)=888(nfs)

开机启动netfs服务,用于nfs挂载

如果在开机自启动服务里设置并启动了netfs服务,放入fstab里也是可以开机挂载的

[root@web01 mnt]# chkconfig netfs on
[root@web01 mnt]# tail -1 /etc/fstab 
172.16.1.31:/data       /mnt                    nfs     rw,sync         0 0

nfs挂载安全相关

[root@m01 data]# tail -1 /etc/fstab 
172.16.1.31:/data       /data                   nfs     rw,sync,nosuid,nodev,noexec 0 0

重新挂载

mount -o remount,rw /

生产场景优化,以下参数,但是跟默认的性能一样

mount -t nfs -o noatime,nodiratime,nosuid,noexec,nodev,rsize=131072,wsize=131072  10.0.0.7:/data  /mnt

mount一些参数

mount -o remount,rw /dir

NFS服务配置参数

NFS服务器端的权限设置,即/etc/exports文件配置格式中小括号()里的参数集,如下表:

NFS配置权限设置常用参数说明

参数名称参数用途※
rw※Read-write,表示可读写权限。※
roRead-only,表示只读权限。
sync※请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回。※
优点,数据安全不会丢,缺点,性能比不启用该参数要差
async※写入时数据会先写到内存缓冲区,直到硬盘有空档才会再写入磁盘,这样可以提升写入效率!风险为若服务器宕机或不正常关机,会损失缓冲区中未写入磁盘对的数据(解决办法:服务器主板电池或加UPS不间断电源)!※
no_root_squash访问NFS Server共享目录的用户如果是root的话,则它的权限将被压缩成匿名用户,同时它的UID和GID通常会变成nfsnobody账号身份
all_squash※不管访问NFS Server共享目录的用户身份如何,它的权限都将被压缩成匿名用户,同时它的UID和GID都会变成nfsnobody账号身份。※
在生产中配置NFS的重要技巧:
1)确保所有客户端服务器对NFS共享目录具备相同的用户访问权限
a.all_squash把所有客户端都压缩成固定的匿名用户(UID相同)
b.就是anonuid,anongid指定的UID和GID的用户
2)所有的客户端和服务端都需要有一个相同的UID和GID的用户,即nfsnobody(UID必须相同)
anonuid※参数以anon*开头即指anonymous匿名用户,这个用户的UID设置值通常为nfsnobody的UID值,当然也可以自行设置这个UID值。但是,UID必须存在于/etc/passwd中。如多台web Server共享一个NFS目录,通过这个参数可以使得不同的NFS Clients写入的数据对所有NFS Clients保持同样的用户权限,即为配置的匿名UID对应用户权限,这个参数很有用,一般为默认即可。※
anongid※同anonuid,区别就是把uid(用户id)换成gid(组id)※

客户端查看挂载参数

[root@backup ~]# cat /proc/mounts 
rootfs / rootfs rw 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
devtmpfs /dev devtmpfs rw,relatime,size=231860k,nr_inodes=57965,mode=755 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /dev/shm tmpfs rw,relatime 0 0
/dev/sda2 / ext4 rw,relatime,barrier=1,data=ordered 0 0
/proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0
/dev/sda1 /boot ext4 rw,relatime,barrier=1,data=ordered 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
nfs01:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.41,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

mount挂载参数

NFS Client mount挂载参数列表:

mount参数参数功能默认参数
fg(foreground)
bg(background)
当在客户端执行挂载时,可选择是前台(fg)还是在后台(bg)执行。若在前台执行,则mount会持续尝试挂载,知道成功或挂载时间超时为止,若在后台执行,则mount会在后台持续多次进行mount,而不会影响到前台的其他程序操作。如果网络联机不稳定,或是服务器常常需要开关机,建议使用bg比较妥当fg
soft
hard
当NFS Client以soft挂载service时,若网络或service出现问题,造成Client和server无法传输资料,client就会一直尝试,知道timeout后显示错误才停止。
若使用soft mount的话,可能会在timeout出现时造成资料丢失,故一般不建议使用(害羞的男孩)
若用hard模式挂载硬盘时,刚好和soft相反,此时client会一直尝试连线到servier,若server有回应就继续刚才的操作,若没有回应NFS client会一直尝试,此时无法umount或kill,所以常常会配合intr使用。这是默认值(死缠烂打厚脸皮的男孩)
hard
intr当使用hard挂载的资源timeout后,若有指定intr参数,可以在timeout后把它中断掉,这避免出问题时系统整个被NFS锁死,建议使用intr
rsize
wsize
读出与写入的区块大小(block size),这个设置值可以影响客户端与服务器端传输数据的缓冲存储量,一般来说,如果在局域网内(LAN),并且客户端与服务器端都具有足够的内存,这个值可以设置大一点,比如说65535(bytes),提升缓冲区块将可提升NFS文件系统的传输能力。但设置的值也不要太大,最好以网络能够传输的最大值为限rsize=1024
wsize=1024

centos6:默认值
rsize=131072
wsize=131072
proto=udp使用UDP协议来传输资料,在LAN中会有比较好的性能。若要跨越Internet的话,使用proto=tcp多传输的数据会有比较好的纠错能力proto=tcp
sync该参数和async相反。有I/O操作时,都会同步处理I/O,即把数据同步写入硬盘。此参数会牺牲一点I/O性能,但是,换来的是掉电后数据的安全性
dirsync目录更新时同步写入磁盘
atime在每一次数据访问时,会同步更新访问文件的inode时间戳,是默认选项,在高并发的情况下,建议通过明确加上noatime,来取消这个默认项,以到达提升I/O性能,优化I/O的目的
noatime访问文件时不更新文件的inode时间戳,高并发环境,推荐显示应用该选项,可以提高系统I/O性能
nodiratime不更新文件系统上的directory inode时间戳,高并发环境,推荐显示应用该选项,可以提高系统I/O性能
ro以只读的方式挂载一个文件系统
rw以可写的方式挂载一个文件系统
auto能够被自动挂载通过mount -a选项(/etc/fstab)
noauto不会自动挂载文件系统
defaults这是fstab里的默认值,包括rw、suid、dev、exec、auto、nouser、async and relatime,默认情况大部分都是默认值
exec允许文件系统执行二进制文件,取消这个参数,可以提升系统安全
noexec在挂载的文件系统中不允许直接执行任何二进制的程序,注意,仅对二进制程序有效,即使设置了noexec、shell、php程序还是可以执行的
nosuid不允许ser-user-idenrifier or ser-group-identifier位生效
suid允许ser-user-idenrifier or ser-group-identifier位生效
nodev不读文件系统上的字符或块设备
nouser禁止一个普通用户挂载该文件系统,这是默认挂载时的默认选项
remount尝试重新挂载一个已经挂载了的文件系统,这通常被用来改变一个文件系统的挂载标志,从而使得一个只读文件系统变得可写,这个动作不会改变设备或挂载点。当系统故障时今日single或rescue模式修复系统时,会发现根文件系统经常会变成只读文件系统,不允许修改,此时该命令就派上用处了。具体命令为:mount -o remount,rw /,表示将根文件系统重新挂载使得可写。single或rescue模式修复系统时这个命令十分重要

mount挂载性能优化参数选项

下面介绍几个在企业生产环境下,NFS性能优化挂载的例子。

1)禁止更新目录及文件时间戳挂载,命令如下:

mount -t nfs -o noatime,nodiratime 10.0.0.7:/data

2)安全加优化的挂载方式如下:(兼顾安全性能

mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,intr,rsize=131072,wsize=131072 10.0.0.7:/data /mnt

3)默认的挂载方式如下:

mount -t nfs 10.0.0.7:/data /mnt

/proc/sys/net/core/rmem_defaule 该文件制定了接收套接字缓存区大小的默认值(以字节为单位),默认设置:124928

/proc/sys/net/core/rmem_max 该文件指定了接收套接字缓冲区大小的最大值(以字节为单位),默认设置:124928

/proc/sys/net/core/wmem_default 该文件指定了发送套接字缓冲区大小的默认值(以字节为单位),默认设置:124928

/proc/sys/net/core/wmem_max 该文件指定了发送套接字缓冲区大小的最大值(以字节为单位),默认设置:124928

服务器对应的具体内核优化命令如下:

cat >>/etc/sysctl.conf<<EOF
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
EOF
sysctl -p

nfs帮助

man nfs mount

http://nfs.sourceforge.net/nfs-howto/

http://nfs.sourceforge.net/nfs-howto/ar01s02.html#whatis_nfs

http://en.wikipedia.org/wiki/Network_File_System_(protocol)

nfs cluster

http://ben.timby.com/?p=109

http://www.faqs.org/rfcs/rfc1094.html

http://www.tldp.org/HOWTO/NFS-HOWTO/index.html

http://www.citi.umich.edu/projects/nfsv4/linux/

http://www.vanemery.com/Linux/NFSv4/NFSv4-no-rpcsec.html

http://www.ibm.com/developerworks/cn/linux/l-network-filesystems/

nfs诡异故障

nfs无法启动,message日志如下错误

rpc.nfsd[23732]: error starting threads: errno 12 (Cannot allocate memory)

解决

sync && echo 3 > /proc/sys/vm/drop_caches
  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值