【Day1-3】(Windows版)Open Harmony PC 命令行适配指南环境准备篇(2)

启动WSL中的Ubuntu

确保已安装WSL和Ubuntu发行版。在Windows搜索栏输入Ubuntu或通过命令行wsl -d Ubuntu启动。首次启动需设置用户名和密码。

备份原有源列表

修改前建议备份原有源文件:

bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

替换为国内镜像源

编辑源列表文件,以下为常用国内源(以阿里云为例):

bash 复制代码
sudo sed -i 's|http://archive.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list
sudo sed -i 's|http://security.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list

其他可选镜像源(如清华、中科大):

  • 清华源:https://mirrors.tuna.tsinghua.edu.cn
  • 中科大源:https://mirrors.ustc.edu.cn

更新软件包列表

替换完成后运行以下命令更新:

bash 复制代码
sudo apt update
sudo apt upgrade -y

验证镜像源

检查更新是否来自国内镜像:

bash 复制代码
apt policy | grep mirrors

若输出显示镜像域名(如aliyun),则配置成功。

可选:手动编辑源列表

如需更精细控制,可直接编辑文件:

bash 复制代码
sudo nano /etc/apt/sources.list

替换内容为以下格式(以阿里云为例):

复制代码
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

根据Ubuntu版本(如22.04为jammy)调整上述focal字段。

方式二

WSL 常用命令总结

列出已安装的虚拟机
wsl -l 或 wsl --list:显示所有已安装的 WSL 发行版名称。
wsl -l -v:显示更详细的信息,包括发行版状态和 WSL 版本。
wsl -l -o:列出可从在线商店安装的可用发行版列表。

关闭虚拟机
wsl -t <发行版名称>:终止指定名称的发行版(如 wsl -t ubuntu)。
wsl --shutdown:强制关闭所有正在运行的 WSL 实例及虚拟机引擎。

启动与运行
wsl -d <发行版名称>:启动指定发行版并进入终端(如 wsl -d ubuntu)。
wsl -u root:以 root 用户身份启动默认发行版。
wsl:直接启动默认发行版的终端。

设置默认用户

通过对应发行版的 .exe 文件修改默认用户:
<发行版名称>.exe config --default-user <用户名>

例如:ubuntu.exe config --default-user root 将 Ubuntu 的默认用户设为 root。

安装与管理发行版
wsl --install:安装默认发行版(通常为 Ubuntu)。
wsl --install -d <发行版名称>:安装指定发行版(如 wsl --install -d Debian)。
wsl --unregister <发行版名称>:卸载指定发行版并删除其文件。

导入与导出
wsl --export <发行版名称> <输出路径>:将发行版导出为 .tar 备份文件。
wsl --import <发行版名称> <安装路径> <备份文件路径>:从备份文件导入发行版。

其他实用命令
wsl -s <发行版名称>:设置默认发行版(后续直接输入 wsl 会启动该发行版)。
wsl --status:查看 WSL 的全局状态,包括默认版本和内核信息。
wsl --help:显示所有支持的 WSL 命令及选项说明。

配置文件说明

在发行版的 /etc/wsl.conf 中可配置默认用户:

ini 复制代码
[user]
default = DemoUser
进入Ubuntu操作系统,并查看镜像源
bash 复制代码
C:\Windows\System32>wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2
bash 复制代码
C:\Windows\System32>wsl -d Ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
bash 复制代码
ubuntu@zjpc:/mnt/c/Windows/System32$ pwd
/mnt/c/Windows/System32
ubuntu@zjpc:/mnt/c/Windows/System32$ cd ~
ubuntu@zjpc:~$ pwd
/home/ubuntu
ubuntu@zjpc:~$ sudo cat /etc/apt/sources.list
更换镜像源
bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
bash 复制代码
sudo vim /etc/apt/sources.list

编辑 sources.list 文件

vim编辑器命令模式下,输入ggdG删除所有内容,输入i,并添加以下任意源:

bash 复制代码
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

保存文件并退出编辑器:

更新源和软件

bash 复制代码
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get upgrade

参考文章

DAY1-Open Harmony PC 命令行适配指南(Windows版)环境准备篇

相关推荐
广东程序猿小展28 分钟前
谷歌相机配置文件怎么导入(GCam 配置导入与不生效排查)
windows·工具·软件
李小白杂货铺1 天前
Windows 桌面 GUI 原型快速开发技术选型指南(Mac 开发 + Windows 交付场景)
windows·原型·gui·原型开发·技术选型·快速开发·技术路线
承渊政道1 天前
magic-resume简历工具教程:Windows部署、在线编辑与远程访问
windows·远程访问·在线编辑·简历制作·工具教程·magic-resume
观察员2 天前
告别频繁锁屏:5行 PowerShell 脚本实现 Windows 自动防休眠
windows·命令行
Funlang2 天前
在 Windows 95 上写前端
前端·javascript·windows
一切皆有可能!!2 天前
【SoduWork测评】本机跑源码,找到并修了7个Bug
windows·开源·bug·ai编程·soduwork
xx z2 天前
打开游戏提示 d3dx9_43.dll 缺失怎么办(DirectX 修复步骤)
windows·游戏·工具·软件
如夏倾城2 天前
五大远程桌面协议:RDP、SSH、VNC/RFB、Citrix ICA/HDX 与 VMware Blast Extreme
运维·windows·网络协议·安全·自动化·ssh·远程工作
Vex2une3 天前
JDK 8 下载安装与环境变量配置完整指南(Windows新手图文教程)
java·windows·环境配置·环境变量·jdk8·jdk下载安装·java入门教程