Before you can access the files on a storage device, you must first mount its filesystem to a directory on your system. This process involves a device location, a filesystem type, and a mount point. The mount point is simply an existing directory where the filesystem will be attached.
How to Mount a Filesystem
First, you need to create a mount point. Let's create a directory for this purpose:
sudo mkdir /mydrive
With the mount point ready, you can use the mount command to attach your device. The -t flag specifies the filesystem type.
sudo mount -t ext4 /dev/sdb2 /mydrive
It's as simple as that! Now, if you navigate to the /mydrive directory, you will see the contents of your device's filesystem.
How to Unmount a Filesystem in Linux
When you are finished with a device, you should unmount it to ensure all data is safely written and the filesystem is cleanly detached. The standard command for this operation in Linux is umount. To perform a linux unmount, you can specify either the mount point or the device name.
Using the mount point:
sudo umount /mydrive
Alternatively, using the device name:
sudo umount /dev/sdb2
It is best practice to use sudo umount to ensure you have the necessary permissions to detach the filesystem. This command is universal across Linux distributions, so the same syntax applies whether you are on Ubuntu, Fedora, or performing a debian umount. Note that you cannot umount a device if it is currently in use (e.g., if a file is open or your current working directory is on the device).
Using UUIDs for Stable Mounting
The kernel names devices in the order it discovers them, which means a device name like /dev/sdb2 could change between reboots. To avoid issues, you can use a device's universally unique ID (UUID), which remains constant.
To view the UUIDs for your block devices, use the blkid command:
pete@icebox:~$ sudo blkid
/dev/sda1: UUID="130b882f-7d79-436d-a096-1e594c92bb76" TYPE="ext4"
/dev/sda5: UUID="22c3d34b-467e-467c-b44d-f03803c2c526" TYPE="swap"
/dev/sda6: UUID="78d203a0-7c18-49bd-9e07-54f44cdb5726" TYPE="xfs"
This output shows device names, their filesystem types, and their corresponding UUIDs. You can then mount a device using its UUID:
sudo mount UUID=130b882f-7d79-436d-a096-1e594c92bb76 /mydrive
While you won't always need to mount devices via their UUIDs, it is the recommended method for automatically mounting filesystems at startup, such as a secondary hard drive. We will cover that process in the next lesson.