这是我参与「第五届青训营 」伴学笔记创作活动的第 2 天
为了在我的 windows 本上愉快的进行 kitex代码生成,完成青训大项目任务,我还有几步路要走。
6. 网络配置
我的终极目标是使用同一局域网下的 macbook 连接 WSL 操作,同时我也希望我在局域网中所有的设备(Windows、WSL、macbook)都可以互联。
先配置 WSL 2 访问 Windows 网络的 SOCKS5 代理。首先需要使用主机的 IP 地址进行访问,使用以下命令获取主机 IP 地址:
$ cat /etc/resolv.conf | grep -oP '(?<=nameserver\ ).*'
# 172.20.144.1
以在 WSL 2 (Ubuntu 20.04) 中使用 Windows 网络代理(此处为 socks5 代理)为例,向 ~/.bashrc 中写入以下内容:
export hostip=$(cat /etc/resolv.conf | grep -oP '(?<=nameserver\ ).*')
alias proxy='
export https_proxy="socks5://${hostip}:10808";
export http_proxy="socks5://${hostip}:10808";
export all_proxy="socks5://${hostip}:10808";
echo -e "Acquire::http::Proxy "socks5h://${hostip}:10808";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
echo -e "Acquire::https::Proxy "socks5h://${hostip}:10808";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
'
alias unproxy='
unset https_proxy;
unset http_proxy;
unset all_proxy;
sudo sed -i -e "/Acquire::http::Proxy/d" /etc/apt/apt.conf.d/proxy.conf;
sudo sed -i -e "/Acquire::https::Proxy/d" /etc/apt/apt.conf.d/proxy.conf;
'
在 bash 中运行 proxy 可设置 bash、apt 走代理,运行 unproxy 则关闭代理。
6.1. Windows 访问 WSL 2
在 Windows 使用 localhost 可以直接访问 WSL 2 中运行的网络应用。记得 WSL 上要启动 sshd 服务哦~
6.2. WSL 2 访问 Windows
Windows 想要被访问,要保证安装了 OpenSSH 服务器。运行指令如下:
# 检查 SSH 服务器和客户端是否处于活动状态
$ Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
# 在我的例子中,客户端已安装,但 OpenSSH 服务器未安装。
# 安装 OpenSSH 服务器
$ Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
启动 OpenSSH 服务器服务:
# 将服务标记为自动启动
$ Set-Service -Name sshd -StartupType 'Automatic'
# 启动 OpenSSH 服务器服务
$ Start-Service sshd
# 在防火墙中配置 SSH
$ if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) { Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 } else { Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists." }
尝试 ssh 连局域网 ip 能通,密码是微软账户密码,注意这里 WSL 连 Windows 用 localhost 是不行的,会报密码错误,但可能能解决。
6.3. mac 访问 Windows 和 WSL
但是同一局域网下的 mac 还是不能互相 ssh 通。究竟是为何呢??
windows 端:
mac 端:
不知道是什么原因,总之此处留个坑吧,希望以后学明白计网再来填上。