- Регистрация
- 9 Май 2015
- Сообщения
- 1,483
- Баллы
- 155

Hey there, Linux adventurers!

This post will demystify the essential concepts of mounting and unmounting filesystems in Linux, equipping you with the knowledge to control your storage like a pro. Get ready to connect and disconnect with confidence!


In Linux (and Unix-like systems), everything is a file. But how do your separate storage devices (like hard drives, USB sticks, network shares, or even CD/DVDs) become part of this unified file system tree? That's where mounting comes in!
Think of mounting as connecting a new branch (your storage device's filesystem) to an existing tree (your Linux directory structure). This connection point is called a mount point, which is simply an empty directory on your existing filesystem. Once mounted, the contents of the storage device become accessible through this mount point.
Key Concepts:
- Filesystem: The structure and organization of data on a storage device (e.g., ext4, XFS, NTFS, FAT32).
- Mount Point: An empty directory where a filesystem is attached.
- Device File: A special file in the /dev directory (e.g., /dev/sda1, /dev/sdb) that represents a physical storage device or partition.
- /etc/fstab: A configuration file that defines static filesystems to be mounted automatically at boot time.
- Accessing Data: It's the only way to read from or write to a storage device.
- System Organization: Integrates diverse storage into a single, logical file hierarchy.
- Flexibility: Allows you to add and remove storage dynamically.

The mount command is your primary tool for attaching filesystems. It tells the kernel to make the filesystem on a specified device available at a specific mount point.
Major Options/Commands for mount:
Command | Description | Example |
---|---|---|
mount (without arguments) | Show all currently mounted filesystems. | mount |
mount -t <fstype> <device> <mount_point> | Mount a device with a specified filesystem type. | sudo mount -t ext4 /dev/sdb1 /mnt/mydata |
mount <device> <mount_point> | Mount a device (kernel tries to guess fstype). | sudo mount /dev/sdb1 /mnt/mydata |
mount -a | Mount all filesystems listed in /etc/fstab. | sudo mount -a |
mount -o ro <device> <mount_point> | Mount device as read-only. | sudo mount -o ro /dev/cdrom /media/cdrom |
mount -o remount,rw <mount_point> | Remount an already mounted filesystem as read-write. | sudo mount -o remount,rw /mnt/mydata |
mount -o loop <image.iso> <mount_point> | Mount an ISO image as a loop device. | sudo mount -o loop my_distro.iso /mnt/iso |
mount --bind <old_dir> <new_dir> | Bind mount: make the contents of one directory appear elsewhere. | sudo mount --bind /var/log /mnt/log |
-v | Verbose output (show what's being done). | sudo mount -v /dev/sdb1 /mnt/usb |
- Permissions: You often need sudo (root privileges) to use mount, especially for physical devices.
- Empty Mount Point: The target directory must exist and typically should be empty before mounting. If it contains files, those files will be hidden as long as the device is mounted.
- Autodetection: If you omit -t <fstype>, mount will try to automatically detect the filesystem type. This usually works well for common types.

Just as important as mounting is unmounting. When you're done with a device, you must unmount it properly before physically disconnecting it. Unmounting flushes any buffered data to the device and updates the filesystem's metadata, preventing data corruption.
Major Options/Commands for umount:
Command | Description | Example |
---|---|---|
umount <mount_point> | Unmount the filesystem at the specified mount point. | sudo umount /mnt/mydata |
umount <device> | Unmount the filesystem on the specified device. | sudo umount /dev/sdb1 |
umount -a | Unmount all filesystems listed in /etc/mtab (currently mounted). | sudo umount -a (use with extreme caution! ![]() |
umount -f <mount_point> | Force unmount (use only when necessary and with caution! ![]() | sudo umount -f /mnt/stuck_drive |
umount -l <mount_point> | Lazy unmount: detach the filesystem from the hierarchy now, clean up later. | sudo umount -l /mnt/usb_drive |
- Data Integrity: Prevents data loss or corruption by ensuring all pending writes are completed.
- Resource Release: Frees up system resources tied to the mounted filesystem.
- Safe Removal: Allows safe physical removal of external devices.
Often, umount fails because the filesystem is "busy." This means some process is still using files or directories within the mount point.
- Identify Busy Processes: Use lsof <mount_point> or fuser -m <mount_point> to find processes using the mount point.
- Kill Processes (Carefully!): Terminate identified processes (e.g., sudo kill <PID>) or close applications accessing the mount point.
- Change Directory: Ensure your current working directory (and any open terminals) are outside the mount point.

For filesystems that you want to be available every time your system boots, you can configure them in the /etc/fstab file. This file acts as a static table of filesystems.
/etc/fstab Entry Structure:
Each line in /etc/fstab describes a single filesystem and follows this format:
- <device>: The device to mount (e.g., /dev/sda1, UUID=..., LABEL=...). Using UUID or LABEL is recommended for robustness as device names can change.
- <mount_point>: The directory where the device will be mounted.
- <filesystem_type>: The type of filesystem (e.g., ext4, xfs, ntfs, auto).
- <options>: Mount options (comma-separated, no spaces). Common options include:
- defaults: Equivalent to rw, suid, dev, exec, auto, nouser, async.
- auto: Mount at boot (mount -a).
- noauto: Only mount manually.
- rw: Read-write access.
- ro: Read-only access.
- user: Allows any user to mount/unmount.
- nouser: Only root can mount/unmount (default).
- exec: Allow execution of binaries.
- noexec: Disallow execution of binaries (security).
- sync: All I/O to the filesystem is done synchronously.
- async: All I/O to the filesystem is done asynchronously.
- nofail: Do not report errors for this device if it does not exist.
- <dump>: Used by the dump command for backups (0 for no backup, 1 for dump). Usually 0.
- <pass>: Determines the order of filesystem checks at boot by fsck. 0 means no check. Root filesystem (/) should be 1. Other filesystems 2.
UUID=1234abcd-5678-90ef-abcd-1234567890ab /data ext4 defaults 0 2
Before editing /etc/fstab, always back it up! A mistake can prevent your system from booting.

- mount: For temporary mounts, checking current mounts, or mounting filesystems not configured in /etc/fstab (like USB drives, ISOs, network shares).
- umount: To safely disconnect any mounted filesystem. Always use this before physically removing external media!
- /etc/fstab: For permanent, automatic mounting of filesystems at system startup (e.g., dedicated data partitions, network storage that's always available).

Understanding mounting and unmounting is fundamental to navigating the Linux filesystem and managing your storage effectively. From connecting a simple USB stick to configuring complex server storage, these commands are your gateway. Always remember to umount before unplugging, and explore /etc/fstab for persistent storage solutions!
What's your most memorable mounting or unmounting adventure? Share your tips and tricks in the comments below!

Источник: