In this article I will cover various topics using Linux mount command to perform temporary and permanent mount. We will try to access file system, USB drives, ISO images, network drives using Linux mount command and unmount them using umount command with examples. before you mount USB drive using Linux mount command, you must know how to list USB drives and check USB ports in Linux so you know the connected USB drives in Linux. I will also share some fstab example, explain fstab options and syntax to mount permanently using Linux mount command.
I have used RHEL/CentOS 7/8 to demonstrate the examples from this article on Linux mount command and it’s usage.
Linux Mount Command Scenarios and Examples
Below I will cover various scenarios to access file systems temporarily and permanent using Linux mount command with examples.
Mount File System (ext3, ext4, xfs and so on..)
Using Linux mount command you can mount various types of system file system. But this will be active only for the current session (not reboot persistent). For such requirement you can use /mnt mount point along with Linux mount command.
Syntax to mount file system
# mount [-t fstype] DEVICE DIR
Here,
-t The argument following the -t is used to indicate the filesystem type.
DEVICE Here DEVICE is our File System /dev/sdb1
DIR Here DIR is our target mount point
-t option is not given, or if the auto type is specified, mount
will try to guess the desired type. Mount uses the blkid library for
guessing the file system type; if that does not turn up anything that
looks familiar, mount will try to read the file /etc/filesystems, or,
if that does not exist, /proc/filesystems.
So we can mount /dev/sdb1 without specifying file system type
# mount /dev/sdb1 /mnt
or with -t option
# mount -t xfs /dev/sdb1 /mnt
To permanently mount file system, you must add device and mount point
entry in /etc/fstab
I will create a new mount point directory
# mkdir /mydata
Now we can mount our xfs file system /dev/sdb1 on /mydata mount
point.
# mount -t xfs /dev/sdb1 /mydata
But this is again temporary, to mount file system permanently we have to
update /etc/fstab in the below fstab format. fstab example from my
setup:
# echo "UUID=2c566ef5-fe73-4e1c-aa71-9a8cd77c0b31 /mydata xfs defaults 0 0" >> /etc/fstab
How to get UUID and file system type?
To get UUID of a file system using blkid
# blkid /dev/sdb1
/dev/sdb1: UUID="2c566ef5-fe73-4e1c-aa71-9a8cd77c0b31" TYPE="xfs" PARTUUID="09181a39-01"
In the same output you can see the file system type as XFS
What are the different fstab options?
In /etc/fstab we have 6 different section per column with different
possible options. Each row is broken into six fields of data, separated
by whitespace. Below are the different fstab options
- The device to mount (Here we have used UUID or you can also use /dev/sdb1).
- The mount point (/mydata).
- The filesystem type (xfs).
- The mount options (defaults).
- Dump level (0). This field is related to the dump command and is rarely used.
- The fsck pass field (0). A value of 0 means “do not run fsck on this filesystem during system boot,” whereas a value of 1 or higher means “run fsck on this filesystem during system boot.”
Mount ISO File
There are two possible scenarios to mount ISO files:
- Mount ISO File from Virtual Media
- Mount ISO File DVD
In either scenario to mount ISO file you have to use iso9660 as the file system type along with Linux mount command.
/media directory.On a system that does not have a
running GUI, this auto mount process does not take place. You can
configure the system so a regular user can mount a removable device by
using /etc/fstab entry:
Virtual Media ISO Image
On RHEL/CentOS 7 any ISO File on Virtual media is available under
/dev/srX file system. For example my ISO File is available under
/dev/sr0 and /dev/sr1
# lsscsi
[0:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr0
[1:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr1
[2:0:0:0] disk ATA VBOX HARDDISK 1.0 /dev/sda
To mount ISO file from virtual media on /media using Linux mount
command:
# mount -t iso9660 /dev/sr0 /media
mount: /media: WARNING: device write-protected, mounted read-only.
To permanently mount ISO file using /etc/fstab we use similar syntax
with below fstab format as we used to mount file system. fstab
example from my setup
# echo "/dev/sr0 /media iso9660 defaults 0 0" >> /etc/fstab
ISO DVD Image
If you have an ISO DVD Image File available on your Linux system, you
can mount ISO file DVD using again iso9660 as the file system type
with Linux mount command:
# mount -t iso9660 -o loop /root/rhel-8.1-x86_64-dvd.iso /media/
mount: /media: WARNING: device write-protected, mounted read-only.
Again to permanently mount ISO DVD File use /etc/fstab we use similar
fstab format. Below is fstab example from my setup to mount iso
image
# echo "/root/rhel-8.1-x86_64-dvd.iso /media iso9660 defaults 0 0" >> /etc/fstab
Mount USB Drive
Before I share the steps to mount USB drive, you must be familiar to check and list USB devices connected to your Linux host and check USB ports. There are various methods and commands available to list USB devices and check USB ports, I will try to share some of them here:
6 commands to list USB devices and check USB ports
Below I will share various commands you can use in Linux to view and list connected USB devices to your Linux host and check USB ports.
1. Journalctl or /var/log/messages
You can use journalctl
or /var/log/messages to check usb ports and list usb devices
which were connected to your Linux machine. below is a snippet of my
/var/log/messages on RHEL/CentOS 7/8 node. Observe the highlighted
section to list USB devices connected.
Nov 20 22:27:33 centos-8 kernel: usb 1-1: new full-speed USB device number 2 using ohci-pci
Nov 20 22:27:33 centos-8 kernel: usb 1-1: config 1 interface 0 altsetting 0 endpoint 0x81 has invalid maxpacket 512, setting to 64
Nov 20 22:27:33 centos-8 kernel: usb 1-1: config 1 interface 0 altsetting 0 endpoint 0x2 has invalid maxpacket 512, setting to 64
Nov 20 22:27:33 centos-8 kernel: usb 1-1: New USB device found, idVendor=0781, idProduct=5567, bcdDevice= 1.00
Nov 20 22:27:33 centos-8 kernel: usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Nov 20 22:27:33 centos-8 kernel: usb 1-1: Product: Cruzer Blade
Nov 20 22:27:33 centos-8 kernel: usb 1-1: Manufacturer: SanDisk
Nov 20 22:27:33 centos-8 kernel: usb 1-1: SerialNumber: 4C531001480518118400
Nov 20 22:27:33 centos-8 kernel: usb-storage 1-1:1.0: USB Mass Storage device detected
Nov 20 22:27:33 centos-8 kernel: scsi host4: usb-storage 1-1:1.0
Nov 20 22:27:33 centos-8 kernel: usbcore: registered new interface driver usb-storage
Nov 20 22:27:33 centos-8 kernel: usbcore: registered new interface driver uas
Nov 20 22:27:34 centos-8 kernel: scsi 4:0:0:0: Direct-Access SanDisk Cruzer Blade 1.00 PQ: 0 ANSI: 6
Nov 20 22:27:34 centos-8 kernel: sd 4:0:0:0: Attached scsi generic sg4 type 0
Nov 20 22:27:34 centos-8 kernel: sd 4:0:0:0: [sdc] 30595072 512-byte logical blocks: (15.7 GB/14.6 GiB)
Nov 20 22:27:34 centos-8 kernel: sd 4:0:0:0: [sdc] Write Protect is off
Nov 20 22:27:34 centos-8 kernel: sd 4:0:0:0: [sdc] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Nov 20 22:27:34 centos-8 kernel: sdc: sdc1
Nov 20 22:27:34 centos-8 kernel: sd 4:0:0:0: [sdc] Attached SCSI removable disk
2. lsusb
You can check usb ports in linux using
lsusb. lsusb tool is part of
usbutils rpm. So install
usbutils to check USB ports in Linux. For example I have a
single USB connected on Port 1:
# lsusb -t
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ohci-pci/12p, 12M
|__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 12M
3. lsscsi
You can list all the devices connected to Linux using
lsscsi. So to list USB devices also
you can use lsscsi command in Linux. Here my USB drive is mounted on
/dev/sdc
# lsscsi
[0:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr0
[1:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr1
[2:0:0:0] disk ATA VBOX HARDDISK 1.0 /dev/sda
[3:0:0:0] disk ATA VBOX HARDDISK 1.0 /dev/sdb
[4:0:0:0] disk SanDisk Cruzer Blade 1.00 /dev/sdc
4. lsblk
Use lsblk to show and list usb devices and partition which are connected to your linux machine.
# lsblk /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdc 8:32 1 14.6G 0 disk
└─sdc1 8:33 1 14.6G 0 part
5. blkid
Use
blkid to list usb devices and all
the required details. As you can see my USB drive is NTFS file system.
If you execute blkid without any arguments, it will list all the
connected devices to Linux host.
# blkid /dev/sdc1
/dev/sdc1: LABEL="Deepak-16G" UUID="B48646258645E886" TYPE="ntfs" PARTUUID="2633de4d-01"
6. fdisk
Using fdisk you can list all the connected storage devices. We will
use fdisk to list usb devices. If you have multiple drives connected
to your Linux machine then the list can be long, you can look towards
the end of fdisk -l output, for example
Disk /dev/sdc: 14.6 GiB, 15664676864 bytes, 30595072 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: 0x2633de4d
Device Boot Start End Sectors Size Id Type
/dev/sdc1 * 2048 30595071 30593024 14.6G 7 HPFS/NTFS/exFAT
As you see this is dos disk with size of 14.6 GB and type as HPFS/NTFS/exFAT so this is most likely our USb drive, you can again confirm this by using lsscsi or any other commands from above list.
USB Drive with NTFS File System
Now that you can list USB devices and check USB ports, to mount USB
drive with NTFS file system type using Linux mount command we need
ntfs-3g driver in Linux. If you attempt to mount USB drive without
ntfs-3g driver then mount will fail
# mount -t ntfs-3g /dev/sdc1 /mnt
mount: /mnt: unknown filesystem type 'ntfs'.
ntfs-3g driver.
ntfs-3g is an NTFS driver, NTFS supports several filename namespaces:
DOS, Win32 and POSIX. While the ntfs-3g driver handles all of them, it
always creates new files in the POSIX namespace for maximum portability
and interoperability reasons
# yum -y install ntfs-3g.x86_64
Create a mount point
# mkdir /mnt/usb
Now try to mount USB drive using Linux mount command
# mount -t ntfs /dev/sdc1 /mnt/usb
Mount is successful
# df -h /mnt/
Filesystem Size Used Avail Use% Mounted on
/dev/sdc1 15G 364M 15G 3% /mnt/usb
Use similar fstab format as used in above examples to permanently mount
usb drive with ntfs file system type. Below is fstab example to
mount usb drive with ntfs file system.
# echo "UUID="B48646258645E886" /mnt/usb ntfs defaults 0 0" >> /etc/fstab
blkid for permanent mounting rather than
drive name. Use blkid to get the UUID of USB drive.
USB Drive with FAT32 or vFAT File System
You will list USB devices, check USB ports and use blkid to check the
file system type, as you see the file system type of this USB drive is
vFAT. To mount USB drive with vfat file system type using Linux mount
command we need vfat module.
# blkid /dev/sdc1
/dev/sdc1: LABEL="DEEPAK-16G" UUID="5E92CAC292CA9E41" TYPE="vfat" PARTUUID="2633de4d-01"
Check if vfat module is loaded on the system:
# lsmod | grep vfat
vfat 17411 0
fat 65913 1 vfat
If the vfat is not loaded, load it by running:
# modprobe vfat
dosfstools rpm to load vfat related modules and
tools to be able to mount USB drive with vfat file system.
Next to mount USB drive with FAT32 or vFAT file system using Linux mount command:
# mount -t vfat /dev/sdc1 /mnt
Similar to NTFS file system type use same fstab format, use below
fstab example to mount usb drive permanently using /etc/fstab
# echo "UUID="5E92CAC292CA9E41" /mnt/usb vfat defaults 0 0" >> /etc/fstab
blkid for permanent mounting rather than
drive name. Use blkid to get the UUID of USB drive.
Mount Network Drive
I have a NFS server available at 192.168.0.121 and have shared /mydata
directory on this server. To mount network drive from NFS
Syntax:
mount -t <fstype> -o vers=<version> <nfs_server>:<shared_path_on_nfs_server> <mount_point_on_localhost>
Mount NFS Network Drive using NFSv4
# mount -t nfs -o vers=4 192.168.0.121:/mydata /mnt
Mount NFS Network Drive using NFSv3
# mount -t nfs -o vers=3 192.168.0.121:/mydata /mnt
nfs-utils rpm installed on the NFS Server and Client node to
mount network drive using NFS.
To mount network drive permanently use /etc/fstab the fstab format
will remain the same, except the first column you have to give NFS
server details and the source path. Below is fstab example from my NFS
share (/mydata) on 192.168.0.121
# echo "192.168.0.121:/mydata /mnt nfs defaults 0 0" >> /etc/fstab
How to unmount file system and other removable devices?
Use umount command to unmount any type of file system, usb drives, network drives or iso images.
umount <mount_point>
So if my file system is mounted on /mnt
# umount /mnt
You may face error “umount: /mnt/usb: target is busy” some times while truing to umount file system. This means that the file system is in use, this error is similar to the error we see in Windows while trying to eject a USB drive “The device is currently in use”
In such case you can use
lazy umount using
“-l”
# umount -l /mnt/usb
which means as soon as /mnt/usb is not used by any other process,
it will unmount itself
Or alternatively you can look for processes which is using /usb/mnt
using lsof
# lsof /mnt/usb/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 7033 root cwd DIR 8,49 4096 5 /mnt/usb
lsof 12636 root cwd DIR 8,49 4096 5 /mnt/usb
lsof 12637 root cwd DIR 8,49 4096 5 /mnt/usb
Then you can decide to kill those process (kill -9 <PID>) but remember by doing so any ongoing task or operation will be killed.
Lastly I hope the steps from this article on Linux mount command to mount ISO file, USB drive, network drive and file systems, list USB devices, check USB ports, understanding fstab options, fstab format and examples were helpful. So, let me know your suggestions and feedback using the comment section.


