在使用Windows Server 2019 (1809)的EC2上安装WSL运行Ubuntu Linux

一、背景

在Windows 10上可以使用WSL和新的Terminal直接运行Linux,同时,还可以通过Windows Store在线商店安装需要的Linux发行版。但在Windows Server上,没有在线商店可用。因此,安装方法可以参考如下。

首先检查确认版本高于 Windows Server 2019 (version 1709) 版本。例如EC2上选择Windows Server 2019的话版本是1809版本,可运行WSL 1。如果系统版本是1903或者更高可运行WSL2。

二、安装

注意:以下所有安装是基于Windows单用户的,用安装和后续使用必须在同一个用户名下。如果Windows上有多个用户名,例如使用Administrator安装,然后使用admin01登陆运行WSL Ubuntu,则会报错。

用管理员权限打开Powershell,运行如下命令:

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

这将启用WSL。可不用重启。

安装OS补丁:

curl -O wsl_update_x64.msi https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
msiexec.exe /package wsl_update_x64.msi  --quiet

三、获取Linux发行版

从这个网页上下载WSL使用的Linux发行版的安装包:

https://docs.microsoft.com/en-us/windows/wsl/install-manual

找到这个页面下的 Step 6 - Install your Linux distribution of choice 这一章节之下的 Downloading distributions 章节,里边有离线下载地址。

在Powershell下,还可以使用如下命令直接下载:

Invoke-WebRequest -Uri https://aka.ms/wslubuntu2204 -OutFile Ubuntu.appx -UseBasicParsing

下载后即可在当前目录下获得Ubuntu.appx文件。

四、安装Linux发行版

将这个文件改命为zip文件,解压缩,在其中可看到对应的ARM架构和X64架构的appx包。选择对应架构,再将其修改扩展名为zip,然后继续解压缩。解压缩后进入目录,运行里边的ubuntu可执行文件。以上几条命令在Powershell下如下:

Rename-Item .\Ubuntu.appx .\Ubuntu.zip
Expand-Archive .\Ubuntu.zip .\WSL
cd .\WSL
Rename-Item .\Ubuntu_2204.1.7.0_x64.appx ubuntu2204_x64.zip
Expand-Archive .\ubuntu2204_x64.zip
cd ubuntu2204_x64
.\ubuntu.exe

安装正常的话,提示需要输入Linux的用户名和密码,输入后即可完成安装。提示信息如下。

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: lxy
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 4.4.0-17763-Microsoft x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This message is shown once a day. To disable it please create the
/home/lxy/.hushlogin file.
lxy@EC2AMAZ-649E4T7:~$

现在执行身份还不是root,可运行sudo -i命令成为root。即可正常安装软件包。

apt-get update
apt-get upgrade -y
apt install tmux fish htop -y

五、设置系统路径

在上一步中,运行安装程序的路径是:

C:\Users\Administrator\Documents\WSL\ubuntu2204_x64

安装完毕后,将启动文件加入系统PATH,请替换如下命令中的路径为上一步解压缩的路径执行如下命令:

$addPath='C:\Users\Administrator\Documents\WSL\ubuntu2204_x64'; $target='Machine'; $path = [Environment]::GetEnvironmentVariable('Path', $target); $newPath = $path + ';' + $addPath; [Environment]::SetEnvironmentVariable('Path', $newPath, $target)

添加到路径完毕后,可以在任意路径下执行 ubuntu2204 即可启动WSL。

六、使用VScode启动WSL(可选)

至此安装完成。如果需要在VScode中的Terminal内使用WSL,可以在VScode中安装WSL插件,即可在Terminal窗口内调用WSL。如下截图。

七、参考文档

Manual installation steps for older versions of WSL:

https://learn.microsoft.com/en-us/windows/wsl/install-manual