Posted on 9 Comments

Assign fixed USB port names to your Raspberry Pi

Raspberry Pi 4 fixed USB port names

If you have multiple devices connected to the USB ports of your Raspberry Pi, the automatically assigned USB port number could unexpectedly change after a reboot and could end up confusing when using this USB port number in your scripts or Node-RED flows. Your USB device assigned to ttyUSB1 could be suddenly assigned the ttyUSB0 port number for example.

In this tutorial we’ll learn how to assign a fixed USB port name for each connected device. After completed this tutorial, you will have a USB device list with a fixed USB port name to each connected device. No possible mismatch anymore. Afterwards, it will allow you to use this USB device list with self-assigned names in your scripts.

Rock 4C+ kit

The Rock 4C+ board is a perfect alternative to the Raspberry Pi 4

Our starter kits are available and ready to be shipped !

Prepare your Pi

For this tutorial, you need to have your Raspberry Pi running on Raspberry Pi OS.

Figure out the connected devices

First, we need to find out which device has been assigned to which USB port number. The easiest way to figure this out is to disconnect all devices and reboot your Pi. Once your Pi is up and running, connect the first device and open the Terminal window.

Raspberry Pi terminal icon

At the command line we enter :

dmesg | grep ttyUSB

Raspberry Pi ttyUSB

  Start a list and write down the information of your first device :

  • the name of your connected device (you can’t find it in the terminal window, instead you give your device a name that will allow you to recognize it later) : in our case : DEVICE1
  • the characters after “usb” : in our case : 1-1.2
  • the automatically assigned USB port : in our case : ttyUSB0

Connect your second device now and enter the same command at the command line :

raspberry pi ttyUSB

Have a look at the newly added line on your Terminal and add following information to the list you just started :

  • the name of your second connected device : in our case : DEVICE2
  • the characters after “usb” : in our case : 1-1.3
  • the automatically assigned USB port : in our case : ttyUSB1

If you have more than 2 connected devices, continue this process by connecting one by one each additional device to your Pi.

Look for the attributes of your connected devices

To be able to recognize a connected device later, we’ll use some specific attributes of that device. We’re looking for the attributes which have unique properties. Typically these attributes are : vendor ID, product ID, serial number.

Using the automatically assigned USB port numbers we wrote down previously, we are able to find the attributes for all our devices. For the device connected to ttyUSB0 for example, we have to enter following command in the Terminal :

udevadm info --name=/dev/ttyUSB0 --attribute-walk

The info we get here, are all the attributes of the complete chain of devices linked to the specified USB port. We now have to take our previously written list and look for the characters after “usb”. In our case we look for : “1-1.2” .

Raspberry Pi attributes

In the list of attributes, we now have to choose some unique properties of the device. In our case we’ll take following 2 attributes :

  • idProduct : 7523
  • idVendor : 1a86

Write the attribute name and value down, as we’ll need them later.

Repeat the same command in the Terminal for your other USB port names : ttyUSB1 , … And again write down the gathered attributes and values. Make sure at least one value is different between all your devices. If necessary, to be able to distinguish your devices, you can choose more attributes.

Create a USB device list file with the accompanying USB port name rules

It’s time now to create the link from the USB ports to the devices. For this, we’ll create a file which will specify the rule for each USB device based on the unique properties we just wrote down.

In the Terminal, enter following command to access the file with the rules :

sudo nano /etc/udev/rules.d/10-usb-serial.rules

An empty file should appear on your screen. For each device we’ll write one line with the rule. At the end of the line, type the device name you have chosen previously. In our case “ttyUSB_DEVICE1” for example. Replace the code below with the attributes and the values with those of your written list. In our example it looks as follows :

SUBSYSTEM=="tty", ATTRS{idProduct}=="7523", ATTRS{idVendor}=="1a86", SYMLINK+="ttyUSB_DEVICE1"
SUBSYSTEM=="tty", ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", SYMLINK+="ttyUSB_DEVICE2"
raspberry pi USB port name rules

Once you’ve finished, use Ctrl + x to exit. And save the changes by entering “Y”.

Load the new rules

Enter following command in the Terminal to let the rules from the saved file take effect :

sudo udevadm trigger

Check the new USB port names

You can check the new names you just created by entering the following command in the Terminal :

ls -l /dev/ttyUSB*

Raspberry Pi new USB port names

As shown in the window above, you should see the new names (in light blue) of the USB ports. You can now use these names in your scripts or Node-RED flows. Even after rebooting or when the device has been disconnected, the name will always remain the same and there is no chance for confusing any more.

Great! You made it much more stable now to access USB connected devices to your Raspberry Pi.


If a self assigned USB port name doesn’t appear, there is a big chance you made a typo in the USB device list file with the rules. Check your attribute names and attribute values.

And by the way, if you are interested in learning to program electronic components on your Raspberry Pi, visit our shop. We have a nice kit which contains all the things you need to start.

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 43

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

9 thoughts on “Assign fixed USB port names to your Raspberry Pi

  1. Hello
    Great tips.
    But how to do this if all the USB devices have the same idVendor and same idProduct ( more than one Arduino Nano for example) ?

    1. Hi Laurent,
      Check any other attribute as the “bcdDevice” attribute for example. If there isn’t any difference, I suggest you to replace one of the boards with a similar one but from another manufacturer.
      Good luck!

      1. I have the same exact problem , I have two Arduino Mega (Original ) I found the Deference in Serial attribute , But when i list it to verify I found none of them has nick name .

        1. It is indeed an issue to use this method when you have exactly the same devices to be connected. I would suggest to replace one Arduino device by another clone version for example. I can’t guarantee it, but it will probably solve your problem.
          Another solution could be to make a serial connection with your Arduino throught the GPIO pins of your Pi.

  2. Works great, thank you

  3. How does one handle a USB device with multiple ports ? My cell modem uses /dev/ttyACM0 through /dev/ttyACM4. I need to access two of these ports even when re-enumeration occurs. When I use udevadm, idVendor and idProduct are the same, of course, for the multiple ports.

    That gets me to the second question. When re-enumeration occurs, I sometimes get a bogus /dev/ttyACM0, with characteristics different than legitimate modem ports. Why does it occur and how do I get rid of it ? Can I use ‘rm /dev/ttyACM0’ ? If I can get rid of the bogus port, can I force re-enumeration and get the correct ports ?

    1. If your cellular modem uses multiple USB ports, there is a big chance that there is a difference in one of the attributes. So take a good look at the attribute list and use the attribute with different values to make the difference between the 2 ports. If all the attributes have the exact same values, you won’t be able to tell them apart using this method.

      Regarding your second question, I have no idea why sometimes you get different modem ports. But if you can set up the USB rules as described in the tutorial it will probably solve your problem. I would not use ‘rm /dev/ttyACM0’.

  4. Very nice tutorial.

  5. Very useful tutorial. Thanks for this!

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 43

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter respects your privacy. Read our privacy policy on how we handle your personal information.