阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

基于CentOS 7实现的NFS

152次阅读
没有评论

共计 3262 个字符,预计需要花费 9 分钟才能阅读完成。

NFS

NFS(Network FileSystem,网络文件系统),最早由 Sun 公司所发展出来的,主要是通过网络让不同的主机、不同的操作系统,可以彼此分享个别档案,因此我们也可以简单把 NFS 看成是一个文件服务器。通过 NFS 我们的 PC 可以将网络端的 NFS 服务器分享的目录挂载到本地端的机器当中,在本地端的机器看起来,远程主机的目录就好像是自己的一个磁盘分区一样。

基于 CentOS 7 实现的 NFS

NFS 服务器与客户端通过随机选择小于 1024 以下的端口来进行数据传输,而这个端口的确定需要借助 RPC(Remote Procedure Call, 远程过程调用)协议的协助。RPC 最主要的功能就是在指定每个 NFS 服务所对应的 port number,并且回报给客户端,让客户端可以连结到正确的端口上去。当我们启动 NFS 服务时会随机取用数个端口,并主动向 RPC 注册,因此 RPC 可以知道每个端口对应的 NFS,而 RPC 又是固定使用 port 111 监听客户端的需求并回报客户端正确的端口。

基于 CentOS 7 实现的 NFS

1. 客户端向服务器端的 RPC(port 111)发出 NFS 的请求;
          2. 服务器端找到对应的已注册的 NFS daemon 端口后,回报给客户端;
   3. 客户端知道正确的端口后,直接与 NFS daemon 联机。

实现 NFS

实验环境

[root@nfs ~]# lsb_release -r
Release:    7.2.1511
[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# rpm -qi nfs-utils
Name        : nfs-utils
Epoch      : 1
Version    : 1.3.0
Release    : 0.54.el7
Architecture: x86_64
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# setenforce 0
[root@nfs ~]# mkdir /nfs_dir
[root@nfs ~]# vim /nfs_dir/welcome
This is NFS test file.

NFS 相关配置 

相关配置文件:
配置文件:/etc/exports
配置文件目录:/etc/exports.d

NFS 服务的主配置文件为 /etc/exports,用于定义共享的目录以及客户端的权限,格式如下:
/PATH/TO/SOME_DIR clients1(export_options,…) clients2(export_options,…)
其中 clients 支持以下几种格式:
single host:ipv4,ipv6,FQDN
network:address/netmask
wildcards:主机名通配,例如,*.magedu.com
netgroups:NIS 域内的主机组,@group_name
anonymous:使用 * 通配所有主机
export_options 的常见参数可以分为以下两类:
General Options:
    ro:客户端挂载后,其权限为只读,默认选项;
    rw: 读写权限;
    sync:同时将数据写入到内存与硬盘中;
    async:异步,优先将数据保存到内存,然后再写入硬盘;
    Secure:要求请求源的端口小于 1024
User ID Mapping:
    root_squash: 当 NFS 客户端使用 root 用户访问时,映射到 NFS 服务器的匿名用户;
    no_root_squash: 当 NFS 客户端使用 root 用户访问时,映射到 NFS 服务器的 root 用户;
    all_squash: 全部用户都映射为服务器端的匿名用户;
    anonuid=UID:将客户端登录用户映射为此处指定的用户 uid;
    anongid=GID:将客户端登录用户映射为此处指定的用户 gid
更多参数信息可以通过命令 man exports 查看帮助手册

配置 NFS

[root@nfs ~]# vim /etc/exports
/nfs_dir 192.168.4.*(rw,sync,root_squash)
[root@nfs ~]# systemctl start nfs-server

客户端测试

# 查看 nfs 能挂载的选项
[root@client ~]# showmount -e 192.168.4.119
Export list for 192.168.4.119:
/nfs_dir 192.168.4.*
[root@client ~]# mount -t nfs 192.168.4.119:/nfs_dir /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
welcome
[root@client mnt]# cat welcome
This is NFS test file.
[root@client mnt]# touch file
touch: cannot touch‘file’: Permission denied

可以看到无法在共享目录下创建文件,明明已经给分配了 rw 权限,这是因为 root_squash 把我们的访问权限压缩为 nobody 权限,自然无法对该目录进行写入操作。
对 NFS 的配置文件重新进行修改:

[root@nfs ~]# vim /etc/exports
/nfs_dir 192.168.4.*(rw,sync,no_root_squash)
# 使用 exportfs 重读 NFS 配置,不需要重启服务
[root@nfs ~]# exportfs -rv
exporting 192.168.4.*:/nfs_dir

客户端重新测试:

[root@client mnt]# touch file
[root@client mnt]# ll
total 4
-rw-r–r–. 1 root root  0 Jun 28 14:11 file  #可以看到属主属组为 root
-rw-r–r–. 1 root root 23 Jun 28 11:08 welcome
[root@client mnt]# echo 123 > file
[root@client mnt]# rm file
rm: remove regular empty file‘file’? y  #可以删除文件
[root@client mnt]# ll
total 4
-rw-r–r–. 1 root root 23 Jun 28 11:08 welcome

很明显,将客户端访问共享文件用户映射为 NFS 服务器上的 root 是一种不安全的做法,我们可以指定客户端映射到 NFS 服务器的用户,配置如下:

[root@nfs ~]# useradd nfSUSEr -s /sbin/nologin
[root@nfs ~]# id nfsuser
uid=1003(nfsuser) gid=1003(nfsuser) groups=1003(nfsuser)
[root@nfs ~]# chown -R nfsuser:nfsuser /nfs_dir/
[root@nfs ~]# vim /etc/exports
[root@nfs ~]# cat /etc/exports
/nfs_dir 192.168.4.*(rw,sync,all_squash,anonuid=1003,anongid=1003)
[root@nfs ~]# exportfs -rv
exporting 192.168.4.*:/nfs_dir

客户端进行测试:

[root@client mnt]# touch file
[root@client mnt]# ll
total 4
-rw-r–r–. 1 1003 1003  0 Jun 28 14:27 file
-rw-r–r–. 1 1003 1003 23 Jun 28 11:08 welcome
[root@client mnt]# echo 123 > file
[root@client mnt]# cat welcome
This is NFS test file.
[root@client mnt]# rm file
rm: remove regular file‘file’? y
[root@client mnt]# ll
total 4
-rw-r–r–. 1 1003 1003 23 Jun 28 11:08 welcome

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3262字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中