NFS服务搭建

NFS简介

NFS是Network File System的简写,即网络文件系统.

NFS服务搭建

NFS挂载原理介绍:

如上图,当我们在NFS服务端设置好一个共享存储目录/video后,其它有权限访问NFS服务端的NFS客户端可以将这个/video共享目录,挂载到NFS客户端本地系统的某个挂载点(其实就是一个目录,可以随意指定),上图客户端本地的挂载点分别是    /v/video和/video。

当正确挂载完毕后,进入NFS客户端的/v/video或/video目录,就可以看到NFS服务端/video下的所有数据,看起来NFS服务端的/video目录就相当于NFS客户端本地的磁盘分区一样,根据NFS服务端授予的NFS共享权限,在NFS客户端操作/v/video或/video挂载的目录,就可以将数据轻松存取到NFS服务端上的/video目录中。

什么是RPC

因为NFS支持的功能相当多,而不同的功能都会使用不同的程序来启动。每启动一个功能就会启用一些端口来传输数据,因此NFS的功能所对应的端口才没有固定,而是采用随机取用一些未被使用的小于1024的端口来作为传输之用。但如此一来又造成客户端要连接服务器时的困扰,因为客户端要知道服务器端的相关端口才能够联机,此时我们需要远程过程调用(RPC)的服务。RPC最主要的功能就是指定每个NFS功能所对应的端口号,并且回报给客户端,让客户端可以连接到正确的端口上。当服务器在启动NFS时会随机选用数个端口,并主动地向RPC注册。因此RPC可以知道每个端口对应的NFS功能。然后RPC固定使用端口111来监听客户端的请求并回报客户端正确的端口,所以可以让NFS的启动更为容易。注意,启动NFS之前,要先启动RPC;否则NFS会无法向RPC注册。另外,重新启动RPC时原本注册的数据会不见,因此RPC重新启动后它管理的所有程序都需要重新启动以重新向RPC注册。
当客户端有NFS文件要存取请求时,它如何向服务器端要求数据?
(1)客户端会向服务器端的RPC(port 111)发出NFS文件存取功能的询问请求。
(2)服务器端找到对应的已注册的NFS daemon端口后会回报给客户端。
(3)客户端了解正确的端口后,就可以直接与NFS守护进程来联机。
由于NFS的各项功能都必须要向RPC注册,因此RPC才能了解NFS服务的各项功能的port number、PID和NFS在主机所监听的IP等,而客户端才能够通过RPC的询问找到正确对应的端口。即NFS必须要有RPC存在时才能成功地提供服务,因此我们称NFS为RPC Server的一种。事实上,有很多这样的服务器都向RPC注册。例如,NIS(Network Information Service)也是RPC Server的一种。所以如图7-3所示,不论是客户端还是服务器端,要使用NFS都需要启动RPC。

NFS工作原理简图

NFS服务搭建

说明:启动NFS服务端的时候,首先要启动portmap服务,因为启动NFS服务时要向portmap服务注册端口


NFS服务端的安装

NFS软件

要部署NFS服务必须安装至少下面的两个软件包

1nfs-utils:NFS主程序

2portmap:RPC主程序

[[email protected]_Server ~]# rpm -qa |egrep "nfs|portmap"

nfs-utils-1.0.9-60.el5

portmap-4.0-65.2.2.1

nfs-utils-lib-1.0.8-7.9.el5

如上服务端机器已经安装

如果如上两个包没有安装可以使用

yum -y install  nfs-utils portmap


启动NFS服务端(在启动NFS服务前,一定要先启动portmap服务)

[[email protected]_Server ~]# /etc/init.d/portmap start

Starting portmap:                                          [  OK  ]

[[email protected]_Server ~]# /etc/init.d/nfs start

Starting NFS services:                                     [  OK  ]

Starting NFS quotas:                                       [  OK  ]

Starting NFS daemon:                                       [  OK  ]

Starting NFS mountd:                                       [  OK  ]

检查服务启动状态,(查看portmap111端口已经找开并处于监听状态)

[[email protected]_Server ~]# netstat -lnt

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State    

tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:706                 0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:681                 0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:649                 0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:35660               0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN    

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN    

[[email protected]_Server ~]# ps -ef |grep nfsd

root     28050    17  0 11:52 ?        00:00:00 [nfsd4]

root     28052     1  0 11:52 ?        00:00:00 [nfsd]

root     28053     1  0 11:52 ?        00:00:00 [nfsd]

root     28054     1  0 11:52 ?        00:00:00 [nfsd]

root     28055     1  0 11:52 ?        00:00:00 [nfsd]

root     28056     1  0 11:52 ?        00:00:00 [nfsd]

root     28057     1  0 11:52 ?        00:00:00 [nfsd]

root     28058     1  0 11:52 ?        00:00:00 [nfsd]

root     28059     1  0 11:52 ?        00:00:00 [nfsd]

root     28096  4207  0 11:56 pts/2    00:00:00 grep nfsd

启动NFS客户端(NFS客户端只需要开启portmap服务就可以了)

[ro[email protected]_Client_01 ~]# service portmap start

Starting portmap:                                          [  OK  ]

[[email protected]_Client_01 ~]# netstat -lnt

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State    

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN    

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN    

tcp        0      0 0.0.0.0:701                 0.0.0.0:*                   LISTEN


配置NFS服务端及客户端开机自启动

[[email protected]_Server ~]# chkconfig nfs off && chkconfig nfs on

[[email protected]_Server ~]# chkconfig --list |egrep nfs

nfs             0:off   1:off   2:on    3:on    4:on    5:on    6:off

nfslock         0:off   1:off   2:off   3:on    4:on    5:on    6:off

[[email protected]_Server ~]# chkconfig  portmap off  && chkconfig portmap on  

[[email protected]_Server ~]# chkconfig --list |egrep portmap

portmap         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[[email protected]_Client_01 ~]# chkconfig  portmap  off && chkconfig portmap on  

[[email protected]_Client_01 ~]# chkconfig --list |egrep portmap

portmap         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[[email protected]_Client_01 ~]#


配置NFS服务端(默认这个文件是空的)

[[email protected]_Server ~]# ll /etc/exports

-rw-r--r-- 1 root root 0 Jan 13  2000 /etc/exports

/etc/exports配置文件格式为

NFS共享目录 客户端地址1(参数1,参数2…..)客户端地址2(参数1,参数2…..)

其各项的参数含义如下:

1NFS共享目录:为NFS服务端要共享的实际目标绝对路径。(如/home/zhangjie

2、客户端地址:为服务器授权的可访问共享目录的客户端服务地址,可以为单独的ip地址或域名,也可以是整个网段地址,还可以使用*来匹配所有服务器可以访问

3、参数集:对授权的客户端的访问权限设置。

指定NFS客户端地址的配置祥细说明

客户端地址

具体例子

说明

授权单一客户端访问NFS服务

10.0.0.10

一般生产环境这种配置不多

授权整个网段可以访问NFS服务

10.0.0.0/24

其中24等同于255.255.255.0 指定整个网段在生产环境下是最常见的

授权事个网段可以访问NFS服务

10.0.0.*

指定网段的另外一种写法

授权整个域名客户端可以访问NFS服务

www.abc.com

生产环境下不常用,因为不安全


生产环境下实例

生产环境下的实例

备注

常用格式说明

要共享的目录,客户端的ip地址或ip段(参数1 参数2...)


配置实例一

/home/zhangjie  10.0.0.0/24(rw,sync)

rw表示读写,sync表示数据同步写入到内存和磁盘

配置实例二

/home/tools 10.0.0.0/24(rw,rsync,all_squash,anouuid=2000,anougid=2000 )

生产环境常用配置,适合多客户端共享一个NFS目录

配置实例三

/home/soft 10.0.0.0/24(ro)

只读共享


说明:all_squash,anouuid=2000,anougid=2000 表示客户端无论以什么身份过来访问NFS服务,都给默认


NFS参数说明

参数值

内容说明

rw

该目录共享的权限是可读写 (read-write) 或只读 (read-only),但最终能不能读写,还是与文件系統的 rwx 及身份有关。

ro

sync

sync 代表文件会同步写入到内存和磁盘中,async 则代表文件会先暂存到内在中,而非直接写入硬盘!

async

no_root_squash

客户端使用 NFS 文件系統的帐号若为 root 时,系统该如何判断这个帐号的身份?默认的情況下,客户端 root 的身份会由 root_squash 的设置压缩成 nfsnobody, 这样会对此服器的系統会较有保障。但如果你想要开放客户端使用 root 身份来操作NFS的文件系统,那么这里就得要开 no_root_squash 才行!

root_squash

all_squash

不论登入 NFS 的使用者身份如何, 他的身份都会被压缩成为匿名用户,通常也就是 nobody(nfsnobody) 啦!

anonuid

anon 意指 anonymous (匿名用户) 前面关于 *_squash 提到的匿名使用者的 UID 设定值,通常为 nobody(nfsnobody),但是你可以自行设定这个 UID 的值!当然,这个 UID 必需要存在于你的 /etc/passwd 当中! anonuid 指的是 UID 而 anongid 则是組的 GID 哦。

anongid


NFS配置实例

1、创建一个NFS共享目录

[[email protected]_Server ~]# mkdir /video

[[email protected]_Server ~]# ll /video -d

drwxr-xr-x 2 root root 4096 Jul 11 13:29 /video

2、编辑NFS服务端的配置文件 /etc/exports

[[email protected]_Server ~]# vim /etc/exports

[[email protected]_Server ~]# cat /etc/exports

/video 192.168.17.0/24(rw,sync)

[[email protected]_Server ~]# /etc/init.d/nfs reload

从客户端mount  NFS的共享目录192.168.17.167这个地址是NFS服务端的ip地址

[[email protected]_Client_01 ~]# showmount -e 192.168.17.167

Export list for 192.168.17.167:

/video 192.168.17.0/24

[[email protected]_Client_01 ~]# mount -t nfs 192.168.17.167:/video /mnt

[[email protected]_Client_01 ~]# df -hT

Filesystem    Type    Size  Used Avail Use% Mounted on

/dev/sda3     ext3     19G  2.1G   16G  12% /

/dev/sda1     ext3     99M   12M   82M  13% /boot

tmpfs        tmpfs    500M     0  500M   0% /dev/shm

192.168.17.167:/video

              nfs     19G  2.1G   16G  12% /mnt

NFS服务端共享目录写文件验证

[[email protected]_Server ~]# cd /video/

[[email protected]_Server video]# touch `seq 10`

[[email protected]_Server video]# ls

1  10  2  3  4  5  6  7  8  9

查看客户端,发现已经同步过来了

[[email protected]_Client_01 ~]# cd /mnt/

[[email protected]_Client_01 mnt]# ls

1  10  2  3  4  5  6  7  8  9

从客户端往NFS服务端上写文件,这里发现写不了,是因为虽然NFS服务端允许客户端写入的权限,但是NFS服务端本地不允许写也是不行滴。

[[email protected]_Client_01 mnt]# touch test1 test2 test3

touch: cannot touch `test1': Permission denied

touch: cannot touch `test2': Permission denied

touch: cannot touch `test3': Permission denied

NFS服务端给本地的共享目录/video目录权限,这里由于测试就给777权限

[[email protected]_Server ~]# ll -d /video/

drwxr-xr-x 2 root root 4096 Jul 11 13:39 /video/

[[email protected]_Server ~]# chmod 777 /video/

[[email protected]_Server ~]# ll -d /video/  

drwxrwxrwx 2 root root 4096 Jul 11 13:39 /video/

再次从客户端往NFS挂载目录写文件

[[email protected]_Client_01 mnt]# touch test1 test2 test3

[[email protected]_Client_01 mnt]# ls

1  10  2  3  4  5  6  7  8  9  test1  test2  test3

查看NFS服务端情况

[[email protected]_Server ~]# cd /video/

[[email protected]_Server video]# ls

1  10  2  3  4  5  6  7  8  9  test1  test2  test3


查看NFS服务端的所有配置

[[email protected]_Server video]# cat /var/lib/nfs/etab

/video 192.168.17.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)

查看哪些客户端访问过我的NFS服务端

[[email protected]_Server video]# cat /var/lib/nfs/rmtab

192.168.17.169:/video:0x00000001


客户端开机启动挂载NFS共享目录

1、修改/etc/fstab

[[email protected]_Client_01 ~]# echo "192.168.17.167:/video   /mnt                    nfs     defaults        0 0">>/etc/fstab

[[email protected]_Client_01 ~]# tail -1 /etc/fstab    

192.168.17.167:/video   /mnt                    nfs     defaults        0 0

2、加到/etc/rc.local

[[email protected]_Client_01 ~]# echo "mount -t nfs  192.168.17.167:/video /mnt">>/etc/rc.local

[[email protected]_Client_01 ~]# tail -1 /etc/rc.local

mount -t nfs  192.168.17.167:/video /mnt

卸载NFS挂载目录

umount /mnt

强制卸载NFS挂载目录

umount –lf /mnt