Chinaunix首页 | 论坛 | 博客
  • 博客访问: 572915
  • 博文数量: 151
  • 博客积分: 3330
  • 博客等级: 中校
  • 技术积分: 1686
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 02:41
文章存档

2011年(151)

分类: LINUX

2011-05-02 20:04:29

网络文件系统(Network File System,NFS ),是在 Unix 系统间实现磁盘文件共享的一种方法,它支持应用程序在客户端通过网络访问位于服务器磁盘中数据的一种文件系统协议。

NFS 的基本原则是“容许不同的客户端及服务端通过一组 RPCs 分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。

远程过程调用(Remote Procedure Call,RPC)是一个计算机通信协议。该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程。如果涉及的软件采用面向对象编程,那么远程过程调用亦可称作远程调用或远程方法调用。

远程过程调用是一个的(Client/Server)的例子,它简单而又广受欢迎。远程过程调用总是由对发出一个执行若干过程请求,并用客户端提供的参数。执行结果将返回给客户端。



NFS 提供了以下的服务:

    在目录(directory)中查找文件
    列出目录中的文件
    管理目录
    取得各文件的属性(file attribute)
    文件的读/写

NFS是如何工作的

服务器必须运行以下服务:

服务        描述
nfsd        NFS,为来自NFS客户端的 请求服务。
mountd      NFS挂载服务,处理nfsd(8)递交过来的请求。
rpcbind     此服务允许 NFS 客户程序查询正在被 NFS 服务使用的端口。

NFS 依赖portmapper 服务,called portmap or rpc.portmap,因此启动NFS之前需要先启动portmap服务.

NFS serving is taken care of by five daemons: rpc.nfsd, which does most of the work; rpc.lockd and rpc.statd, which handle file locking; rpc.mountd, which handles the initial mount requests, and rpc.rquotad, which handles user file quotas on exported volumes. Starting with 2.2.18, lockd is called by nfsd upon demand, so you do not need to worry about starting it yourself. statd will need to be started separately. Most recent Linux distributions will have startup scripts for these daemons.

NFS服务所需的软件包:
portmap
nfs-utils
nfs-utils-lib
setup

NFS服务的配置文件:
/etc/exports,
/etc/hosts.allow,
/etc/hosts.deny

主要配置文为exports,设置共享的,allow,deny用于安全性设置

/etc/exports 配置文件格式:

directory machine1(option11,option12)
           machine2(option21,option22)

option:

ro: The directory is shared read only; the client machine will not be able to write it. This is the default.

rw: The client machine will have read and write access to the directory.

no_root_squash: By default, any file request made by user root on the client machine is treated as if it is made by user nobody on the server. (Exactly which UID the request is mapped to depends on the UID of user "nobody" on the server, not the client.) If no_root_squash is selected, then root on the client machine will have the same level of access to the files on the system as root on the server. This can have serious security implications, although it may be necessary if you want to perform any administrative work on the client machine that involves the exported directories. You should not specify this option without a good reason.

no_subtree_check: If only part of a volume is exported, a routine called subtree checking verifies that a file that is requested from the client is in the appropriate part of the volume. If the entire volume is exported, disabling this check will speed up transfers.

sync: By default, all but the most recent version (version 1.11) of the exportfs command will use async behavior, telling a client machine that a file write is complete - that is, has been written to stable storage - when NFS has finished handing the write over to the filesystem. This behavior may cause data corruption if the server reboots, and the sync option prevents this. See Section 5, “Optimizing NFS Performance” for a complete discussion of sync and async behavior.

用户映射选项

all_squash 将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);
no_all_squash 与all_squash取反(默认设置);
root_squash 将root用户及所属组都映射为匿名用户或用户组(默认设置);
no_root_squash 与rootsquash取反;
anonuid=xxx 将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
anongid=xxx 将远程访问的所有用户组都映射为匿名用 户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

其它选项

secure 限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
insecure 允许客户端从大于1024的tcp/ip端口连接服务器;
sync 将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
async 将数据先保存在内存缓冲区中,必要时才写入磁盘;
wdelay 检查是否有相关的写操作,如果有则将这些写操作 一起执行,这样可以提高效率(默认设置);
no_wdelay 若有写操作则立即执行,应与sync配合使用;
subtree 若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
no_subtree 即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;


客户端指定方式:

指定ip地址的主机 192.168.1.1
指定子网中的所有主机 192.168.1.0/24
指定域名的主机 client0.cia.com
指定域中的所有主机 *.cia.com
所有主机 *

举例:
vim /etc/export
/usr/local   192.168.1.1(ro) 192.168.1.2(ro)
/home        192.168.1.1(rw) 192.168.1.2(rw)
/usr/local 192.168.0.0/255.255.255.0(ro)
/home      192.168.0.0/255.255.255.0(rw)

每当设置了新的共享目录,使用exportfs命令刷新即可

-a 输出在/etc/exports文件中所定义的所有目录;
-r 重新读取/etc/exports文件,不需要重起服务;
-u 停止输出某一目录;
-v 在屏幕上显示过程;

重新输出NFS目录:
exportfs -rv

查看NFS服务输出的共享目录
showmount -e hostname/ip

查看客户端连接信息
showmount -a hostname/ip

停止输出所有共享目录
exportfs -auv

启动或停止NFS服务器
先启动portmap ,后启动nfs,否则nfs无法向portmap注册动态端口号


service portmap start|stop|restart

sevice nfs start|stop|restart

查看RPC 信息
rpcinfo -p Hostname

客户端如何挂载nfs server 共享的目录
mount -t nfs nfserverip/hostname:nfsdirectory localdir

挂在后通过df 查看下是否挂在上了

客户端卸载NFS 挂载的目录
umount /dir


NFS时在防火墙上要开放的端口

1. portmap 端口 111 udp/tcp;
2. nfsd 端口 2049 udp/tcp;
3. mountd 端口 "xxx" udp/tcp
系统 RPC服务在 nfs服务启动时默认会为 mountd动态选取一个随机端口(32768--65535)来进行通讯,我们可以通过编

辑/etc/services 文件为 mountd指定一个固定端口:
# vi /etc/services
在末尾添加
mountd 1011/udp
mountd 1011/tcp
保存该文件
# stopsrc -s rpc.mountd
# startsrc -s rpc.mountd
# exportfs -a
# rpcinfo -p Hostname
现在我们会发现 mountd已经和端口 1011绑定了。

nfs中需要通讯的服务还有 rpc.lockd和 rpc.statd,其中对lockd我们可以用类似的方法来指定固定端口,
# vi /etc/services
添加
lockd 35000/ucp
lockd 35000/tdp
# stopsrc -s rpc.lockd
# startsrc -s rpc.lockd
# exportfs -a

nfs客户端mount文件系统
# rpcinfo -p Hostname但 rpc.statd无法通过该方法来指定端口,它只能使用随机端口。

在防火墙上要根据下面命令的显示来开放随机端口:

# no -a |grep ephemeral
tcp_ephemeral_high = 65535
tcpp_ephemeral_low = 32768
udp_ephemeral_high = 65535
udp_ephemeral_low = 32768

当然也可以不开放 rpc.statd需要的随机端口,带来的影响就是如果 nfs连接中断(比如server或client宕掉了),系统将无法通过statd来恢复连接中断前的状态,而必须重新 mount该nfs文件系统。

阅读(723) | 评论(0) | 转发(0) |
0

上一篇:linux 命令大全

下一篇:进程间通信IPC

给主人留下些什么吧!~~