This guide explains how to automatically set up the CAN interface (can0
) using a systemd
service file. Follow these steps to enable the CAN interface with a specified bitrate when the device is connected.
-
Create a new file named
/etc/systemd/system/setup-can0.service
:sudo nano /etc/systemd/system/setup-can0.service
-
Add the following content to the service file:
[Unit] Description=Setup CAN0 interface Wants=network-pre.target Before=network-pre.target BindsTo=sys-subsystem-net-devices-can0.device After=sys-subsystem-net-devices-can0.device [Service] Type=oneshot ExecStart=/sbin/ip link set can0 up type can bitrate 500000 ExecStop=/sbin/ip link set can0 down RemainAfterExit=yes [Install] WantedBy=multi-user.target
- The
[Unit]
section specifies dependencies and the order of activation. - The
[Service]
section defines the commands to set up and tear down the CAN interface. - The
[Install]
section determines when the service should be activated.
- The
Run the following commands to reload systemd
and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable setup-can0.service
To start the service immediately without rebooting:
sudo systemctl start setup-can0.service
After enabling the service, disconnect and reconnect your CAN hardware. The can0
interface should be configured automatically with the specified bitrate.
If the service doesn’t start as expected, check the status and logs using:
sudo systemctl status setup-can0.service
sudo journalctl -xe
This systemd
service method is suitable for automatically configuring CAN devices on boot or when the hardware is connected, making it ideal for persistent and reliable configurations.