3 案例3:配置并访问NFS共享
3.1 问题
服务器利用NFS机制发布2个共享目录,要求如下:
将目录/root共享给192.168.2.100,客户机的root用户有权限写入
将/usr/src目录共享给192.168.2.0/24网段,只开放读取权限
- 1
- 2
从客户机访问NFS共享:
分别查询/挂载上述NFS共享目录
查看挂载点目录,并测试是否有写入权限
- 1
- 2
3.2 方案
使用2台RHEL7虚拟机,其中一台作为NFS共享服务器(192.168.2.5)、另外一台作为测试用的Linux客户机(192.168.2.100),如图-5所示。
图-5
NFS共享的配置文件:/etc/exports 。
配置记录格式:文件夹路径 客户地址1(控制参数… …) 客户地址2(… …) 。
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:配置NFS服务器,发布指定的共享
1)确认服务端程序、准备共享目录
软件包nfs-utils用来提供NFS共享服务及相关工具,而软件包rpcbind用来提供RPC协议的支持,这两个包在RHEL7系统中一般都是默认安装的:
[root@proxy ~]# rpm -q nfs-utils rpcbind
nfs-utils-1.3.0-0.48.el7.x86_64
rpcbind-0.2.0-42.el7.x86_64
- 1
- 2
- 3
根据本例的要求,需要作为NFS共享发布的有/root、/usr/src这两个目录:
[root@proxy ~]# ls -ld /root /usr/src/
dr-xr-x---. 35 root root 4096 1月 15 18:52 /root
drwxrwxr-x+ 4 root root 4096 1月 15 17:35 /usr/src/
- 1
- 2
- 3
2)修改/etc/exports文件,添加共享目录设置
默认情况下,来自NFS客户端的root用户会被降权,若要保留其root权限,注意应添加no_root_squash控制参数(没有该参数,默认root会被自动降级为普通账户);另外,限制只读的参数为ro、可读可写为rw,相关配置操作如下所示:
[root@proxy ~]# vim /etc/exports
/root 192.168.2.100(rw,no_root_squash)
/usr/src 192.168.2.0/24(ro)
- 1
- 2
- 3
3)启动NFS共享相关服务,确认共享列表
依次启动rpcbiind、nfs服务:
[root@proxy ~]# systemctl restart rpcbind ; systemctl enable rpcbind
[root@proxy ~]# systemctl restart nfs ; systemctl enable nfs
- 1
- 2
使用showmount命令查看本机发布的NFS共享列表:
[root@proxy ~]# showmount -e localhost
Export list for localhost:
/usr/src 192.168.2.0/24
/root 192.168.2.100
- 1
- 2
- 3
- 4
步骤二:从客户机访问NFS共享
1)启用NFS共享支持服务
客户机访问NFS共享也需要rpcbind服务的支持,需确保此服务已开启:
[root@web1 ~]# systemctl restart rpcbind ; systemctl enable rpcbind
- 1
2)查看服务器提供的NFS共享列表
[root@web1 ~]# showmount -e 192.168.2.5
Export list for 192.168.2.5:
/usr/src 192.168.2.0/24
/root 192.168.2.100
- 1
- 2
- 3
- 4
3)从客户机192.168.2.100访问两个NFS共享,并验证权限
将远程的NFS共享/root挂载到本地的/root5文件夹,并验证可读可写:
[root@web1 ~]# mkdir /root5 //建立挂载点
[root@web1 ~]# mount 192.168.2.5:/root /root5 //挂载NFS共享目录
[root@web1 ~]# df -hT /root5 //确认挂载结果
Filesystem Type Size Used Avail Use% Mounted on
192.168.2.5:/root nfs 50G 15G 33G 31% /root5
[root@web1 ~]# cd /root5 //切换到挂载点
[root@web1 root5]# echo "NFS Write Test" > test.txt //测试写入文件
[root@web1 root5]# cat test.txt //测试查看文件
NFS Write Test
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
将远程的NFS共享/usr/src挂载到本地的/mnt/nfsdir,并验证只读:
[root@web1 ~]# mkdir /mnt/nfsdir //建立挂载点
[root@web1 ~]# mount 192.168.2.5:/usr/src /mnt/nfsdir/ //挂载NFS共享目录
[root@web1 ~]# df -hT /mnt/nfsdir/ //确认挂载结果
Filesystem Type Size Used Avail Use% Mounted on
192.168.2.5:/usr/src nfs 50G 15G 33G 31% /mnt/nfsdir
[root@web1 ~]# cd /mnt/nfsdir/ //切换到挂载点
[root@web1 nfsdir]# ls //读取目录列表
debug install.log kernels test.txt
[root@web1 nfsdir]# echo "Write Test." > pc.txt //尝试写入文件失败
-bash: pc.txt: 只读文件系统
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
!!!! 如果从未授权的客户机访问NFS共享,将会被拒绝。比如从NFS服务器本机尝试访问自己发布的/root共享(只允许192.168.2.100访问),结果如下所示:
[root@proxy ~]# mkdir /root5
[root@proxy ~]# mount 192.168.2.5:/root /root5
mount.nfs: access denied by server while mounting 192.168.2.5:/root
- 1
- 2
- 3
4)设置永久挂载
[root@web1 ~]# vim /etc/fstab
.. ..
192.168.2.5:/usr/src nfsdir nfs default,ro 0 0
192.168.2.5:/root root5 nfs default 0 0
- 1
- 2
- 3
- 4
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">1</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了355 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>