How to Mount and Unmount File Systems in Linux

When you add a new disk to your system Linux, you need to create partitions with tools like fdisk or gparted, then you need to create file systems with mkfs (ext3, ext4, XFS …. ). There is one last step before you can use these partitions: mounting with the command mount.

In this article we will see how to Mount and Unmount File Systems in Linux.

Why we need to mount a file system?

In order to use a partition that you have just formatted, you must create a mount point through which you can access your file system and store folders and files.

mount command’s syntax :

To mount a file system, simply type the following command :

mount -t FS_type -o options device mount_point

FS_type : file system type of the partition you want to mount.

o options : you can mount a partition with options like read-only options.

– device : this is the device you want to mount (devices are located in /dev).

– mount point : this is the directory through which you access your file system.

Mounting a partition

We Suppose you want to mount the /dev/sda1 partition whose file system is ext3.

You must first create the directory via which you will access your partition, in my case it is via the directory /data :

# mkdir /data

Then run the following command :

# mount -t ext4 /dev/sda1 /data

Now we can use our partition and access it via the /data directory.

If you do not specify the file system of the partition (here ext4) the command will try to detect it.

Mounting with label

You can also mount a file system by LABELS instead of device names by using the -L parameter :

# mount -t ext3 -L DATA /data

Mounting with UUID

Each file system has a unique identifier called UUID (Universal Unique Identifier). you can also mount a file system using its UUID :

# mount -t ext4 -U 97f654e7-122e-4188-9j71-355fb8db28ie /data

To find out the UUID of your file system you can list the contents of /dev/disk/by-uuid which contains UUID symbolic links pointing to the corresponding device file :

# ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 mar 18 14:00 47DF-9209 -> ../../sdb5
lrwxrwxrwx 1 root root 10 mar 18 14:00 67f6e4b8-635c-4103-9a81-
877fb7db29fe -> ../../sdb1
lrwxrwxrwx 1 root root 10 mar 3 09:23 a1cc2282-b6f4-46e1-bc94-
91585f1c5872 -> ../../sda5
lrwxrwxrwx 1 root root 10 mar 3 09:23 C2B072B5B072AF91 -> ../../sda1
lrwxrwxrwx 1 root root 10 mar 3 09:23 c56b96b5-e52f-453a-ba9daa1df6f0c3c0
-> ../../sda7
lrwxrwxrwx 1 root root 10 mar 3 09:23 dd5e92d3-b931-4c18-91a3-
5edccc57ced9 -> ../../sda6

Mounting options :

file systems can be mounted with options that modify its behavior.

For exemple :

The command below mounts the file system as read-only with the ro option:

# mount -t ext3 -o rw /dev/sda1 /data

Here are some other options :

defaults : includes the rw, suid, dev, exec, auto,nouser, et async options.

sync/async : Enables or disables synchronous writes. With async the writes go through a buffer that defers the writes (more efficient) making the hand faster. It is preferable to enable synchronous writes on external media (USB keys, USB/Firewire/eSATA disks, etc.).

exec/noexec : Allows the execution or not of binary files on the device.

auto/noauto : The file system is automatically mounted/can only be mounted explicitly (see fstab file).

user/nouser : Any user can mount the file system (involves noexec, nosuid, and nodev)/only root has the right to mount the file system.

rw: mount the file system as read and write.

Acl : Enables the use of Access Control Lists.

remount option

The remount option is very useful, it allows to apply an option to a file system without having to unmount and remount it :

For example, I have a file system that I have mounted in ro and I want to create files on it so I need it to be mounted in rw.

instead of unmounting the file system and remounting it with the rw option I use the remount option so that the change is applied immediately.

In addition, there is no need to write the whole line, just specify the device or the mount point :

# mount -o rw,remount /data

unmount a file system

To unmount a file system, detach the file system from the mount point, use the umount command :

# umount /data

If the file system is in use, the system cannot be unmounted

# umount /mnt/data
umount: /mnt/DATA: device is in use

You can use the lsof command to find out what is occupying the file system :

# lsof /data
gzip 1563 jean cwd DIR 8,17 4096 5 /mnt/data

As you can see, the gzip command running by the user jean is using a file from the file system /data.

If you want to force the process using the mount point to stop, use the command fuser :

# fuser -km /data

To see this article in French, click here.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *