{"id":2523,"date":"2023-03-18T20:32:19","date_gmt":"2023-03-18T15:02:19","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2523"},"modified":"2023-03-22T17:15:09","modified_gmt":"2023-03-22T11:45:09","slug":"how-to-mount-a-drive-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-mount-a-drive-in-linux\/","title":{"rendered":"How to Mount a Drive in Linux"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
The Basic structure of Where, The name of the Example<\/strong>:<\/p>\n\n\n\n You can also omit the You can also mount your drives using UUID:<\/p>\n\n\n\n To find a device’s UUID, look into Similarly, you can also use the PARTUUID variable to identify your partition:<\/p>\n\n\n\n Here, to find the PARTUUID of a drive, look into Using PARTUUID is more robust since it is independent of the file system and hence, cannot be changed by You can also use the Where, The result can be seen in the output of To get changes in one directory reflected in another, you need to use ‘bind mounts’. The flag Command structure:<\/p>\n\n\n\n Example:<\/p>\n\n\n\n Create these directories and tries to create some files, do many things. You can see that the contents of both are the same.<\/p>\n\n\n\n \ud83d\udcdd Note<\/strong>: to mount a hard disk or SSD automatically at boot, you will need to set the corresponding entries in the fstab<\/a> file To install<\/strong> udisks, execute one of the following commands:<\/p>\n\n\n\n Debian<\/strong><\/p>\n\n\n\n Ubuntu<\/strong><\/p>\n\n\n\n Alpine<\/strong><\/p>\n\n\n\n Arch Linux<\/strong><\/p>\n\n\n\n Kali Linux<\/strong><\/p>\n\n\n\n CentOS<\/strong><\/p>\n\n\n\n Fedora<\/strong><\/p>\n\n\n\n Windows (WSL2)<\/strong><\/p>\n\n\n\n Raspbian<\/strong><\/p>\n\n\n\nmount<\/code> command in Linux is a very powerful command. Using it you can do a lot of things.<\/p>\n\n\n\n
mount<\/code> command<\/strong>:<\/p>\n\n\n\n
$ sudo mount <source> <destination><\/code><\/pre>\n\n\n\n
<source><\/code> is the drive you want to mount and
<destination><\/code> is the location where you want to mount.<\/p>\n\n\n\n
<source><\/code> device can be obtained using the
lsblk<\/code> command:<\/p>\n\n\n\n
\u276f lsblk --paths\nNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS\n\/dev\/loop0 7:0 0 56G 0 loop \/mnt\/VHD\n\/dev\/nvme1n1 259:0 0 931.5G 0 disk \n\u251c\u2500\/dev\/nvme1n1p1 259:1 0 16M 0 part \n\u2514\u2500\/dev\/nvme1n1p2 259:2 0 931.5G 0 part<\/code><\/pre>\n\n\n\n
$ sudo mount \/dev\/nvme1n1p2 \/mnt\/crucial<\/code><\/pre>\n\n\n\n
<destination><\/code>. In that case,
mount<\/code> will search for it in the
\/etc\/fstab<\/code> file. So, you need to configure the file for this functionality.<\/p>\n\n\n\n
$ sudo mount \/dev\/sdb1<\/code><\/pre>\n\n\n\n
How to mount a device using its UUID<\/h3>\n\n\n\n
$ sudo mount UUID=bd4da364-6f99-4859-8ca4-326c89e9b11f \/mnt\/Crucial<\/code><\/pre>\n\n\n\n
\/dev\/disk\/by-uuid\/<\/code>. All the drives are symlinked:<\/p>\n\n\n\n
$ ls -al '\/dev\/disk\/by-uuid' \ntotal 0\ndrwxr-xr-x 2 root root 160 Mar 16 18:42 .\ndrwxr-xr-x 9 root root 180 Mar 16 18:41 ..\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 00E8EAD5E8EAC7CC -> ..\/..\/nvme0n1p3\nlrwxrwxrwx 1 root root 11 Mar 16 18:42 5627befe-b7a4-48c7-ae19-a71689a67d7e -> ..\/..\/loop0\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 B2E8-9A01 -> ..\/..\/nvme0n1p1\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 E4CA6C29CA6BF5E8 -> ..\/..\/nvme1n1p2\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 EAD2EB39D2EB08A1 -> ..\/..\/nvme0n1p4\nlrwxrwxrwx 1 root root 15 Mar 16 18:42 fc986af0-b252-4c73-a921-a7cb75eb4c5f -> ..\/..\/nvme0n1p5<\/code><\/pre>\n\n\n\n
How to mount a device using its PARTUUID<\/h3>\n\n\n\n
$ sudo mount PARTUUID=partuuid \/mnt\/crucial<\/code><\/pre>\n\n\n\n
\/dev\/disk\/by-partuuid\/<\/code> directory.<\/p>\n\n\n\n
mkfs<\/code> and
mkswap<\/code> command. On the other hand, UUID gets changed when we format a drive.<\/p>\n\n\n\n
How to mount iso images and virtual hard disks in Linux<\/h3>\n\n\n\n
mount<\/code> command to mount an
*.iso<\/code> image to read\/copy its content. The
mount<\/code> command can also mount virtual Hard disks<\/a>. For this, use
-o loop<\/code>:<\/p>\n\n\n\n
$ sudo mount -o loop \/media\/VHD.img \/mnt\/VHD\/<\/code><\/pre>\n\n\n\n
$ sudo mount -o loop \/media\/iso_file.iso \/mnt\/iso\/<\/code><\/pre>\n\n\n\n
loop<\/code> is used to mount the file to loop device (
\/dev\/loopn<\/code> where,
n<\/code> is the next remaining loop number). you can use
loop=\/dev\/loop0<\/code> instead of
loop<\/code> to mount at the
loop0<\/code>.<\/p>\n\n\n\n
df<\/code>:<\/p>\n\n\n\n
$ df \nFilesystem 1K-blocks Used Available Use% Mounted on\ndev 7060864 0 7060864 0% \/dev\nrun 7081468 1380 7080088 1% \/run\n\/dev\/nvme0n1p5 57135216 33081828 21118604 62% \/\ntmpfs 7081468 67376 7014092 1% \/dev\/shm\ntmpfs 7081468 3368 7078100 1% \/tmp\n\/dev\/nvme1n1p2 976744444 552065892 424678552 57% \/mnt\/crucial\n\/dev\/nvme0n1p1 262144 35772 226372 14% \/boot\/EFI\n\/dev\/loop0 57483360 18569620 35961344 35% \/mnt\/VHD<\/span>\ntmpfs 1416292 3160 1413132 1% \/run\/user\/1000<\/code><\/pre>\n\n\n\n
What are ‘bind mounts’ in the mount command in Linux<\/h3>\n\n\n\n
--bind<\/code> in mount enables you to use this feature.<\/p>\n\n\n\n
sudo mount --bind <source> <destination><\/code><\/pre>\n\n\n\n
$ sudo mount --bind dir1 dir2<\/code><\/pre>\n\n\n\n
\/etc\/fstab<\/code>. After the fstab entry, your computer automatically unmounts your drive before shutting itself down as can be seen in the
journalctl | grep <location><\/code>:<\/p>\n\n\n\n
$ journalctl | grep \/mnt\/Maxtor\n\n'unmounted \/mnt\/Maxtor'<\/code><\/pre>\n\n\n\n
How to mount a device in linux using udiskctl<\/h2>\n\n\n\n
apt-get install udisks2<\/code><\/pre>\n\n\n\n
apt-get install udisks2<\/code><\/pre>\n\n\n\n
apk add udisks2<\/code><\/pre>\n\n\n\n
pacman -S udisks2<\/code><\/pre>\n\n\n\n
apt-get install udisks2<\/code><\/pre>\n\n\n\n
yum install udisks2<\/code><\/pre>\n\n\n\n
dnf install udisks2<\/code><\/pre>\n\n\n\n
sudo apt-get update sudo apt-get install udisks2<\/code><\/pre>\n\n\n\n
apt-get install udisks2<\/code><\/pre>\n\n\n\n