├── assets └── images │ ├── 局域网设置.jpg │ └── 防火墙规则.png └── README.md /assets/images/局域网设置.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cherrol/wsl2-development/HEAD/assets/images/局域网设置.jpg -------------------------------------------------------------------------------- /assets/images/防火墙规则.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cherrol/wsl2-development/HEAD/assets/images/防火墙规则.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 一、安装WSL2 2 | 3 | 参照 https://docs.microsoft.com/en-us/windows/wsl/install-win10 4 | > 当你电脑配置比较高的时候一定要设置一下 wsl 资源占用,不然 wsl 会占用过多资源导致电脑卡, 5 | > 配置文件位置 C:\Users\\[user]\\.wslconfig 6 | > 如下是我的配置(仅供参考,我的电脑配置: i7 11800H、40GB RAM) 7 | 8 | ``` 9 | [wsl2] 10 | # 自定义 Linux 内核的绝对路径 11 | #kernel= 12 | # 给 WSL 2 虚拟机分配的内存大小 13 | memory=10GB 14 | # 为 WSL 2 虚拟机分配的处理器核心数量 15 | processors=4 16 | # 为 WSL 2 虚拟机分配的交换空间,0 表示没有交换空间 17 | swap=16GB 18 | # 自定义交换虚拟磁盘 vhd 的绝对路径 19 | # swapFile= 20 | # 是否允许将 WSL 2 的端口转发到主机(默认为 true) 21 | # localhostForwarding= 22 | 23 | # `` 必须是带反斜杠的绝对路径,例如 `C:\\Users\\kernel` 24 | # `` 必须在后面加上单位,例如 8 GB 或 512 MB 25 | ``` 26 | ## 设置免密登录 27 | > 参考 https://dev.to/marcelopalin/how-to-enable-user-passwordless-sudo-in-wsl2-linux-38f7 28 | ``` 29 | # 打开 wsl2 终端执行设置免密登录 30 | echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/dont-prompt-$USER-for-sudo-password 31 | 32 | # 取消免密登录 33 | sudo rm /etc/sudoers.d/dont-prompt-$USER-for-sudo-password 34 | ``` 35 | 36 | 37 | # 二、安装docker desktop windows 38 | 39 | 参照 https://docs.docker.com/docker-for-windows/install/ 40 | 41 | 42 | # 三、docker在wsl2下的设置 43 | 44 | 参照 https://docs.docker.com/docker-for-windows/wsl/ 45 | 46 | 47 | # 四、WSL2科学上网 48 | 49 | > 对应的软件设置为允许局域网连接(我使用的是SSR) 50 | 51 | ![alt](assets/images/局域网设置.jpg "局域网设置") 52 | 53 | > 防火墙设置放行 54 | 55 | ![alt](assets/images/防火墙规则.png "局域网设置") 56 | 57 | 58 | > 在wsl2 terminal中打开```vim ~/.bashrc```(vscode中打开为 ```code ~/.bashrc```),在最下边添加如下代码 59 | 60 | ```bash 61 | # update 2023-01-02 新版 wsl 配置参考 https://github.com/microsoft/WSL/issues/10753#issuecomment-1814839310,也不需要再设置防火墙放行和代理转发 62 | 63 | # set http proxy 64 | WSL_MASTER_HOST_IP=`cat /etc/resolv.conf | grep nameserver | awk '{print $2}'` 65 | 66 | export http_proxy="http://${WSL_MASTER_HOST_IP}:1080" # 此处端口对应SSR的本地端口1080 67 | 68 | export https_proxy=$http_proxy 69 | 70 | export all_proxy=$http_proxy 71 | 72 | # set git config http proxy 73 | 74 | if [ "`git config --global --get http.proxy`" != "http://$WSL_MASTER_HOST_IP:1080" ]; then 75 | 76 | git config --global http.proxy http://$WSL_MASTER_HOST_IP:1080 77 | 78 | fi 79 | ``` 80 | 81 | 如果是使用zsh则是打开 ```vim ~/.zshrc```(或者```code ~/.zshrc```),添加上边的代码到末尾 82 | 83 | > wsl2 ping 不通主机时,需要设置 wsl2 网络允许通过防火墙(需要管理员权限打开cmd/powershell执行) 84 | ```bash 85 | # cd folder 86 | cd C:\WINDOWS\system32 87 | 88 | # run 89 | New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow 90 | ``` 91 | 92 | > 测试连接(wsl 环境下) 93 | ```bash 94 | # 查看 wsl 的ip 95 | env 96 | # 找到环境变量 http_proxy ,查看主机 ip,假设为 window_ip 97 | ping window_ip 98 | ``` 99 | 100 | # 五 Git相关设置 101 | > 设置wsl ssh共享请参考https://devblogs.microsoft.com/commandline/sharing-ssh-keys-between-windows-and-wsl-2/ 102 | 103 | > 设置github代理,使用http:// 协议代替 git:// 104 | > 打开 vim ~/.gitconfig,将下边代码添加至末尾 105 | ``` bash 106 | [url "https://github.com/"] 107 | insteadOf = git://github.com/ 108 | ``` 109 | 110 | > 设置多个ssh连接参照 https://linuxize.com/post/using-the-ssh-config-file/ 111 | > 例如本地配置github、gitee的ssh ```~/.ssh/config``` 112 | ```bash 113 | Host github.com 114 | User git 115 | Port 22 116 | Hostname github.com 117 | # 注意修改路径为你的路径 118 | IdentityFile "~/.ssh/id_rsa" 119 | TCPKeepAlive yes 120 | 121 | Host ssh.github.com 122 | User git 123 | Port 443 124 | Hostname ssh.github.com 125 | # 注意修改路径为你的路径 126 | IdentityFile "~/.ssh/id_rsa" 127 | TCPKeepAlive yes 128 | 129 | Host gitee.com 130 | User git 131 | Port 22 132 | Hostname gitee.com 133 | # 注意修改路径为你的路径 134 | IdentityFile "~/.ssh/id_rsa" 135 | TCPKeepAlive yes 136 | 137 | Host ssh.gitee.com 138 | User git 139 | Port 443 140 | Hostname ssh.gitee.com 141 | # 注意修改路径为你的路径 142 | IdentityFile "~/.ssh/id_rsa" 143 | TCPKeepAlive yes 144 | 145 | ``` 146 | 147 | # 六、docker项目中文件权限 148 | > 运行docker ps(或相关docker指令)有权限问题时需要修复docker权限 149 | ```bash 150 | sudo addgroup --system docker 151 | sudo adduser $USER docker 152 | newgrp docker 153 | ``` 154 | 155 | > 项目文件权限问题,在wsl2下设置文件夹权限为777即可 156 | ```bash 157 | # 例如我的工作目录为 ~/workspace 158 | cd ~/ 159 | sudo chmod -R 777 workspace/ 160 | ``` 161 | 162 | # 注意事项 163 | > 启动ssr或者相关软件时请关闭windows下其他代理(检查windows代理端口是否正确),防止冲突导致ssr不生效 164 | 165 | 166 | # update 2023-01-02 新版 wsl 配置参考 https://github.com/microsoft/WSL/issues/10753#issuecomment-1814839310 ,也不需要再设置防火墙放行和代理转发 167 | # .bashrc config 168 | ```bash 169 | #... 170 | 171 | # proxy 172 | export WSL_MASTER_HOST_IP=`cat /etc/resolv.conf | grep nameserver | awk '{print $2}'` 173 | export my_custom_proxy="http://@${username}:@${password}-@${WSL_MASTER_HOST_IP}:10808"; 174 | # export my_custom_proxy="http://${WSL_MASTER_HOST_IP}:10808"; 175 | export all_proxy=$my_custom_proxy 176 | export ALL_PROXY=$my_custom_proxy 177 | export http_proxy=$my_custom_proxy 178 | export https_proxy=$my_custom_proxy 179 | alias addproxy=' 180 | export all_proxy=$my_custom_proxy 181 | export ALL_PROXY=$my_custom_proxy 182 | export http_proxy=$my_custom_proxy 183 | export https_proxy=$my_custom_proxy 184 | echo -e "Acquire::http::Proxy \"$my_custom_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null 185 | echo -e "Acquire::https::Proxy \"$my_custom_proxy\";" | sudo tee -a /etc/apt/apt.conf > /dev/null 186 | ' 187 | alias rmproxy=' 188 | unset all_proxy 189 | unset ALL_PROXY 190 | unset http_proxy 191 | unset https_proxy 192 | ' 193 | # add update && upgrade alias 194 | alias upall="sudo apt update -y && sudo apt upgrade -y" 195 | 196 | #... 197 | ``` 198 | --------------------------------------------------------------------------------