Loading

wsl 配置记录 干货分享

官方文档:
https://docs.microsoft.com/en-us/windows/wsl/about

win10在2004正式版中提供了wsl2。wsl命令已具备之前wslconfig的所有功能

启用wsl

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

启用wsl2

启用wsl2所需的虚拟机平台

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

安装wsl2内核更新 https://aka.ms/wsl2kernel

设置使用wsl2

wsl --set-default-version 2
wsl --set-version debian 2

常用命令

查看分发

wsl -l --all -v

重装

wsl --unregister debian
debian install --root #--root表示使用默认的root账户

设置默认用户

debian config --default-user root

设置默认发行版

wsl -s debian

查看帮助

wsl --help

使用社区支持的分发版

https://github.com/yuk7/wsldl
https://github.com/DDoSolitary/LxRunOffline
https://github.com/RoliSoft/WSL-Distribution-Switcher

wsl2访问本地的hyper-v 虚拟机

https://github.com/microsoft/WSL/issues/4288

Get-NetIPInterface | select ifIndex,InterfaceAlias,AddressFamily,ConnectionState,Forwarding | Sort-Object -Property IfIndex | Format-Table

Set-NetIPInterface -ifindex MYvEthernetWSLID -Forwarding Enabled
Set-NetIPInterface -ifindex MYvEthernetSWITCHID -Forwarding Enabled

Get-NetIPInterface | where {$_.InterfaceAlias -eq 'vEthernet (WSL)' -or $_.InterfaceAlias -eq 'vEthernet (NAT-VM)'} | Set-NetIPInterface -Forwarding Enabled

function wslfwd{
    Get-NetIPInterface | where {$_.InterfaceAlias -eq 'vEthernet (WSL)' -or $_.InterfaceAlias -eq 'vEthernet (NAT-VM)'} | Set-NetIPInterface -Forwarding Enabled
}

wsl ssh服务开机启动

创建.vbs文件

set ws=wscript.createobject("wscript.shell")
ws.run "C:\Windows\System32\bash.exe -c 'sudo service ssh start'",0

运行shell:startup,将文件拷贝进去

git换行问题

由于windows换行符问题git会显示大量文件修改,此时需要在wsl中设置autocrlf设置,参考:https://www.scivision.dev/git-line-endings-windows-cygwin-wsl/

git config --global core.autocrlf input

debian wsl 无法打开Windows程序 $PATH变量问题

debian的/etc/profile复写了$PATH变量,而ubuntu没有,所以debian无法正常开启Windows程序,如notepad,cmd等。而Ubuntu正常。
删除以下内容,并重启wsl,可修复此问题

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi

参考:
https://github.com/microsoft/WSL/issues/5779
https://docs.microsoft.com/en-us/windows/wsl/troubleshooting#command-not-found-when-executing-windows-exe-in-linux

wsl安装git

windows安装git后,wsl共用credential helper

apt-get install git
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

wsl使用图形化界面

gwsl支持在win10上运行XServer: https://opticos.github.io/gwsl/

posted @ 2018-10-30 21:00  wswind  阅读(2820)  评论(0编辑  收藏  举报