显示 / 隐藏 文章目录 ]

wsl2

上次更新: 2024-01-04 23:08:20

安装 wsl

前置条件,主板 bios 开启 intel 虚拟化

ms app store如果打不开、转圈 -> 关闭小飞机,也可以试试 改 ipv4 host 4.2.2.2

  • windows terminal 必备,ms store 下载

巨硬官方文档

ubuntu软件源

阿里开源镜像站

下面是 ubuntu20.04 用的

# 备份apt默认源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
# 替换 /srouces.list
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


# 更新apt
sudo apt upgrade
sudo apt update

安装 zsh

注意.zshrc在安装的用户目录下,别和root搞混了

# 安装
sudo apt install zsh
# 将 zsh 设置为默认 shell
chsh -s /bin/zsh
# 检查,若没成功,重启试试看
echo $SHELL

安装 oh-my-zsh

# 443 confused 手动 vim oh-my-zsh.sh 然后 bash ./oh-my-zsh.sh 
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

配置 oh-my-zsh 主题/插件/alias

安装插件

  • 自动补全:
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

# 主题
ZSH_THEME="ys"
# 引入插件
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
# alias
alias cls='clear'
alias ga='git add'
alias gc='git commit -m'
alias gp='git push'
alias gf='git fetch'
alias update='sudo apt update'
alias upgrade='sudo apt upgrade'
alias install='sudo apt install'
# windows文件管理器打开wsl文件
alias open='explorer.exe'

安装 Nodejs

巨硬文档赛高

安装完注意文字提示

# =>Appending nvm source string to /home/mozzie/.zshrc
# => Appending bash_completion source string to /home/mozzie/.zshrc
# => Close and reopen your terminal to start using nvm or run the following to use it now:

照它说的做

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

验证nvm安装 nvm ls,会看见类似

iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

安装 nodejs stable

nvm install node
# node -v | npm -v 验证安装版本

外网/LAN 访问 wsl2 服务

巨硬官方解释

When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.

This isn’t the default case in WSL 2. WSL 2 has a virtualized ethernet adapter with its own unique IP address. Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine. (We are looking into ways to improve this experience.)

Here’s an example PowerShell command to add a port proxy that listens on port 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP address 192.168.101.100

netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100

注意端口覆盖的问题,避免 windows 端口和端口 wsl2 冲突

netsh 端口映射

  • listenaddress: 监听地址, 0.0.0.0 表示匹配所有地址

  • listenport: 监听的 windows 端口

  • connectaddress: 转发到 wsl2 的 ip地址, 这里设置为localhost,默认从 windows 可以通过localhost 访问 wsl2

  • connectport: 转发到 wsl2 的端口

例如 windows 的 ip 为 192.168.1.100,监听 windows 的 3000 端口,转发到 wsl2 ip localhost 的 3000 端口

# windows-terminal 管理员权限执行
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=localhost
# 删除端口监听
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=3000

配置 windows defender 防火墙入站规则

新建规则 -> 端口 -> TCP / 特定本地端口(3000) -> 允许链接 -> 下一步 -> 取个名字 -> Done

docker

windows 宿主机安装 docker desktop设置 -> 资源 -> WSL INTEGRATION 打开 对应的 linux发行版,即使用

mysql 容器

# brdige
docker network create --driver bridge --subnet=172.21.0.0/16 wsl2
# pull
docker pull mysql:5.7.38
# 生产 mysql 5.7.38 容器
docker run --restart=always --privileged=true -p 3306:3306 --name mysql --net wsl2 --ip 172.21.0.5 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.38

mongo 容器

docker pull mongo:5.0
# 宿主机 /mongo/data
docker run --restart=always -d -p 27017:27017 -v /mongo/data:/data --name mongo --net wsl2 --ip 172.21.0.6 mongo:5.0 --auth
# 初始化
docker exec -it mongo /bin/bash
# 进入 mongo shell
mongo
# admin
use admin
# 创建root用户,管理全部数据库的权限,这会可以navicat等gui链接数据库,用户名密码root,验证数据库admin
db.createUser({
   user: "root",
   pwd: "root",
   roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
# 退出
exit
# 再次进入
mongo
# 使用root登录授权,正确返回 1
db.auth("root","root")
# 创建 testDB 数据库
use testDB
# 创建 test 用户 管理 testDB
db.createUser({user:'test',pwd:'test',roles:[{role:'dbOwner',db:'testDB'}]})

使用 navicat 登录,验证数据库 admin ,用户名密码 test

默认 wsl root 用户登录

# wsl -l 查看 ubuntu版本,例如 Ubuntu-20.04
Ubuntu2004 config --default-user root

重拾纯粹的写作