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

准备服务器和客户端:

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

client 192.168.31.90

1 手动挂载

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

  1. Usage:
  2. mount [-lhV]
  3. mount -a [options]
  4. mount [options] [--source] <source> | [--target] <directory>
  5. mount [options] <source> <directory>
  6. mount <operation> <mountpoint> [<target>]
  7. Options:
  8. -a, --all mount all filesystems mentioned in fstab
  9. -c, --no-canonicalize don't canonicalize paths
  10. -f, --fake dry run; skip the mount(2) syscall
  11. -F, --fork fork off for each device (use with -a)
  12. -T, --fstab <path> alternative file to /etc/fstab
  13. -h, --help display this help text and exit
  14. -i, --internal-only don't call the mount.<type> helpers
  15. -l, --show-labels lists all mounts with LABELs
  16. -n, --no-mtab don't write to /etc/mtab
  17. -o, --options <list> comma-separated list of mount options
  18. -O, --test-opts <list> limit the set of filesystems (use with -a)
  19. -r, --read-only mount the filesystem read-only (same as -o ro)
  20. -t, --types <list> limit the set of filesystem types
  21. --source <src> explicitly specifies source (path, label, uuid)
  22. --target <target> explicitly specifies mountpoint
  23. -v, --verbose say what is being done
  24. -V, --version display version information and exit
  25. -w, --rw, --read-write mount the filesystem read-write (default)
  26. -h, --help display this help and exit
  27. -V, --version output version information and exit

挂载执行命令

  1. mount HOST:REMOTE-PATH LOCAL-PATH

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

2 自动挂载

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

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

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

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

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

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

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

2.写入启动脚本,并将其设定systemd 服务中,可参考文章如何加入系统systemd 开机服务,示例如下:

3 Autofs 自动挂载

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

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

3.1 安装

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

  1. sudo yum install autofs

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

  1. 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 映射文件的内容:

  1. #
  2. # Sample auto.master file
  3. # This is a 'master' automounter map and it has the following format:
  4. # mount-point [map-type[,format]:]map [options]
  5. # For details of the format look at auto.master(5).
  6. #
  7. /misc /etc/auto.misc
  8. #
  9. # NOTE: mounts done from a hosts map will be mounted with the
  10. # "nosuid" and "nodev" options unless the "suid" and "dev"
  11. # options are explicitly given.
  12. #
  13. /net -hosts
  14. #
  15. # Include /etc/auto.master.d/*.autofs
  16. # The included files must conform to the format of this file.
  17. #
  18. +dir:/etc/auto.master.d
  19. #
  20. # Include central master map if it can be found using
  21. # nsswitch sources.
  22. #
  23. # Note that if there are entries for /net or /misc (as
  24. # above) in the included master map any keys that are the
  25. # same will not be seen as the first read key seen takes
  26. # precedence.
  27. #
  28. +auto.master

1

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

2

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

3

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

4

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

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

  1. mount point map name options

mount point

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

map name

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

options

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

3.3操作和调试

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

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

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

  1. sudo vim /etc/auto.master

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

  1. /nfs      /etc/auto.nfs      --timeout=10

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

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

data  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 即可:

  1. /net -hosts
  1. systemctl restart autofs

例如,如果您有名为 server 的服务器以及名为 /export 的 NFS 共享,您可以在命令行上键入

  1. # cd /net/server/export

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

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

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

  1. /home /etc/auto.home

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

  1. wilber server.com:/home/wilber
  2. penguin server.com:/home/penguin
  3. tux server.com:/home/tux
  4. [...]

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

  1. * server:/home/&

Linux 目录挂载服务的更多相关文章

  1. 磁盘分区以及Linux目录挂载详解

    一.背景 一直以来,对于磁盘的分区以及Linux目录挂载的概念都不是很清晰,现在趁着春暖花开周末在家没事就研究了下它们,现在来分享我的理解. 二.概念详解 1.磁盘分区 磁盘分区是把物理的磁盘空间按照 ...

  2. 把Linux目录挂载到开发板、设置开发板从NFS启动、取消开发板从NFS启动

    声明:文中"PC虚拟机Linux"是指在PC上安装了虚拟机,然后在虚拟机中装的Linux. 关于NFS的详细介绍可参考:http://www.cnblogs.com/nufangr ...

  3. Linux--简单实现nfs的目录挂载,ntp时间同步

    一.NFS (Network FileSystem) 网络文件系统 是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源. 在NFS的应用中,本地NFS的客户端 ...

  4. Linux服务器SMB服务挂载目录

    挂载方法 mount -o username=账号,password=密码 //SMB服务器IP/共享目录 /挂载点 smbclient链接 smbclient //SMB服务器IP/共享目录/ -U ...

  5. Linux 怎么查看服务的启动进程所占用的目录

    lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 ...

  6. Linux远程目录挂载

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/ad99ab1d-1 ...

  7. Windows10通过NFS挂载linux目录

    大致分为以下三大步骤: 一.启动NFS服务器 二.启动NFS客户端 三.挂载NFS目录 工具: win10.虚拟机Ubuntu18.0系统 一. 启动linux的NFS服务端: 以下均为Ubuntu操 ...

  8. Linux 系统安装 AutoFs 挂载服务

    关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 作者: Escape 链接: https://escapelife.github.io/pos ...

  9. Linux 磁盘分区、格式化、目录挂载

    实验环境: Citrix虚拟化, RHEL6.4系统环境 本文目的: 熟悉Linux的基本磁盘分区.格式化.目录挂载. 显示当前主机目录 磁盘分区 磁盘格式化 挂载目录 1. 显示当前主机目录 命令d ...

随机推荐

  1. 2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP)

    2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP) https://www.luogu.com.cn/problem/P4052 题意: JSOI 交给队员 ZYX ...

  2. async异步函数的执行顺序

    1 async function async1(){ 2 console.log('async1 start') //2 3 await async2() 4 //await async2()后面的内 ...

  3. python基础练习题(题目 有序列表插入元素)

    day26 --------------------------------------------------------------- 实例039:有序列表插入元素 题目 有一个已经排好序的数组. ...

  4. Dapr 远程调试之 Nocalhost

    虽然Visual studio .Visual studio code 都支持debug甚至远程debug ,Dapr 搭配Bridge to Kubernetes 支持在计算机上调试 Dapr 应用 ...

  5. Enum枚举类型实战总结,保证有用!

    一般在我们开发时如果能使用枚举罗列的,一般都会定义一个枚举类型.将枚举类型作为方法的参数,可以方便的进行调用,给我们带来不少的遍历,当然有时候它还不如直接用一个int类型带来,带来一定灵活性.但只要能 ...

  6. Redis实现并发阻塞锁方案

    由于用户同时访问线上的下订单接口,导致在扣减库存时出现了异常,这是一个很典型的并发问题,本篇文章为解决并发问题而生,采用的技术为Redis锁机制+多线程的阻塞唤醒方法. 在实现Redis锁机制之前,我 ...

  7. 开发并发布npm包,支持TypeScript提示,rollup构建打包

    前言: 工作了几年,想把一些不好找现成的库的常用方法整理一下,发布成npm包,方便使用.也学习一下开发发布流程. 主要用到的工具:npm. 开发库:babel.typescript.rollup.es ...

  8. Ruby 趣学笔记(二)

    Ruby 趣学笔记(二) 本文写于 2020 年 5 月 7 日 类的继承 之前忘记写了,Ruby 的继承写法是: class IPhone < Phone def initialize(id, ...

  9. JWT 访问令牌

    JWT 访问令牌 更为详细的介绍jwt 在学习jwt之前我们首先了解一下用户身份验证 1 单一服务器认证模式 一般过程如下: 用户向服务器发送用户名和密码. 验证服务器后,相关数据(如用户名,用户角色 ...

  10. Golang:将日志以Json格式输出到Kafka

    在上一篇文章中我实现了一个支持Debug.Info.Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手.有兴趣的可以通过这个链接前往:https://github.com/ ...