How do you swap /dev/sda with /dev/sdb?

I have the same problem to exchange the sda and sdb disk name. I tried to write some similar udev rules with the above post on my own HP servers. But I used the size of the disk in /etc/udev/rules.d/00-corrections.rules

KERNEL=="sd?", ATTR{size}=="781357232", NAME="sda"
KERNEL=="sd??", ATTRS{size}=="781357232", NAME="sda%n"
KERNEL=="sda", ATTR{size}=="3125515952", NAME="sdb"
KERNEL=="sda?", ATTRS{size}=="3125515952", NAME="sdb%n"

Before this rule I find the size of devices by cat /sys/block/sda/size and cat /sys/block/sdb/size which is described here : Finding information from sysfs.

But when I try to test the udev rule by udevadm test /sys/block/sdb I see this line in the output:

NAME="sda" ignored, kernel device nodes cannot be renamed; please fix it in /etc/udev/rules.d/00-corrections.rules:1

I have ubuntu 18.04 and I found that this is impossible (at least in ubuntu 18.04) based on this post: Is there a way to change device names in /dev directory?


These days, the Linux kernel dynamically populates /dev/ according to UDEV rules.

Let me first explain how device files work. Each device file, typically a block device file, has a major and a minor number. These numbers actually describe what device the file points to. The name does not play any role in this. Let's have a look at our specific case of disks:

# ls -l sd*
brw-rw---- 1 root disk 8, 0 Aug 22 15:45 sda
brw-rw---- 1 root disk 8, 1 Aug 22 15:45 sda1
brw-rw---- 1 root disk 8, 2 Aug 22 15:45 sda2
brw-rw---- 1 root disk 8, 3 Aug 22 15:45 sda3
brw-rw---- 1 root disk 8, 5 Aug 22 15:45 sda5
brw-rw---- 1 root disk 8, 6 Aug 22 15:45 sda6

Here you see that my first disk has various partitions and that I booted on Aug 22, at 3pm, which is when the kernel created the files according to rules. You can also see that the major number is 8 and the minor numbers are used to access partitions (0 pointing to the whole disk). The 'b' in the beginning of each line tells that each of these is a special "block device" file.

As I said, the kernel creates the files dynamically "these days". It was not always like that and it is not like that on other Unix systems. There, files would be created statically, and, the user would create or manipulate these files.

It is perfectly possible to create your own device files, with your own name, and major/minor numbers. See mknod (man mknod) for that. However, after you boot again, your custom files will disappear.

The second possibility is to change UDEV rules. The rules will be processed during system boot, and guarantee you a permanently consistent behavior. A good guide on these rules ca be found here: http://www.reactivated.net/writing_udev_rules.html

You will see that it is possible to define a rule that creates "sda*" given specific hardware info that matches your device. You will need to replace the original rules that would create sda with yours. How this works depends on your distribution.

As I think this is a dangerous business for the novice, I will not explain you specific steps; the document I linked above will give you all information you need, and you should indeed read it all.