3

In Ubuntu server 18.04, what is the best way to use netplan to configure ethernet interfaces, AND configure and use CAN interfaces?

I can successfully use netplan to configure my ethernet interfaces, but I can't find much about how to also get CAN devices configured and running along-side it.

In the past, we'd use the /etc/network/interfaces file to specify CAN bus configuration, but I'd like to move away from that with an eye towards upgrading to ubuntu 20 in the future (where netplan actually supports can busses)

I can use ip link set can0 type can bitrate 500000 to manually configure my can interface, but that's not persistent. I can put that into a service that starts at boot... but that feels pretty jenk to me.

Any better suggestions?

1 Answers1

1

Update

As Ollie pointed out, Ubuntu 18.04 comes equipped with systemd v237, which doesn't support configuring CAN interfaces. CAN support was added in this commit, which was first released in systemd v239. You can check your version of systemd by typing systemctl --version. If you can, you should update to Ubuntu 20.04 to use this functionality.

If you're stuck using Ubuntu 18.04, then you could manually update your version of systemd, but this is generally considered to be a bad idea.


Original Answer

I ran into into a similar issue. At the moment it doesn't look like netplan supports can interfaces, which is a bit of a bummer. I did find, though, that systemd-networkd does support can and vcan interfaces. The following is untested - I'm waiting for CAN hardware to arrive - but should work.

  1. Add the can modules to /etc/modules so that they get loaded at boot. (i.e., cat, raw-can, can-dev, mttcan, etc.)

  2. Create a new file under /etc/systemd/network/ with a .network extension and with the following content:

     [Match]
     Name=can0
    

    [CAN] BitRate=500000

  3. Make sure that systemd-networkd is enabled on boot. Warning - this could cause issues if you are already running NetworkManager and both networkd and NetworkManager try to configure the same interfaces... Use with caution

     sudo systemctl enable systemd-networkd
    

Now whenever you boot your system your can0 interface should be configured to the specified bitrate.

Additionally, if you want to specify that your link is 'up', then you can add an extra argument to your .network file:

[Link]
ActivationPolicy=up

As a side note, networkd can also be used to bring up a vcan network. In this case you need to make a .netdev file specifying the virtual network you want to create and a .network file to automatically bring up the interface on boot.

Useful documentation:

https://www.freedesktop.org/software/systemd/man/systemd.network.html

https://www.freedesktop.org/software/systemd/man/systemd.netdev.html

Related forums/issues:

https://github.com/linux-can/can-utils/issues/68

https://github.com/systemd/systemd/issues/4042

https://unix.stackexchange.com/questions/178871/systemd-networkd-wont-reload-virtual-network-interface-configuraion-file

https://superuser.com/questions/1450668/network-configuration-systemd-networkd-vs-networking-service-and-compatiblity

/etc/network/interfaces, systemd-networkd and NetworkManager: how do they coexist together?

  • Given that this question is about Ubuntu 18.04, it's worth pointing out that CAN support was added in version 239 whereas 18.04 uses version 237. Apparently the first version of Ubuntu to support this was 19.04 but as that is now end of life you will need Ubuntu 20.04 LTS or later. Source: this comment in the can-utils GitHub issue 68. – Ollie Oct 27 '21 at 10:18