CodeGym /Courses /Docker SELF /Mounting and Unmounting File Systems: mount, umount

Mounting and Unmounting File Systems: mount, umount

Docker SELF
Level 6 , Lesson 3
Available

Mounting and Unmounting File Systems: mount, umount

1. What is Mounting?

Welcome to another exciting lecture on working with Linux! Today, we’re gonna talk about mounting and unmounting file systems — the process that opens the door to working with USB drives, additional hard disks, and even network storage. We’ll learn how to set up auto-mounting so your data’s ready to go right when your system boots up. And most importantly, we’ll try it all out hands-on!

Mounting is the process of attaching a device’s file system (hard disk, USB drive, ISO image) to the Linux file system tree. Unlike Windows, where you see devices as "D:\" or "E:\", in Linux all devices are "integrated" into the file structure at a specific folder, called the mount point.

Analogy: Train and Station

Imagine a train (device) and a railway station (mount point). For people to be able to exit the train and start using the infrastructure (files), the train needs to arrive at the platform and dock. That’s pretty much how mounting a device to its point works in Linux.


2. The mount Command

Let's start with the command that performs the mounting — mount. This is one of those commands you'll use regularly, especially if you're working with external drives or network storage.

Main Syntax

mount [options] <device> <mount point>
  • <device> — this is where your device is connected in the system, for example, /dev/sdb1. You can find it using the lsblk command.
  • <mount point> — the folder where the device's data will be accessed. For example, this could be /mnt/disk.

Example of connecting a USB drive:

sudo mount /dev/sdb1 /mnt/usb

After running the command, the files from the device /dev/sdb1 will be accessible inside the folder /mnt/usb.

Useful Options for the mount Command

  1. Specifying the file system:

    If Linux doesn’t recognize the device's file system, use the -t flag. For example:

    sudo mount -t ext4 /dev/sdb1 /mnt/usb
    

    This is handy if you're working with rare file systems.

  2. Mounting as read-only:

    If you want to protect the device's data from changes, use the -o ro option:

    sudo mount -o ro /dev/sdb1 /mnt/usb
    
  3. Mounting with character encoding:

    For FAT32 or NTFS drives, sometimes you need to specify the character encoding:

    sudo mount -o iocharset=utf8 /dev/sdb1 /mnt/usb
    

3. The umount Command

Now that you've mounted the device, the question arises: how do you unmount it? It's important so Linux "closes" all connections to the disk and finishes writing.

Main Syntax

umount <device or mount point>

Here's an example for an already mounted USB drive:

sudo umount /mnt/usb

Or you can specify the device itself:

sudo umount /dev/sdb1

What to Do If umount Doesn't Work?

Sometimes the umount command might return an error: "Device is busy". This means someone (in file system terms) is "sitting on the tracks." Maybe you or another program currently have a file open on that device.

In such cases, the fuser command can help and show processes blocking the device:

fuser -v /mnt/usb

Once you've found the culprit, you can terminate the process:

kill <process ID>

After that, retry the umount command.

Important!

Remember in Windows, before ejecting a flash drive, you had to click the Eject button? That's essentially performing an unmount command.


4. Auto-mounting with /etc/fstab

Manual mounting is handy, but what do you do if you need the device to connect every time the system boots up? That's where the /etc/fstab file comes in.

What is /etc/fstab?

It's a config file that has a list of devices Linux should auto-mount. With fstab, you can set parameters like file system type, mount point, and access rights.

How to add a device to /etc/fstab?

  1. Find your device's UUID using the blkid command:

    sudo blkid
    

    For example, the device /dev/sdb1 might have a UUID like 1234-5678.

  2. Open the /etc/fstab file for editing:

    sudo nano /etc/fstab
    
  3. Add a new line:

    UUID=1234-5678 /mnt/usb ext4 defaults 0 2
    
    • UUID — the unique identifier of your device.
    • /mnt/usb — the mount point.
    • ext4 — the file system of the device.
    • defaults — standard mount parameters.
    • 0 and 2 — parameters for checking the device on boot (customizable).
  4. Test the configuration by mounting the device:

    sudo mount -a
    

Now the device will auto-mount when the system reboots.

Risks

⚠️ If you configure /etc/fstab incorrectly, your system might not boot. Always double-check the configuration using:

sudo mount -a

If there are no errors, you're good to go.


5. Example: Mounting and Unmounting a USB Drive

Let's say you plugged in a USB drive, and the system detected it as /dev/sdb1.

  1. Create a mount point:

    sudo mkdir -p /mnt/usb
    
  2. Mount the drive:

    sudo mount /dev/sdb1 /mnt/usb
    
  3. Check the contents:

    ls /mnt/usb
    
  4. When done, unmount the device:

    sudo umount /mnt/usb
    

Now the device is safely disconnected, and you can remove it.


6. Practical Assignment

Task

  1. Connect a USB drive or virtual disk.
  2. Create a mount point /mnt/test-disk.
  3. Mount the device to this folder and check its contents.
  4. Set up mounting through /etc/fstab using UUID.
  5. Disconnect the device and make sure the settings work after a reboot.

Hints

  • If there is no device, create one using the formatting command from the previous lecture (mkfs.ext4).
  • Check the fstab settings before rebooting.

Now you know how to mount and unmount devices in Linux, automate their connection through the /etc/fstab configuration, and safely diagnose any issues. Mounting is an important step for comfortable data handling, and this knowledge will be useful both in home projects and on a work server.

1
Task
Docker SELF, level 6, lesson 3
Locked
Mounting a device
Mounting a device
1
Task
Docker SELF, level 6, lesson 3
Locked
Specifying the file system type during mounting
Specifying the file system type during mounting
1
Task
Docker SELF, level 6, lesson 3
Locked
Mounting with additional parameters
Mounting with additional parameters
1
Task
Docker SELF, level 6, lesson 3
Locked
Setting up automatic mounting via `/etc/fstab`
Setting up automatic mounting via `/etc/fstab`
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION