I have multiple similar USB network devices, in this case CAN (can0 and can1) connections to different networks but I figure it is a similar problem with multiple other networks (eg WiFi, Ethernet etc). These currently show as can0 and can1 and I may connect more.

I am trying to maintain some sort of persistency in the locations for access to each. For non-network devices using udev allows custom symlinks to be referenced to the idVendor, idProduct, and serial numbers so I can ensure the correct code accesses the correct device.

How can à similar persistency be achieved for divices under ifconfig? I have the IDs and serial details for each ready to reference

I don't want device x to always be can0 and device y to always be can1. As there might only be one device which I think would need to be can0. But if they are plugged in differently or started in a different order then I want to check that can0 is device x or y and run the correct code, or if not run the alternative.

I figured this is the better approach than the udev logic but can't seem to find out the methodology

Hoping for some pointers on this one. TIA

1 Answer 1


You can do this with udev:

# /etc/udev/rules.d/99-usb-can.rules

ACTION=="add", SUBSYSTEM=="net", ENV{ID_VENDOR_ID}=="...", ENV{ID_PRODUCT_ID}=="...", ATTR{...}=="...", NAME="myCustomCan", RUN+="/path/to/script.sh myCustomCan"

Use as many ENV/ATTRs as you need to identify your can device. ENV variables are more stable than ATTRs so they should be preferred.

# /path/to/script.sh


sudo ip link set $INTERFACE type can bitrate 500000
sudo ip link set $INTERFACE up 

Useful commands:

sudo udevadm info /sys/class/net/can0 - Get a list of ENV variables and values.

sudo udevadm info -a /sys/class/net/can0 - Get a list of ATTR/ATTRS variables and values.

sudo udevadm control --reload-rules && sudo udevadm trigger - Reload udev rules.

sudo udevadm control --log-priority=debug - Set udev logging output to debug mode.

tail -f /var/log/syslog | grep -A 1 --color 'can0|myCustomCan' - Get output of udev to check if interfaces are detected properly. You need to reconnect the device to see some output.

ip -details link show myCustomCan - This also shows the configuration (e.g. bitrate).

I used these commands on Ubuntu 20.04. They may be different on other distributions.

  • Thank you for this. I just spotted it so about to test next week
    – ShaunMc
    Commented Mar 18, 2023 at 19:51

You must log in to answer this question.

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