In this article, I will tell you how to mount and unmount a drive (hard drive, SSD, pen drive, android device) using mount/umount, gio, and udisksctl commands in Linux.
Table of Contents
- Installation and early preparation
- How to mount a drive in Linux using the mount command
- How to mount a device in Linux using udiskctl
- How to mount using a device using ‘gio mount’ in Linux
- How to unmount a drive in Linux
- Conclusion
Installation and early preparation
Before mounting any device in Linux, make sure that you have installed appropriate packages such as NTFS-3g for NTFS file system, dosfstools for file systems of the FAT family, and mtools for MS-DOS disks (typically a floppy disk). I will talk about further installations in the upcoming headings.
How to mount a drive in Linux using the mount command
The mount
command in Linux is a very powerful command. Using it you can do a lot of things.
Basic structure of mount
command:
$ sudo mount <source> <destination>
Where, <source>
is the drive you want to mount and <destination>
is the location where you want to mount.
The name of the <source>
device can be obtained using the lsblk
command:
❯ lsblk --paths
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
/dev/loop0 7:0 0 56G 0 loop /mnt/VHD
/dev/nvme1n1 259:0 0 931.5G 0 disk
├─/dev/nvme1n1p1 259:1 0 16M 0 part
└─/dev/nvme1n1p2 259:2 0 931.5G 0 part
Example:
$ sudo mount /dev/nvme1n1p2 /mnt/crucial
You can also omit the <destination>
. In that case, mount
will search for it in the /etc/fstab
file. So, you need to configure the file for this functionality.
$ sudo mount /dev/sdb1
How to mount a device using its UUID
You can also mount your drives using UUID:
$ sudo mount UUID=bd4da364-6f99-4859-8ca4-326c89e9b11f /mnt/Crucial
To find a device’s UUID, look into /dev/disk/by-uuid/
. All the drives are symlinked:
$ ls -al '/dev/disk/by-uuid'
total 0
drwxr-xr-x 2 root root 160 Mar 16 18:42 .
drwxr-xr-x 9 root root 180 Mar 16 18:41 ..
lrwxrwxrwx 1 root root 15 Mar 16 18:42 00E8EAD5E8EAC7CC -> ../../nvme0n1p3
lrwxrwxrwx 1 root root 11 Mar 16 18:42 5627befe-b7a4-48c7-ae19-a71689a67d7e -> ../../loop0
lrwxrwxrwx 1 root root 15 Mar 16 18:42 B2E8-9A01 -> ../../nvme0n1p1
lrwxrwxrwx 1 root root 15 Mar 16 18:42 E4CA6C29CA6BF5E8 -> ../../nvme1n1p2
lrwxrwxrwx 1 root root 15 Mar 16 18:42 EAD2EB39D2EB08A1 -> ../../nvme0n1p4
lrwxrwxrwx 1 root root 15 Mar 16 18:42 fc986af0-b252-4c73-a921-a7cb75eb4c5f -> ../../nvme0n1p5
How to mount a device using its PARTUUID
Similarly, you can also use the PARTUUID variable to identify your partition:
$ sudo mount PARTUUID=partuuid /mnt/crucial
Here, to find the PARTUUID of a drive, look into /dev/disk/by-partuuid/
directory.
Using PARTUUID is more robust since it is independent of the file system and hence, cannot be changed by mkfs
and mkswap
command. On the other hand, UUID gets changed when we format a drive.
How to mount iso images and virtual hard disks in Linux
You can also use the mount
command to mount an *.iso
image to read/copy its content. The mount
command can also mount virtual Hard disks. For this, use -o loop
:
$ sudo mount -o loop /media/VHD.img /mnt/VHD/
$ sudo mount -o loop /media/iso_file.iso /mnt/iso/
Where, loop
is used to mount the file to loop device (/dev/loopn
where, n
is the next remaining loop number). you can use loop=/dev/loop0
instead of loop
to mount at the loop0
.
The result can be seen in the output of df
:
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
dev 7060864 0 7060864 0% /dev
run 7081468 1380 7080088 1% /run
/dev/nvme0n1p5 57135216 33081828 21118604 62% /
tmpfs 7081468 67376 7014092 1% /dev/shm
tmpfs 7081468 3368 7078100 1% /tmp
/dev/nvme1n1p2 976744444 552065892 424678552 57% /mnt/crucial
/dev/nvme0n1p1 262144 35772 226372 14% /boot/EFI
/dev/loop0 57483360 18569620 35961344 35% /mnt/VHD
tmpfs 1416292 3160 1413132 1% /run/user/1000
What are ‘bind mounts’ in the mount command in Linux
To get changes in one directory reflected in another, you need to use ‘bind mounts’. The flag --bind
in mount enables you to use this feature.
Command structure:
sudo mount --bind <source> <destination>
Example:
$ sudo mount --bind dir1 dir2
Create these directories and tries to create some files, do many things. You can see that the contents of both are the same.
📝 Note: to mount a hard disk or SSD automatically at boot, you will need to set the corresponding entries in the fstab file /etc/fstab
. After the fstab entry, your computer automatically unmounts your drive before shutting itself down as can be seen in the journalctl | grep <location>
:
$ journalctl | grep /mnt/Maxtor
'unmounted /mnt/Maxtor'
How to mount a device in linux using udiskctl
To install udisks, execute one of the following commands:
Debian
apt-get install udisks2
Ubuntu
apt-get install udisks2
Alpine
apk add udisks2
Arch Linux
pacman -S udisks2
Kali Linux
apt-get install udisks2
CentOS
yum install udisks2
Fedora
dnf install udisks2
Windows (WSL2)
sudo apt-get update sudo apt-get install udisks2
Raspbian
apt-get install udisks2
Source: command-not-found
Udisksctl is one of the simplest methods to mount a removable drive. Unlike mount
command, it does not require a sudo password.
For example, to mount a drive /dev/sdc1
in /run/media/$USER/
folder, execute the command:
$ udisksctl mount --block-device=/dev/sdc1
How to mount using a device using ‘gio mount’ in Linux
Like udiskctl
, it also does not require root permissions.
Most of the linux distributions already install it. You can test whether it is installed or not by using which
command:
$ which gio
/usr/bin/gio
Or, if it is not installed, try installing glib
(glib2 or something like that depending upon your distro). Check command-not-found for your package providing the gio command.
For all available options in gio mount
, execute the command:
$ gio mount
gio: No locations given
Usage:
gio mount [OPTION…] [LOCATION…]
Mount or unmount the locations.
Options:
-m, --mountable Mount as mountable
-d, --device=ID Mount volume with device file, or other identifier
-u, --unmount Unmount
-e, --eject Eject
-t, --stop=DEVICE Stop drive with device file
-s, --unmount-scheme=SCHEME Unmount all mounts with the given scheme
-f, --force Ignore outstanding file operations when unmounting or ejecting
-a, --anonymous Use an anonymous user when authenticating
-l, --list List
-o, --monitor Monitor events
-i, --detail Show extra information
--tcrypt-pim=PIM The numeric PIM when unlocking a VeraCrypt volume
--tcrypt-hidden Mount a TCRYPT hidden volume
--tcrypt-system Mount a TCRYPT system volume
Example:
$ gio mount --device=/dev/bus/usb/001/015
The above will mount /dev/bus/usb/001/015
at /run/media/$USER
. Similarly, you can mount /dev/sda1
After mounting the device, you can check it using --list
(focus on underlined outputs):
$ gio mount --list --detail
...
Drive(2): Kingston DataTraveler 3.0
Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
ids:
unix-device: '/dev/sda'
themed icons: [media-removable] [media] [media-removable-symbolic] [media-symbolic]
symbolic themed icons: [media-removable-symbolic] [media-symbolic] [media-removable] [media]
is_removable=1
is_media_removable=0
has_media=1
is_media_check_automatic=1
can_poll_for_media=0
can_eject=1
can_start=0
can_stop=1
start_stop_type=shutdown
sort_key=01hotplug/1661971449133300
Volume(0): 31 GB Volume
Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
ids:
class: 'device'
unix-device: '/dev/sda1'
uuid: 'DDF8-FB82'
uuid=DDF8-FB82
themed icons: [drive-harddisk-usb] [drive-harddisk] [drive] [drive-harddisk-usb-symbolic] [drive-harddisk-symbolic] [drive-symbolic]
symbolic themed icons: [drive-harddisk-usb-symbolic] [drive-harddisk-symbolic] [drive-symbolic] [drive-harddisk-usb] [drive-harddisk] [drive]
can_mount=0
can_eject=1
should_automount=0
sort_key=gvfs.time_detected_usec.1661971449235103
Mount(0): 31 GB Volume -> file:///run/media/ajay/DDF8-FB82
Type: GProxyMount (GProxyVolumeMonitorUDisks2)
default_location=file:///run/media/ajay/DDF8-FB82
themed icons: [drive-harddisk-usb] [drive-harddisk] [drive] [drive-harddisk-usb-symbolic] [drive-harddisk-symbolic] [drive-symbolic]
symbolic themed icons: [drive-harddisk-usb-symbolic] [drive-harddisk-symbolic] [drive-symbolic] [drive-harddisk-usb] [drive-harddisk] [drive]
can_unmount=1
can_eject=1
is_shadowed=0
sort_key=gvfs.time_detected_usec.1661972834014018
Volume(0): Mi A3
Type: GProxyVolume (GProxyVolumeMonitorMTP)
ids:
unix-device: '/dev/bus/usb/001/005'
activation_root=mtp://Xiaomi_Mi_A3_be7541f9d27f/
themed icons: [phone]
symbolic themed icons: [phone-symbolic] [phone]
can_mount=1
can_eject=0
should_automount=1
Mount(0): Mi A3 -> mtp://Xiaomi_Mi_A3_be7541f9d27f/
Type: GProxyShadowMount (GProxyVolumeMonitorMTP)
default_location=mtp://Xiaomi_Mi_A3_be7541f9d27f/
themed icons: [phone]
symbolic themed icons: [phone-symbolic] [phone]
can_unmount=1
can_eject=0
is_shadowed=0
Mount(1): mtp -> mtp://Xiaomi_Mi_A3_be7541f9d27f/
Type: GDaemonMount
default_location=mtp://Xiaomi_Mi_A3_be7541f9d27f/
themed icons: [multimedia-player] [multimedia] [multimedia-player-symbolic] [multimedia-symbolic]
symbolic themed icons: [drive-removable-media-symbolic]
can_unmount=1
can_eject=0
is_shadowed=1
Note: The gio
can also mount Android devices. For that, install gvfs-mtp
in Arch Linux (in Ubuntu and other distros it might already be there). Just connect your phone using a USB cable, unlock it, set USB USE FOR File Transfer
in your phone, and mount using the above command. It will be mounted in /run/user/1000/gvfs
.
How to unmount a drive in Linux
Unmount using umount command
Please note that it is umount
not unmount.
Here, only <source>
of drive or <destination>
i.e. file path location where the drive is mounted needs to be given.
The general syntax to unmount is:
$ sudo umount <destination>
Or,
$ sudo umount <source>
Example 1:
$ sudo umount /mnt/crucial
Example 2:
$ sudo umount /dev/sdb1
--recursive
flag unmounts all the drives mounted in the given location. For example the command
$ umount --recursive /mnt
will unmount all drives mount in the /mnt
location.
If umount
is unable to unmount a drive due to some reason (say, still in use), you can unmount it ‘lazily’ using -l
flag. It will completely halt the job and prevent the system from hanging during reboot.
umount -l /mnt
Unmount using udisksctl
To safely remove and power off a drive, use the command:
udisksctl power-off --block-device=/dev/sdc
It will close all the processes associated with the drive. All files, if any, will be written to the device. It is similar to the ‘eject’ in Windows. Now, you can safely eject the hard disk from your pc.
Unmount using gio
To unmount using gio, you just need to supply the location where the device is mounted:
$ gio mount --unmount /run/media/ajay/DDF8-FB82
On the other hand, gio mount --stop
ejects the device. For example,
$ gio mount --stop=/dev/sda
Conclusion
That’s all folks. To know more about them read their man pages. And if you have any queries or suggestions, use the comment section below. Thanks.
this text made me want to ride even more on the attractive material. (original text: ce texte m’a donné envie de rider encore davantage sur la matière attractive.)
Pingback: How to send notifications in linux using dunstify/notify-send | SmartTech101