SocketCAN Basic Configuration
Use the SocketCAN Command Generator to configure, bring up or down, restart and inspect interfaces.
Core Concepts
Linux registers SocketCAN-capable devices as network interfaces such as can0 and can1. Replace can0 below with your actual interface name. Match the bit rates of the other bus nodes. CAN FD has a separately configured data bit rate.
Environment Preparation
On Debian / Ubuntu:
sudo apt install iproute2 can-utilsKernel Support Check
Install the Linux driver appropriate to the device and current firmware. For the kcan driver supplied in the KH-UCANFD Linux SDK, follow the SDK installation instructions before loading kcan. Other firmware modes require their corresponding drivers.
lsmod | grep can
sudo modprobe can
sudo modprobe can_rawlsmod lists loadable modules; built-in drivers may not appear. Check interface availability and driver logs as well.
Device Identification
# USB devices and drivers
lsusb -t
# Physical CAN interfaces
ip -details link show type can
# A specific interface (also usable with vcan0)
ip -details link show dev can0Device Configuration
Bring the interface down before changing bit timing; this interrupts traffic on that channel. Choose one configuration below. Changing interfaces requires administrator privileges or CAP_NET_ADMIN. Supported rates and modes depend on the controller and driver.
Classic CAN
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can bitrate 500000 fd off listen-only off restart-ms 0 &&
sudo ip link set dev can0 upCAN FD
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can bitrate 500000 dbitrate 2000000 fd on listen-only off restart-ms 0 &&
sudo ip link set dev can0 up&& runs each step only if the previous step succeeds. fd on enables FD capability on the interface; the transmit command separately controls BRS. The data bit rate must not be lower than the nominal bit rate.
To specify sample points, configure the following while the interface is down. Choose values appropriate to the bus and controller:
sudo ip link set dev can0 type can bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.8 fd onUse the CAN Baud Rate Calculator for detailed bit timing.
Controller mode and interface state
Listen-only mode requires driver support. It neither transmits frames nor acknowledges received frames. Bring the interface down before changing the mode:
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can listen-only on &&
sudo ip link set dev can0 upChange listen-only on to listen-only off to restore normal operation. To bring an interface up or down separately:
sudo ip link set dev can0 up
sudo ip link set dev can0 downInterface Status
# Bit timing, controller state, traffic and error counters
ip -details -statistics link show dev can0
# All physical CAN interfaces
ip -details -statistics link show type can| Field | Meaning |
|---|---|
UP flag | Administratively enabled; it does not mean the bus is error-free |
CAN state ERROR-ACTIVE | Controller is error active |
CAN state ERROR-PASSIVE | Transmit or receive error count reached the error-passive threshold |
CAN state BUS-OFF | Controller has left the bus and cannot communicate normally |
CAN state STOPPED | Controller is stopped |
berr-counter tx/rx | Controller transmit/receive error counters; availability depends on the driver |
RX/TX errors/dropped | Network-interface statistics, distinct from controller error counters |
mtu 16 / mtu 72 | Classic CAN / CAN FD frame structure size, not payload byte count |
Growing error counts can result from rate, wiring, termination, ACK or interference problems. The count alone does not identify the cause. Controller error counting has multiple rules and exceptions; it is not simply a count of lost frames.
Error Handling
Bus-Off recovery
First check bit rates, wiring, termination and other nodes. After resolving the bus fault, manually restart an UP interface in Bus-Off with automatic restart disabled (restart-ms 0):
sudo ip link set dev can0 type can restartAlternatively, configure automatic restart, for example with a 100 ms delay:
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can restart-ms 100 &&
sudo ip link set dev can0 uprestart-ms 0 disables automatic restart. Inspect the state again after recovery. Repeated Bus-Off requires resolving the underlying bus fault.
Next: Send and receive frames with can-utils. Reference: Linux kernel SocketCAN documentation.