Skip to content

Instantly share code, notes, and snippets.

@manavortex
Forked from yonatanh20/README.md
Last active June 18, 2025 11:00

Revisions

  1. manavortex revised this gist Mar 10, 2025. 1 changed file with 49 additions and 7 deletions.
    56 changes: 49 additions & 7 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ git clone https://github.com/microsoft/WSL2-Linux-Kernel
    cd WSL2-Linux-Kernel
    ```

    2. Check out the correct tag and run `make prepare modules_prepare`:
    2. Check out the correct tag:
    ```
    git checkout $(git tag --list | grep $(uname -r | grep -oE '[0-9]+(\.[0-9]+)+' | head -n 1))
    ```
    @@ -94,30 +94,72 @@ Now that the vmlinux file was created, we will load it.
    kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
    ```

    ## Restart WSL
    From a **Windows** cmd prompt, run
    3. Restart WSL: From a **Windows** cmd prompt, run
    ```
    wsl --shutdown
    wsl
    ```

    Now you can use WSL with the new kernel:
    4. Now you can use WSL with the new kernel:
    ```
    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan
    ```

    *You need to enable the modules every time we restart the WSL kernel. To automate that, you can create a batch script to automate these commands*

    And now you are able to create virtual can devices, for example:
    5. 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
    ```

    2. Set the file content:
    ```sh
    #!/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
  2. manavortex revised this gist Mar 10, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ If you try now to use candump for example you'd get this error message:

    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 it for can and vcan support.
    # Get the latest WSL2 kernel and configure (v)can support

    ## Prerequisites

  3. manavortex revised this gist Mar 10, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -19,12 +19,12 @@ 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
    If you try now to use candump for example you'd get this error message:
    `socket: Address family not supported by protocol`

    This just means that WSL2 doesn't come with the CAN interface support enabled, so we need to build the wsl kernel and enable it.
    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 it for can and vcan support.
    # Get the latest WSL2 kernel and configure it for can and vcan support.

    ## Prerequisites

  4. manavortex revised this gist Mar 10, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #Enabling SocketCAN on WSL2
    # 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.
  5. manavortex revised this gist Mar 10, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -68,7 +68,7 @@ make prepare modules_prepare
    7. Save and exit the CLI
    8. Build the kernel (this can take a while):
    ```
    make_modules
    make modules
    ```
    9. Keep building the kernel:
    ```
  6. manavortex revised this gist Mar 3, 2025. 1 changed file with 77 additions and 23 deletions.
    100 changes: 77 additions & 23 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -25,58 +25,112 @@ socket: Address family not supported by protocol
    This just means that WSL2 doesn't come with the CAN interface support enabled, so we need to build the wsl kernel and enable it.

    Get the latest WSL2 kernel and configure it for can and vcan 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
    git checkout 'uname -r'
    ```

    2. Check out the correct tag and run `make prepare modules_prepare`:
    ```
    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`

    3. Generate the config and run `make prepare`:
    ```
    cat /proc/config.gz | gunzip > .config
    make prepare modules_prepare
    make menuconfig
    ```
    The config cli app will pop up and we need to enable the following settings.
    Enter Networking support
    Change CAN bus subsystem support to M and enter
    Change Raw CAN Protocol to M
    Enter CAN Device Drivers
    Change Virtual Local CAN Interface to M
    Make sure CAN bit-timing calculation is set to *
    Optionally change CAN devices debugging messages to *
    Save and exit
    4. Leave the prompts at default values
    5. run `make menuconfig`
    6. The config command line interface will pop up now. Make sure the following settings under `Networking Support` are checked/enabled:
    ![iage preview](https://i.imgur.com/kLyjvKr.png)
    - 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 => *

    And now we will build the kernel and export it to your Windows user lcoation.
    7. Save and exit the CLI
    8. Build the kernel (this can take a while):
    ```
    make_modules
    ```
    9. Keep building the kernel:
    ```
    make modules
    sudo make modules_install
    make -j $(nproc)
    sudo make install
    ```
    10. Copy the newly created `vmlinux` file to your Windows user’s home directory:
    ```
    cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
    ```
    The file vmlinux was created and now we will load it.
    We need to create config file for wsl to load the custom kernel.
    Create the file `.wslconfig` in your Windows user folder C:/Users/<yourwindowsloginname>/ with the following content:

    ## 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
    ```
    Now you can reset WSL with the new kenrel.
    We need to enable the modules every time we restart the WSL kernel:

    ## Restart WSL
    From a **Windows** cmd prompt, run
    ```
    wsl --shutdown
    wsl
    ```

    Now you can use WSL with the new kernel:
    ```
    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan
    ```
    And now you are able to create virtual can devices to run. For example:

    *You need to enable the modules every time we restart the WSL kernel. To automate that, you can create a batch script to automate these commands*

    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
    ```

    # Troubleshooting

    Sources:
    * [charliex2's comment on the Reddit thread with most of the information given here](https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/)
    * [WSL github issue: Add SocketCAN support](https://github.com/microsoft/WSL/issues/5533)
    * [weifengdq's tutorial that encompases most of this tutorial](https://chowdera.com/2022/01/202201082236197554.html)
    ## `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
    ```
    3. If you want to use this, please find out how to make it work and update the gist.
  7. @yonatanh20 yonatanh20 revised this gist Aug 29, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -77,6 +77,6 @@ sudo ip link set up vcan1


    Sources:
    [charliex2's comment on the Reddit thread with most of the information given here](https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/)
    [WSL github issue: Add SocketCAN support](https://github.com/microsoft/WSL/issues/5533)
    [weifengdq's tutorial that encompases most of this tutorial](https://chowdera.com/2022/01/202201082236197554.html)
    * [charliex2's comment on the Reddit thread with most of the information given here](https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/)
    * [WSL github issue: Add SocketCAN support](https://github.com/microsoft/WSL/issues/5533)
    * [weifengdq's tutorial that encompases most of this tutorial](https://chowdera.com/2022/01/202201082236197554.html)
  8. @yonatanh20 yonatanh20 revised this gist Aug 29, 2022. 4 changed files with 45 additions and 41 deletions.
    67 changes: 45 additions & 22 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,41 @@
    Preface: this walkthrough is hand-holdy step by step tutorial to enable SocketCan on your WSL2 instance (Ubuntu 20.04).
    #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:
    * WSL2
    * back up your wsl image (optional) follow here
    https://www.virtualizationhowto.com/2021/01/wsl2-backup-and-restore-images-using-import-and-export/
    * back up your wsl image (optional) follow [here (wsl2-backup-and-restore-images)](https://www.virtualizationhowto.com/2021/01/wsl2-backup-and-restore-images-using-import-and-export/)
    Steps:
    First we will update wsl and our linux to the latest versions.

    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

    This just means that WSL2 doesn't come with CAN support, so we need to build the wsl kernel and enable can support.
    This just means that WSL2 doesn't come with the CAN interface support enabled, so we need to build the wsl kernel and enable it.

    # dependencies to build the kernel
    Get the latest WSL2 kernel and configure it for can and vcan support.
    ```
    sudo apt install build-essential flex bison libssl-dev libelf-dev

    cd ~
    git clone https://github.com/microsoft/WSL2-Linux-Kernel
    cd WSL2-Linux-Kernel
    git checkout 'uname -r'
    cat /proc/config.gz | gunzip > .config
    make prepare modules_prepare

    # config can and vcan support
    make menuconfig

    ```
    The config cli app will pop up and we need to enable the following settings.
    Enter Networking support
    Change CAN bus subsystem support to M and enter
    Change Raw CAN Protocol to M
    @@ -43,17 +45,38 @@ Enter Networking support
    Optionally change CAN devices debugging messages to *
    Save and exit

    And now we will build the kernel and export it to your Windows user lcoation.
    ```
    make modules
    sudo make modules_install

    Congratulations you've built a wsl kernel with CAN support.
    Now you've got to deploy it.

    WINUSER='cmd.exe /c echo %username%'

    make -j $(nproc)
    sudo make install
    cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
    ```
    The file vmlinux was created and now we will load it.
    We need to create config file for wsl to load the custom kernel.
    Create the file `.wslconfig` in your Windows user folder C:/Users/<yourwindowsloginname>/ with the following content:
    ```
    [wsl2]
    kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
    ```
    Now you can reset WSL with the new kenrel.
    We need to enable the modules every time we restart the WSL kernel:
    ```
    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan
    ```
    And now you are able to create virtual can devices to run. 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
    ```


    Sources:
    https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/
    https://github.com/microsoft/WSL/issues/5533
    https://chowdera.com/2022/01/202201082236197554.html
    [charliex2's comment on the Reddit thread with most of the information given here](https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/)
    [WSL github issue: Add SocketCAN support](https://github.com/microsoft/WSL/issues/5533)
    [weifengdq's tutorial that encompases most of this tutorial](https://chowdera.com/2022/01/202201082236197554.html)
    3 changes: 0 additions & 3 deletions rerunOnWslStartup
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan
    2 changes: 0 additions & 2 deletions run1st.cmd
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    wsl --shutdown
    wsl --update
    14 changes: 0 additions & 14 deletions run2nd.sh
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    #!/bin/bash
    sudo apt-get update -y
    sudo apt install -y can-utils build-essential flex bison libssl-dev libelf-dev
    cd ~
    git clone https://github.com/microsoft/WSL2-Linux-Kernel
    cd WSL2-Linux-Kernel
    git checkout 'uname -r'
    cat /proc/config.gz | gunzip > .config
    make prepare modules_prepare
    make menuconfig
    make modules
    sudo make modules_install

    cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
  9. @yonatanh20 yonatanh20 revised this gist Mar 3, 2022. 4 changed files with 21 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -46,12 +46,8 @@ Save and exit
    make modules
    sudo make modules_install

    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan


    Congratulations you've enabled CAN support in your wsl.
    Congratulations you've built a wsl kernel with CAN support.
    Now you've got to deploy it.

    WINUSER='cmd.exe /c echo %username%'

    3 changes: 3 additions & 0 deletions rerunOnWslStartup
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan
    2 changes: 2 additions & 0 deletions run1st.cmd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    wsl --shutdown
    wsl --update
    14 changes: 14 additions & 0 deletions run2nd.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #!/bin/bash
    sudo apt-get update -y
    sudo apt install -y can-utils build-essential flex bison libssl-dev libelf-dev
    cd ~
    git clone https://github.com/microsoft/WSL2-Linux-Kernel
    cd WSL2-Linux-Kernel
    git checkout 'uname -r'
    cat /proc/config.gz | gunzip > .config
    make prepare modules_prepare
    make menuconfig
    make modules
    sudo make modules_install

    cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
  10. @yonatanh20 yonatanh20 created this gist Feb 27, 2022.
    63 changes: 63 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    Preface: this walkthrough is hand-holdy step by step tutorial to enable SocketCan on your WSL2 instance (Ubuntu 20.04).

    Requirements:
    * WSL2
    * back up your wsl image (optional) follow here
    https://www.virtualizationhowto.com/2021/01/wsl2-backup-and-restore-images-using-import-and-export/
    Steps:

    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

    This just means that WSL2 doesn't come with CAN support, so we need to build the wsl kernel and enable can support.

    # dependencies to build the kernel
    sudo apt install build-essential flex bison libssl-dev libelf-dev

    cd ~
    git clone https://github.com/microsoft/WSL2-Linux-Kernel
    cd WSL2-Linux-Kernel
    git checkout 'uname -r'
    cat /proc/config.gz | gunzip > .config
    make prepare modules_prepare

    # config can and vcan support
    make menuconfig

    Enter Networking support
    Change CAN bus subsystem support to M and enter
    Change Raw CAN Protocol to M
    Enter CAN Device Drivers
    Change Virtual Local CAN Interface to M
    Make sure CAN bit-timing calculation is set to *
    Optionally change CAN devices debugging messages to *
    Save and exit

    make modules
    sudo make modules_install

    sudo modprobe can
    sudo modprobe can-raw
    sudo modprobe vcan


    Congratulations you've enabled CAN support in your wsl.

    WINUSER='cmd.exe /c echo %username%'



    Sources:
    https://www.reddit.com/r/CarHacking/comments/ot3gjf/socketcancanutils_on_windows/
    https://github.com/microsoft/WSL/issues/5533
    https://chowdera.com/2022/01/202201082236197554.html