通过命令的方式安装ubuntu子系统
以下是在命令行中安装 Ubuntu 的步骤:
- 打开 Windows PowerShell 作为管理员。
- 运行以下命令以启用 Microsoft-Windows-Subsystem-Linux 功能:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- 运行以下命令以启用虚拟机平台功能(如果未启用):
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- 下载及安装 WSL 2 的 Linux 核心,并将其设置为默认并启用:
下载链接:https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
进入到你下载的 wsl_update_x64.msi 所在的目录,运行以下命令:msiexec /i wsl_update_x64.msi
- 在 PowerShell 命令行中输入以下命令,启动 WSL 2 并将其设置为默认:
wsl --set-default-version 2
- 下载 Ubuntu 发行版的安装包,可以从官方网站下载,例如:
(PS:这将下载 Ubuntu 20.04 发行版的安装包到当前目录。)Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing
- 安装 Ubuntu,运行以下命令:
Add-AppxPackage .\Ubuntu.appx
- 运行以下命令以启动 Ubuntu:
这将打开一个 Linux 终端窗口,并提示你输入用户名和密码以进行配置。完成配置后,你就可以在终端中输入 Linux 命令了。ubuntu
Linux环境配置
1.设置root密码,执行:sudo passwd,然后输入密码。
2.设置源,更换为国内下载源
打开终端,备份源下载源文件
sudo cp /etc/apt/sources.list /opt/sources.list.backup
编辑sources.list文件
sudo vi /etc/apt/sources.list
删除sources.list文件中的官方源(Tips:将光标移动到最后一行,esc进入一般模式,依次按dgg,即可全文删除)
阿里源(阿里源站点)
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
保存内容
ESC
:wq
:w !sudo tee %(这里如果提示“只读文件”,这个命令可以在只读文件里保存)
配置网络环境DNS
如果没法上网,可以通过以下方式设置DNS
1.运行Linux 的命令窗口
2.备份当前的DNS配置:sudo mv /etc/resolv.conf /etc/resolv.conf.bak
3.创建resolv.conf文件并编辑内容:sudo vim /etc/resolv.conf
讲以下内容添加到文件:
nameserver 8.8.8.8
nameserver 8.8.4.4
4.将 /etc/resolv.conf 文件设置为只读属性的命令(避免被系统修改,不然每次开机都会改变)sudo chattr +i /etc/resolv.conf
注:重新想要编辑执行sudo chattr -i /etc/resolv.conf
设置默认启动用户
每次打开终端时,都是超级用户,想要设置成别的其他用户,powershell中执行:
ubuntu.exe config --default-user username_xxx
手动切换到其他用户
进入普通用户:sudo su – username_xxx
ubuntu迁移
1.要用管理员权限启动powershell
2.先关闭wsl :
wsl --shutdown
- 导出名称为Ubuntu的系统到D盘根目录下:
wsl --export Ubuntu D:\Ubuntu.tar
- 取消注册Ubuntu :
wsl --unregister Ubuntu
- 导入Ubuntu到D:\wsl路径下
wsl --import Ubuntu D:\wsl D:\Ubuntu.tar --version 2
反复弹出RemoteApp窗口
弹出”The identity of the remote computer cannot be verified. Do you want to connect anyway?”窗口,
解决方法:
1.在windows用户Users目录下,创建.wslconfig文件,
文件内容
[wsl2]
guiApplications=false