安装
wsl --install
升级
wsl --update
wsl --shutdown
自动识别串口工具
winget install usbipd
设置默认
wsl --list
wsl --setdefault Ubuntu
wsl --list
查看USB设备
usbipd list
安装linux虚拟串口工具
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip `ls /usr/lib/linux-tools/*/usbip | tail -n1` 20
将USB设备加载到linux
usbipd wsl attach --busid=1-12
可以在linux下查看
lsusb
ls /dev/serial/by-id/*
安装其他工具
sudo apt update
sudo apt install dbus policykit-1 daemonize
sudo touch /usr/local/bin/wsl2hack
sudo chmod +x /usr/local/bin/wsl2hack
sudo nano /usr/local/bin/wsl2hack
#!/bin/bash
# your WSL2 username
UNAME="<YOURUSER>"
UUID=$(id -u "${UNAME}")
UGID=$(id -g "${UNAME}")
UHOME=$(getent passwd "${UNAME}" | cut -d: -f6)
USHELL=$(getent passwd "${UNAME}" | cut -d: -f7)
if [[ -p /dev/stdin || "${BASH_ARGC}" > 0 && "${BASH_ARGV[1]}" != "-c" ]]; then
USHELL=/bin/bash
fi
if [[ "${PWD}" = "/root" ]]; then
cd "${UHOME}"
fi
# get pid of systemd
SYSTEMD_PID=$(pgrep -xo systemd)
# if we're already in the systemd environment
if [[ "${SYSTEMD_PID}" -eq "1" ]]; then
exec "${USHELL}" "$@"
fi
if [[ -z ${SYSTEMD_PID} ]]; then
# start systemd
/usr/bin/daemonize -l "${HOME}/.systemd.lock" /usr/bin/unshare -fp --mount-proc /lib/systemd/systemd --system-unit=basic.target
# wait for systemd to start
retries=50
while [[ -z ${SYSTEMD_PID} && $retries -ge 0 ]]; do
(( retries-- ))
sleep .1
SYSTEMD_PID=$(pgrep -xo systemd)
done
if [[ $retries -lt 0 ]]; then
>&2 echo "Systemd timed out; aborting."
exit 1
fi
fi
# export WSL variables
export WINPATH="$(echo "$PATH"|grep -o ':/mnt/c.*$'|sed 's!^:!!')"
RUNOPTS=""
RUNOPTS="$RUNOPTS -l"
RUNOPTS="$RUNOPTS -w WINPATH"
RUNOPTS="$RUNOPTS -w WSL_INTEROP"
RUNOPTS="$RUNOPTS -w WSL_DISTRO_NAME"
# enter systemd namespace
exec /usr/bin/nsenter -t "${SYSTEMD_PID}" -m -p --wd="${PWD}" /sbin/runuser $RUNOPTS -s "${USHELL}" "${UNAME}" -- "${@}"
启用systemd
echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf
查看是否启用成功
ps --no-headers -o comm 1
查看已启用的systemd service
通过systemctl的list-units、list-unit-files命令,都可以很好的查看目前的systemd状态,命令如下:
systemctl list-units --type=service
systemctl list-unit-files --type=service --state=enabled