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(目录名称是服务器的主机名),其 NFS 共享名为 /home/share
,您只需键入即可访问该共享
# cd /net/fileserver/home/share
/etc/conf.d/autofs
中删除该选项,以便启动 AutoFS 守护程序。-hosts
选项使用类似于 showmount
命令的机制来检测远程共享。 您可以通过键入来查看导出的共享
# showmount servername -e
手动 NFS 配置
要在位置 /mnt/foo
上为 file_server 将 NFS 共享挂载到 /srv/shared_dir
,添加一个新的配置,例如 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
创建一个包含 fuse
的 /etc/modules-load.d/fuse.conf
文件,以便在每次系统启动时加载它。
接下来,在 /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 访问远程文件系统 over 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
媒体传输协议 (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 之前)中,您应该将 nis
添加到 /etc/nsswitch.conf
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#设置静态设备名称。
AutoFS 权限
如果 AutoFS 对您不起作用,请确保模板文件的权限正确,否则 AutoFS 将无法启动。 如果您以未保留文件模式的方式备份了配置文件,则可能会发生这种情况。 以下是配置文件应具有的模式
0644
—/etc/autofs/auto.master
0644
—/etc/autofs/auto.media
0644
—/etc/autofs/auto.misc
0644
—/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,即使您使用 user=
选项,也可能无法卸载 autofs 挂载的 fuse 文件系统驱动器。 请参阅邮件列表上的讨论。
调试自动挂载问题
为了更好地进行调试,请尝试在前台运行 automount。
停止 autofs.service
,然后运行
# automount -f -v
或者,如果您想要更多调试信息,请尝试
# automount -f --debug
AutoFS 的替代方案
- Systemd 可以按需自动挂载文件系统; 有关描述,请参阅此处,有关示例,请参阅关于 sshfs 的文章。
- Thunar Volume Manager 是为 Thunar 文件管理器的用户提供的自动挂载系统。
- PCManFM 是一款轻量级文件管理器,内置了对访问远程共享的支持
- Udisks 是一种极简的自动磁盘挂载服务
参见
- FTP 和 SFTP 与 AutoFS 的用法基于这篇 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)