Win10安装并迁移WSL2 + 实现WSL2图形桌面 + 常用配置

一、WIN10安装WSL2:

https://blog.csdn.net/m0_51233386/article/details/127961763

可以在命令行通过指令切换WSL和WSL2版本,指令如下

wsl --set-default-version 2
wsl --set-default-version 1

切换镜像源,不同版本的ubuntu使用的源不同:

https://blog.csdn.net/lxj362343/article/details/120212913

root密码设置和用户切换

安装WSL2只设置了用户名和密码,并没有设置root密码:

book@DESKTOP:~$ sudo passwd 	//这里改的是root密码,不是用户密码!
[sudo] password for book:  	//输入当前用户book的密码
New password: 			//输入root新密码
Retype new password: 		//再次输入确认root密码
passwd: password updated successfully
book@DESKTOP:~$ su //book切换为root
Password: //输入root密码
root@DESKTOP:/home/book# su book //root切换为book
book@DESKTOP:~$ 

首次进入WSL:
sudo apt update
sudo apt upgrade

二、WSL2默认安装到C盘,迁移到其他盘符:

不要在设置里安装应用默认安装位置,会导致很多问题!

正确流程:导出 ubuntu --> 注销原来 Ubuntu --> 重新导入 D盘
迁移和恢复:https://blog.csdn.net/weixin_35723192/article/details/131465760

以上操作均在 PowerShell命令终端执行命令。打开方式如下:
启动方式 “Win” + 输入 powershell
桌面 “Shift” + 右键,选择 “在此处打开 PowerShell 窗口(s)”
或者
使用Microsoft Store安装“终端”

//使用管理员权限打开这个powershell
Windows PowerShell

//查看已经安装的wsl
PS C:\Users\xxxxx> wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Stopped         2


//C盘必须留足够空间, 将分发导出到 tar 文件。//执行完命令后查看指定目录,ubuntu-20.04.tar应该生成
wsl --export Ubuntu-20.04 D:\ubuntu.tar
or
wsl --export Ubuntu D:\ubuntu.tar


//注销旧的wsl
wsl --shutdown
wsl --unregister Ubuntu


//在D 盘新建一个空目录,将指定的 tar 文件作为新分发进行导入
//完成之后删除掉导出的tar文件
mkdir Ubuntu_wsl2
wsl --import Ubuntu D://Ubuntu_wsl2  D://ubuntu.tar
or
wsl --import Ubuntu D:\Ubuntu_wsl2 D:\ubuntu.tar --version 2


//重新启动电脑
//修改默认登录用户
Ubuntu config --default-user user

# 查看WSL分发版本
wsl -l --all -v
# 如果version为2,说明已经安装为wsl2版本,如果为1,输入以下代码:
wsl --set-version Ubuntu-22.04 2 #将Ubuntu-22.04替换为你所下载安装的Linux分发版名称


# 停止WSL
wsl --shutdown

# 导出WSL镜像
wsl --export Ubuntu-22.04  "E:\Wsl\Ubuntu.tar"

# 注销Ubuntu
wsl --unregister Ubuntu-22.04

# 导入WSL镜像
wsl --import Ubuntu-22.04 "E:\Wsl" "E:\Wsl\Ubuntu.tar"  --version 2

# 设置默认登录用户(可以通过 /etc/passwd 文件查看)
Ubuntu2204 config --default-user golemon

三、使用xfce4实现图形桌面(不推荐)

不推荐:隔一晚上,桌面就异常退出了;每次关闭打开桌面,部分应用重置了,比如vscode

https://blog.csdn.net/daobaqin/article/details/121355606?ops_request_misc=&request_id=&biz_id=102&utm_term=linux%E5%AD%90%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%90%AF%E6%A1%8C%E9%9D%A2%E6%98%BE%E7%A4%BA&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-1-121355606.nonecase&spm=1018.2226.3001.4450

注意以下部分:

#编辑环境变量文件
vim ~/.bashrc
 
# 在文件最后追加下面内容,地址使用上面查看到的
export DISPLAY=localhost:0
 
#让环境变量立即生效
source ~/.bashrc

如果export DISPLAY=localhost:0连接不上,可以执行 win+R -> cmd -> ipconfig,使用WSL显示的export DISPLAY=172.22.160.1:0

以太网适配器 vEthernet (WSL):

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::3ab6:b0e4:d766:7172%56
   IPv4 地址 . . . . . . . . . . . . : 172.22.160.1
   子网掩码  . . . . . . . . . . . . : 255.255.240.0
   默认网关. . . . . . . . . . . . . :

四、使用xrdp实现图形桌面(推荐)

推荐使用Windows自带的“远程桌面连接”!
https://zhuanlan.zhihu.com/p/149501381
注意:使用远程桌面连接的方法,每次重启PC后,需要先在WSL2命令行执行:sudo /etc/init.d/xrdp start,才能进行“远程桌面连接”!

含部分其他配置:
https://blog.csdn.net/MANX98/article/details/127486532

报错解决

如果安装xrdp或其他库报错 “The following packages have unmet dependencies ”,先不用去安装“缺少的库”,先按以下方法解决:
https://blog.csdn.net/qq_41566366/article/details/121244177

sudo apt-get update
sudo apt-get clean
sudo apt-get autoremove
sudo apt --fix-broken install

五、使用

1、WSL2访问WIN10文件:

cd /mnt
cd /mnt/d/

2、WIN10访问WSL2文件:

\wsl.localhost\Ubuntu\test_folder

3、vscode访问WSL:

https://zhuanlan.zhihu.com/p/57882542

上面方法太慢了,选择直接在WSL里安装vscode,再用以下指令打开:(推荐)

code --user-data-dir=~/.vscode --no-sandbox

4、Linux 环境变量配置的 6 种方法

推荐修改~/.bashrc
https://blog.csdn.net/FMikasa/article/details/133016423

5、WSL2 Ubuntu安装beyondcompare4:

https://blog.csdn.net/Ciellee/article/details/131313925

六、问题解决:

1、防止使用一段时间后黑屏锁屏:

关闭screenssaver
卸载锁屏应用解决:
sudo apt purge xfce4-screensaver

2、如果报ICE I/O ERR,需要在~/.bashrc中额外加一句:

export WAYLAND_DISPLAY=$DISPLAY

3、安装WSL2后,无法使用VMware:

https://blog.csdn.net/u013250861/article/details/129769473
升级VMware:15.5.7 build-17171714

4、windows下wsl(ubuntu)ldconfig报错:

https://blog.csdn.net/qq_47564006/article/details/135056558

cd /usr/lib/wsl
sudo mkdir lib2
sudo ln -s lib/* lib2

sudo vim /etc/ld.so.conf.d/ld.wsl.conf
将 /usr/lib/wsl/lib 改为 /usr/lib/wsl/lib2

sudo ldconfig
永久修改
sudo vim /etc/wsl.conf
输入:
[automount]
ldconfig = fasle

posted @ 2024-05-24 09:02  solonj  阅读(1829)  评论(0编辑  收藏  举报