进入WSL2 Ubuntu环境的完整指南

进入WSL2 Ubuntu环境的完整指南

一、快速进入方法(最常用)

方法1:使用命令行(推荐)

bash 复制代码
# 在Windows Terminal、PowerShell或CMD中执行

# 1. 直接进入默认发行版
wsl

# 2. 指定进入Ubuntu
wsl -d Ubuntu-22.04

# 3. 简写方式(如果Ubuntu是默认发行版)
wsl ~    # 进入home目录

方法2:使用Windows Terminal(最佳体验)

bash 复制代码
# 安装Windows Terminal(如果还没有)
winget install Microsoft.WindowsTerminal

# 安装后:
# 1. 打开Windows Terminal
# 2. 点击顶部的下拉箭头 ▼
# 3. 选择 Ubuntu-22.04
# 或者使用快捷键 Ctrl+Shift+2(根据配置)

方法3:从开始菜单

cpp 复制代码
1. 点击Windows开始菜单
2. 找到并点击 "Ubuntu 22.04 LTS" 图标
3. 自动打开Ubuntu终端窗口

二、首次进入配置

2.1 首次启动Ubuntu

bash 复制代码
# 首次启动时会看到:
Installing, this may take a few minutes...

# 然后会提示创建用户
Please create a default UNIX user account. 
Enter new UNIX username: [输入用户名]
New password: [输入密码]
Retype new password: [确认密码]

# 设置完成后会自动进入Ubuntu环境

2.2 验证环境

bash 复制代码
# 检查Ubuntu版本
lsb_release -a

# 检查当前用户
whoami

# 检查当前目录
pwd

# 应该显示类似:/home/你的用户名

三、多种进入方式详解

3.1 带参数进入

bash 复制代码
# 以特定用户身份进入
wsl -u root              # 以root用户进入
wsl -u username          # 以指定用户进入

# 直接执行命令
wsl ls -la              # 执行ls命令后退出
wsl pwd                 # 显示当前目录后退出

# 在特定目录启动
wsl ~ cd /mnt/c/Users   # 进入Windows用户目录

# 执行多个命令
wsl bash -c "cd /home && ls -la"

3.2 从Windows资源管理器进入

bash 复制代码
1. 打开Windows文件资源管理器
2. 在地址栏输入:\\wsl$
3. 选择Ubuntu-22.04文件夹
4. 在想要的目录上右键 → "在终端中打开"

或者:

bash 复制代码
# 在Windows目录中打开WSL
# 1. 在Windows资源管理器中导航到目标文件夹
# 2. 在地址栏输入 wsl 并回车
# 3. 自动在该目录打开WSL终端

3.3 通过VS Code进入

bash 复制代码
# 方法1:命令行
code .  # 在WSL中执行,会打开VS Code

# 方法2:VS Code内
# 1. 打开VS Code
# 2. Ctrl+Shift+P 打开命令面板
# 3. 输入 "WSL: New Window"
# 4. 选择要连接的发行版

四、配置默认进入环境

4.1 设置默认发行版

bash 复制代码
# 查看已安装的发行版
wsl --list --verbose
wsl -l -v

# 输出示例:
#   NAME            STATE           VERSION
# * Ubuntu-20.04    Stopped         2
#   Ubuntu-22.04    Running         2

# 设置默认发行版
wsl --set-default Ubuntu-22.04
wsl -s Ubuntu-22.04

4.2 配置Windows Terminal

创建或编辑 Windows Terminal 配置:

java 复制代码
// 打开设置:Ctrl + ,
// 或编辑 settings.json

{
    "defaultProfile": "{guid-of-ubuntu}",  // 设置Ubuntu为默认
    "profiles": {
        "list": [
            {
                "guid": "{生成的GUID}",
                "name": "Ubuntu-22.04",
                "source": "Windows.Terminal.Wsl",
                "startingDirectory": "//wsl$/Ubuntu-22.04/home/用户名",
                "colorScheme": "Ubuntu",
                "fontFace": "Cascadia Code",
                "fontSize": 12,
                "icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png"
            }
        ]
    }
}

4.3 创建桌面快捷方式

bash 复制代码
# PowerShell脚本创建快捷方式
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Ubuntu WSL2.lnk")
$Shortcut.TargetPath = "wsl.exe"
$Shortcut.Arguments = "-d Ubuntu-22.04"
$Shortcut.IconLocation = "C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu22.04LTS_*\Assets\Square44x44Logo.targetsize-48.png"
$Shortcut.Save()

五、在WSL2中的基础操作

5.1 文件系统导航

bash 复制代码
# Linux文件系统(推荐存放项目)
cd ~                    # 进入home目录
cd /home/username       # 进入用户目录
cd /                   # 进入根目录

# 访问Windows文件系统
cd /mnt/c              # C盘
cd /mnt/d              # D盘
cd /mnt/c/Users/你的Windows用户名/Desktop  # Windows桌面

# 创建工作目录
mkdir ~/projects
cd ~/projects

5.2 Windows与WSL互操作

bash 复制代码
# 在WSL中打开Windows程序
explorer.exe .         # 打开当前目录的Windows资源管理器
notepad.exe file.txt   # 用记事本打开文件
code .                 # 打开VS Code

# 在WSL中使用Windows命令
powershell.exe Get-Process
cmd.exe /c dir

# 设置环境变量实现互操作
export PATH=$PATH:/mnt/c/Windows/System32

5.3 网络配置

bash 复制代码
# 查看网络配置
ip addr
ifconfig  # 需要先安装:sudo apt install net-tools

# 查看WSL2的IP地址
hostname -I

# 从Windows访问WSL2服务
# WSL2 IP是动态的,获取方法:
wsl hostname -I  # 在PowerShell中执行

六、优化WSL2 Ubuntu体验

6.1 安装Oh My Zsh(美化终端)

bash 复制代码
# 安装zsh
sudo apt update
sudo apt install -y zsh

# 安装Oh My Zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 设置zsh为默认shell
chsh -s $(which zsh)

# 安装实用插件
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 编辑~/.zshrc,添加插件
plugins=(git zsh-autosuggestions zsh-syntax-highlighting docker kubectl)

6.2 配置别名和快捷命令

bash 复制代码
# 编辑 ~/.bashrc 或 ~/.zshrc
cat >> ~/.bashrc << 'EOF'

# 自定义别名
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias cls='clear'

# Windows互操作
alias explorer='explorer.exe'
alias code='code.exe'

# 快速导航
alias home='cd ~'
alias projects='cd ~/projects'
alias downloads='cd /mnt/c/Users/$USER/Downloads'
alias desktop='cd /mnt/c/Users/$USER/Desktop'

# Git别名
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph'

# Python环境
alias python='python3'
alias pip='pip3'
alias venv='python3 -m venv'
alias activate='source venv/bin/activate'

# 系统更新
alias update='sudo apt update && sudo apt upgrade'
EOF

# 重新加载配置
source ~/.bashrc

6.3 配置SSH密钥

bash 复制代码
# 生成SSH密钥
ssh-keygen -t ed25519 -C "your_email@example.com"

# 启动ssh-agent
eval "$(ssh-agent -s)"

# 添加密钥
ssh-add ~/.ssh/id_ed25519

# 查看公钥(用于GitHub等)
cat ~/.ssh/id_ed25519.pub

七、常见问题解决

7.1 WSL2启动失败

bash 复制代码
# 重启WSL服务
wsl --shutdown
wsl

# 检查WSL状态
wsl --status

# 更新WSL
wsl --update

# 重置特定发行版
wsl --unregister Ubuntu-22.04
wsl --install -d Ubuntu-22.04

7.2 权限问题

bash 复制代码
# 修复权限问题
sudo chown -R $USER:$USER ~/

# 设置正确的文件权限
chmod 755 ~/
chmod 600 ~/.ssh/id_*

7.3 时间同步问题

bash 复制代码
# WSL2时间可能与Windows不同步
sudo hwclock -s

# 或者安装ntpdate
sudo apt install ntpdate
sudo ntpdate time.windows.com

7.4 DNS解析问题

bash 复制代码
# 如果遇到网络问题,编辑
sudo nano /etc/resolv.conf

# 添加Google DNS
nameserver 8.8.8.8
nameserver 8.8.4.4

# 防止自动覆盖
sudo chattr +i /etc/resolv.conf

八、退出WSL2环境

8.1 正常退出

bash 复制代码
# 方法1:退出命令
exit

# 方法2:快捷键
Ctrl + D

# 方法3:登出
logout

8.2 关闭WSL2

bash 复制代码
# 在Windows中关闭所有WSL实例
wsl --shutdown

# 关闭特定发行版
wsl --terminate Ubuntu-22.04
wsl -t Ubuntu-22.04

九、自动化启动脚本

9.1 创建启动脚本

bash 复制代码
# 创建 ~/startup.sh
cat > ~/startup.sh << 'EOF'
#!/bin/bash

# 启动SSH服务
sudo service ssh start

# 启动Docker(如果安装了)
sudo service docker start 2>/dev/null

# 激活Python虚拟环境
source ~/venv/bin/activate 2>/dev/null

# 进入项目目录
cd ~/projects

# 显示欢迎信息
echo "========================================="
echo "Welcome to WSL2 Ubuntu Development Environment"
echo "Python: $(python3 --version)"
echo "Node: $(node --version 2>/dev/null || echo 'Not installed')"
echo "Current directory: $(pwd)"
echo "========================================="
EOF

chmod +x ~/startup.sh

# 添加到.bashrc自动执行
echo "source ~/startup.sh" >> ~/.bashrc

9.2 创建Windows批处理快速启动

创建 start_wsl_dev.bat

bash 复制代码
@echo off
title WSL2 Ubuntu Development Environment

echo Starting WSL2 Ubuntu...
wsl -d Ubuntu-22.04 bash -c "cd ~/projects && exec bash"

pause

十、性能监控和管理

10.1 监控资源使用

bash 复制代码
# 查看内存使用
free -h

# 查看磁盘使用
df -h

# 实时系统监控
htop  # 需要先安装:sudo apt install htop

# 查看进程
ps aux | grep python

10.2 清理和维护

bash 复制代码
# 清理APT缓存
sudo apt clean
sudo apt autoremove

# 清理临时文件
rm -rf /tmp/*

# 压缩WSL2虚拟磁盘(在Windows PowerShell中)
wsl --shutdown
# Optimize-VHD -Path "C:\Users\用户名\AppData\Local\Packages\...\ext4.vhdx" -Mode Full

十一、快速参考卡片

bash 复制代码
# WSL2 Ubuntu 快速命令参考

## 进入/退出
wsl                     # 进入默认WSL
wsl -d Ubuntu-22.04    # 进入指定发行版
exit                   # 退出WSL

## 文件操作
/mnt/c                 # 访问C盘
explorer.exe .         # 打开Windows资源管理器
code .                 # 打开VS Code

## 系统管理
wsl --shutdown         # 关闭WSL
wsl --update          # 更新WSL
wsl -l -v             # 查看发行版状态

## 常用路径
~/                    # Linux home目录
/mnt/c/Users/用户名   # Windows用户目录
\\wsl$                # Windows访问WSL文件

这个指南应该能帮助您顺利进入和使用WSL2 Ubuntu环境。最简单的方法就是在Windows Terminal或PowerShell中输入wsl命令。

相关推荐
IDOlaoluo2 小时前
CentOS 7 系统安装步骤(从U盘启动到桌面详细流程)附镜像下载
linux·运维·centos
ayaya_mana2 小时前
Vim常用命令详解:从入门到高效编辑
linux·编辑器·vim
荣光波比3 小时前
MySQL数据库(八)—— MySQL全量+增量备份方案:从脚本开发到连锁餐饮场景落地
运维·数据库·mysql·云计算
『往事』&白驹过隙;3 小时前
ARM环境日志系统的简单设计思路
linux·c语言·数据结构·物联网·iot·日志系统
轻造科技3 小时前
瓦楞纸箱厂柔性交付:电商订单波动下的纸板裁切与印刷协同技巧
大数据·运维·mes
jiunian_cn5 小时前
【Linux网络】IP协议
linux·网络·tcp/ip
tt5555555555557 小时前
Linux启动流程与字符设备驱动详解 - 从bootloader到驱动开发
linux·运维·驱动开发
一只游鱼9 小时前
linux使用yum安装数据库
linux·mysql·adb
大白的编程日记.11 小时前
【Linux学习笔记】线程概念和控制(三)
linux·笔记·学习