通过玩/ etc / fstab来控制Linux上的文件系统挂载

Controlling the mounting of filesystems is a useful technique for managing Linux systems. The mounting configurations are mostly in the /etc/fstab file. In this post, we will discuss 2 common and useful techniques for controlling the filesystem mounting by playing with the /etc/fstab file: allowing non-root users to mount/unmount filesystems and avoiding mounting failures blocking the booting of Linux.

控制文件系统的挂载是管理Linux 系统的有用技术。 安装配置大部分位于/ etc / fstab文件中。 在本文中,我们将讨论通过使用/ etc / fstab文件来控制文件系统挂载的2种常见和有用的技术:允许非root用户挂载/卸载文件系统,并避免挂载失败阻止Linux的启动。

简要介绍了/ etc / fstab中 (A brief introduction to /etc/fstab )

First, let’s take a brief look at the /etc/fstab file and the format. For more details, please check the fstab man page. If you are already familiar with fstab format, you can skip this part.

首先,让我们简要地看一下/ etc / fstab文件及其格式。 有关更多详细信息,请检查fstab手册页 。 如果您已经熟悉fstab格式,则可以跳过此部分。

The fstab contains descriptive information about the file systems. Each filesystem in the system is described on a line with fields separated by tabs or spaces in the fstab file. Lines starting with ‘#’ are comments and blank lines are ignored.

fstab包含有关文件系统的描述性信息。 系统中的每个文件系统在一行中都有描述,其字段由fstab文件中的制表符或空格分隔。 以“#”开头的行是注释,空白行将被忽略。

Each line has 7 fields. The first field is fs_spec which describes the block special device or remote filesystem to be mounted. The second field is fs_file which describes the mount point for the filesystem. The third field is fs_vfstype which describes the type of the filesystem. The fourth field fs_mntops describes the mount options associated with the filesystem formatted as a comma separated list of options. The fifth field is fs_freq which is used for these filesystems by the dump command to determine which filesystems need to be dumped. And the sixth field fs_passno is used by the fsck program to determine the order in which filesystem checks are done at reboot time.

每行有7个字段。 第一个字段是fs_spec ,它描述要安装的块特殊设备或远程文件系统。 第二个字段是fs_file ,它描述文件系统的安装 。 第三个字段是fs_vfstype ,它描述文件系统的类型。 第四个字段fs_mntops描述与文件系统相关联的安装选项,这些文件格式的格式为逗号分隔的选项列表。 第五个字段是fs_freqdump命令将其用于这些文件系统,以确定需要dump哪些文件系统。 fsck程序使用第六个字段fs_passno来确定重新引导时文件系统检查的执行顺序。

Overall, the line for representing a filesystem is formated as

总体而言,代表文件系统的行的格式为

fs_spec fs_file fs_vfstype fs_mntops fs_freq fs_passno

With the basic understand of the fstab, let’s go to the 2 techniques.

在基本了解fstab的基础上,让我们看一下这两种技术。

允许非root用户装载和卸载文件系统 (Allow non-root users to mount and unmount filesystems )

Usually, only root user can mount/unmount filesystems. It will be useful to let normal users mount/umount some filesystems that are non-critical to the system and can be mounted/unmounted when the filesystems are needed without the help of system admins.

通常,只有root用户可以装载/卸载文件系统。 让普通用户挂载/卸载一些对系统不重要的文件系统,并且在不需要系统管理员的帮助下可以在需要文件系统时挂载/卸载,这将非常有用。

To make a filesystem such as an NFS filesystem localhost:/ mountable by normal users, we can use the user option as follows.

为了使普通用户可以挂载诸如NFS文件系统localhost:/类的文件系统,我们可以使用user选项,如下所示。

localhost:/ /mnt/netshare nfs noauto,user,rw,vers=3,proto=tcp,nolock,noacl,rw,suid,dev,exec,async 0 0

user in the fs_mntops field enables normal users to mount and umount the filesystem. Another option that is used together with user here is noauto that makes it not automatically mount the filesystem during boot or mount -a.

user fs_mntops字段中的fs_mntops可以使普通用户安装和卸载文件系统。 与user一起使用的另一个选项是noauto ,它使它在引导或mount -a期间不会自动挂载文件系统。

The rw,suid,dev,exec,async are those as the default mount fs_mntops except auto and nouser.

rw,suid,dev,exec,async是默认的挂载fs_mntops除了autonouser

defaults
    use default options: rw, suid, dev, exec, auto, nouser, and async.

The other options in the above example are those for the NFS filesystem.

上例中的其他选项是用于NFS文件系统的选项。

避免安装失败阻止Linux启动 (Avoid mounting failures blocking Linux booting )

One common problem that makes Linux fail to boot is the failure of mounting filesystems specified in the /etc/fstab file. Having to get to the fail to boot server and configure it is one of the things that lazy admins do not like. For non-critical filesystems of the system, mounting failures may be fine and the Linux system can boot without these filesystems. After the system boots, the admins can remotely re-configure/disable the filesystem without the need to go to the server room.

导致Linux无法启动的一个常见问题是/ etc / fstab文件中指定的挂载文件系统失败。 懒惰的管理员不喜欢无法启动服务器并对其进行配置,这是懒惰的管理员不喜欢的事情之一。 对于系统的非关键文件系统,挂载故障可能很好,并且Linux系统可以在没有这些文件系统的情况下启动。 系统启动后,管理员可以远程重新配置/禁用文件系统,而无需进入服务器室。

First, we add the noauto option introduced in the above technique to make the non-critical filesystems not mounted during boot time.

首先,我们添加了上述技术中引入的noauto选项,以使非关键文件系统在引导期间不会挂载。

As one option, you may mount the filesystem manually or by a crontab @reboot job after Linux boots. For example, @reboot mount /mnt/large-disk to mount /mnt/large-disk after booting.

作为一种选择,您可以在Linux启动后手动安装文件系统,也可以通过crontab @reboot作业安装文件系统。 例如, @reboot mount /mnt/large-disk mnt / large-disk在引导后挂载/ mnt / large-disk。

If you are using systemd, things will be easier and more convenient—systemd can automatically mount the filesystem when it is first used (yes, similar to the old good automount).

如果您正在使用systemd ,事情将会变得更加轻松和便捷-systemd可以在首次使用文件系统时自动挂载该文件系统(是的,类似于以前的老式automount )。

The option to be added is x-systemd.automount. One example is as follows.

要添加的选项是x-systemd.automount 。 一个例子如下。

UUID=DISK-UUID /mnt/large-disk ext4 noauto,x-systemd.automount,rw,suid,dev,exec,async 1 2

systemd will mount the ext4 filesystem to /mnt/large-disk at the first time /mnt/large-disk is accessed instead of at the system booting time.

systemd将在第一次访问/ mnt / large-disk而不是在系统引导时将ext4文件系统挂载到/ mnt / large-disk。

One more note: you may add nofail to the fstab entry. It is another useful option if the device specified in an entry may not exist during boot:

还有一点说明:您可以在fstab条目中添加nofail 。 如果在启动期间条目中指定的设备可能不存在,则这是另一个有用的选项:

nofail Do not report errors for this device if it does not exist.

nofail如果此设备不存在, 不要报告错误。

翻译自: https://www.systutorials.com/control-filesystem-mounting-on-linux-by-playing-with-etcfstab/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值