After creating disk partitions and formatting them properly, you may want to mount or unmount your drives.
On Linux, mounting drives is done via mountpoints on the virtual filesystem, allowing system users to navigate the filesystem as well as create and delete files on them.
In this tutorial, we are going to see how you can mount and unmount drives on your Linux system.
We are also going to see how you can amend the fstab file in order to make your mount settings permanent.
In order to mount drives and filesystems on Linux, you need to have sudo privileges on your machine.
To verify that you have sudo privileges, you can run the “sudo” command with the “-l” in order to list the privileges you currently own.
$ sudo -l User <user> may run the following commands on schkn-ubuntu: (ALL : ALL) ALL
Mount Drives on Linux
In order to mount drives on Linux, you have to use the “mount” command using the following syntax
$ sudo mount <device> <dir>
First of all, you need to check the disk partitions already created on your system that are not already mounted.
To list partitions with filesystems types, use the “lsblk” command with the “-f” option.
$ lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 ext4 0935df16-40b0-4850-9d47-47cd2daf6e59 sdb ├─sdb1 ext4 b9df59e6-c806-4851-befa-12402bca5828 /
Alternatively, you can use the “blkid” command in order to locate and list block devices on your system.
Unfortunately, this command does not list the current mountpoints used by your drives.
/dev/sda1: TYPE="ext4" PARTUUID="0935df16-40b0-4850-9d47-47cd2daf6e59" /dev/sdb1: UUID="b9df59e6-c806-4851-befa-12402bca5828" TYPE="ext4" PARTUUID="269f8db7-90b7-4ebe-acf4-521d6dc2eadb"
In this case, we want to mount the “sda1” partition on our filesystem.
To mount the “sda1” partition, use the “mount” command and specify the directory where you want it to be mounted (in this case, in a directory named “mountpoint” in the home directory.
$ sudo mount /dev/sda1 ~/mountpoint
If you did not get any error messages in the process, it means that your drive partition was successfully mounted!
Check Mounted Drives on Linux
In order to check that your drive partition was correctly mounted, you can use the “lsblk” and inspect the mountpoint column.
$ lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 ext4 0935df16-40b0-4850-9d47-47cd2daf6e59 /home/user/mountpoint sdb ├─sdb1 ext4 b9df59e6-c806-4851-befa-12402bca5828 /
Your drive partition was correctly mounted on the mountpoint, however there is one little gotcha that you need to be aware about when mounting drives.
Using the “mount” command does not make your mounts permanent : you need to add them to the fstab file for them to be permanent.
If you were to reboot your system without adding your mount settings to the “fstab” file, your drive partition would not be mounted on reboot.
Mounting Drives Permanently using fstab
The “fstab” file is a very important file on your filesystem.
Fstab stores static information about filesystems, mountpoints and several options that you may want to configure.
To list permanent mounted partitions on Linux, use the “cat” command on the fstab file located in /etc.
$ cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> UUID=b9df59e6-c806 / ext4 errors=remount-ro 0 1
The fstab contains multiple columns :
- Filesystem : you can either specify a UUID (for universal unique identifier), a label (if you chose a label for your disk), a network ID or a device name (which is not recommended at all);
- Mountpoint : the directory on the filesystem that you are going to use in order to access data stored on the disk;
- Filesystem type : the type of filesystem you use to format your disk;
- Options : some options that you can specify in order to tune your mount (“ro” for a read-only mount or “noexec” to prevent binary execution);
- Dump : in order to enable to disable filesystem dumping on the system (using the dump command);
- Pass Num : sets the order used in order for the “fsck” utility to check your filesystem. If you are not mounting the root device, you should set this option to “2” or “0” as “1” is reserved for the root device.
As you can see here, besides mounting the “sda1” partition in the previous section, only the partition with the UUID shown above would be mounted.
In order to get the partition associated with this UUID, you can use the use the “blkid” command in the following way.
$ blkid | grep <UUID>
Given the UUID of the fstab file, this would give us
$ blkid | grep b9df /dev/sdb1: UUID="b9df59e6"
Using the blkid command, I am able to know that the “sdb1” partition is mounted at boot time.
However, in this case, I need the “sda1” partition to be mounted on boot.
This is what we are going to configure in the next section.
Add Drive Partition to the fstab file
In order to add a drive to the fstab file, you first need to get the UUID of your partition.
To get the UUID of a partition on Linux, use “blkid” with the name of the partition you want to mount.
$ blkid /dev/sda1 /dev/sda1: UUID="0935df16-40b0-4850-9d47-47cd2daf6e59" TYPE="ext4" PARTUUID="7125fcc698a-01"
Now that you have the UUID for your drive partition, you can add it to the fstab file.
Open the /etc/fstab file and add one line for your new drive partition.
$ sudo nano /etc/fstab # <file system> <mount point> <type> <options> <dump> <pass> UUID=0935df16-40b0-48 /home/user/mountpoint ext4 defaults 0 0
Note : if you have trouble seeing entries using the blkid command, try using it using sudo. Some entries may be hidden for non-sudo users.
Save your file and your mount settings should be saved even if you reboot your system.
You successfully mounted disk drives on Linux and you saved your mount settings using the fstab file.
Listing mountpoints using findmnt
In order to find mountpoints and filesystems on Linux, you can use the findmnt command.
You can either specify the mountpoint or the device name with findmnt.
$ findmnt <device|mountpoint>
In our case, we are going to use findmnt to check if our device was correctly mounted on the target we specified in the fstab file.
$ findmnt /dev/sda1 TARGET SOURCE FSTYPE OPTIONS /home/devconnected/mountpoint /dev/sda1 ext4 rw,relatime
Mounting USB drives on Linux
Now that you you know how you can mount drives, let’s see how you can mount USB drives.
When inserting a USB drive into your computer, the first thing that you want to do is to identify the disk name you just inserted.
Identify USB drive name using fdisk
The easiest way to identify USB drive names is to use the “fdisk” command with a “-l” option for listing.
$ fdisk -l Disk /dev/sdc: 30 GiB, 35899345920 bytes, 167772160 sectors Disk model: STORE USB Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos
As you can see here, you are given the disk model when executing fdisk.
You can make sure that this is the correct drive by executing the “lsusb” command in order to list the USB devices on your machine.
$ lsusb Bus 001 Device 001: ID 1d6b:0001 Store USB
Now that you have identified your USB drive, it is time to mount it on the filesystem.
Make sure also to have a look at the partitions already created on your USB drive.
$ lsblk NAME FSTYPE LABEL UUID MOUNTPOINT sdc ├─sdc1 vfat USB FLASH <uuid>
Mount USB drive using mount
Mounting USB drives is not different from mounting normal hard drives on your computer.
In order to mount a USB drive, use the “mount” command and specify the device name you identified in the first section.
Create a mountpoint in your home directory (in this case named “usb“) and use it as a mountpoint.
$ mkdir -p /home/user/usb $ sudo mount /dev/sdc1 /home/user/usb $ cd /home/user/usb $ ls -l total 200 -rw------- 1 schkn schkn 32768 Apr 20 2019 file1 drwxrwxr-x 6 schkn schkn 4096 May 9 07:01 directory1 lrwxrwxrwx 1 schkn schkn 7 Aug 14 16:38 link1 drwxrwxr-x 2 schkn schkn 4096 Jan 5 2019 directory2
Great! You successfully mounted a USB drive on Linux using the mount command.
Mount USB drive at boot using fstab
As we already learned in the previous section, if you don’t have your device to the fstab file it won’t be mounted automatically.
For your USB drive to be mounted automatically, you need to identify the UUID of your USB drive.
$ sudo blkid /dev/sdc1 /dev/sdc1: LABEL="USB FLASH" UUID="1234-ABCE" TYPE="vfat"
Copy the UUID and add the following content to your /etc/fstab file.
$ sudo nano /etc/fstab # <file system> <mount point> <type> <options> <dump> <pass> UUID=1234-ABCE /home/user/usb vfat defaults 0 0
Save your file – your USB drive should now be mounted at boot!
Note : not sure about which filesystem to use to format your disk partition?
Read our disk formatting guide on Linux.
Unmounting drives on Linux using umount
On Linux, the easiest way to unmount drives on Linux is to use the “umount” command.
Note : the “umount” command should not be mispelled for “unmount” as there are no “unmount” commands on Linux.
To unmount, you can either specify a directory of a device name.
$ sudo umount <device|directory>
For example, in order to unmount the “/dev/sdc1” device we mounted previously, we would run
$ sudo umount /dev/sdc1
In order to check that the drive partition was correctly unmounted, you can use the “lsblk” command and specify the drive partition name.
$ lsblk /dev/sdc1 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdc1 8:33 0 29.3G 0 part
Alternatively, you can use the “findmnt” command and specify the device name.
$ findmnt /dev/sdc1 <empty>
Removing entries from the fstab file
Even if you unmount drives, they will be remounted (or at least the kernel will try to remount them) at the boot time.
In order for the system to stop remounting your drives at boot, you need to remove them from the fstab file.
Unmounting drives lazily
In Linux, it is also possible to unmount drives lazily when the “-l” option.
$ sudo umount -l <device|directory>
Unmount a device lazily means that you detach the filesystem from the file hierarchy (so the filesystem won’t be accessible anymore) but you clean all references to it when it is not busy anymore.
This command can be particularly handy if you are transferring files on the filesystem or if any long operations are running on the filesystem.
Force drive unmounting
On the other hand, it is possible to force a device unmounting by using the “-f” option or the “–force” option.
$ sudo umount --force <device|directory>
This option is not adapted for local drives as you want them to unmount gracefully not to lose any data.
However, in some cases you might loose connectivity to a NFS drive for example : in this case you can force the drive unmount.
Lazy drive unmounting
There is a way to detach a busy device immediately
(even if it is busy and cannot be unmounted forcefully).
You may cleanup all later:
umount -l /PATH/OF/BUSY-DEVICE
umount -f /PATH/OF/BUSY-NFS (NETWORK-FILE-SYSTEM)
- These commands can disrupt a running process, cause data loss OR corrupt open files. Programs accessing target DEVICE/NFS files may throw errors OR could not work properly after force unmount.
- Do execute these commands when not inside mounted Folder/Drive/Device.
In this tutorial, you learnt how you can mount and unmount drives on Linux using the mount and the umount commands.
You also learnt that you have to use the “fstab” file if you want to make your mount settings permanent on reboots.
Note that the commands are the same if you are dealing with LVM : LVM can be mounted and unmounted and you can even format them as regular drives.
If you are interested in Linux system administration, we have a complete section dedicated to it on the website, so make sure to have a look!