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)

大家都在看

  • mybatisplus不支持sum,但支持这个

    我们知道,要对数据求和,写sql很简单:select sum(exp) from table_name我们在用mybatisplus做求和计算的时候,mybatisplus的Wra…

    数据库 2023年6月9日
    0237
  • MyBatis(一)-入门

    ==>>MyBatis中文网 1、第一个 mybastis程序 1.1 导入jar包 3.4.1 5.1.47 org.mybatis mybatis ${mybati…

    数据库 2023年6月16日
    092
  • MySQL排序方案选择

    总结一下,MySQL 总是使用 “最快” 的排序方案。 总结 总结一下,MySQL 总是使用 “最快” 的排序方案: 1、当排序数据…

    数据库 2023年6月9日
    0118
  • leetcode 83. Remove Duplicates from Sorted List 删除排序链表中的重复元素(简单)

    一、题目大意 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 示例 1: 输入:head = [1,1,2]输出:[1,…

    数据库 2023年6月16日
    099
  • Digester解析xml原理

    Tomcat内部是使用Digester来解析xml文件的,将xml转化为java对象。 digester底层是基于SAX+事件驱动+栈的方式来搭建实现的,SAX主要用来解析xml,…

    数据库 2023年6月16日
    080
  • 用SkyWalking监控MySQL(一)工具与方案

    本文适用于SkyWalking v9.1.0。 SkyWalking简介 SkyWalking是一个分布式系统的应用程序性能监视(APM)工具,专为微服务、云原生架构和基于容器(K…

    数据库 2023年5月24日
    0120
  • Jenkins2.350配置gitee

    一、下载JenKins 以War包举例演示 JenKins下载地址JenKins中文文档 二、启动Jenkins 需要配置好jdk,不做多阐述。 java -jar jenkins…

    数据库 2023年6月6日
    093
  • Linux进程管理

    进程管理 基本概念介绍 进程和线程 进程: 一个在内存中运行的应用程序,每一个进程都有自己独立的一块内存空间,并被分配一个ID号(PID),在Windows下可以打开任务管理器查看…

    数据库 2023年6月16日
    0113
  • 西数数码-安装hmx_linux下的环境记录

    [nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/6/x86_64/gpgcheck=0enabled=…

    数据库 2023年6月14日
    061
  • Dubbo源码(六)-服务路由

    前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 今天,来聊点短的,服务路由 Router,本文讲的是路由的调用路径,不讲路由…

    数据库 2023年6月11日
    076
  • 新建vue项目

    1、CMD中输入命令行 vue ui 打开一个创建项目的窗口 2、安装插件element ui 3、Idea 打开这个项目即可 4、Idea 运行vue项目 npm run ser…

    数据库 2023年6月9日
    0106
  • django中的路由层

    1.什么是路由层 简单来说,就是通过路由层中的path函数,告诉django遇到那个url,执行那个视图函数 2.路由层的请求流程 1.&#x5BA2;&#x623…

    数据库 2023年6月14日
    090
  • 测试执行和软件缺陷

    测试执行 1.基本概念 测试执行就是执行测试用例、提交Bug 单、测试结论的评估和总结等一系列测试活动,测试执行不仅包含测试用例的执行,还包括其它测试活动. 2.注意事项 (1) …

    数据库 2023年6月16日
    099
  • 分布式任务调度平台XXL-JOB安装

    安装xxl-job-admin 1.拉取镜像 #拉取镜像 docker pull xuxueli/xxl-job-admin:2.3.0 #新建挂载目录 mkdir /usr/lo…

    数据库 2023年6月11日
    084
  • 《Unix环境高级编程》笔记

    基础 Unix操作系统体系结构 操作系统就是一种软件,它管理控制计算机的硬件资源,提供程序运行的环境,这种软件又被称为内核(Kernel)。内核提供的接口被称为系统调用(Syste…

    数据库 2023年6月11日
    0108
  • 响应式编程-入门介绍

    概念 与传统编程模型对比 传统编程模型,主要特点是 同步阻塞式-Blocking; 而响应式编程(Reactive Programming) 主要特点是 异步非阻塞 Non-Blo…

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