Skip to content

Instantly share code, notes, and snippets.

@manavortex
Forked from yonatanh20/README.md
Last active March 14, 2025 08:44
Show Gist options
  • Save manavortex/cdaf9540784808e5848cbec744d49a19 to your computer and use it in GitHub Desktop.
Save manavortex/cdaf9540784808e5848cbec744d49a19 to your computer and use it in GitHub Desktop.
Enabling SocketCAN on WSL2 walkthrough: update 2025

Enabling SocketCAN on WSL2

Preface: this walkthrough is a hand-holdy step by step tutorial to enable SocketCan on your WSL2 instance (Ubuntu 20.04).

To enable SocketCAN's can-utils on WSL we need to enable the CAN interface module in the WSL, to do so requires a re-building of the WSL kernel.

Requirements:

From cmd / powershell

wsl --shutdown
wsl --update

From wsl

sudo apt update
sudo apt install can-utils

If you try now to use candump for example you'd get this error message:
socket: Address family not supported by protocol

By default, WSL2 doesn't support the CAN interface, so we need to build the wsl kernel where we can enable it.

Get the latest WSL2 kernel and configure (v)can support

Prerequisites

sudo apt-get install libncurses5-dev
sudo apt install bc
sudo apt install dwarves
sudo apt install build-essential flex bison libssl-dev libelf-dev

Building the kernel

  1. Download the kernel to your Linux home directory:
cd ~
git clone https://github.com/microsoft/WSL2-Linux-Kernel
cd WSL2-Linux-Kernel
  1. Check out the correct tag:
git checkout $(git tag --list | grep $(uname -r | grep -oE '[0-9]+(\.[0-9]+)+' | head -n 1))

or git checkout 'linux-msft-wsl-YOUR_VERSION_HERE' - find the correct version number in uname -r

  1. Generate the config and run make prepare:
cat /proc/config.gz | gunzip > .config
make prepare modules_prepare
  1. Leave the prompts at default values
  2. run make menuconfig
  3. The config command line interface will pop up now. Make sure the following settings under Networking Support are checked/enabled: iage preview
  • Network Support
    • CAN bus subsystem support => M -> Enter
      • Raw CAN protocol => M
      • CAN device drivers => Enter
        • Virtual Local CAN Interface (vcan) => M
        • CAN bit-timing calculation => *
  1. Save and exit the CLI
  2. Build the kernel (this can take a while):
make modules
  1. Keep building the kernel:
sudo make modules_install
make -j $(nproc)
sudo make install
  1. Copy the newly created vmlinux file to your Windows user’s home directory:
cp vmlinux /mnt/c/Users/<yourwindowsloginname>/

Configuring WSL

Now that the vmlinux file was created, we will load it.

  1. Create a config file for wsl to load the custom kernel (or edit it if it exists): C:\Users\YOURWINDOWSLOGINNAME\.wslconfig

  2. Put or add the following content:

[wsl2]
kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
  1. Restart WSL: From a Windows cmd prompt, run
wsl --shutdown
wsl
  1. Now you can use WSL with the new kernel:
sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan
  1. And now you are able to create virtual can devices, for example:
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
sudo ip link add dev vcan1 type vcan
sudo ip link set up vcan1

Automating modprobe

As it is, you need to enable the modules every time you restart WSL. This section will show you how to automate that.

Create an init.d script

  1. Create /etc/init.d/init-modprobe and make it executable:
sudo touch /etc/init.d/init-modprobe
sudo chmod +x /etc/init.d/init-modprobe
  1. Set the file content:
#!/usr/bin/env bash

sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan

Set user permissions

Allow your current user to execute the new script without a password:

sudo visudo

Add the following line (your_user_name being the linux login name):

your_user_name ALL=(ALL) NOPASSWD:/etc/init.d/init-modprobe

Executing the script at login

Add the following lines to $HOME/.profile:

sudo /etc/init.d/init-modprobe

Apply the changes via source $HOME/.profile

The CAN tools should now be reinitialized with each WSL session, and it should not be necessary to enter a password.

Troubleshooting

make install throws an error

Example of error:

arch/x86/Makefile:142: CONFIG_X86_X32 enabled but no binutils support
sh ./arch/x86/boot/install.sh 5.15.167.4-microsoft-standard-WSL2+ \
        arch/x86/boot/bzImage System.map "/boot"
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 5.15.167.4-micro
  1. Try updating binutils (sudo apt-get install binutils)
  2. If that doesn't help, disable the config. Quick and dirty way to just remove it from the makefile:
sed -i -e 's/CONFIG_X86_X32/CONFIG_XDOESNOTEXIST_XINVALID/g' ./arch/x86/Makefile
  1. If you want to use this, please find out how to make it work and update the gist.
@philbegg
Copy link

philbegg commented Mar 7, 2025

Step 8:

Build the kernel (this can take a while):
make_modules

should be make modules

@manavortex
Copy link
Author

should be make modules
Fixed, thanks!

@fulmicotone98
Copy link

I followed all the steps and now I can create a running vcan interface, but still I have an error trying to use the interface (with candump for example):
"socket: Address family not supported by protocol"

Has someone ever had this error?
Thanks!

@manavortex
Copy link
Author

I followed all the steps and now I can create a running vcan interface, but still I have an error trying to use the interface (with candump for example): "socket: Address family not supported by protocol"

Has someone ever had this error? Thanks!

Nope, but please update with your solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment