NFS(NetworkFile System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样
1. NFS服务端所需的软件列表:
nfs-utils: 这个是NFS服务主程序(包含rpc.nfsd、rpc.mountd、daemons)
rpcbind: 这个是CentOS6.X的RPC主程序(CentOS5.X的为portmap)
2. 检查是否安装软件
rpm -qa nfs-utils rpcbind #检查安装的软件包
如果没有
yum install nfs-utils rpcbind -y
3. 启动软件
/etc/init.d/nfs start
/etc/init.d/rpcbind start
4. 检查启动结果
/etc/init.d/nfs status
/etc/init.d/rpcbind status
ps –ef | grep nfs | grep –v grep #效果同上
ps -ef | grep rpcbind | grep -v grep #效果同上
netstat –ntulp | grep rpcbind #效果同上
lsof –i:111 #rpc的主端口ID为111 #效果同上
5. 检查并添加开机自启动
chkconfig | grep –E “rpcbind|nfs”
chkconfig /etc/init.d/nfs on
chkconfig /etc/init.d/rpcbind on
6. 查看rpc启动的服务
rpcinfo -p localhost
在NFS服务过程中,必须先启动rpcbind,再启动nfs,这样才能让NFS在rpcbind上注册成功.
7. 检查nfs和rpcbind自启动顺序
less /etc/init.d/rpcbind #第5行
less /etc/init.d/nfs #第5行
<由上面可以看出系统默认会让rpcbind服务先启动,再启动nfs服务,但是在实际生产环境中,我们最好不要用chkconfig来控制服务的开机自启动,我们生产环境中我们一般用rc.local来管理。主要是为了方便以后查阅哪些服务开机自己,并且能控制先后顺序>
echo "/etc/init.d/nfs start" >> /etc/rc.local
echo "/etc/init.d/rpcbind start" >> /etc/rc.local
8. 创建共享目录并设置权限
先创建/data
mkdir /data
改变属主和属组来控制权限
chown -R nfsnobody.nfsnobody /data
9. 配置NFS服务端
/etc/exports 是NFS程序的配置文件。并且默认为空
vim /etc/exports
写入:
/data 192.168.1.0/24(rw,no_root_squash,no_all_squash,sync)
/data 192.168.1.0/24(rw,sync) #最简易配置 效果同上
授权给192.168.1.0/24这一个网段来访问 /data
常见的参数则有:
参数值 内容说明
rw ro 该目录分享的权限是可擦写(read-write) 或只读 (read-only),但最终能不能读写,还是与文件系统的 rwx 及身份有关。
sync async sync 代表数据会同步写入到内存与硬盘中,async 则代表数据会先暂存于内存当中,而非直接写入硬盘!
no_root_squash root_squash 客户端使用 NFS 文件系统的账号若为 root 时,系统该如何判断这个账号的身份?预设的情况下,客户端 root 的身份会由 root_squash 的设定压缩成 nfsnobody, 如此对服务器的系统会较有保障。但如果你想要开放客户端使用root 身份来操作服务器的文件系统,那么这里就得要开 no_root_squash 才行!
all_squash 不论登入 NFS 的使用者身份为何, 他的身份都会被压缩成为匿名用户,通常也就是 nobody(nfsnobody)!
anonuid anongid anon 意指 anonymous (匿名者) 前面关于 *_squash 提到的匿名用户的 UID 设定值,通常为 nobody(nfsnobody),但是你可以自行设定这个 UID 的值!当然,这个 UID 必需要存在于你的 /etc/passwd 当中! anonuid 指的是 UID 而anongid 则是群组的 GID。
10. 配置完成exports后平滑重启NFS服务
/etc/init.d/nfs reload #2条命令效果相同
exportfs –rv # 2条命令效果相同
11. 测试联机功能
showmount -e localhost
12. 客户端配置
安装软件
yum install nfs-utils rpcbind –y
13. 启动软件
/etc/init.d/nfs start
/etc/init.d/rpcbind start #如果需要,可以使rpcbind开机自启动。
chkconfig /etc/init.d/rpcbind on #可选
14. 建立挂载目录
mkdir /xxw
15. 测试挂载
showmount -e 172.16.1.5
16. 挂载到建立的目录上
mount -t nfs 172.16.1.31:/data /xxw #最简易设置
mount -t nfs 172.16.1.8:/data /xxw -o proto=tcp -o nolock
#为了提高NFS的稳定性,使用TCP协议挂载,NFS默认用UDP协议
17. 查看挂载情况
df –h
将挂载放入自启动
echo "mount -t nfs172.16.1.31:/data /xxw">> /etc/rc.local
卸载已挂在的NFS
umount /xxw