×


Different Methods to Mount a Drive in Linux ?

Are you trying to mount a drive in Ubuntu, CentOS or Debian ? 

This guide is for you.


When mounting a disk, the operating system reads information about the file system from the disk's partition table, and assigns the disk a mount point.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related tasks.

In this context, we shall look into how to mount (attach) a drive in Linux with the EXT Linux file system, FAT or NTFS using mount command. 


More information about Mounting a 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.

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

The mount point is a name that refers to the disk, like "C:" in Microsoft Windows, or "/" in Linux, BSD, macOS, and other Unix-like operating systems.

Basically, what “mounting” a disk drive means is to set up your local operating system so that a disk resource (a local disk drive, USB external volume, partition, subdirectory tree, server volume, etc etc) “looks like” a normal local disk, and occupies a “drive” on your computer's operating system.

The mount command mounts a storage device or filesystem, making it accessible and attaching it to an existing directory structure. The umount command "unmounts" a mounted filesystem, informing the system to complete any pending read or write operations, and safely detaching it.


How to List Mounted File Systems and Linux Drives ?

To display all currently attached file systems, execute the command:

$ 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 Fedora Linux computer, you will see a different drive configuration such as:

$ 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.


How to read 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.


How to mount 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).


How to mount Linux USB Drive ?

To start with, you need to create a mount point (directory) with the command:

$ 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

How to mount 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 our command prompt:

$ 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.


How to mount NFS Share in Linux ?

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, we already have an NFS share configured as shown below:

102.168.0.11:/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.


How to mount 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.


How to unmount 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.


The concept of 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

How to 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.


[Need to set up PHP on your Debian Server? We are available to help you.]


Conclusion

This article will guide you on how to use the #mount and #umount command to attach and detach various file systems. You can perform this task on several media options like ISO file, NFS share and USB flash drive. Mounting will not erase everything. The #disk does get modified slightly each time you mount it, though. However, since you have serious directory corruption which cannot be repaired by Disk Utility you need to repair and replace the directory before it can be mounted.