25

Is it possible to use UUIDs to mount drives, rather than using these values in fstab?

I have a script which mounts devices, however there is no way to guarantee that the drive labels such as /dev/sda2 will always be the same.

I'm aware I can mount the drive at boot time using this method with fstab, however in the case of external disks, they may not always be present at boot time.

1
  • 4
    FYI having an entry in fstab does not mean you need the drive connected at boot time. mount will use the data in fstab when called. Commented May 7, 2020 at 15:00

3 Answers 3

46

Yes it's possible, you just use the UUID option:

lsblk -o NAME,UUID NAME UUID sdc ├─sdc1 A190-92D5 └─sdc2 A198-A7BC sudo mount -U A198-A7BC /mnt 

Or

sudo mount UUID=A198-A7BC /mnt 

Or

sudo mount --uuid A198-A7BC /mnt 

The mount --help:

 Source: -L, --label synonym for LABEL= -U, --uuid synonym for UUID= LABEL= specifies device by filesystem label UUID= specifies device by filesystem UUID PARTLABEL= specifies device by partition label PARTUUID= specifies device by partition UUID specifies device by path mountpoint for bind mounts (see --bind/rbind) regular file for loopdev setup 
1
  • 2
    Linux is so great. Thanks, had no clue about these options Commented May 7, 2020 at 15:40
15

If you're interested in having an fstab entry for a drive which may not be present at boot time, you have two options which can help: noauto and nofail:

noauto: do not mount when "mount -a" is given (e.g., at boot time)

nofail: do not report errors for this device if it does not exist.

Imagine you have an fstab entry

UUID={YOUR-UID} /mnt/data ext4 defaults 

If you add noauto to the options, the system will not attempt to mount the drive at boot time. You'll be able to mount it manually with mount /mnt/data.

If you add nofail, the system will try to mount the drive at boot time, but if the drive is not present the boot sequence will not be interrupted. You'll be able to mount the drive if you plug it later using mount /mnt/data.

2
  • 2
    I guess the advantage of this is that users can mount without sudo as well? Commented May 8, 2020 at 14:09
  • 2
    @user3728501 if you add user option in the options column, then yes. Commented May 9, 2020 at 23:02
11

You can use the system-provided symlinks:

mount /dev/disk/by-uuid/{YOUR_UUID} /mnt 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.