Ubuntu-WSL2一键设置代理操作

现状:

  • Window11中拥有自己的代理软件 ,可以科学上网
  • 已在WSL2中安装Ubuntu22.04

需求:

  • Ubuntu-WSL2实现科学上网

实现:

参考:为 WSL2 一键设置代理

  1. Linux 子系统中的网关指向的是 Windows,DNS 服务器指向的也是 Windows
  • 在 Ubuntu 子系统中,通过 cat /etc/resolv.conf 查看 DNS 服务器 IP,如下所示,IP为10.255.255.254
~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 10.255.255.254
  1. WSL2 中配置的代理要指向 Windows 的 IP
  • 创建一个bash脚本实现代理,如下:
touch set_proxy.sh # 创建空白脚本文件
vim set_proxy.sh #编辑
chmod +x set_proxy.sh #增加运行权限
./set_proxy.sh # 运行
# set_proxy.sh文件内容
host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")                                       
export ALL_PROXY="http://$host_ip:7897"                                                                   
echo "设置代理成功,端口:7897,请在Windows中打开客户端允许本地局域网请求" > /home/user/set_proxy.log     # 输出log,验证是否开机运行成功;
exit 0   
  1. Windows 上的代理客户端需要允许来自本地局域网的请求;
    在这里插入图片描述
    4.测试配置是否成功
~$ ping google.com
PING google.com (198.18.0.78) 56(84) bytes of data.
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=1 ttl=63 time=1.89 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=2 ttl=63 time=1.08 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=3 ttl=63 time=1.26 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=4 ttl=63 time=1.12 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=5 ttl=63 time=1.31 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=6 ttl=63 time=0.586 ms
64 bytes from 198.18.0.78 (198.18.0.78): icmp_seq=7 ttl=63 time=0.735 ms
  1. 设置脚本开启启动
sudo cp set_proxy.sh /etc/init.d/
sudo update-rc.d set_proxy.sh defaults
  1. 验证开机启动
wsl --shutdown
Get-Service LxssManager | Restart-Service
wsl
ping google.com #重启后查看ping谷歌是成功

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shine.Zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值