LINUX
How To Use SocketCAN With The Command-Line In Linux
Date Published: | |
Last Modified: |
Contents
Overview
This tutorial shows you how to manipulate SocketCAN interfaces using the Linux command-line. This includes finding out what SocketCAN interfaces are available, printing detailed info about them, and then sending/receiving data.
If you are looking for help controlling a SocketCAN interface from C software, see the How To Use SocketCAN With C In Linux page.
If you are looking for more information about the CAN bus protocol itself, see the CAN Protocol page.

Icon for SocketCAN. Image from https://github.com/linux-can/can-utils.
What Types of CAN Interfaces Are There?
SocketCAN provides the following types of CAN interfaces:
- Native interfaces. These are CAN interfaces associated with real hardware (such as a USB-to-CAN adapter). They are named
canX
, e.g.can0
,can1
, … - Virtual interfaces. These are CAN interfaces that are not associated with any real hardware. They are named
vcanX
, e.g.vcan0
,vcan1
, … - SLCAN based interfaces. SLCAN interfaces provide a serial interface. They are named
slcanX
, e.g.slcan0
,slcan1
, e.t.c
Investigating SocketCAN Interfaces
First off, you want to print all the available ip interfaces and see if you have some can interfaces. This uses the iproute2
suite of tools:
|
|
The last interface listed is can0
. This is a SocketCAN interface!
Print SocketCAN Info
Native CAN devices that support SocketCAN can be seen using the iproute2
suite of tools. For example:
|
|
If you attempt it for a CAN device that does not exist:
|
|
An alternative way to investigate CAN interfaces is to use ifconfig <canx>
:
|
|
Configuring And Enabling The SocketCAN Interface
Before we can enable the SocketCAN interface, we need to configure it:
|
|
can0
that has been created when a CAN-to-USB dongle was connected). For virtual CAN interfaces, you must create the interface with a slightly different command:
|
|
Enabling a specific SocketCAN interface is also called “bringing the interface up”. To bring the connection up:
|
|
Note 1: If you ever get the following error: RTNETLINK answers: Operation not permitted
, try the command again with sudo.
Note 2: And if you get the error: RTNETLINK answers: Operation not supported
then try run sudo modprobe can
(or sudo modprobe vcan
) first.
Send/Receive Data On SocketCAN
If you then want to send/receive data on the CAN interface, you should install can-utils
:
|
|
The repository for can-utils can be found at https://github.com/linux-can/can-utils.
To send data to the CAN bus, use the cansend
utility:
|
|
The above command will send a CAN message on can0 with the identifier 0x123 and the data bytes [ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 ]
. Values are always treated as hexadecimal.
To display a list of received messages on the bus in realtime, use the candump
utility:
|
|
Authors

This work is licensed under a Creative Commons Attribution 4.0 International License .
Related Content:
- July 2017 Updates
- How To Change The IO Scheduling Class And Priority In Linux
- Command-Line CSV Viewer
- vim
- du (disk usage)