• autofs自动挂载服务


    autofs简介

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

    安装autofs服务

    [root@test ~]# yum -y install autofs
    

     将autofs服务加入到开机启动项中

    [root@test ~]# systemctl  enable --now autofs
    

    修改配置文件(主配置+子配置)

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

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

    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    #主配置
    

    自定义子配置文件,把光盘挂载到/media/cdrom目录中,-fstype为文件系统格式参数,iso9660为光盘系统设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,最终/dev/cdrom则是定义要挂载的设备名称。

    [root@test ~]# vim /etc/cdrom.misc 
    cdrom  -fstype=iso9660,ro,nosuid,nodev  :/dev/cdrom
    

     重启autofs服务

    [root@test ~]# systemctl  restart autofs
    

     来查看下当前的设备挂载情况,确认光盘设备目前是没有被挂载使用的

    [root@test ~]# df -h
    Filesystem             Size  Used Avail Use% Mounted on
    devtmpfs               883M     0  883M   0% /dev
    tmpfs                  901M     0  901M   0% /dev/shm
    tmpfs                  901M  9.3M  892M   2% /run
    tmpfs                  901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root   17G  5.9G   12G  35% /
    /dev/sda1             1014M  179M  836M  18% /boot
    

     /media目录中根本就没有一个cdrom子目录,使用cd命令进入/media/cdrom时光盘设备会立即挂载上

    [root@test ~]# ls /media/
    [root@test ~]# cd /media/cdrom
    [root@test cdrom]# ls
    AppStream  EFI   extra_files.json  images    media.repo               RPM-GPG-KEY-redhat-release
    BaseOS     EULA  GPL               isolinux  RPM-GPG-KEY-redhat-beta  TRANS.TBL
    
    [root@test cdrom]# df -h
    Filesystem             Size  Used Avail Use% Mounted on
    devtmpfs               883M     0  883M   0% /dev
    tmpfs                  901M     0  901M   0% /dev/shm
    tmpfs                  901M  9.3M  892M   2% /run
    tmpfs                  901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root   17G  5.9G   12G  35% /
    /dev/sda1             1014M  179M  836M  18% /boot
    /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服务)

    客户端 192.168.248.129 RHEL8

    selinux 关闭

    firewalld 关闭

    服务端 192.168.248.156 RHEL8

    selinux 关闭

    firewalld 关闭

    安装nfs,rpcbind

    [root@localhost ~]# yum -y install nfs-utils  rpcbind
    

    NFS配置的共享参数

    参数 作用
    ro 只读默认
    rw 读写
    root_squash 当NFS客户端使用root访问时,映射为NFS服务端的匿名用户
    no_root_squash 当NFS客户端使用root访问时,映射为NFS服务端的root用户
    all_squash 不论NFS客户端的使用任何账号,都映射为匿名用户
    sync 同时将数据写入到内存和硬盘中
    async 优先将数据写入到内存,然后写入硬盘,效率高,但可能造成数据丢失

    NFS服务端配置

    创建nfs共享文件

    [root@localhost ~]# mkdir -p   /nfsfile/test
    [root@localhost ~]# echo "This is the xxxx" > /nfsfile/test/file
    

    NFS服务端配置文件 “etc/exports”,用于定义共享的目录及权限 【格式为:共享目录的绝对路径,允许访问NFS资源的客户端(权限参数)】

    [root@localhost ~]# vim /etc/exports
    /nfsfile/test 192.168.248.* (rw,sync,root_squash)
    

    启动nfs-server,rpcbind

    [root@localhost ~]# systemctl  start nfs-server
    [root@localhost ~]# systemctl  start rpcbind
    

    NFS客户端配置

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

    参数 作用
    -e 显示NFS服务端列表
    -a 显示本机挂载NFS资源情况
    -v 显示版本号
    [root@test ~]# showmount  -e  192.168.248.156
    Export list for 192.168.248.156:
    /nfsfile/test (everyone)
    

    创建本地挂载目录

    [root@test ~]# mkdir /nfsfile
    
    #挂载远程主机nfsfile目录
    [root@test ~]# mount  192.168.248.156:/nfsfile /nfsfile
    
    #可以看到刚刚写下的内容
    [root@test ~]# cat /nfsfile/test/file
    This is the xxxx
    
    [root@test ~]# umount /nfsfile/
    [root@test ~]# ls /nfsfile/
    

    使用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@test ~]# vim /etc/test.misc
    test  -rw 192.168.248.156:/nfsfile
    

    重启autofs

    [root@test ~]# systemctl  restart autofs
    
    #查看挂载文件中并没有挂载远程主机文件
    [root@test ~]# df -h
    Filesystem                Size  Used Avail Use% Mounted on
    devtmpfs                  883M     0  883M   0% /dev
    tmpfs                     901M     0  901M   0% /dev/shm
    tmpfs                     901M  9.3M  892M   2% /run
    tmpfs                     901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root      17G  5.9G   12G  35% /
    /dev/sda1                1014M  179M  836M  18% /boot
    tmpfs                     181M     0  181M   0% /run/user/0
    
    #进入/nfsfile/test目录触发挂载
    [root@test ~]# cd /nfsfile/test
    [root@test test]# df -h
    Filesystem                Size  Used Avail Use% Mounted on
    devtmpfs                  883M     0  883M   0% /dev
    tmpfs                     901M     0  901M   0% /dev/shm
    tmpfs                     901M  9.3M  892M   2% /run
    tmpfs                     901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root      17G  5.9G   12G  35% /
    /dev/sda1                1014M  179M  836M  18% /boot
    tmpfs                     181M     0  181M   0% /run/user/0
    92.168.248.156:/nfsfile   17G  1.5G   16G   9% /nfsfile/test
    
    #查看挂载文件可以看到刚刚写下的内容
    [root@test test]# ls
    test
    [root@test test]# cat test/file 
    This is the xxxx
    
  • 相关阅读:
    面向对象程序设计(JAVA) 第14周学习指导及要求
    面向对象程序设计(JAVA) 第13周学习指导及要求
    面向对象程序设计(JAVA) 第12周学习指导及要求
    让小球做圆周运动,你有几种办法?
    💥2020面试准备系列(一):JS
    秒懂js的垃圾回收
    jqgrid计算表单
    CSS揭秘实用技巧总结
    利用css 实现 视觉差效果
    前端经典面试题解密:JS的new关键字都干了什么?
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14527846.html
Copyright © 2020-2023  润新知