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
挂载执行命令
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
|
|
虽然这些内容默认会被注释掉 (#),但它依然是简单的自动装入器映射语法示例。 |
|
如果您需要将 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/&
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律