autofs自动挂载技术
自动挂载原理
autofs服务程序与mount命令不同之处在于它是一种守护进程,只有检测到用户试图访问一个尚未挂载的文件系统时才自动的检测并挂载该文件系统(nfs属于主动挂载,要使用mount进行硬挂载或者软挂载,autofs属于被动挂载,只有去访问目录才会被挂载)。
autofs非常方便,主要有两点:
1、设置不需要在开机就挂载的目录,当用的时候才实现自动挂载。
2、用户不使用自动挂载的目录一段的时间,会自动卸载。(默认时间为5分钟),可以在autofs.conf修改配置。
自动挂载配置(autofs客户端实现挂载远程服务器NFS服务共享文件)
在客户端安装autofs
[root@localhost ~]# yum install autofs -y
[root@localhost test]# systemctl enable autofs --开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@localhost cd]# cat /etc/autofs.conf --查看配置文件
timeout = 300 --用户不使用自动挂载的目录一段的时间,会自动卸载。(默认时间为5分钟)
编辑/etc/auto.master
[root@localhost cd]# vim /etc/auto.master --/mnt是要挂载的目录,auto.mis是对挂载目录的描述
/mnt /etc/auto.misc
在autofs服务程序的主配置文件要按照”挂载目录 子配置文件”格式进行填写,挂载目录是客户端设备挂载目录上一级目录。例如光盘设备一般挂载到本地的/mnt/test目录中,但是挂载目录写上一级的/mnt即可,当你cd到/mnt下再cd到test目录下,会将服务端的目录自动挂载到本地/mnt/test下,.misc是对该挂载目录的内的挂载信息做进一步说明的即对test这个目录。
/etc/auto.misc对/mnt/test目录进行描述
服务端nfs共享出去的信息
[root@localhost ~]# exportfs -v
/mnt/cdrom 192.168.179.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
客户端挂载nfs的配置
[root@localhost cd]# vi /etc/test.misc
test -fstype=nfs,ro,sync 192.168.179.100:/mnt/cdrom --test目录可以不存在,只要cd到这个目录就可以实现自动挂载
root@localhost cd]# systemctl restart autofs --注意修改了配置文件就需要重启服务
[root@localhost mnt]# cd /mnt/ --测试一下,可以看到没有test这个目录
[root@localhost mnt]#
[root@localhost mnt]# cd test/ --当你切换到test目录的时候就会看到可以切换到test这个目录,而且还有挂载的内容
[root@localhost test]# ls
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
[root@localhost test]# df -h --可以看到当去访问test这个目录,就将远程192.168.179.100的/mnt/cdrom给挂载过来了
Filesystem Size Used Avail Use% Mounted on
192.168.179.100:/mnt/cdrom 4.3G 4.3G 0 100% /mnt/test
上面是只读的,因为挂载的镜像里面的文件都是只读的,下面是文件系统,涉及权限问题
服务端192.168.179.100: /data/jfedu
[root@localhost ~]# mkdir -p /data/jfedu
[root@localhost ~]# cat /etc/exports
/data/jfedu 192.168.179.0/24(ro,sync) --只给了只读权限
[root@localhost ~]# exportfs -r
[root@localhost ~]# exportfs -v
/data/jfedu 192.168.179.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
客户端192.168.179.99
[root@localhost data]# vim /etc/auto.master
/data /etc/data.misc
[root@localhost test]# vim /etc/data.misc
xiaoming -fstype=nfs,ro,sync 192.168.179.100:/data/jfedu --只给ro权限
xiaowang -fstype=nfs,rw,sync 192.168.179.100:/data/jfedu --只给rw权限
[root@localhost test] systemctl restart autofs
[root@localhost data]# cd /data/
[root@localhost data]# cd xiaoming
[root@localhost xiaoming]# pwd
/data/xiaoming
[root@localhost xiaoming]# ls
fstab
[root@localhost xiaoming]# echo a >> fstab --小明是不能对该文件进行修改
-bash: fstab: Read-only file system
[root@localhost data]# cd xiaowang
[root@localhost xiaowang]# ls
fstab
[root@localhost xiaowang]# echo a >> fstab
-bash: fstab: Read-only file system
这种一般都是服务端nfs设置的权限有问题提示的错误信息
[root@localhost ~]# cat /etc/exports --由ro修改为rw
/data/jfedu 192.168.179.0/24(rw,sync)
[root@localhost ~]# exportfs -v
/data/jfedu 192.168.179.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
[root@localhost xiaowang]# echo a >> fstab --可以看到权限有问题
-bash: fstab: Permission denied
[root@localhost ~]# ll -ld /data/jfedu/ --在服务端可以看到该共享目录对于其他人others来说是一个r-x权限,没有w权限(客户端不能够创建,删除,重命名文件),客户端不再是root身份,是nfsnobody身份,身份被压缩了。
[root@localhost jfedu]# ll
-rw-r--r--. 1 root root 501 Apr 13 14:09 fstab --因为客户端身份为nfsnobody所以对fstab没有写权限
drwxr-xr-x. 2 root root 19 Mar 3 20:33 /data/jfedu/
[root@localhost ~]# chmod o+w /data/jfedu/
[root@localhost ~]# ll -ld /data/jfedu/ --该目录对于其他人来说是可写的了
drwxr-xrwx. 3 root root 31 Mar 3 20:44 /data/jfedu/
[root@localhost ~]# cd /data/xiaowang
[root@localhost xiaowang]# touch a.txt
[root@localhost xiaowang]# ll
total 4
-rw-r--r-- 1nfsnobody nfsnobody 0 Mar 3 20:51 a.txt
-rw-r--r-- 1 root root 501 Mar 3 20:33 fstab
[root@localhost data]# cd xiaoming
[root@localhost xiaoming]# touch a.txt --这个是因为在配置文件里面配置的是 xiaoming -fstype=nfs,ro,
touch: cannot touch ?..txt?. Read-only file system
总结
总结一下,在服务端分享出去的目录/data/jfedu是不需要修改目录权限,使用目录权限即可,不要将目录权限设置为777,那么客户端的用户会在里面可以随意创建修改文件,还是在共享出去的目录下单独创建一个目录给用户活动,这样便于管理
[root@localhost ~]# mkdir -p /data/jfedu/test --创建test目录限制用户只能在test目录里面才可以为所欲为
[root@localhost ~]# chmod 777 /data/jfedu/test
[root@localhost test]# cd /data/xiaowang
[root@localhost xiaowang]# cd test
[root@localhost test]# touch a.txt
最后说一下,假设在服务端分享出去的目录为/data/test,在客户端test目录下操作文件就像root一样有几种方式
- 在服务端修改jfedu目录others的权限为rwx,因为客户端身份会被压缩nfsnobody
- 在服务端/data/jfedu/目录下创建一个新的目录给others权限为7,这样身份被压缩了但是在新创建的目录里面还是可以为所欲为
- 在服务端exports文件里面加上no_root_squash,不压缩身份,即在客户端还是为root身份