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.
Hi admin,
It is, ok, with mount -a and reboot too…
Thank you very much,
have a nice week,
Eric
Thank you Admin !
give ==> parse error at line 8 — ignored
is ok
I will look how to rename the ‘
Disque Dur
‘ to avoid space, but it come with the provider (free), so I am not sure we can rename it. Anyways, it is ok with the magic ‘040
‘ ASCII code, until a test after reboot to validate completely,Thank again Admin, have a good week,
Regards
Eric
You can test without rebooting the node by manually unmounting
/mnt/freebox
then add the entry in/etc/fstab
and executefrom the terminal as root user (or with sudo)
This should mount all the filesystems from
/etc/fstab
Hi admin,
Again me, I have a pb that I can’t solve. Perhaps it is easy for you with one magic correct command ?
I have my network drive on the box that I mount whithout problem with the following terminal command:
But in FSTAB I don’t find the correct sentence.
I tried this without success.
I have googled but no more success ?
Do you know the correct command in FSTAB for this disk ?
Again, Admin thank you
Can you try to use following line in
/etc/fstab
:Here
040
is the ASCII code for space in octal numeral system. Butmount.cifs
will not support 040 code so you will have to use backslash or double quotes for the whitespace in your PATH NAME.alternatively you can use double quotes to ignore the whitespace in your PATH
I would suggest to remove the whitespace and use
Disque_dur
instead which should be more appropriate. These spaces in names and paths can be really hectic when working with shares at times and should be avoided.Hi admin,
After severals attemps, it is impossible to format this disk to ext4. I tried what you propose, I tried severals things without succes, pb is with super-blocks.
format with dd
wipefs
Anyway, I have bought a new disk size of 4To, and everything are ok now for ext4.
I format back to Fat32 for the ‘firewire’ with uid/gid and it is ok.
Thank you
Thanks for sharing the result. This mostly tells that your disk was corrupted, I am glad you bought a new hardware. May be time to retire the old one.
Hi admin,
Thank you for information and advices. I have followed your instructions. Here the result:
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
After reboot
and
If I go back to Fat32 (gparted) it should be ok for format the sdc1, so I don’t really understand what happen,
thank you for any help
This is really weird but I see there are a number of users who seem to face similar issues reported on multiple forums. Can you try wiping the filesystem from your pen drive using
and also
Replace sdx with your USB device. Run this command carefully as this can destroy your Linux FS
and then try to use fdisk /dev/sdc, delete the partition and save the changes using “w” (Do not create a new partition here)
Next remove the external disk and replug it. Now you can create new partition using fdisk /dev/sdc
and then try to use mkfs.ext4 /dev/sdc1
Hi admin,
Again me, below the result of format to ext4 :
and
there is no UUID, it is normal ?
Thank a lot for advice,
Here is the problem:
If you read my previous comments, you cannot directly assign a file system on NTFS/vFAT device. So you must delete the partition first and then create a new partition.
Hi admin,
I have a backup script that is intended to preserve rights/credentials ( I want to say owner/permission of uid,gid), like this :
The -p option should preserve attributs of the owner from the source to the target, but when it is executed :
In English it means that it doesn’t support to keep the owner permissions to the target, because the target is Fat32 and you explain me I can’t change these permissions after FSTAB.
So now, I will try to pass the disk under ext4. I will give you if you agree the exact error and what I do.
Thank you again,
Regards
Eric
If you are taking a backup then it should not fail unless you are trying to overwrite a file with the same name in which case you will get the permission issue.
So to overcome this you can create a new directory every time you take a backup and it should work
Hello admin,
Thank you! UID and GID options do the job.
Now I am owner of the folders/files, but I am not sur that it will be suficient for the backup script to preserve credentails because I have understand that now I will be the owner of all the files (old and news ) even the source is different and the user is under the GID . right ?
Here are the results of fdisk
As you can guess my pb is that I want to preserve credentials from a backup and 2° pb I can’t save More 4GB on this disk, that why I tri to pass it under ext4, to save some VMachines.
Thank for any help,
Eric
I am not sure if I understand your requirement. Can you please help me understand the question and the issue.
Hi admin,
Thank you for trying helping me.
Here my last /etc/fstab for the external DD (maxtor old 20 years):
As you see I mounted directly the disk on the root like this : /firewire (but it is usb link). I have tried under /mnt/firewire but it is the same. Do you think this point is important ?
Here the mistake I have when I want to change the owner :
I am under opensuse Leap 15.2.
So I tried first to format with yast to ext4 and it said that an error was raised, but I don’t remember exactly the details. If you think it is important I can try to format again and give you the exact error. I’ve tried also under command line, but gave the same : some error I don’t remember exactly
Thank again for any idea / clue,
eric
Hello Dienot,
Thanks for sharing these details.
Unfortunately we cannot change permissions of existing files and folders on an external driver with NTFS and vFAT file system so you can mount these drives using uid=XXX and gid=XXX options where you can add the UID and UID of the respective user to which you wanted to change the ownership.
Regarding the formatting an vfat with ext4, it worked for me properly using fdisk. If you can share any error snippet then I can try to reproduce and help you recover.
Here would be the brief steps. For example your external drive is on
/dev/sdb
WARNING: This step would delete all your DATA
Hello,
Thank you for this article, very helpfull. I mounted an external DD with USB and put this in FSTAB
UUID=09B1-F7B2 /firewire vfat rw,user,exec,umask=000,iocharset=utf8 0 0
The disk is mounted, work fine, but I can’t change the owner of the folders (Operation not permitted
).
It is always Root:root the owners, and I want to change, because when I make backup from system files I want to preserve the owner from the source.
I have tried to format the disk in ext4 but it seems that it is not possible ? How can I know if the disk is copatible to ext4 ?
In advance thanks for any help
Eric from France
Hi Eric,
1. If the external Disk is mounted as read write then I don’t think you should get that error. Which folder permission are you trying to change? The folder where the disk is mounted or any other folders inside the mounted path
2. What error you get while trying to format the disk with ext4?