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

挂载执行命令

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 装入项如下:

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 脚本将在启动时处理这些文件系统的装入。

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

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 控制。autofs 的 systemctl 命令的一般语法为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      /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 即可:

/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/&

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. Net程序崩溃了怎么去查找定位问题

    工具 这里用到两个工具分别为Procdump+Windbg Procdump:ProcDump是一个命令行实用工具,主要目的是监视应用程序,以便在管理员或开发人员可用于确定峰值的原因期间监视 CPU ...

  2. 【论文笔记】A Survey on Federated Learning: The Journey From Centralized to Distributed On-Site Learning and Beyond(综述)

    A Survey on Federated Learning: The Journey From Centralized to Distributed On-Site Learning and Bey ...

  3. golang /js index 转换excel字母表头

    Golang 1 package main 2 3 import "fmt" 4 5 func main() { 6 var Letters = []string{"A& ...

  4. vscode golang 不能自动补全问题

    问题描述: 使用vscode编辑go语言时,有时候会莫名其妙的代码不能自动补全,struct的属性值不能自动提示,这时候如果重新启动vscode也没有效果,就可能是gocode插件出了问题或者有了更新 ...

  5. 从零开始,开发一个 Web Office 套件(16):拖动控制点,调整编辑器大小

    这是一个系列博客,最终目的是要做一个基于 HTML Canvas 的.类似于微软 Office 的 Web Office 套件(包括:文档.表格.幻灯片--等等). 博客园:<从零开始, 开发一 ...

  6. 微服务生态组件之Spring Cloud OpenFeign详解和源码分析

    Spring Cloud OpenFeign 概述 Spring Cloud OpenFeign 官网地址 https://spring.io/projects/spring-cloud-openfe ...

  7. RocketMq 完整部署

    目录 RocketMq 部署 环境 物理机部署 自定义日志目录 自定义参数和数据存放位置 服务启动 启动name server 启动broker 关停服务 尝试发送消息 常见报错 部署 rockerm ...

  8. spring4+hibernate4 整合

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...

  9. 大陆出境海缆TPE APCN NCP APG简介

    目前我国的登陆站主要设立在三个城市 山东 山东青岛登陆站(隶属中国联通) EAC-C2C TPE(美国方向) 上海 上海崇明登陆站(隶属中国电信) APCN2(亚太) NCP(长线--美国,新建,亚太 ...

  10. zabbix 添加监控交换机温度item

    首先需要获取到交换机温度对应的OID,可以官方文档进行查询(多为私有OID),以盛科为例 官方文档查询到温度节点对于的OID为 10.0.3.102 1.3.6.1.4.1.27975.37.1.3. ...