首页 > 其他 > 详细

NFS服务器搭建与autofs自动挂载

时间:2019-11-15 11:11:00      阅读:86      评论:0      收藏:0      [点我收藏+]

环境:

hostname ip OS
server 192.168.220.138 centos7
client 192.168.220.139 centos7
一、NFS概述
NFS(Network File System)网络文件系统。最大功能就是通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。网络文件系统(Network File System,NFS) 是 Linux 系统支持的一种网络服务,通过 NFS ,网络中的计算机可以发布共享信息,从而可使远程客户像使用本地文件一样访问该共享资源,如果想使用远程计算机上的文件,只要用 mount 命令将远程的目录挂载在本地文件系统下,就可像使用本地文件一样使用相关资源
二、端口
[root@server ~]# vim /etc/services
374 nfs 2049/tcp nfsd shilp # Network File System
375 nfs 2049/udp nfsd shilp # Network File System
376 nfs 2049/sctp nfsd shilp # Network File System
三、安装
[root@server ~]# yum install nfs-utils -y
[root@server ~]# systemctl start nfs
[root@server ~]# netstat -antup | grep 2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp6 0 0 :::2049 :::* LISTEN -
udp 0 0 0.0.0.0:2049 0.0.0.0:* -
udp6 0 0 :::2049 :::* -

四、/etc/exports配置文件的语法和参数
[root@server ~]# vim /etc/exports
/gx *(rw)
[共享目录] [主机(权限)] [可用主机名表示] [可用通配符表示]
[root@server ~]# mkdir /gx
[root@server ~]# chmod 777 /gx
[root@server ~]# systemctl restart nfs
===============================client===================================
[root@client ~]# showmount -e 192.168.220.138 #showmount 可以查看NFS共享出来的资源目录
Export list for 192.168.220.138:
/gx *
[root@client ~]# mount 192.168.220.138:/gx /opt/
[root@client ~]# mount | grep gx
192.168.220.138:/gx on /opt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.220.139,local_lock=none,addr=192.168.220.138)
[root@client opt]# touch 1.txt
[root@client opt]# ll
总用量 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 7月 22 10:18 1.txt
这里用户为nfsnobody,可以查看以下。不论登陆NFS的用户身份为何,他的身份都会被压缩成匿名用户。也就是nfsnobody.
[root@server ~]# cat /etc/passwd |grep nfsnobody
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
权限参数
参数值 说明
rw
ro 该目录共享的权限是可读写(read-write)或只读(read-only),但最终能不能读写,还是与文件系统的rwx及身份相关
sync
async sync表示数据会同步写入到内存与硬盘中,async则代表数据会先暂存于内存当中,而非直接写入硬盘。
no_root_squash
root_squash 客户端使用NFS系统为root时
root_squash会将身份压缩成nfsnobody
no_root_squash可以开放客户端root身份
all_squash 不论登陆NFS用户为何身份,都会被压缩成nfsnobody
anonuid
anongid anon指(anonymous)匿名用户。匿名用户通常为nfsnobody,我们也可以自己设置uid,gid。但这个UID必须存在于/etc/passwd当中。
五、权限测试
=====================server============================
[root@server ~]# mkdir -p /gx/{all_squash,no_root_squash}
[root@server ~]# chmod 777 -R /gx/
[root@server ~]# cat /etc/exports
/gx/no_root_squash *(rw,no_root_squash)
/gx/all_squash 192.168.220.0/24(rw,all_squash,anonuid=500,anongid=500)
[root@server ~]# systemctl restart nfs
=====================client============================
[root@client ~]# showmount -e 192.168.220.138
showmount -e 192.168.220.138
Export list for 192.168.220.138:
/gx/no_root_squash *
/gx/all_squash 192.168.220.0/24

no_root_squash
[root@client ~]# mkdir no_root_suqash
[root@client ~]# mount 192.168.220.138:/gx/no_root_squash no_root_suqash/
[root@client ~]# cd no_root_suqash/
[root@client no_root_suqash]# touch 1.txt
[root@client no_root_suqash]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 22 11:16 1.txt
没有压制root用户身份。
all_squash(rw,all_squash,anonuid=500,anongid=500)
[root@client ~]# mkdir all_squash
[root@client ~]# mount 192.168.220.138:/gx/all_squash all_squash/
[root@client ~]# groupadd -g 500 squash
[root@client ~]# useradd squash -u 500 -g 500
[root@client ~]# id squash
uid=500(squash) gid=500(squash) 组=500(squash)
[root@client ~]# touch all_squash/gx.txt
[root@client ~]# ll all_squash/gx.txt
-rw-r--r-- 1 squash squash 0 7月 22 11:26 all_squash/gx.txt
六、NFS连接查看
NFS关于目录权限设置数据很多。可以在/var/lib/nfs/etab中查看。
[root@server ~]# cat /var/lib/nfs/etab
/gx/all_squash 192.168.220.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=500,anongid=500,sec=sys,rw,secure,root_squash,all_squash)
/gx/no_root_squash *(rw,sync,wdelay,hide,nocrossmnt,secure,no_root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,no_root_squash,no_all_squash)
如果有客户端挂载了你的NFS文件系统,这些信息就会被记录在/var/lib/nfs/etab 当中。
七、exportfs
当设置完/etc/exports后可以不重新启动NFS。通过exportfs来完成。
[root@server ~]# exportfs -h
usage: exportfs [-adfhioruvs] [host:/path]
选项 (OPTIONS)
-a 打开或取消所有目录共享。

-r 重新共享所有目录。它使 /var/lib/nfs/xtab 和 /etc/exports 同步。
它将 /etc/exports 中已删除的条目从 /var/lib/nfs/xtab
中删除,将内核共享表中任何不再有效的条目移除。

-u 取消一个或多个目录的共享。

-v 输出详细信息。当共享或者取消共享时,显示在做什么。
显示当前共享列表的时候,同时显示共享的选项。
1.重新挂载/etc/exports的设置
[root@server ~]# exportfs -arv
exporting 192.168.220.0/24:/gx/all_squash
exporting *:/gx/no_root_squash

2.将共享的NFS目录资源,全部卸载
[root@server ~]# exportfs -auv
[root@client ~]# showmount -e 192.168.220.138
Export list for 192.168.220.138:
这是客户端看不到任何资源了。
八、autofs
autofs 这个服务会持续地检测某个指定目录,并预先设置当使用到该目录下的某个子目录树,将会取得来自服务端的NFS文件系统资源,并进行自动挂载。

主配置文件/etc/auto.master
[root@client ~]# vim /etc/auto.master
/opt/gx /etc/auto.nfs
挂载目录/opt/gx,此目录不需要事先存在,autofs会自动建立。/etc/auto.nfs为挂载参数
建立挂载信息(/etc/auto.nfs)此文件不存在,自行配置。
[root@client ~]# cat /etc/auto.nfs
a -fstype=nfs 192.168.220.138:/opt/gx/nfs
[本地端子目录] [挂载参数] [服务器提供目录]
[root@client ~]# systemctl restart autofs
[root@client ~]# ls /opt/gx/ #可以注意到目录为空
[root@client ~]# cd /opt/gx/a #进入目录触发自动挂载
[root@client a]#

NFS服务器搭建与autofs自动挂载

原文:https://www.cnblogs.com/chenzijuan/p/11864837.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!