Skip to main content
CAN BUS · LINUX

What Is SocketCAN?

SocketCAN is the CAN bus support built into the Linux kernel. It treats each CAN interface as a network device, so software can send and receive CAN messages through standard sockets, and common tools such as candump and cansend work with any supported CAN adapter or onboard CAN port.

01

What SocketCAN is

SocketCAN is the CAN bus support built into the Linux kernel. It treats each CAN controller as a network interface, named can0, can1 and so on, and lets programs send and receive CAN frames through the same socket calls they would use for a network connection. It was contributed largely by Volkswagen Research and merged into the mainline kernel in version 2.6.25 in 2008.

Before SocketCAN, each CAN adapter came with its own driver and programming interface, so software written for one card had to be rewritten for another. SocketCAN gives Linux one standard way to use CAN: applications open a socket in the PF_CAN protocol family, and the same code works whichever hardware sits underneath, from a USB adapter to a CAN controller inside the processor. Our explainer on what CAN bus is covers the network itself.

02

How SocketCAN works

SocketCAN slots CAN into the kernel’s networking layer. A device driver handles the CAN controller and presents it as a network device; the CAN protocol layer then offers several socket types to applications:

  • //
    Raw sockets. Send and receive individual CAN or CAN FD frames, with optional filters so a program only receives the identifiers it asks for.
  • //
    Broadcast manager. Lets the kernel send cyclic messages and watch for changes, so an application does not have to time every frame itself.
  • //
    ISO-TP. Transport protocol (ISO 15765-2) that splits messages longer than one frame, used for vehicle diagnostics. It has been in the mainline kernel since 5.10.
  • //
    J1939. A kernel implementation of the SAE J1939 transport and addressing used on trucks and machinery, available since kernel 5.4.
03

Setting up a CAN interface

Because a CAN port is a network interface, it is configured with the standard ip tool from iproute2. The bit rate must match the rest of the bus, and the interface must be brought up before use. For a 500 kbit/s network, for example, the commands are ip link set can0 type can bitrate 500000 followed by ip link set can0 up. CAN FD needs a data bit rate as well, set with dbitrate and fd on.

The same tool shows error counters and the bus state, and a restart-ms setting lets the controller recover automatically if it goes bus-off after repeated errors. For testing without hardware, the vcan module creates a virtual CAN interface, vcan0, that software can talk to as if it were a real bus.

04

The can-utils tools

The can-utils package provides command-line tools that work with any SocketCAN interface. They are usually the first thing an engineer reaches for when bringing up a new CAN connection.

Tool What it does Typical use
candump Prints or logs every frame received, with optional filters Checking traffic on a new bus; recording a log
cansend Sends a single frame with a given identifier and data Testing a device’s response to a command
cangen Generates random or patterned frames Load testing and checking wiring
cansniffer Shows which identifiers change and how Working out what an unknown message means
canplayer Replays a candump log onto an interface Reproducing a fault on the bench
canbusload Calculates bus load per interface Checking a network has headroom

For application code, C programs use the socket API directly, and Python programs commonly use the python-can library, which has a SocketCAN back end.

05

Hardware that works with SocketCAN

Any CAN controller with a SocketCAN driver in the kernel can be used this way. That includes the CAN controllers built into many embedded processors, such as the controllers on NVIDIA Jetson modules supported in Linux for Tegra, SPI-connected CAN chips on carrier boards, and many USB and PCIe adapters. When choosing an adapter for Linux, check that its driver is in the mainline kernel or supplied by the maker, and whether it supports CAN FD.

Our CAN bus I/O modules include USB, mPCIe and PC/104 options for adding ports to a computer, and many in-vehicle computers have isolated CAN ports on board.

FAQ · 01
What do fleet telematics systems do with CAN data?
They read engine, fuel, speed and fault data from the vehicle’s CAN or J1939 network and send it, with GPS position, to a central platform. Fleet managers use it to track vehicles, plan maintenance and monitor driving.
Read the full FAQ —
06

Where SocketCAN is used

SocketCAN is how a Linux in-vehicle computer joins the vehicle network. It logs engine and body signals, reads J1939 data on trucks and machinery, and sends commands to equipment such as lifts or spreaders. In the refuse collection vehicle case study, vehicle signals are used alongside camera detection. It is also used on robots and AGVs, where CANopen drives and battery systems share a bus, and in test benches that simulate or record vehicle traffic. Because the interface is standard, the same logging or gateway software can move from a development PC with a USB adapter to an in-vehicle computer with onboard CAN without code changes.

07

Quick answers on SocketCAN

SocketCAN: common questions

Is SocketCAN only for Linux?

Yes. SocketCAN is part of the Linux kernel. Windows applications use the CAN adapter maker’s own driver and API, although libraries such as python-can hide the difference.

What is can0?

The first CAN interface on the system. SocketCAN names CAN ports can0, can1 and so on, in the same way Ethernet ports are named eth0 or similar.

Does SocketCAN support CAN FD?

Yes. CAN FD has been supported since kernel 3.6, as long as the controller and its driver support it. Enable it with fd on and set a data bit rate.

What is vcan?

A virtual CAN interface. It lets software and tools send and receive frames with no hardware attached, which is useful for development and automated testing.

How do I see CAN traffic on Linux?

Run candump. With the interface up at the right bit rate, candump can0 prints every frame on the bus with its identifier and data.

We fit CAN ports and modules to Linux in-vehicle and edge computers as standard, install the drivers and check each interface with SocketCAN before the system ships. Tell us your bit rate, protocol and connectors, and we will specify the interface to match.