Loading

WSL2的迁移、默认设置、ssh、固定IP

迁移ext4.vhdx 到其他磁盘

cmd执行下面指令:

mkdir F:\WSL2
mkdir F:\WSL2\ubuntu1804
wsl --export Ubuntu-18.04 F:\WSL2\ubuntu1804.tar    # 在指定位置创建当前磁盘的 tar 备份
wsl --unregister Ubuntu-18.04                       # 删除原有wsl虚拟机,同时会删除原 ext4.vhdx
wsl --import Ubuntu-18.04 F:\WSL2\ubuntu1804 F:\WSL2\ubuntu1804.tar    # 在指定目录创建 ext4.vhdx 并从 tar 导入,然后注册wsl虚拟机

上述步骤完成后,会将原C盘的 ext4.vhdx 迁移到 “F:\WSL2\ubuntu1804” 目录。

https://github.com/MicrosoftDocs/WSL/issues/412

设置默认wsl:

wsl --set-default Ubuntu-18.04

设置默认登录用户

Ubuntu-18.04为例:

ubuntu1804.exe config --default-user chud

这个 ubuntu1804.exe 是 C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2020.824.0_x64__79rhkp1fndgsc\ubuntu1804.exe ,没注意是否是 import 过程中重新生成的,但用它设置默认用户是有效的。(it just works)

此外,比较可靠的方法就是修改 /etc/wsl.conf

[user]
default=chud

设置hostname

wsl 的 hostname 默认跟随 windows 机器名称,无法通过 hostnamectl 设置,如需修改可在 /etc/wsl.conf 添加如下字段:

[network]
hostname = Ubuntu
generateHosts = false

https://superuser.com/questions/1566022/how-to-set-default-user-for-manually-installed-wsl-distro

开启 ssh

首次开启可能没有 ssh key,需要先生成一次 key ,否则会报 sshd: no hostkeys available -- exiting.

sudo ssh-keygen -A
sudo service ssh start

固定IP

Windows 开机后 WSL 的地址可能会变掉,为了解决这个问题可以写一个bat脚本并开机执行:

@echo off
setlocal enabledelayedexpansion

REM 顺便开启ssh
wsl -d Ubuntu-22.04 -u root service ssh start

:: set wsl2 ip
wsl -d Ubuntu-22.04 -u root ip addr | findstr "172.17.156.13" > nul
if !errorlevel! equ 0 (
    echo wsl ip has set
) else (
    wsl -d Ubuntu-22.04 -u root ip addr add 172.17.156.13/28 broadcast 172.17.156.15 dev eth0 label eth0:1
    echo set wsl ip success: 172.17.156.13
)

:: set windows ip
ipconfig | findstr "172.17.156.1" > nul
if !errorlevel! equ 0 (
    echo windows ip has set
) else (
    netsh interface ip add address "vEthernet (WSL)" 172.17.156.1 255.255.255.240
    echo set windows ip success: 172.17.156.1
)

pause

切换python版本

ubuntu里可使用 update-alternatives 进行切换:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

然后 update-alternatives --config python 会让你选择:

安装pip2

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python2 get-pip.py

换源

focal阿里源:

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

其他源 vim 替换一下 focal 即可。

安装 ohmyzsh

sudo apt update && sudo apt install zsh -y
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
posted @ 2022-05-31 23:58  dylanchu  阅读(1601)  评论(0编辑  收藏  举报