24

Currently I have Linux Mint installed on my PC with a USB hard drive partition mounted as /home. This is working well.

If I install a second USB hard drive, is there any chance Linux will get confused between the two, and try mount the second hard drive's partition as /home on boot? That would be bad.

Coming from Windows, I've seen it happen often that drive letters are not "remembered" correctly causing all sorts of issues.

I guess the main question is: How does Linux actually know which USB hard drive is /dev/sdb and which is /media/misha/my_2nd_drive?

4
  • with a USB hard drive partition mounted as /home Can you please explain how you did this? Commented Sep 22, 2015 at 12:43
  • I did it during Linux Mint installation. At that point it's quite easy. My SSD appeared as sda and the USB hard drive appeared as sdb. I put / and swap on sda, and /home on sdb. But I wouldn't know how to change an existing installation's /home mount, too new for that!
    – misha256
    Commented Sep 22, 2015 at 20:40
  • The answers say what you should do, but I don't think they directly say that sd[a-z] are simply named in detection order. No attempt at all is made at keeping the names the same. It's typically deterministic (same kernel on same HW will give the same naming), but a new kernel with an update to the SATA-controller driver could make it scan the SATA drives in the opposite order. Instead of even trying to rename block devices (as is done with nameif or w/e for ethernet device names, usually with udev rules), UUID, label, and id (by drive serial num) names exist. Commented Sep 23, 2015 at 9:08
  • I've seen servers with SAS disks where rebooting would usually lead to a different ordering of sd[a-z]: the probing of disks is done simultaneously (in parallel) and whatever disk happens to be detected first becomes sda, etc.
    – wurtel
    Commented Sep 24, 2015 at 9:28

2 Answers 2

26

Usually the location of the USB port (Bus/Device) determines the order it's detected on. However, don't rely on this.

Each file system has a UUID which stands for universally unique identifier (FAT and NTFS use a slightly different scheme, but they also have an identifier that can be used as a UUID). You can rely on the (Linux) UUID to be unique. For more information about UUIDs, see this Wikipedia article.

Use the disk UUID as a mount argument. To find out what the UUID is, run this:

$ sudo blkid /dev/sdb1

(blkid needs to read the device, hence it needs root powers, hence the sudo. If you've already become root, the sudo is not needed.)

You can then use that UUID in /etc/fstab like this:

UUID=7e839ad8-78c5-471f-9bba-802eb0edfea5 /home ext4 defaults 0 2

There can then be no confusion about what disk is to be mounted on /home.

For manual mounting you can use /dev/disk/by-uuid/.....

2
  • 1
    Awesome, I didn't even know about fstab until now (that's how new I am to Linux). I've looked at my fstab file and everything is already identified by UUID. The Linux Mint installer did good.
    – misha256
    Commented Sep 22, 2015 at 9:48
  • You can improve this answer by explaining briefly what the UUID is, and why it's unique which make it a reliable identifier. Commented Sep 22, 2015 at 12:44
11

If your fstab references partitions as /dev/sdx, then adding a new drive can certainly confuse the system. The recent practice, however, is to use Partition UUIDs to identify the partitions.

If your system has udev installed (must be the case for 2.6+ Linux kernels), you can use persistent naming in fstab. By default, partitions are identified via UUIDs:

/dev/disk/by-uuid/31f8eb0d-612b-4805-835e-0e6d8b8c5591

You can, however create a meaningful partition label like "home" and reference your partition like this:

/dev/disk/by-label/home

Those IDs can be used as a replacement for /dev/sdx in fstab.

7
  • I wish I could mark your answer correct too. Wurtel was in a touch earlier. But thank you anyway, you get my +1.
    – misha256
    Commented Sep 22, 2015 at 9:55
  • @misha256 The irony is that I don't even use UUIDs on my systems (because the output from df and mount becomes unreadable). Internal HDDs have quite high chances of being detected first, and in the same order every time. Commented Sep 22, 2015 at 10:05
  • 1
    If you use labels, you can use LABEL=home in your fstab. I like to use labels like t-home for the home directory on my machine called tesla. So if I stick the disk in another system, there won't be two filesystems with the same label. This is why Linux installers generate random UUIDs and use them in fstab, instead of having default labels that would give most systems the same labels for their FSes. @misha256, you might want to think about this if you label your FSes. Commented Sep 22, 2015 at 22:52
  • 1
    @PeterCordes I do indeed use labes, and I have a naming scheme aimed at making these labels unique. Commented Sep 23, 2015 at 8:17
  • 1
    UUIDs are great and work as noted in the answers. But, they are not human friendly. Whenever I create partitions (anywhere), I give them unique human readable labels that make sense to me. This is especially helpful when writing backup scripts dealing with several drives. You don't want to get a source and destination reversed by accident! Also, whenever you reformat a partition, it gets a new UUID and you have to edit anything that used that UUID, whereas you can still use the same label as before and not have to change anything.
    – Joe
    Commented Sep 26, 2015 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .