SocketCAN on Linux: Set Up a Virtual vcan Interface with can-utils
Create a virtual CAN interface with ip link, then send and capture frames with cansend and candump from can-utils. No CAN hardware needed.
Contents
SocketCAN is the Linux kernel’s CAN bus layer. It exposes CAN controllers as network interfaces, so you can work with them using the Berkeley socket API and ip. The vcan driver provides a virtual interface, which lets you practice with CAN tools without any hardware.
What SocketCAN gives you
The kernel documentation describes SocketCAN as an implementation of CAN protocols that uses the Berkeley socket API. A program opens a raw CAN socket and binds it to an interface by index. Binding to index 0 receives frames from all CAN interfaces.
A classical CAN frame carries a CAN ID and 0 to 8 bytes of payload. CAN FD frames can carry up to 64 bytes.
The can-utils project is a collection of userspace tools built on these sockets. This article uses three:
cansendsends a single frame.candumpdisplays, filters, and logs frames.cangengenerates random traffic.
The project also includes tools for ISO-TP, J1939, bus load measurement, and log replay with canplayer.
Install can-utils
The package is named can-utils on Debian, Ubuntu and Fedora. On other distributions, check your package manager or build from the GitHub project.
apt-get update
apt-get install -y can-utils iproute2dnf install -y can-utils iprouteCreate a virtual CAN interface
The kernel documentation shows how to create vcan devices with ip link:
ip link add type vcan # automatic name
ip link add dev vcan42 type vcan # explicit name
ip link del vcan42 # remove itWhen vcan is built as a module, the driver is vcan.ko. The documentation does not list a modprobe step. The commands below load the module by name and need a kernel that provides vcan. A container shares the host kernel, so run them on a real machine or VM:
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
ip -details link show vcan0Unlike a physical interface such as can0, a vcan interface needs no bitrate. For real hardware you configure the bitrate with ip link set can0 up type can bitrate 500000. See the ip command guide for the general syntax.
Send and capture frames
Open two terminals. In the first, start a capture:
candump vcan0In the second, send frames:
cansend vcan0 123#DEADBEEF
cansend vcan0 5A1#11.2233.44556677.88
cansend vcan0 1F334455#1122334455667788
cansend vcan0 123#RThe frame syntax, taken from the cansend usage text:
123#DEADBEEF: a 3-digit hex ID,#, then up to 8 data bytes in hex. The usage text also shows dots between bytes, as in the second example.1F334455#...: an 8-digit ID selects the extended format.123#R: a remote transmission request.123##1...: a CAN FD frame, with##followed by one flag digit and then the data. FD support depends on the interface MTU, which the kernel documentation covers.
Useful candump options
All values below come from the candump usage text:
-t a|d|z|Aselects timestamps: absolute, delta, zero-based, or absolute with date.-n <count>stops after a number of frames.-Lprints in log file format.-xprints extra message info, such as RX/TX.-eprints error frames in readable form.
Filters follow the interface name, separated by commas, in the form can_id:can_mask. A frame matches when received_id & mask == id & mask. For example, candump vcan0,123:7FF shows only ID 123.
Generate load
cangen produces random frames, which helps when you test a filter or a logger:
cangen vcan0Stop it with Ctrl+C.
Loopback behavior
Per the kernel documentation, loopback is enabled by default. Frames one socket sends are delivered to other local sockets that match, which is why candump sees what cansend sends. A socket does not receive its own frames unless it sets CAN_RAW_RECV_OWN_MSGS.
Clean up
sudo ip link del vcan0Once you are comfortable here, the same tools work on real hardware. A physical can0 interface needs the bitrate setup shown above. For background on board hardware descriptions, see Device Tree basics.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.