Mounting a file system in Linux refers to attaching a file system (such as a partition on a hard disk, a USB drive, or a network file system) to a directory structure so that the user can access it. A mounted file system becomes part of the overall file hierarchy and can be accessed just like any other directory.
Basic Concepts:
- Mount Point:
- A mount point is the directory where a file system is attached. For example, when you mount a USB drive at
/mnt/usb
, you can access the contents of the USB drive by navigating to that directory.
- A mount point is the directory where a file system is attached. For example, when you mount a USB drive at
- Mount Command:
- The
mount
command is used to attach file systems to the directory tree.
- The
- Unmount Command:
- The
umount
command is used to safely detach the file system before removing or disconnecting it.
- The
Mounting a File System
To mount a file system, you need to specify:
- Device name: The partition or disk to be mounted (e.g.,
/dev/sda1
). - Mount point: The directory where you want to attach the file system (e.g.,
/mnt/disk
).
Example: Mounting a Partition
Create a Mount Point:
Before mounting a file system, create a directory that will serve as the mount point (if it doesn’t already exist):
sudo mkdir /mnt/mydisk
Mount the File System:
Use the mount
command to mount the file system to the mount point:
sudo mount /dev/sda1 /mnt/mydisk
/dev/sda1
: The partition to be mounted./mnt/mydisk
: The directory where the file system will be accessible.
Check Mounted File Systems:
To verify that the file system has been mounted successfully, you can use the df
or mount
command:
df -h
or
mount | grep /dev/sda1
Access the Mounted File System: Once mounted, you can access the contents of the file system by navigating to the mount point:
cd /mnt/mydisk ls
Unmounting a File System
To safely remove the file system, it’s important to unmount it first:
Unmount the File System: After unmounting, you will no longer be able to access the file system from the mount point.
sudo umount /mnt/mydisk
Check Unmounted File Systems: To ensure the file system has been successfully unmounted, use:
mount | grep /mnt/mydisk
If no output is shown, the file system has been unmounted.
Persistent Mounting with /etc/fstab
If you want a file system to be automatically mounted at boot time, you can add an entry to the /etc/fstab
file. This file defines file systems and their mount points.
Example fstab
Entry:
/dev/sda1 /mnt/mydisk ext4 defaults 0 0
- /dev/sda1: The partition to be mounted.
- /mnt/mydisk: The mount point.
- ext4: The file system type.
- defaults: Default mount options.
- 0 0: Dump and fsck options (used for backup and consistency checks).
Lab Exercise: Mounting and Unmounting File Systems
Objective:
Learn how to mount and unmount file systems manually and automatically using /etc/fstab
.
Steps:
Check Existing Partitions: Use the lsblk
or fdisk
command to list available partitions:
lsblk
or
sudo fdisk -l
Create a Mount Point:
sudo mkdir /mnt/disk
Mount the Partition: Mount a partition (e.g., /dev/sdb1
) to the mount point /mnt/disk
:
sudo mount /dev/sdb1 /mnt/disk
Access the File System: Navigate to the mount point and view the files:
cd /mnt/disk ls
Unmount the File System: After accessing the files, unmount the partition:
sudo umount /mnt/disk
Add to /etc/fstab
for Automatic Mounting: Edit the /etc/fstab
file to include an entry for automatic mounting:
sudo nano /etc/fstab
Add a line similar to:
/dev/sdb1 /mnt/disk ext4 defaults 0 0