Linux 目录挂载服务

Linux 服务器挂载文件目录通常有三种形式,手动挂载、自动挂载、Autofs 自动挂载,下面对这三个挂载做一下介绍,接受一下这三个区别以及使用场景:

准备服务器和客户端:

server 192.168.31.89 (企业里一般有专门的文件服务器,这里以一台服务器作为文件服务器,具体搭建较为简单,不再赘述)

client 192.168.31.90

1 手动挂载

这种方式比较简单,重点是掌握 mount命令知识即可,

Usage:
 mount [-lhV]
 mount -a [options]
 mount [options] [--source] <source> | [--target] <directory>
 mount [options] <source> <directory>
 mount <operation> <mountpoint> [<target>]

Options:
 -a, --all               mount all filesystems mentioned in fstab
 -c, --no-canonicalize   don't canonicalize paths
 -f, --fake              dry run; skip the mount(2) syscall
 -F, --fork              fork off for each device (use with -a)
 -T, --fstab <path>      alternative file to /etc/fstab
 -h, --help              display this help text and exit
 -i, --internal-only     don't call the mount.<type> helpers
 -l, --show-labels       lists all mounts with LABELs
 -n, --no-mtab           don't write to /etc/mtab
 -o, --options <list>    comma-separated list of mount options
 -O, --test-opts <list>  limit the set of filesystems (use with -a)
 -r, --read-only         mount the filesystem read-only (same as -o ro)
 -t, --types <list>      limit the set of filesystem types
     --source <src>      explicitly specifies source (path, label, uuid)
     --target <target>   explicitly specifies mountpoint
 -v, --verbose           say what is being done
 -V, --version           display version information and exit
 -w, --rw, --read-write  mount the filesystem read-write (default)

 -h, --help     display this help and exit
 -V, --version  output version information and exit</target></src></list></list></list></type></path></target></mountpoint></operation></directory></directory>

挂载执行命令

mount HOST:REMOTE-PATH  LOCAL-PATH

示例代码: mount -t nfs -o rw 192.168.31.89:/root/data/nfs /data 挂载执行结果

Linux 目录挂载服务

2 自动挂载

手动挂载在于命令执行简单,操作快捷,但缺点是仅能在当前会话中保持,一旦机器重启或发生宕机,则挂载取消,进而导致挂载执行失败,我们需要在手动挂载执行的基础,修改配置文件:

1.修改 /etc/fstab 文件,将挂载目录放在其中,例如下图,这样就可以保证开机时候可以自动挂载:

Linux 目录挂载服务

通常, /etc/fstab 中的 NFSv3 装入项如下:

nfs.example.com:/data /local/path nfs rw,noauto 0 0

对于 NFSv4 装入,请在第三列中使用 nfs4 而不是 nfs

nfs.example.com:/data /local/pathv4 nfs4 rw,noauto 0 0

如果您没有输入 noauto 选项,系统的 init 脚本将在启动时处理这些文件系统的装入。

Linux 目录挂载服务

3 Autofs 自动挂载

这点和上面两种有略微区别,重点在于用时挂载,不用会自动取消挂载,减少系统服务进程消耗,同时也减轻服务器连接压力。

1)Autofs与Mount/Umount的不同之处在于,它是一种看守程序。如果它检测到用户正试图访问一个尚未挂接的文件系统,它就会自动检测该文件系统,如果存在,那么Autofs会自动将其挂接。
2)另一方面,如果它检测到某个已挂接的文件系统在一段时间内没有被使用,那么Autofs会自动将其卸载。因此一旦运行了Autofs后,用户就不再需要手动完成文件系统的挂接和卸载。

3.1 安装

RedHat Linux Enterprise Server 上默认未安装 autofs。要使用它的自动装载功能,请先使用下面的命令安装该程序

sudo yum install autofs

SUSE Linux Enterprise Server 请先使用下面的命令安装该程序

sudo zypper install autofs

3.2 配置

使用 vim 等文本编辑器编辑 autofs 的配置文件来手动配置它。配置 autofs 有两个基本步骤 — master 映射文件和特定映射文件。

autofs 的默认 master 配置文件是 /etc/auto.master。可通过在 /etc/sysconfig/autofs 文件中更改 DEFAULT_MASTER_MAP_NAME 选项的值来更改其位置。以下是 Linux Enterprise Server 中默认 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
#
NOTE: mounts done from a hosts map will be mounted with the
      "nosuid" and "nodev" options unless the "suid" and "dev"
      options are explicitly given.

#
/net    -hosts
#
Include /etc/auto.master.d/*.autofs
The included files must conform to the format of this file.

#
+dir:/etc/auto.master.d
#
Include central master map if it can be found using
nsswitch sources.

#
Note that if there are entries for /net or /misc (as
above) in the included master map any keys that are the
same will not be seen as the first read key seen takes
precedence.

#
+auto.master

1

autofs 手册页 (man autofs) 提供了许多有关该自动装入器映射格式的重要信息。

2

虽然这些内容默认会被注释掉 (#),但它依然是简单的自动装入器映射语法示例。

3

如果您需要将 master 映射分割成几个文件,请将该行取消注释,并将映射(后缀为 .autofs)置于 /etc/auto.master.d/ 目录中。

4

+auto.master 可确保使用 NIS的用户仍可找到其 master 映射。

auto.master 中的项有三个字段,语法如下:

mount point      map name      options

mount point

要在其中装入 autofs 文件系统的基本位置,例如 /home

map name

装入时所用映射源的名称。

options

这些选项(如指定)将作为默认值应用于给定映射中的所有项。

3.3操作和调试

autofs 服务的操作由 systemd 控制。 autofssystemctl 命令的一般语法为 sudo systemctl enable|start|stop|reload |restart autofs

例如同样加载NFS,相关配置方案如下:

1 编辑 master 映射文件 /etc/auto.master

sudo vim /etc/auto.master

2 在 /etc/auto.master 末尾为新的 NFS 装入添加一条新项

/nfs &#xA0; &#xA0; &#xA0;/etc/auto.nfs &#xA0; &#xA0; &#xA0;--timeout=10

它告诉 autofs 基本安装点是 /nfs,NFS 共享在 /etc/auto.nfs 映射中指定,并且此映射中的所有共享将在 10 秒不活动后自动卸载

编辑或创建映射文件 vim /etc/auto.nfs 对每个 NFS 共享, /etc/auto.nfs 通常都会包含单独的一行。

data&#xA0; 192.168.31.89:/root/data/nfs

上面的行表示当收到请求时,系统会将 192.168.31.89 主机上的 /root/data/nfs 目录自动装入到本地主机上的 /nfs/data 目录( /nfs 取自 auto.master 映射)。 /nfs/data 目录将由 autofs 自动创建。

3 重新启动 autofs 并检查它是否正常工作

sudo systemctl restart autofs

如果您能看到远程共享上的文件列表,则表示 autofs 工作正常

4 /net 安装点

如果您使用了许多 NFS 共享,这个助手安装点将非常有用。 /net 会根据需要自动装入本地网络上的所有 NFS 共享。该项在 auto.master 文件中已经存在,因此,您只需将其取消注释,然后重启动 autofs 即可:

/net      -hosts
systemctl restart autofs

例如,如果您有名为
server

的服务器以及名为 /export

的 NFS 共享,您可以在命令行上键入

cd /net/server/export

5 使用通配符自动装入子目录

如果您的某个目录含有多个子目录,并且您需要将这些子目录单个自动装入(一般情况下,该目录是包含各个用户主目录的 /home 目录), autofs 提供了便捷的解决方案。

如果这些子目录是主目录,则在 auto.master 中添加下行:

/home      /etc/auto.home

现在,您需要在 /etc/auto.home 文件中添加正确的映射,以便自动装入用户的主目录。一种方法是为每个目录创建单独的项:

wilber      server.com:/home/wilber
penguin      server.com:/home/penguin
tux      server.com:/home/tux
[...]

这种方法非常麻烦,因为您需要在 auto.home 中管理用户列表。您可以使用星号”*”取代安装点,使用符号”&”取代要装入的目录。

*      server:/home/&

Original: https://www.cnblogs.com/xiong97/p/16435067.html
Author: 静言善思
Title: Linux 目录挂载服务

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611702/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Python 垃圾回收总结

    前言 最近在阅读《垃圾回收的算法与实现》,里面将讲到了一些常用的垃圾回收(Garbage Collect)算法,如:标记-清除、引用计数、分代回收等等。后面讲到了 Python 的…

    数据库 2023年6月6日
    0110
  • haproxy服务部署

    haproxy haproxy 一、haproyx是什么 二、负载均衡类型 三、部署haproxy 1.源码部署haproxy 2.Haproxy搭建http负载均衡 一、hapr…

    数据库 2023年6月14日
    0111
  • SSM简单整合!!!

    3. 在maven中添加依赖 org.mybatis mybatis-spring 2.0.7 org.springframework spring-jdbc 5.3.2 org….

    数据库 2023年6月16日
    087
  • [spring]spring的bean自动装配机制

    是spring满足bean依赖的一种方式 spring会在上下文中自动寻找,并自动给bean装配属性 spring的装配方式: (1)手动装配 在people类中依赖了cat和do…

    数据库 2023年6月16日
    086
  • 重构

    参数过长 影响: 方法不易被理解、使用,方法签名容易不稳定,不易维护 解决方法:反复使用提炼方法+内联方法,消除多余参数 ​ 尽量把方法移进相关的类中 ​ 如实体类中的get方法在…

    数据库 2023年6月16日
    0212
  • Spark学习(2) RDD编程

    RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变、可分区、弹性、里面的元素可并行计算的集合 R…

    数据库 2023年6月16日
    0114
  • 斗地主游戏的案例开发

    关于java后端的斗地主游戏开发案例(只实现后端部分) 斗地主游戏的案例开发 业务需求分析: 斗地主的做牌, 洗牌, 发牌, 排序(拓展知识), 看牌。业务: 总共有54张牌。点数…

    数据库 2023年6月16日
    079
  • TypeScript语言基础

    一、什么是TypeScript 编程语言包括动态类型语言和静态类型语言。动态类型语言是指在程序运行阶段才检查变量数据类型的语言,在定义变量时不需要指定变量的数据类型,通常在编译时不…

    数据库 2023年6月14日
    095
  • 渗透攻防Web篇-深入浅出SQL注入

    1 背景 京东SRC(Security Response Center)收录大量外部白帽子提交的sql注入漏洞,漏洞发生的原因多为sql语句拼接和Mybatis使用不当导致。 2 …

    数据库 2023年5月24日
    0108
  • Redis的Java客户端

    Redis 的 Java 客户端 Jedis 优点:以 Redis 命令作为方法名称,学习成本低廉,简单且实用 缺点:Jedis 的实例是线程不安全的,在多线程的环境下需要基于线程…

    数据库 2023年6月16日
    092
  • 十一章 配置文件参数化

    把Spring配置文件中需要经常修改的字符串信息,转移到一个更小的配置文件中 1. 小配置文件(.properties) 2. 好处 : 利于维护 1.配置文件参数化开发步骤 已数…

    数据库 2023年6月14日
    082
  • JavaScript详解

    一、快速入门 1.JavaScript的两种引入方式 方式一:直接在html里面写 目的:实现简单的弹窗 点击查看代码 <!DOCTYPE html> <html…

    数据库 2023年6月16日
    068
  • 万恶的Jackson

    一、吐槽 已经是凌晨12点了我还是睡不着我所有的实体类时间用的j8的LocalDateTime这就导致一个问题:jackson不能序列化时间,因为它不支持j8的Api,让我添加 j…

    数据库 2023年6月6日
    084
  • 视野 | KeyDB:为 Web 应用而生的高性能 Redis 分支

    王奇 顾问软件工程师目前从事 PaaS 中间件服务(Redis / MongoDB / ELK 等)开发工作,对 NoSQL 数据库有深入的研究以及丰富的二次开发经验,热衷对 No…

    数据库 2023年5月24日
    093
  • 对炒股看法

    1. 自己的炒股情况 2017 年左右接触炒股, 陆续入迷. 个人情况输的多. 主要输在阅历浅, 策略不连贯, 心态不稳. 期间翻阅了大量书籍, 学到了很多, 开拓了眼界. 真的是…

    数据库 2023年6月9日
    087
  • City of stars

    本文来自博客园,作者:ukyo–BlackJesus,转载请注明原文链接:https://www.cnblogs.com/ukzq/p/16747306.html Or…

    数据库 2023年6月11日
    0106
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球