How to Mount a Drive in Linux

how to mount drive in linux

Mount is a command used in Linux to attached filesystems and drives and umount command is used to detach (unmount) any attached file systems or devices.

In this tutorial, we will learn how to mount (attach) a drive in Linux with the EXT Linux file system, FAT or NTFS using mount command. We can use the same command to mount the USB drive and ISO image as well.

On both Linux and UNIX like operating systems we can use mount/umount command to attach/detach devices.

List Mounted File Systems and Linux Drives

To display all currently attached file systems we will type:

$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=491472k,nr_inodes=122868,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=100904k,mode=755)
/dev/sda1 on / type ext4 (rw,relatime,data=ordered)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
....

By default, the output will include all of the file systems including the virtual ones such as cgroup, sysfs, and others. Each line contains information about the device name, the directory to which the device is mounted, the filesystem type and the mount options.

To list only certain types of file systems we will use -t option:

$ mount -t ext4
/dev/sda1 on / type ext4 (rw,relatime,data=ordered)

Listing only ext4 Linux file system will usually display our Linux drives.

You can use fdisk to have an idea of what kind of Linux partitions and devices you have, for example:

$ sudo fdisk -l
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
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
Disk identifier: 0x817e2210

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 20971486 20969439  10G 83 Linux


Disk /dev/sdb: 10 MiB, 10485760 bytes, 20480 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

On my Fedora Linux computer, we can see a different drive configuration:

$ sudo fdisk -l
Disk /dev/sda: 223.58 GiB, 240057409536 bytes, 468862128 sectors
Disk model: TS240GSSD220S   
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: gpt
Disk identifier: 7AA5C627-6B3A-4500-91B2-757F762207CF

Device       Start       End   Sectors   Size Type
/dev/sda1     2048    411647    409600   200M EFI System
/dev/sda2   411648   2508799   2097152     1G Linux filesystem
/dev/sda3  2508800 468860927 466352128 222.4G Linux LVM


Disk /dev/mapper/fedora_localhost--live-root: 70 GiB, 75161927680 bytes, 146800640 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/fedora_localhost--live-swap: 7.84 GiB, 8401190912 bytes, 16408576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/fedora_localhost--live-home: 144.56 GiB, 155206025216 bytes, 303136768 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Now when I insert USB flash drive, we get another bit of information with fdisk -l:

Disk /dev/sdb: 1.88 GiB, 1998585856 bytes, 3903488 sectors
Disk model: DataTraveler 2.0
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
Disk identifier: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1  *     2048 3903487 3901440  1.9G  c W95 FAT32 (LBA)

This is important when we need to find which partition we wish to mount (attach) or detach. In this case, we see the USB drive is /dev/sdb1.

Reading mount options from a drive

We can use mount -t command to list the drive mount options. USB drive I inserted before was auto-mounted, and we saw that the filesystem is VFAT.

$ mount -t vfat
/dev/sda1 on /boot/efi type vfat (rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro)
/dev/sdb1 on /run/media/slax/tribal type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

We see that the USB drive (partition) is mounted as /dev/sdb1 on /run/media/slax/tribal.

You can read about specific options in mount man page.

Mounting a Linux FileSystem

To mount a drive we need to assign it a root directory (mounting point) with mount command in the following format:

$ mount [OPTIONS...] DRIVE DIRECTORY

Usually, the mount command will auto-detect the filesystem type, but some filesystems are not recognized and need to be specified as a parameter.

Use the -t option to specify the file system type:

$ mount -t FILESYSTEM DRIVE DIRECTORY

To specify additional mount options, you can use the -o option:

$ mount -o OPTIONS DRIVE DIRECTORY

You can use multiple options by separating them with a comma (do not insert a space after a comma).

Mounting Linux USB Drive

So first we need to create a mount point (directory):

$ sudo mkdir /media/myusb

Next, we mount the drive to /media/myusb:

$ sudo mount /dev/sdb1 /media/myusb/

With dh -H we can see on the last line that our USB device is mounted successfully.

$ df -H
Filesystem                               Size  Used Avail Use% Mounted on
devtmpfs                                 8.4G     0  8.4G   0% /dev
tmpfs                                    8.4G  149M  8.2G   2% /dev/shm
tmpfs                                    8.4G  2.0M  8.4G   1% /run
/dev/mapper/fedora_localhost--live-root   74G   22G   49G  31% /
tmpfs                                    8.4G  103k  8.4G   1% /tmp
/dev/sda2                                1.1G  229M  725M  24% /boot
/dev/mapper/fedora_localhost--live-home  152G   60G   85G  42% /home
/dev/sda1                                210M   21M  189M  10% /boot/efi
tmpfs                                    1.7G   14M  1.7G   1% /run/user/1000
/dev/sdb1                                2.0G  4.1k  2.0G   1% /media/myusb

Mounting a FileSystem using /etc/fstab file

Mounting points and their options configured in /etc/fstab will automount upon system start.

The /etc/fstab file contains a list of entries in the following form:

| File System | Mount Point | FileSystem Type | Options | Dump | Pass |

Here is the sample /etc/fstab file from my computer:

$ cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Aug  4 04:28:13 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
192.168.0.15:/volume1/NetBackup /mnt/perun	nfs	_netdev,defaults	0 0
/dev/mapper/fedora_localhost--live-root /                       ext4    defaults        1 1
UUID=a061115a-5965-4525-a3e9-b4c52c43ecf1 /boot                   ext4    defaults        1 2
UUID=1D56-1DD9          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
/dev/mapper/fedora_localhost--live-home /home                   ext4    defaults        1 2
/dev/mapper/fedora_localhost--live-swap none                    swap    defaults        0 0

So you can add an entry, and configure mounting point in /etc/fstab to have it always mounted on system boot.

Mounting NFS Share

NFS stands for Network File System. To mount an NFS share you'll need to have the NFS client package installed on your Linux system.

To install NFS client on Ubuntu and Debian, type:

$ sudo apt install nfs-common

To install NFS client on CentOS and Fedora:

$ sudo yum install nfs-utils

As we have seen in my sample /etc/fstab file entries before, I already have an NFS share configured on my computer:

192.168.0.15:/volume1/NetBackup /mnt/perun	nfs	_netdev,defaults	0 0

You can add an entry to /etc/fstab on your computer using Nano or Vim, and enter the URL path to your remote NFS shared directory and a local mounting point (directory) which will be used to access the remote files.

Mounting ISO Files

You can mount an ISO file using the loop device which is a special pseudo-device that makes a file accessible as a block device.

Start by creating the mount point, directory to be used with ISO file:

$ sudo mkdir /media/iso

Mount the ISO file to the mount point by typing the following command:

$ sudo mount /path_to_image.iso /media/iso -o loop

In the sample command above, path_to_image.iso is the path to your ISO file, of course.

Unmounting a File System

To detach a mounted filesystem, use the umount command followed by either the directory where it has been mounted (mount point) or the device name. If we use the USB drive from before as a sample, the commands would look like this:

$ sudo umount /run/media/slax/tribal
$ sudo umount /dev/sdb1

If the file system is in use the umount command will fail to detach the file system.

Lazy unmount

Use the -l (--lazy) option to unmount a busy file system as soon as it is not busy anymore.

$ sudo umount -l /dev/sdb1

Force unmount

Use the -f (--force) option to force an unmount. This option is usually used to unmount an unreachable NFS system. In the case of my NFS Share, the command would be:

$ sudo umount -f /mnt/perun

It is not a good idea to force unmount as it may corrupt the data on the file system.

Conclusion

By the end of this tutorial, you should have a good understanding of how to use the mount and umount command to attach and detach various file systems. We have also seen how to achieve that in various ways and with several media options like ISO file, NFS share and USB flash drive.

Slavisa Milojkovic 11:46 am

Comments

Your email address will not be published. Required fields are marked *

4 Comments

  1. You don't mount drives. /dev/sdb is the drive. You are mounting /dev/sdb1 which is the first primary partition on the file system.

    1. Hello, you're technically correct, but in the sense that we use these commands and steps to mount any number of partitions of a drive, may it be 2 or 5 or whatever number of partitions, we used the "How to mount a drive in Linux" title.