wsl
支持哪些Linux操作系统,可以输入命令:wsl --list -o
来查看,当前支持版本如下 :PS C:\Users\Lenovo> wsl --install -d ubuntu-18.04
Installing: Ubuntu 18.04 LTS
Ubuntu 18.04 LTS has been installed.
Launching Ubuntu 18.04 LTS...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: evan
Enter new UNIX password:
Retype new UNIX password:
The operation completed successfully.
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
当前WSL默认ubuntu版本是22.04版本,如果想要升级到最新的24.04版本LTS,可以按如下方法进行:
vi /etc/update-manager/release-upgrades
确保Prompt
为LTS
:
sudo apt-get update
sudo apt-get upgrade
sudo do-release-upgrade -d
如果执行上面命令时报如下错误时,可以尝试强行升级:
$ sudo do-release-upgrade
Checking for a new Ubuntu release
Please install all available updates for your release before upgrading.
sudo apt-get dist-upgrade
exit
退出系统;wsl --terminate Ubuntu
结束ubuntu运行;wsl
或者ubuntu
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
可以看到系统已经升级成功。
安装多个版本时,只需要在安装命令时,带上对应版本系统名称即可,如当前WSL支持如下版本:
PS C:\Users\Lenovo> wsl --list -o
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
Ubuntu-24.04 Ubuntu 24.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
openSUSE-Leap-15.5 openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed openSUSE Tumbleweed
比如我们系统当前安装了默认系统Ubuntu,之后又想安装Ubuntu-18.04
,则只需要如此安装即可:
wsl --install -d Ubuntu-18.04
随后等待安装完成即可。
PS C:\Users\Lenovo> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
Ubuntu-18.04 Running 2
方法1:在过访问 Windows“开始”菜单并键入已安装的发行版的名称,可以直接打开 Linux 发行版。 例如:“Ubuntu”。 这会在其自己的控制台窗口中打开 Ubuntu;
方法2:在Powershell
或terminal
中输入wsl -d <DistributionName>
,其中替换为对应系统版本即可,如ubuntu-18.04
则输入ubuntu-1804
即可
设置/应用
,找到对应Linux安装程序,进行卸载PS C:\Users\Lenovo> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
Ubuntu-18.04 Stopped 2
PS C:\Users\Lenovo> wsl -unregister ubuntu-18.04
Invalid command line argument: -unregister
Please use 'wsl.exe --help' to get a list of supported arguments.
PS C:\Users\Lenovo> wsl --unregister ubuntu-18.04
Unregistering.
The operation completed successfully.
sudo vi /etc/update-manager/release-upgrades
把Prompt=lts
修改成为Prompt=normal
后保存。
sudo apt-get update
sudo apt-get upgrade
sudo do-release-upgrade
exit
退出系统;wsl --terminate Ubuntu
停止系统wsl
或者ubuntu
即可启动系统,此时系统就会为最新版本的Ubuntu了。从Ubuntu24.04开始,源的配置文件发生了变化 :
evan@DESKTOP-9572KKG:~$ cat /etc/apt/sources.list
# Ubuntu sources have moved to the /etc/apt/sources.list.d/ubuntu.sources
# file, which uses the deb822 format. Use deb822-formatted .sources files
# to manage package sources in the /etc/apt/sources.list.d/ directory.
# See the sources.list(5) manual page for details.
根据上面的提示可以知道,新的配置文件移到了/etc/apt/sources.list.d/
,ls /etc/apt/sources.list.d/
可见新的配置文件是:
evan@DESKTOP-9572KKG:~$ ls -l /etc/apt/sources.list.d/
total 4
-rw-r--r-- 1 root root 3016 Jul 1 15:52 ubuntu.sources
打开文件查看下(cat /etc/apt/sources.list.d/ubuntu.sources
):
Types: deb
URIs: http://archive.ubuntu.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://security.ubuntu.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
就文件内容而言,配置项少多了,也简化了许多,24.04源可以在网上找到一大堆,我们替换下即可。
比如阿里源:
# 阿里云
Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
我们可以备份下配置文件(sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak)后,使用命令sudo vi /etc/apt/sources.list.d/ubuntu.sources
替换文件内容即可。
Ctrl+Shift+X
),搜索并安装Remote - WSL
扩展。 code .
命令,即可启动 VSCode 并打开当前目录。注意:首次运行时,wsl会安装vscode server,注意联网!
Installing, this may take a few minutes...
WslRegisterDistribution failed with error: 0x800701bc
Error: 0x800701bc WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel
Press any key to continue...
出现以上错误时,说明WSL版本过低,可以执行如下命令升级WSL(执行命令时,terminal或powershell需要在管理员权限下运行):
wsl --update