Autofs
AutoFS 在插入或访问可移动媒体或网络共享时提供自动挂载功能。
安装
autofs4 模块。配置
AutoFS 使用配置文件模板,这些模板位于 /etc/autofs。主模板称为 auto.master,它可以指向一个或多个用于特定媒体类型的其他模板。
使用您喜欢的编辑器打开文件 /etc/autofs/auto.master,您将看到类似以下内容
/etc/autofs/auto.master
#/media /etc/autofs/auto.media
每行上的第一个值确定了模板中所有媒体的挂载基础目录,第二个值是使用的模板。默认基础路径是 /media,但您可以将其更改为任何其他首选位置。例如
/etc/autofs/auto.master
/media/misc /etc/autofs/auto.misc --timeout=5 /media/net /etc/autofs/auto.net --timeout=60
ENTER)。如果不存在正确的 EOF (文件结束) 行,AutoFS 守护进程将无法正确加载。timeout 设置在卸载目录之前等待的秒数。将此值设置为 0 将禁用超时。如果基础目录在您的系统中不存在,它将被创建。基础目录将被挂载以加载动态加载的媒体,这意味着在 AutoFS 运行时,基础目录中的任何内容都将无法访问。但是,此过程是非破坏性的,因此如果您不小心将自动挂载到正在使用的目录,您只需更改 auto.master 中的位置并重新启动 AutoFS 即可恢复原始内容。
如果您仍希望将媒体自动挂载到目标非空目录,并且在动态加载的目录挂载后仍希望访问原始文件,您可以使用 autofs 将它们挂载到另一个目录 (例如 /var/autofs/net) 并创建软链接。
# ln -s /var/autofs/net/share_name /media/share_name
或者,您可以让 autofs 将您的媒体挂载到特定文件夹,而不是通用文件夹内。
/etc/autofs/auto.master
/- /etc/autofs/auto.template
/etc/autofs/auto.template
/path/to/folder -options :/device/path /home/user/usbstick -fstype=auto,async,nodev,nosuid,umask=000 :/dev/sdb1
打开文件 /etc/nsswitch.conf 并为 automount 添加一个条目
automount: files
完成模板配置 (见下文) 后,通过 启用 和启动 autofs.service 来以 root 身份启动 AutoFS 守护进程。
设备现在在访问时自动挂载,只要您访问它们,它们将保持挂载状态。
可移动媒体
可移动设备根据下一个可用位置分配块设备位置,例如,如果 /dev/sd{a,b,c} 已被占用,下一个可移动媒体将被分配块 /dev/sdd。与其基于不可靠的块设备路径分配挂载点,不如使用可移动媒体的 UUID 或 PARTUUID 作为映射文件中的位置,这是一种更健健的方法。
例如,要将特定的 USB 驱动器挂载到路径 /mnt/black,请配置模板文件和映射文件
/etc/autofs/auto.master
# master template file /mnt /etc/autofs/auto.mnt # [options here]
使用 blkid 查找要挂载分区的 UUID,然后生成映射文件
# _ID=$( blkid --output value --match-tag PARTUUID /dev/sdXY )
# printf "%s %s\n" "black -fstype=auto :PARTUUID=" "${_ID}" >/etc/autofs/auto.mnt
NFS 网络挂载
AutoFS 提供自动发现和挂载 NFS-共享到远程服务器 (autofs5 已移除 /etc/autofs/auto.net 中的 AutoFS 网络模板)。要启用从所有可访问服务器自动发现和挂载网络共享,而无需进一步配置,您需要在 /etc/autofs/auto.master 文件中添加以下内容
/net -hosts --timeout=60
例如,如果您有一个远程服务器 fileserver (目录名称是服务器的主机名) 并且有一个名为 /home/share 的 NFS 共享,您只需键入即可访问该共享
# cd /net/fileserver/home/share
/etc/conf.d/autofs 中移除该选项以启动 AutoFS 守护进程。-hosts 选项使用与 showmount 命令类似的机制来检测远程共享。您可以通过键入以下命令查看导出的共享
# showmount servername -e
手动 NFS 配置
要在位置 /mnt/foo 处为 file_server 在 /srv/shared_dir 上挂载 NFS 共享,请 添加 新配置,例如 file_server.autofs
/etc/autofs/auto.master.d/file_server.autofs
/mnt /etc/autofs/auto.file_server --timeout 60
/etc/autofs/auto.file_server
foo -rw,soft,rsize=8192,wsize=8192 file_server:/srv/shared_dir
Samba
单个共享
将以下内容添加到 /etc/autofs/auto.master
/media/[my_server] /etc/autofs/auto.[my_server] --timeout 60 --browse
其中 --timeout 定义了在文件系统被卸载之前等待的秒数。--browse 选项会在映射文件中为每个挂载点创建空文件夹,以防止超时,如果无法联系到网络共享。
接下来创建一个文件 /etc/autofs/auto.[my_server]
[any_name] -fstype=cifs,[other_options] ://[remote_server]/[remote_share_name]
您可以在 other_options 部分指定要使用的用户名和密码
[any_name] -fstype=cifs,username=[username],password=[password],[other_options] ://[remote_server]/[remote_share_name]
$ 和其他字符。多个共享
您可以在 /etc/autofs/auto.[my_server] 中指定多个共享,例如
[any_name] -fstype=cifs,[other_options] /photos ://[remote_server]/photos /music ://[remote_server]/music /video ://[remote_server]/video
自动发现
请参阅 /etc/autofs/auto.smb 中的注释。
FTP 和 SSH (配合 FUSE)
使用 FUSE,一个虚拟文件系统层,可以无缝地通过 AutoFS 访问远程 FTP 和 SSH 服务器。
远程 FTP
首先,安装 curlftpfs 包。
加载 fuse 模块
# modprobe fuse
创建一个 /etc/modules-load.d/fuse.conf 文件,其中包含 fuse,以便在每次系统启动时加载它。
接下来,在 /etc/autofs/auto.master 中为 FTP 服务器添加新条目
/media/ftp /etc/autofs/auto.ftp --timeout=60
创建文件 /etc/autofs/auto.ftp 并以 ftp://myuser:mypassword@host:port/path 格式添加服务器
servername -fstype=curl,rw,allow_other,nodev,nonempty,noatime :ftp\://myuser\:mypassword\@remoteserver
df (仅限已挂载服务器) 或查看文件 /etc/autofs/auto.ftp 的人都是可见的。如果您想要稍微提高安全性,可以创建文件 ~root/.netrc 并将密码保存在那里。密码仍然是纯文本,但您可以设置模式为 600,并且 df 命令不会显示它们 (无论是否挂载)。此方法对密码中的特殊字符 (否则必须转义) 也不太敏感。格式如下
machine remoteserver login myuser password mypassword
/etc/autofs/auto.ftp 中的行如下,不包含用户名和密码
servername -fstype=curl,allow_other :ftp\://remoteserver
使用此代码创建文件 /sbin/mount.curl
/sbin/mount.curl
#!/bin/sh curlftpfs $1 $2 -o $4,disable_eprt
使用此代码创建文件 /sbin/umount.curl
/sbin/umount.curl
#!/bin/sh fusermount -u $1
为两个文件设置权限
# chmod 755 /sbin/mount.curl # chmod 755 /sbin/umount.curl
重新启动后,您应该可以通过 /media/ftp/servername 访问您的新 FTP 服务器。
远程 SSH
这是使用 AutoFS 通过 SSH 访问远程文件系统的基本说明。
安装 sshfs 包。
加载 fuse 模块
# modprobe fuse
如果您还没有 /etc/modules-load.d/fuse.conf 文件,请创建一个包含 fuse 的文件,以便在每次系统启动时加载它。
安装 openssh。
生成 SSH 密钥对
$ ssh-keygen
当生成器询问密码时,只需按 ENTER。使用没有密码的 SSH 密钥安全性较低,但将 AutoFS 与密码一起运行会带来一些额外的困难,这些困难目前 (尚未) 在本文档中涵盖。
接下来,将公钥复制到远程 SSH 服务器
$ ssh-copy-id username@remotehost
以 root 身份,检查您是否可以登录到远程服务器
# ssh username@remotehost
known_hosts 列表中。也可以手动将主机添加到 /etc/ssh/ssh_known_hosts。在 /etc/autofs/auto.master 中为 SSH 服务器创建一个新条目
/media/ssh /etc/autofs/auto.ssh --timeout=60
创建文件 /etc/autofs/auto.ssh 并添加一个 SSH 服务器
/etc/autofs/auto.ssh
servername -fstype=fuse,rw,allow_other,IdentityFile=/home/username/.ssh/id_rsa :sshfs\#username@host\:/
重新启动后,您应该可以通过 /media/ssh/servername 访问您的 SSH 服务器。
MTP
Media Transfer Protocol (MTP) 用于某些 Android 设备。
安装 mtpfs 包。
在 /etc/autofs/auto.misc 中为 MTP 设备创建一个新条目
android -fstype=fuse,allow_other,umask=000 :mtpfs
故障排除和调整
本节包含一些解决 AutoFS 常见问题的方案。
使用 NIS
AutoFS 的 5.0.5 版本对 NIS 提供了更高级的支持。要将 AutoFS 与 NIS 一起使用,请在 /etc/autofs/auto.master 中的模板名称前添加 yp:
/home yp:auto_home --timeout=60 /sbtn yp:auto_sbtn --timeout=60 +auto.master
在早期版本的 NIS (5.0.4 之前) 中,您应该在 /etc/nsswitch.conf 中添加 nis
automount: files nis
可选参数
您可以在 /etc/default/autofs 中为所有 AutoFS 媒体设置系统范围的参数,例如 timeout
- 打开文件
/etc/default/autofs并编辑OPTIONS行OPTIONS='--timeout=5'
- 要启用日志记录 (默认不记录任何内容),请在
/etc/default/autofs中取消注释OPTIONS行并添加--verbose,例如OPTIONS='--verbose --timeout=5'
重新启动 autofs 守护进程后,可以在 单元状态 或 日志 中看到详细输出。
识别多个设备
如果您使用多个 USB 驱动器/密钥,并希望轻松区分它们,您可以使用 AutoFS 设置挂载点,并使用 Udev 为您的 USB 驱动器创建不同的名称。有关设置 Udev 规则的说明,请参阅 udev#Setting static device names。
AutoFS 权限
如果 AutoFS 对您不起作用,请确保模板文件的权限正确,否则 AutoFS 将无法启动。如果您以未保留文件模式的方式备份了配置文件,可能会出现这种情况。以下是配置文件应具有的模式
0644—/etc/autofs/auto.master0644—/etc/autofs/auto.media0644—/etc/autofs/auto.misc0644—/etc/conf.d/autofs
一般而言,脚本 (如之前的 auto.net) 应设置 可执行 位,而挂载列表不应设置。
如果您在 /var/log/daemon.log 中看到类似以下的错误,则存在权限问题
May 7 19:44:16 peterix automount[15218]: lookup(program): lookup for petr failed May 7 19:44:16 peterix automount[15218]: failed to mount /media/cifs/petr
fusermount 问题
使用某些版本的 util-linux,您可能无法卸载由 autofs 挂载的 fuse 文件系统驱动器,即使您使用了 user= 选项。请参阅 邮件列表上的讨论。
调试自动挂载问题
为了更好地调试,请尝试在前台运行 automount。
停止 autofs.service,然后运行
# automount -f -v
或者,如果您想要更多调试信息,可以尝试
# automount -f --debug
AutoFS 的替代方案
- systemd 可以在需要时自动挂载文件系统;请参阅 fstab#Automount with systemd 了解描述,以及关于 sshfs 的文章,其中包含一个示例。
- Thunar Volume Manager 是 Thunar 文件管理器用户的自动挂载系统。
- PCManFM 是一个内置远程共享访问支持的轻量级文件管理器。
- Udisks 是一个极简的自动磁盘挂载服务。
参见
- AutoFS 的 FTP 和 SFTP 使用基于此 Gentoo Wiki 文章: https://web.archive.org/web/20130414074212/http://en.gentoo-wiki.com/wiki/Mounting_SFTP_and_FTP_shares
- 更多关于 SSH 的信息可以在本 wiki 的 SSH 和 使用 SSH 密钥 页面找到。
- Ubuntu 的 Autofs 帮助 wiki 位于 https://help.ubuntu.com/community/Autofs
- 有关文件系统特定的挂载选项,请参阅 mount(8) § FILESYSTEM-SPECIFIC MOUNT OPTIONS
- 有关 fuse 特定的挂载选项,请参阅 mount.fuse(8)