NFS网络文件系统构建详解

2023年 7月 16日 22.0k 0

从某种意义上来讲,NFS和windows映射网络驱动器很相似

NFS:NetWork File System
传统意义上:文件系统在内核中实现

远程过程调用,函数调用(远程主机上的函数)
一部分功能由本地程序完成,另一部分功能由远程主机的函数完成

Remote Procedure Call Protocol
半结构数据:
XML:eXtended Mark Lanuage
jsqn
http

NIS:network INformation system
身份认证:集中某服务器其完成身份认证!

NFS:基于IP认证
NFS.tcp/udp端口:2049

 

rpc服务
rpcinfo -p
rpcinfo -p "server-ip"
要想启动NFS,首先查看portmapper在工作状态

 

查看NFS服务器端共享的文件系统:
showmount -e NFSSERVER_IP

 

挂载NFS文件系统:
mount -t nfs SERVER:/path/to/sharedfs /path/to/mount_point

 

/etc/exports:
文件系统 客户端(选项) 客户端(选项)

客户端:IP、FQDN或DOMAIN、NETWORK

exportfs:维护exports文件导出的文件系统表的专用工具:
export -ar: 重新导出所有的文件系统
export -au: 关闭导出的所有文件系统
export -u FS: 关闭指定的导出的文件系统
nfs服务器:nfsd.mo
# yum -y install nfs-utils

rpm -ql nfs-utils
/sbin/mount.nfs
/sbin/mount.nfs4
/sbin/rpc.statd
第一次启动报错!
[root@localhost ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).
[FAILED]
Starting NFS mountd: [FAILED]
Starting NFS daemon:

而后依次启动守护进程,问题解决

[root@localhost ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
[root@localhost ~]# /etc/init.d/rpcidmapd start
Starting RPC idmapd: [ OK ]
[root@localhost ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: 磁盘配额相关 [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
[root@localhost ~]#

可以使用rpcinfo -p查看监听的端口,
mountd端口为随机端口

如何配置NFS:
只需编辑/etc/exports文件即可

补充材料:

/etc/exports 文件中的项的格式相当简单。要共享一个文件系统,只需要编辑 /etc/exports 并使用下面的格式给出这个文件系统(和选项)即可:
directory (or file system) client1(option1, option2) client2(option1, option2)
例如:
1,我们尝试创建一个目录,将它发布出去
[root@localhost ~]# mkdir /shared/nfs -pv
mkdir: created directory `/shared'
mkdir: created directory `/shared/nfs'
2,我们授权用户IP访问
vim /etc/expprts
/shared/nfs 172.16.249.205(rw) 172.16.250.167(ro),

3,重启
[root@localhost shared]# service nfs restart

我们通过showmount -e NFS_SERVER_IP查看共享是否成功,当然,远程主机也可以查看的,but,需要安装showmount
[root@localhost shared]# showmount -e 172.16.249.249
Export list for 172.16.249.249:
/shared/nfs 172.16.250.167,172.16.249.205

4,而后在挂载NFS文件系统。
例如:将NFS挂载至172.16.249.202/mnt目录下
PS:如果你挂载本地,请在配置文件中定义本地iP和权限
[root@localhost ~]# ssh root@172.16.249.205
The authenticity of host '172.16.249.205 (172.16.249.205)' can't be established.
RSA key fingerprint is 81:80:c0:0a:61:21:77:cb:4d:ef:c8:98:a0:71:4f:94.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.249.205' (RSA) to the list of known hosts.
root@172.16.249.205's password:
Last login: Wed Apr 22 20:27:33 2015
[root@www ~]#mount -t nfs 172.16.249.249:/shared/nfs /mnt
[root@www mnt]#mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
172.16.249.249:/shared/nfs on /mnt type nfs (rw,addr=172.16.249.249)
[root@www mnt]#

这里的挂载虽然给了写权限,其实是以nfsnobody运行的,是不具备写权限的

如果需要远程用户具备写权限:
1,创建用户fedora:useradd -u 600 fedora
2,setfacl权限控制:setfacl -m u:gentoo:rwx /shared/nfs/
3,su gentoo:可以touch ,mkdir等操作

PS:如果远程主机上有某用户名的id号与gentoo一致,那么这个相同id号的用户便可以对此NFS文件具备gentoo权限。因此NFS认证用户名是依赖ID号来识别用户,因此当远程用户查看NFS属组属主时看到的属性一致为nobody

用户映射的选项包括:
root_squash: 这个选项不允许 root 用户访问挂载上来的 NFS 卷。
no_root_squash: 这个选项允许 root 用户访问挂载上来的 NFS 卷。
all_squash: 这个选项对于公共访问的 NFS卷来说非常有用,它会限制所有的 UID 和 GID,只使用匿名用户。缺省设置是 no_all_squash。
anonuid 和 anongid: 这两个选项将匿名 UID 和 GID 修改成特定用户和组帐号。

当我们修改完配置文件后必须重启才能重读配置文件。使用exportfs可以沉默读取,相当于reload
1,我们在vim /etc/exportfs添加/mnt,允许172.16.0.0所有主机可访问

/shared/nfs 172.16.249.205(rw) 172.16.250.167(ro) 172.16.249.249(rw)
/mnt/ 172.16.0.0(rw)

2,使用showmount -e 命令查看

[root@localhost ~]# showmount -e 172.16.249.249
Export list for 172.16.249.249:
/shared/nfs 172.16.249.249,172.16.250.167,172.16.249.205

3,使用exportfs -ar导出配置 ,也可以叫做重读配置

[root@localhost ~]# exportfs -ar
exportfs: /mnt does not support NFS export

4,使用showmount -e 命令再次查看,已经出现了,静默重新导出完成

[root@localhost ~]# showmount -e 172.16.249.249
Export list for 172.16.249.249:
/mnt 172.16.0.0
/shared/nfs 172.16.249.249,172.16.250.167,172.16.249.205
[root@localhost ~]#

 

NFS开机挂载:客户端
写入 /etc/fstab
172.16.249.249:/shared/nfs/ /mnt ext4 defaults 0 0

客户端挂载时可以使用的特殊选项:

Client
Mounting remote directories
Before mounting remote directories 2 daemons should be be started first:
rpcbind
rpc.statd

rsize 的值是从服务器读取的字节数。wsize 是写入到服务器的字节数。默认都是1024, 如果使用比较高的值,如8192,可以提高传输速度。

The timeo value is the amount of time, in tenths of a second, to wait before resending a transmission after an RPC timeout. After the first timeout, the timeout value is doubled for each retry for a maximum of 60 seconds or until a major timeout occurs. If connecting to a slow server or over a busy network, better performance can be achieved by increasing this timeout value.
The intr option allows signals to interrupt the file operation if a major timeout occurs for a hard-mounted share.
整个NFS的配置是相当简单的,明白其原理才能排错1

NFS起来之后,不但自身,而后mountd挂载守护进程,用户进程来了之后交给mound验证,用户客户端是否允许在用户客户端的访问列表中,如果在就允许,如果不在则不允许。

一次连接的过程!

2015-05-06_230309

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论