autofs自动挂载服务

AutoFs服务与mount/umount命令不同在与它是处于一种守护进程,只有检测到用户试图访问一个尚未挂载的文件系统时,会自动检测并挂载该文件系统,也就是说,将挂载信息写入/etc/fstab文件后将会在每次开机都会启动时都会将其挂载,而运行AutoFs后则是,当需要使用该文件系统了才会的动态挂载,节约了网络与系统资源。

//安装设置开机自启动
[root@localhostl ~]# yum -y install autofs
[root@localhostl ~]# systemctl  enable --now autofs

autofs服务程序的主配置文件 "/etc/auto.master"中需要按照“挂载目录 子配置文件”的格式写入参数。挂载目录是文件要挂载位置的上一级目录,

例如对光盘进行挂载到/media/cdrom中配置文件中写入/media即可,然后在子配置中进行进一步说明,子配置自定义即可。子配置文件中应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式写入参数。

[root@localhostl ~]# vim /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc   /etc/auto.misc
/media  /etc/cdrom.misc   //加入这行

//自定义子配置文件,把光盘挂载到/media/cdrom目录中,-fstype为文件系统格式参数,iso9660为光盘系统设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,最终/dev/cdrom则是定义要挂载的设备名称。
[root@localhostl ~]# vim /etc/cdrom.misc
cdrom -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
[root@localhostl ~]# systemctl restart autofs
[root@localhostl ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G     0  1.9G   0% /dev/shm
tmpfs                  1.9G   17M  1.9G   1% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  3.1G   14G  18% /
/dev/nvme0n1p1        1014M  179M  836M  18% /boot
tmpfs                  376M     0  376M   0% /run/user/0
//media目录中根本就没有一个cdrom子目录,使用cd命令进入/media/cdrom时光盘设备会立即挂载上
[root@localhostl ~]# ls /media/
[root@localhostl ~]# cd /media/cdrom
[root@localhostl cdrom]# ls
AppStream  EULA              images      RPM-GPG-KEY-redhat-beta
BaseOS     extra_files.json  isolinux    RPM-GPG-KEY-redhat-release
EFI        GPL               media.repo  TRANS.TBL
[root@localhostl cdrom]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G     0  1.9G   0% /dev/shm
tmpfs                  1.9G   17M  1.9G   1% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  3.1G   14G  18% /
/dev/nvme0n1p1        1014M  179M  836M  18% /boot
tmpfs                  376M     0  376M   0% /run/user/0
/dev/sr0               7.9G  7.9G     0 100% /media/cdrom

NFS文件系统

NFS(Network Files System)网络文件系统,NFS文件系统协议允许网络中的主机通过TCP/IP协议进行资源共享,NFS客户端可以像使用本地资源一样读写远程NFS服务端的资料,NFS依赖于RPC服务与外部通信,必须要保证RPC能够正常注册服务的的端口信息才可以正常使用NFS服务。(RHEL7系统默认已安装NFS服务)

客户端localhost192.168.44.128RHEL8selinux 关闭firewalld 关闭
服务端node2 192.168.44.131 RHEL8 selinux 关闭firewalld 关闭

NFS配置的共享参数

参数作用
ro 只读默认
rw 读写
root_squash 当NFS客户端使用root访问时,映射为NFS服务端的匿名用户
no_root_squash 当NFS客户端使用root访问时,映射为NFS服务端的root用户
all_squash 不论NFS客户端的使用任何账号,都映射为匿名用户
sync 同时将数据写入到内存和硬盘中
async 优先将数据写入到内存,然后写入硬盘,效率高,但可能造成数据丢失
[root@node2 ~]# yum -y install nfs-utils  rpcbind
//创建nfs共享文件
[root@node2 ~]# mkdir -p /nfsfile/test
[root@node2 ~]# echo "hello world" > /nfsfile/test/file

//NFS服务端配置文件 “etc/exports”,用于定义共享的目录及权限 【格式为:共享目录的绝对路径,允许访问NFS资源的客户端(权限参数)】
[root@node2 ~]# vim /etc/exports
/nfsfile/test 192.168.44.* (rw,sync,root_squash)
[root@node2 ~]# systemctl start nfs-server
[root@node2 ~]# systemctl start rpcbind

NFS客户端配置

使用showmount命令查询NFS服务端共享信息(格式为:“showmount” [参数] [远程主机])

参数作用
-e 显示NFS服务端列表
-a 显示本机挂载NFS资源情况
-v 显示版本号
[root@localhost ~]# yum -y install nfs-utils  rpcbind
[root@localhostl ~]# showmount -e 192.168.44.131
Export list for 192.168.44.131:
/nfsfile/test (everyone)
[root@localhostl ~]# mkdir /nfsfile
//挂载node2上的nfsfile目录
[root@localhostl ~]# mount 192.168.44.131:/nfsfile /nfsfile
[root@localhostl ~]# cat /nfsfile/test/file 
hello world
//使用autofs自动挂载
[root@test ~]# vim /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc   /etc/auto.misc
/media  /etc/cdrom.misc
/nfsfile  /etc/test.misc   //添加这行
[root@localhostl ~]# vim /etc/test.misc
test  -rw 192.168.44.131:/nfsfile
[root@localhostl ~]# systemctl restart autofs
[root@localhostl ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G     0  1.9G   0% /dev/shm
tmpfs                  1.9G   17M  1.9G   1% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  2.5G   15G  15% /
/dev/nvme0n1p1        1014M  179M  836M  18% /boot
tmpfs                  376M     0  376M   0% /run/user/0
//进入nfsfile/test自动触发挂载
[root@localhostl ~]# cd /nfsfile/test
[root@localhostl test]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G   17M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root     17G  2.5G   15G  15% /
/dev/nvme0n1p1          1014M  179M  836M  18% /boot
tmpfs                    376M     0  376M   0% /run/user/0
192.168.44.131:/nfsfile   17G  2.9G   15G  17% /nfsfile/test
[root@localhostl test]# cat test/file 
hello world
原文地址:https://www.cnblogs.com/chensongling/p/14535165.html