How to mount disks from the command line

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,490
Reaction score
3,224
Credits
31,265
Let us say I have a disk located at /dev/sdb1 and I want to mount it.

Almost all distro's have a mount point called /mnt

/mnt is simply an empty directory. You create you own mount points, simply by creating directories.

Code:
mkdir /mnt2

Now to mount the disk, the easiest way is by the device path. This is great if you're only temporarily mounting a disk for a short time.

Code:
mount /dev/sdb1 /mnt

The only problem is, next time you reboot, the disk won't be mounted. So how do we make this more permanent?

You edit the /etc/fstab file.

Before
Code:
# /etc/fstab
# Created by anaconda on Tue Oct 29 17:21:01 2024
#
# 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.
#
UUID=1d4948ad-1029-4d1f-9692-d5cbaf206a6d /                       xfs     defaults        0 0
UUID=a82b9479-3944-461e-95d7-1128ddaaebc9 /boot                   xfs     defaults        0 0
UUID=979C-4DC8          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
UUID=f41546e4-5afe-4648-ae12-53b29edf0ba4 /home                   xfs     defaults        0 0
UUID=7a1b35db-077e-4631-b877-2d31addf48ef /var                    xfs     defaults        0 0
UUID=fc45c653-912e-4ba8-83b4-6c695ab69f1c none                    swap    defaults        0 0

After
Code:
# /etc/fstab
# Created by anaconda on Tue Oct 29 17:21:01 2024
#
# 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.
#
UUID=1d4948ad-1029-4d1f-9692-d5cbaf206a6d /                       xfs     defaults        0 0
UUID=a82b9479-3944-461e-95d7-1128ddaaebc9 /boot                   xfs     defaults        0 0
UUID=979C-4DC8          /boot/efi               vfat    umask=0077,shortname=winnt 0 2
UUID=f41546e4-5afe-4648-ae12-53b29edf0ba4 /home                   xfs     defaults        0 0
UUID=7a1b35db-077e-4631-b877-2d31addf48ef /var                    xfs     defaults        0 0
UUID=fc45c653-912e-4ba8-83b4-6c695ab69f1c none                    swap    defaults        0 0
/dev/sdb1          /mnt           exfat         defaults     0  0

Notice I have a new line at the bottom of my fstab file. This is the quick and dirty way to do this, but it has some disadvantages.
First if my USB drive with /dev/sdb1 is missing, now my system won't reboot, I'll have to login in emergency mode and fix my fstab.
Also, if I put another USB in, the letters can move around, drive one might be sda, but drive two is is sdb, even if I remove
drive one, it will remain sdb until I reboot, then it will go back to being sda.... * sigh * too much to remember.

So one way to get around this is, to use the block ID instead of the devie path.

Code:
blkid /dev/sda2
/dev/sda2: UUID="ff67396f-0b29-45ec-aa46-ccce1dd3537b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="6fed2b33-1fcd-42b1-8bb3-78cd63aa3783"

Now I would this line to the bottom of my /etc/fstab file.

Code:
UUID="ff67396f-0b29-45ec-aa46-ccce1dd3537b"        /mnt           exfat         defaults     0  0

Now it doesn't matter if my USB stick is in slot one or slow 2, or is sda. or sdb, or even sdc. Now
the OS will find it by the block id.

There is yet a third way to do this. Add this line to your /etc/fstab file.

Code:
LABEL=DATA /data/ auto nosuid,nodev,nofail,x-gvfs-show 0 0

And run this command on the disk you want to mount.

Code:
e2label /dev/sdb2 DATA

Don't forget to create your mount point.

Code:
mkdir /data

That will "label" that disk partition as DATA.

Now when you reboot the system, it doesn't matter if the disk is at sda, sdc, sdf, or whatever. Also, because of the "nofail"
option, the computer will boot even if the disk is missing. Make sure you only have one disk labeled as DATA.
But of course you can have several disks labeled differently and have a label based mount point for them.

So we have path based mounts,
UUID based mounts,
and label based mounts.

How to unmount a disk?

Code:
/umount /mnt

or

Code:
umount /data

Note that with label based mounts, you still use the mount point to mount and unmount.
 
Last edited:


/usr/sbin/tune2fs also allows setting the volume label for a freshly checked file system, along with so many other things.

It is generally a good idea to mount using a UUID and one must make sure that all UUIDs are in fact unique. I use a small bash script to handle this kind of thing. This way I can specify all of the mount options on that command line. I also add them, except for the cold storage drive, to /etc/fstab.

Signed,

Matthew Campbell
 


Members online

No members online now.

Latest posts

Top