Linux 文件系统挂载

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

Introduction

In the heart of a mystical night market, hidden among various arcane stalls, there lies a secluded tent draped with shadows and whispers. Within this secret haven, a revered collector of magical artifacts conducts their clandestine business, exchanging enchantments for the right price. This collector, a known conjuror of digital spells, has an array of esoteric knowledge that they are ready to impart on an apprentice willing to learn the arcane arts of Linux File System Mounting.

Your quest, should you choose to accept it, is to dive into the depths of the Linux file system, mastering the magical rites of mounting and unmounting drives, unveiling hidden directories, and preserving precious data. Through this journey, your objective is to become adept in the knowledge and manipulation of file systems, a skill highly valued by the enigmatic collector.

The allure of the night market waits, and the hidden trove of knowledge is ready to be unlocked. Will you rise to become a master of mounts and file systems? The collector awaits your presence.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/mount -.-> lab-271335{{"`Linux 文件系统挂载`"}} end

Preparing the Mounting Spell

In this step, you will learn the basic incantations necessary to mount a file system. Before embarking on this magical journey, you must create a mystical storage device, known in the common tongue as a "loopback device", to simulate the experience of mounting physical storage.

First, navigate to the tent's workspace:

cd ~/project

Now, conjure a blank file that will serve as your enchanted device:

dd if=/dev/zero of=magic_storage.img bs=1M count=100

This spell will create a 100MB file named magic_storage.img, which will act as your storage device. Explain to the students that dd is a powerful utility for copying and converting data, and in this context, it is generating a file filled with zeros.

Next, you must create a file system on this device:

mkfs.ext4 magic_storage.img

Once created, prepare a secret chamber where you will mount this device:

mkdir ~/project/magic_mount

Now, perform the mounting ritual:

sudo mount magic_storage.img ~/project/magic_mount

With the device mounted, use df -hT to verify that your device appears in the list of mounted file systems.

Invoking the Mounting Charms

Now that you are familiar with the initial rites, it's time to enhance your technique by mounting with additional options. You will explore the 'noexec' and 'ro' (read-only) incantations.

Begin by unmounting the previous magical device:

sudo umount ~/project/magic_mount

Now, remount the enchanted device with the 'noexec' restriction, preventing the execution of any binaries from within your mount:

sudo mount -o noexec magic_storage.img ~/project/magic_mount

And prove that it worked:

cd ~/project/magic_mount

Let's switch to the root user:

sudo su -

Now, attempt to execute a script from within the mount:

echo "echo Hello, magical world\!" > hello.sh
chmod +x hello.sh
./hello.sh || echo "Spellbinding\! Execution is prevented\!"

Attempt to create a file in the read-only mount to show that it's indeed immutable:

sudo touch ~/project/magic_mount/test_file && echo "File creation permitted" || echo "File creation blocked by read-only protection"

引言

在一个神秘的夜市深处,隐藏在各种神秘的摊位之间,有一个被阴影和低语笼罩的隐秘帐篷。在这个秘密的庇护所里,一位备受尊敬的魔法器物收藏家进行着他们的秘密交易,以合适的价格交换魔法物品。这位收藏家是一位著名的数字咒语施法者,他们拥有一系列深奥的知识,准备传授给愿意学习 Linux 文件系统挂载(Mounting)这一神秘艺术的学徒。

你的任务,如果你选择接受它,就是深入 Linux 文件系统的深处,掌握挂载和卸载驱动器的魔法仪式,揭示隐藏的目录,并保护珍贵的数据。通过这段旅程,你的目标是熟练掌握文件系统的知识和操作,这是一项被这位神秘收藏家高度重视的技能。

夜市的诱惑在等待,隐藏的知识宝库已经准备好被解锁。你是否愿意崛起,成为挂载和文件系统的大师?收藏家正等待着你的到来。


准备挂载咒语

在这一步中,你将学习挂载文件系统所需的基本咒语。在踏上这段魔法之旅之前,你必须创建一个神秘的存储设备,通常称为“回环设备”(loopback device),以模拟挂载物理存储的体验。

首先,导航到帐篷的工作区:

cd ~/project

现在,召唤一个空白文件,它将作为你的魔法设备:

dd if=/dev/zero of=magic_storage.img bs=1M count=100

这个咒语将创建一个名为 magic_storage.img 的 100MB 文件,它将作为你的存储设备。向学生们解释,dd 是一个强大的工具,用于复制和转换数据,在这里,它生成了一个充满零的文件。

接下来,你必须在这个设备上创建一个文件系统:

mkfs.ext4 magic_storage.img

创建完成后,准备一个秘密的房间,你将在这里挂载这个设备:

mkdir ~/project/magic_mount

现在,执行挂载仪式:

sudo mount magic_storage.img ~/project/magic_mount

设备挂载后,使用 df -hT 来验证你的设备是否出现在已挂载文件系统的列表中。


施展挂载魔法

现在你已经熟悉了初始的仪式,是时候通过使用额外的选项来提升你的技巧了。你将探索 noexecro(只读)咒语。

首先,卸载之前的魔法设备:

sudo umount ~/project/magic_mount

现在,使用 noexec 限制重新挂载魔法设备,防止从挂载点内执行任何二进制文件:

sudo mount -o noexec magic_storage.img ~/project/magic_mount

并验证它是否生效:

cd ~/project/magic_mount

切换到 root 用户:

sudo su -

现在,尝试从挂载点内执行一个脚本:

echo "echo Hello, magical world\!" > hello.sh
chmod +x hello.sh
./hello.sh || echo "Spellbinding\! Execution is prevented\!"

尝试在只读挂载中创建一个文件,以证明它确实是不可变的:

sudo touch ~/project/magic_mount/test_file && echo "File creation permitted" || echo "File creation blocked by read-only protection"

总结

在这个实验中,我们穿越了 Linux 文件系统挂载的秘密路径,引导参与者进入挂载、设备和文件系统操作的数字传说。挂载挑战被精心设计在一个引人入胜的叙事中,以激发参与者的兴趣和好奇心。

参与者通过创建回环设备、使用不同选项挂载和卸载文件系统、理解 noexecro 的后果,以及熟悉基本的命令行工具,获得了实际操作经验。

在受控环境中观察文件挂载的魔法,使参与者能够更深入地理解与 Linux 文件系统交互时所需的平衡和预防措施,确保他们在离开实验时不仅知识更加丰富,而且对 Linux 魔法的世界充满敬畏。

您可能感兴趣的其他 Linux 教程