接上篇:http://iceeggplant.blog.51cto.com/1446843/967951
三、 nfs共享给其它客户端方法之
法一:
服务端portmap,nfs启动,并加至开机启动
chkconfig portmap on
chkconfig nfs on
配置文件更改: /etc/exports
/data 192.168.1.0/24(rw,sync)
将server端共享目录属主nfsnobody. 前提server端和client端的nfsnobody UID/GID对应是相同的.
/var/lib/nfs/etab可查看到anonuid/gid=65534
注:rpcinfo -p ser_ip检查protmap是否有问题
客户端只需启动portmap(详见nfs原理说明).
showmount -e ser_ip查看共享出来的目录
mount -t nfs ser_ip:/data /mnt
/data共享目录,nfs客户端访问过来是以UID 65534过来的,所以如不设置服务端的共享目录UID为65534的用户则无写入权限.
法二:
服务器exports配置时加上all_squash参数,无论客户端什么用户都会压缩成匿名用户nfsnobody
/data 192.168.1.0/24(rw,sync,all_squash)
注:64位的nfsnodby UID默认不是65534且本地无对应UID的用户,此时需要更改对应参数.
如在nfs服务端做个修改,(rw,sync,all_squash,anonuid= ? ,anongid= ? )
法三:
服务端和客户端新建共同的帐号和组.然后设置参数anonuid的id号为新建的共同帐号uid/gid.
一般来说,当NFS服务器提供的只是普通数据(图片,附件,视频)等,可以更优化安全的方法:
mount -t nfs -o nosuid,noexec,nodev,rw 192.168.1.2:/testnfs /testnfs
四、 nfs生产环境实例
不论是32位或64位nfs 有个匿名帐号(nfsnobody)UID,GID。可通过设置nfs匿名帐号来使多个客户端读取或写入nfs服务器时都有权限。即不论是什么帐号只要保证nfs服务端共享目录的属主组uid,gid与配置文件中anonuid/gid一致就可以。因为客户端访问过来的用户会映射成exports配置文件中设置的uid用户。
32位系统默认uid:65534
64位系统默认:4249....
nfs-server
[root@SN121_test_db01 tmp]# rpm -qa nfs-utils nfs-utils-1.0.9-50.el5 [root@SN121_test_db01 tmp]# /etc/init.d/portmap start 启动 portmap: [确定] [root@SN121_test_db01 tmp]# /etc/init.d/nfs start 启动 NFS 服务: [确定] 关掉 NFS 配额: [确定] 启动 NFS 守护进程: [确定] 启动 NFS mountd: [确定] 正在启动 RPC idmapd: [确定] [root@SN121_test_db01 tmp]# chkconfig portmap on [root@SN121_test_db01 tmp]# chkconfig nfs on [root@SN121_test_db01 tmp]# vi /etc/exports /data 192.168.40.0/24(rw,sync) [root@SN121_test_db01 tmp]# grep nfsnobody /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@SN121_test_db01 tmp]# chown nfsnobody /data -R;chmod 755 /data [root@SN121_test_db01 tmp]# /etc/init.d/nfs reload 查看: [root@SN121_test_db01 tmp]# cat /var/lib/nfs/etab /data/tmp 192.168.40.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,mapping=identity,anonuid=65534,anongid=65534) [root@SN121_test_db01 tmp]# rpcinfo -p 192.168.40.93 程序 版本 协议 端口 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100011 1 udp 731 rquotad 100011 2 udp 731 rquotad 100011 1 tcp 734 rquotad 100011 2 tcp 734 rquotad 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100021 1 udp 50582 nlockmgr 100021 3 udp 50582 nlockmgr
说明:
1.先启动portmap服务,再启动nfs服务。
2.设开机自启动。 开关机自启动的顺序可查看相关服务的启动脚本
如more /etc/init.d/portmap
#chkconfig: 345 13 87 启动级别3/4/5 启动开始顺序13,越小越靠前先启动 结束顺序87
3./etc/exports
/data 192.168.1.0/24(rw,sync)
sync当客户端将数据写入服务器时同时同步写入到硬盘上。将/data作为共享目录共享给nfs客户端默认配置more /var/lib/nfs/etab anonuid/gid=65534可以确保所有客户端向服务端写入数据时使用的是65534 uid,gid.
4./etc/init.d/nfs reload portmap已启动的时候,可以nfs reload避免需要portmap重新注册获取端口,等同exportfs –r 命令.
5.客户端访问过来映射的是nfs配置文件里设置好的anonuid:65534,所以共享目录/data必须要给65534的uid读写权限。/data 777权限或将/data属主组改为655534的uid属主组。
chown nfsnodbody.nfsnodby /data -R
nfs-client
[root@SN121_test_db02 tmp]# /etc/init.d/portmap start Starting portmap: [ OK ] [root@SN121_test_db02 tmp]# grep nfsnobody /etc/passwd #存在共同的65534uid用户 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@SN121_test_db02 tmp]# showmount -e 192.168.40.93 mount clntudp_create: RPC: Program not registered #nfs服务器端的rpc没注册上有问题,portmap重启后依然,后来查明服务器端hosts.allow做了限制,开通对本机的访问即可. [root@SN121_test_db02 tmp]# showmount -e 192.168.40.93 Export list for 192.168.40.93: /data 192.168.40.0/24 [root@SN121_test_db02 tmp]# mount -t nfs 192.168.40.93:/data /data [root@SN121_test_db02 tmp]# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 4.9G 1.4G 3.3G 30% / /dev/xvda3 11G 221M 11G 3% /var tmpfs 257M 0 257M 0% /dev/shm 192.168.40.93:/data 49G 205M 46G 1% /data [root@SN121_test_db02 data]# touch aa bb cc #新建文件默认是nfsnobody用户 [root@SN121_test_db02 data]# ll total 28 -rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 16 09:57 aa -rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 16 09:57 bb -rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 16 09:57 cc drwx------ 2 root root 16384 Jul 2 10:23 lost+found
注:还有另外一种方法,客户端和服务器端新建一统一帐号和uid/gid=1207,nfs服务端更改/data共享目录属主组,但客户端多的话这种方式比较麻烦.更改配置文件如下:
/data 192.168.40.0/24(rw,sync,all_squash,anonuid=1207,anongid=1207) à指定uid为1207的帐号
具体操作:
nfs-client:
[root@SN121_test_db02 data]# groupadd -g 1207 testnfs [root@SN121_test_db02 data]# useradd -u 1207 -g testnfs testnfs [root@SN121_test_db02 data]# id testnfs uid=1207(testnfs) gid=1207(testnfs) groups=1207(testnfs)
nfs-server:
[root@SN121_test_db01 tmp]# groupadd -g 1207 testnfs [root@SN121_test_db01 tmp]# useradd -u 1207 -g 1207 testnfs [root@SN121_test_db01 tmp]# id testnfs uid=1207(testnfs) gid=1207(testnfs) groups=1207(testnfs) [root@SN121_test_db01 tmp]# chown testnfs.testnfs /data -R [root@SN121_test_db01 tmp]# /etc/init.d/nfs reload [root@SN121_test_db01 tmp]# cat /etc/exports /data 192.168.40.0/24(rw,sync,all_squash,anonuid=1207,anongid=1207)
五、 nfs客户端可挂载的详细参数:
mount -t nfs -o nosuid,noexec,nodev,rw 10.0.0.22:/data /mnt 特殊权限的挂载
mount主要参数:
-a Mount all filesystems (of the given types) mentioned in fstab
-n Mount without writing in /etc/mtab. This is necessary for example when /etc is on a read-only file sys-tem.
-t type
-o options
-r Mount the file system read-only. A synonym is -o ro
-w Mount the file system read/write. This is the default. A synonym is -o rw
man mount 查看里面的-o 选项:sync atime auto exec....参数
-o options 设置设备或文件的挂载方式,常用options: | |
ro,rw | 采用只读或读写方式挂载 |
async,sync | 此partition为同步写入(sync)或异步写入( 默认是async) |
loop | 用来把一个文件当成硬盘分区挂载上系统(比如将iso镜像文件挂在硬盘分区上) |
dev,nodev | 是否允许此partition上可以建立档案,dev为可以. |
exec,noexec | 是否允许此partition上拥有可执行的binary档案 |
suid,nosuid | 是否允许此partition含有suid/sgid的档案格式. |
auto | Can be mounted with the -a option. -a Mount all filesystems (of the given types) mentioned in fstab. mount –a 对所有fstab文件中的设置进行自动挂载 |
defaults | 默认值为:rw,suid,dev,exec,auto,nouser,async |
remount | 重新挂载,在系统出错或重新更新参数时很有用 如fstab的修复进入单用户模式,/分区只读的状况. |
user,nouser | 是否允许此partition让user执行mount,一般来说mount仅有root可以操作 |
内核优化:(待补充)
输入队列的内存优化cat /proc/sys/net/core/rmem_max
可以调大点262144 看nfs官方文档说明
rsize,wsize 优化nfs的参数,读取和写入时block大小
对于高并发或其它要求的优化参数:
fg/bg 当执行挂载时是前台fg还是后台bg执行,若前台执行则持续尝试挂载,直到成功或time out为止,若为后台执行,则mount会在后台持续多次进行mount,而不会影响到前台的程序操作。如果网络不稳定,或服务器常常需要开关机,则建立bg使用。
soft/hard 挂载时会使用RPC呼叫,如果是hard情况,则当两者之间有任何一台主机离线,则RPC会持续呼叫,直到对方恢复联机为止。如果是soft,则RPC会在time out后重复呼叫,而非持续呼叫,因此系统的延迟会不这么明显。
intr 当使用hard方式挂载时,或加上intr参数,则当RPC持续呼叫时,呼叫是可以被中断的。
rsize/wsize 读出rsize与写入wsize的block区块大小。这个设置值可以影响客户端与服务器传输数据的缓冲存储容量。一般来说,如果在局域网内,并且客户端与服务端都具有足够的内存,这个值可以设置大一点,比如32768bytes,提升缓冲区块将可提升NFS文件系统的传输能力。
rsize=1024
wsize=1024
mount -t nfs -o nosuid,noexec,nodev,rw,hard,intr,rsize=32768,wsize=32768 192.168.1.2:/data /data
六、 nfs总结
1. /var/lib/nfs/etab查看nfs配置状态
/var/lib/nfs/rmtab 查看哪些nfs客户端挂载过来的记录
2. 客户端将挂载放到rc.local开机自动挂载mount -t nfs 192.168.x.x:/.. /...
mount 写入/etc/fstab时可以将fsck检测磁盘设为0,不检测防止因nfs错误启动不了系统。
mount –a 自动挂载fstab里的内容
showmount –e ip 查看nfs-server端共享出来的目录
3. server端查看rpcinfo -p localhost 查看开放的nfs端口
nfs可能出现的问题:
umount时,device is busy
1.可能没有退出当前挂载的nfs目录,则会提示
2.当nfs server挂了,nfs client会出问题(df -h窗口会死掉)
强制卸载umount -lf /data
3. mount clntudp_create: RPC: Program not registered nfs服务器端的rpc服务注册有问题或网络策略限制.
4.mount.nfs: access denied by server while mounting.....
nfs共享一直挂载不上,原因是:被共享的机器发起请求的端口超过1024了,无法挂载网络共享目录.
I googled and found that since the port is over 1024 I needed to add the "insecure" option to the relevant line in /etc/exports on the server. Once I did that (and ran exportfs -r), the mount -a on the client worked..
//如果端口号大于1024,则需要将 insecure 选项加入到配置文件(/etc/exports)相关选项中mount客户端才能正常工作:
nfs优缺点:
简单易操作
部署维护简单
数据可靠性高。
适合于中小型网站
局限:
单点故障,rsync+inotify或负载均衡可解决或nfs+drbd+heartbeat实现高可用.
高并发时nfs性能有限(一般千万以下PV网站不是瓶颈)
多机器挂载服务器时,NFS服务端出问题后,所有客户端都挂掉状态(可使用autofs自动挂载解决。 )
补充:
man exportfs
exportfs -auv 停止输出所有目录
exportfs -rv 启用所有目录
exportfs -r =/etc/init.d/nfs reload
exportfs -o rw,sync,all_squash,anonuid=65535,anongid=65535,mp,fsid=2 10.0.0.0/24:/wyan 也可以不用在exports配置文件中设置,即时生效.
说明:
1. portmap服务启动,加开机自启动
2.showmount -e 192.168.40.93 查看服务端已开放的挂载点
mount -t nfs 192.168.40.93/data /data
注:上例中/data 192.168.40.0/24(rw,sync,all_squash)如加all_squash参数 无论客户端什么用户都会压缩成nfsnobody用户。这适用于既有32位又有64位的混合文件系统中,因为64位系统nfsnodboy默认uid不是65534.
nfs客户端都是64位系统时,默认nfsnobody的uid为42949... 这与服务器端违背,这就需要更改服务器端默认uid是65534的配置
/etc/exports
/data 192.168.1.0/24(rw,sync,all_squash,anonuid=42949...,anongid=42949....)
/etc/init.d/nfs reload
补充:
centos6里面可是portmap服务改名了,可以使用yum来安装rpcbind, service rpcbind start来启动
转载于:https://blog.51cto.com/iceeggplant/967960