一、环境准备
1.1 准备虚拟机test1,test2
test1:10.100.10.175
test2:10.100.10.176
1.2 关闭selinux(test1、test2都要操作)
[root@test1 ~]# vim /etc/selinux/config SELINUX=disabled
1.3 关闭防火墙(test1、test2都要操作)
[root@test1 ~]# systemctl stop firewalld
[root@test1 ~]# systemctl disable firewalld
二、NFS服务基础
• Network File System,网络文件系统
– 用途:为客户机提供共享使用的文件夹
– 协议:NFS( 2049)、RPC( 111)
• 所需软件包:nfs-utils
• 系统服务:nfs-server
2.1 NFS服务部署
test1:服务端配置(server)
[root@test1 ~]# yum -y install nfs-utils #下载安装nfs
[root@test1 ~]# mkdir /myfile #创建共享目录
[root@test1 ~]# echo "hallo my Switch" > /myfile/1.txt
[root@test1 ~]# cat /myfile/1.txt
hallo my Switch
[root@test1 ~]# vim /etc/exports
文件夹路径 客户机地址(权限)
/myfile *(ro) #允许所有客户端进行只读访问
[root@test1 ~]# systemctl restart rpcbind
[root@test1 ~]# systemctl restart nfs-server
[root@test1 ~]# systemctl enable nfs-server
test2:客户端配置(client)
[root@test2 ~]# yum -y install nfs-utils #下载安装nfs
[root@test2 ~]# mkdir -p /mnt/mynsd #创建挂载目录
[root@test2 ~]# systemctl start nfs-server
[root@test2 ~]# systemctl enable nfs-server
[root@test2 ~]# mount 10.100.10.175:/myfile /mnt/mynsd #将test1共享目录挂载到test2上
[root@test2 ~]# ls /mnt/mynsd #查看tets1共享文件是否挂载到test2上
[root@test2 ~]# df -h /mnt/mynsd #查看正在挂载的设备信息
文件系统 容量 已用 可用 已用% 挂载点
10.100.10.175:/myfile 48G 1.7G 46G 4% /mnt/mynsd
2.2 实现开机自动挂载
_netdev:声明网络设备,系统在具备网络参数后,再进行挂载本设备
[root@test2 ~]# vim /etc/fstab
10.100.10.175:/myfile /mnt/mynsd nfs _netdev 0 0
三、触发挂载
• 由 autofs服务提供的“按需访问”机制
– 只要访问挂载点就会触发响应,自动挂载指定设备
– 闲置超过时限(默认5分钟)后,会自动卸载
3.1 test2下载安装autofs服务
[root@test2 ~]# yum -y install autofs
[root@test2 ~]# systemctl restart autofs
3.2 autofs服务挂载要求和配置文件说明
触发挂载实现,必须多级的目录结构: /监控目录/挂载点目录
• 主配置文件 /etc/auto.master
– 监控点目录 挂载配置文件的路径
• 挂载配置文件,比如 /opt/xixi.txt
触发点子目录 -挂载参数 :设备名
3.3 案例演示
虚拟机test2访问/myauto1/nsd,光驱设备挂载/myauto1/nsd(前提是test2上存有光驱)
[root@test2 ~]# mkdir -p /myauto1/nsd
[root@test2 ~]# vim /etc/auto.master
/myauto1 /opt/xixi.txt
[root@test2 ~]# vim /opt/xixi.txt
nsd -fstype=iso9660 :/dev/cdrom
[root@test2 ~]# systemctl restart autofs
[root@test2 ~]# ls /myauto1/ (访问/myauto1/是否触发查看光驱挂载)
[root@test2 ~]# ls /myauto1/nsd(访问/myauto1/nsd是否触发光驱挂载)
四、 触发挂载进阶autofs与NFS
4.1 案列演示
虚拟机test2访问/myauto1/nsd,虚拟机test1 的nfs共享目录/myfile挂载到/myauto1/nsd
[root@test2 ~]# vim /etc/auto.master
/myauto1 /opt/xixi.txt
[root@test2 ~]# vim /opt/xixi.txt
nsd -fstype=iso9660 :/dev/cdrom
nfs -fstype=nfs 10.100.10.175:/myfile
[root@test2 ~]# systemctl restart autofs
[root@test2 ~]# ls /myauto1/nfs