How to mount a drive in Linux
Mount is a command used to connect file systems and drives in Linux, while the umount command is used to detach (unmount) any attached file system or device.
In this tutorial, we will learn how to mount (attach) a drive in Linux with EXT Linux file system FAT or NTFS using the mount command. We can also use the same command to mount USB drives and ISO images.
On operating systems like Linux and UNIX, we can use the mount / umount command to attach / detach devices.
List mounted file systems and Linux drives
To display all currently attached file systems, we would enter:
$ 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 file systems, including virtual file systems, such as cgroup, sysfs, etc. Each line contains information about the device name, device installation directory, file system type, and installation options.
To list only certain types of file systems, we will use the -t option:
$ mount -t ext4 /dev/sda1 on / type ext4 (rw,relatime,data=ordered)
Listing only the ext4 Linux file system usually shows our Linux drive.
You can use fdisk to learn what kind of Linux partitions and devices you have, such as:
$ 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 different drive configurations:
$ 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 plug in a USB flash drive we will get more information 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 partitions to be mounted (attached) or detached. In this case, we see that the USB drive is / dev / sdb1.
Read installation options from the drive
We can use mount -t
The command lists drive installation options. The USB drive I inserted was mounted automatically and we see that the file system 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 the USB drive (partition) mounted as / dev / sdb1 on / run / media / slax / tribal.
You can learn about specific options in the installation man pages.
Mount Linux file system
To mount a drive, we need to assign it a root directory (mount point) using the mount command in the following format:
$ mount [OPTIONS...] DRIVE DIRECTORY
Normally, the mount command will automatically detect the file system type, but some file systems are not recognized and need to be specified as parameters.
Specify the file system type with the -t option:
$ mount -t FILESYSTEM DRIVE DIRECTORY
To specify other installation options, you can use the -o option:
$ mount -o OPTIONS DRIVE DIRECTORY
You can use multiple options, separated by commas (don’t insert spaces after the commas).
Mount Linux USB Drive
So first we need to create a mount point (directory):
$ sudo mkdir /media/myusb
Next, mount the drive to / media / myusb:
$ sudo mount /dev/sdb1 /media/myusb/
use dh -H
We can see in the last line that the USB device has been successfully installed.
$ 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
Mount file system with / etc / fstab file
The mount points configured in / etc / fstab and their options will be mounted automatically at system startup.
The / etc / fstab file contains a list of entries in the following format:
| File System | Mount Point | FileSystem Type | Options | Dump | Pass |
This is a sample / etc / fstab file on 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 the mount point in / etc / fstab so that it always mounts when the system boots.
Mount the NFS share
NFS stands for Network File System. To mount an NFS share, you need to install the NFS client package on your Linux system.
To install the NFS client on Ubuntu and Debian, enter:
$ sudo apt install nfs-common
To install the NFS client on CentOS and Fedora:
$ sudo yum install nfs-utils
As we saw in the example / etc / fstab file entry, I have configured the NFS share on the computer:
192.168.0.15:/volume1/NetBackup /mnt/perun nfs _netdev,defaults 0 0
You can use Nano or Vim to add an entry in / etc / fstab on your computer, then enter the URL path of the remote NFS shared directory and the local mount point (directory) for accessing the remote files.
Mount ISO file
You can mount ISO files using a loop device, which is a special pseudo device that allows files to be accessed as block devices.
First create the mount point directory to be used with the ISO file:
$ sudo mkdir /media/iso
Mount the ISO file to the mount point by typing:
$ sudo mount /path_to_image.iso /media/iso -o loop
Of course, in the example command above, path_to_image.iso is the path to your ISO file.
Unmount file system
To detach a mounted file system, use the umount command followed by the directory (mount point) or device name of the mounted file system. If we use the previous USB drive as an example, the command looks like this:
$ sudo umount /run/media/slax/tribal $ sudo umount /dev/sdb1
If the file system is in use, the umount command cannot detach the file system.
Lazy unloading
Once it is no longer busy, use the -l (–lazy) option to unmount the busy file system.
$ sudo umount -l /dev/sdb1
Force uninstall
Use the -f (–force) option to force uninstallation. This option is usually used to unmount an unreachable NFS system. For my NFS share, the command is:
$ sudo umount -f /mnt/perun
Forcibly unmounting is not a good idea because it can damage the data on the file system.
in conclusion
By the end of this tutorial, you should have a good understanding of how to attach and detach various file systems using the mount and umount commands. We also saw how this can be achieved in various ways and with several media options such as ISO files, NFS shares and USB flash drives.