解决wsl2 各类软件源和代理问题
标签搜索
侧边栏壁纸
  • 累计撰写 5 篇文章
  • 累计收到 0 条评论

解决wsl2 各类软件源和代理问题

那年夏天
2022-06-03 / 0 评论 / 10 阅读 / 正在检测是否收录...

解决wsl2 各类软件源和代理问题

需求背景说明:

日常用wsl2 开发和搞ctf 需要使用linux环境,但是使用git、pip、npm、apt、nuget等软件包管理时,国内与局域网访问很慢,需要使用宿主机上的clash代理,但是发现有许多坑需要注意,搜索网上资料后,总结如下。

wsl2 走clash代理的关键要点

  1. 在主页 打开allow lan选项

w1.png

  1. 在找到wsl虚拟网卡的地址,作为在wsl2系统中的代理地址 (clash默认端口号7890)

w2.png

我这里得到的wsl虚拟网卡地址是 172.29.192.1 可在wsl2 中使用 cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*' 获取

系统级代理设置

#root 权限下
vim vim ~/.bashrc  # .bashrc 文件 是linux设置用户环境变量的配置文件,每次用户进入终端都会执行一次里面的配置。
#添加如下配置
# wls2 的下宿主windwos的ip地址在/etc/resolv.conf 文件内的 nameserver 配置项内,通过下面的第5行命令可动态获取
export hostip=$(cat /etc/resolv.conf |grep -oP '(?<=nameserver\ ).*')
export https_proxy="http://${hostip}:7890"
export http_proxy="http://${hostip}:7890"
export all_proxy="socks5://${hostip}:7890"
export ALL_PROXY="socks5://${hostip}:7890"
# 默认开启代理,可通过unproxy 和proxy 别名命令 关闭或重新开启代理
alias proxy='export https_proxy="http://${hostip}:7890";export http_proxy="http://${hostip}:7890";export all_proxy="socks5://${hostip}:7890";export ALL_PROXY="socks5://${hostip}:7890";'
alias unproxy='unset https_proxy; unset http_proxy; unset all_proxy; unset ALL_PROXY;'

#在vim编辑保存后,使用source命令生效环境变量 或者退出终端重新进入生效
source ~/.bashrc

参考资料:

  1. WSL2使用clash for windows代理
  2. Windows 原生运行Linux如何自由访问互联网 WSL2 使用 Clash for Windows做代理
  3. Linux环境变量设置

git clash代理

参考文章:

一文让你了解如何为 Git 设置代理

针对所有域名生效

git config --global http.proxy protocol://wsl网卡地址:port

注意--glboal 选项指的是修改 Git 的全局配置文件 ~/.gitconfig,而非各个 Git 仓库里的配置文件 .git/configprotocol 指的是代理的协议,如 http,https,socks5 等。port 则为端口号。

针对特域名生效

git config --global http.url.proxy protocol://wsl网卡地址:port 

注意

  • 此处的 url 即为你需要走代理的仓库域名,urlhttp://https:// 打头的均用这个方法。
  • 网上很多中文教程,可能会告诉你 https:// 打头的 url 使用“git config --global https.https://example.com.proxy protocol://127.0.0.1:port”,这种做法其实是错的!记住一点:Git 不认 https.proxy ,设置 http.proxy 就可以支持 https 了。
  • 如果想了解 url 的更多模式,如子域名等的情况,可参照 Git 的官方文档 。网页内容搜索 http.<url>.*,即可找到相关信息。

设置github走clash代理

  • http代理
git config --global http.https://github.com.proxy http://wsl网卡地址:7890
  • socks5 代理
git config --global http.https://github.com.proxy socks5://wsl网卡地址:7891

python pip 设置国内软件源

设置单独一次命令走国内源

pip install markdown -i https://pypi.tuna.tsinghua.edu.cn/simple

全局更改软件源

# 清华源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
# 腾讯源
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
# 豆瓣源 没有https 可能会报错
pip config set global.index-url https://pypi.douban.com/simple/
# windows 更改软件源的配置文件位置
# 默认路径: Writing to C:\Users\%USERNAME%\AppData\Roaming\pip\pip.ini

apt 包管理设置国内源

ubuntu 20.04

  • 先备份

    # 先备份
    cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑文件完成后,执行如下命令进行更新缓存

sudo apt update 
sudo apt upgrade -y
vim /etc/apt/sources.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

清华大学镜像源

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

网易镜像源

deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse

中科大镜像源

deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

Debian 11 bullseye

  • 先备份

    # 先备份
    $ cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑文件完成后,执行如下命令进行更新缓存

$ sudo apt update 
$ sudo apt upgrade -y
$ vim /etc/apt/sources.list 
# 删除原来的文件内容吗粘贴下放任意一个软件源

清华源

如果遇到无法拉取 https 源的情况,请先使用 http 源并安装:

$ sudo apt install apt-transport-https ca-certificates


# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

Kali 更换国内源

与debian 和Ubuntu大同小异 前面的备份和vim环境不再赘述

Kali官方源

deb http://http.kali.org/kali kali-rolling main no-free contrib
deb-src http://http.kali.org/kali kali-rolling main non-free contrib

官方源速度不稳定,有时快有时慢;

中科大Kali镜像源

deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib
deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib

阿里云Kali镜像源

deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib
deb-src http://mirrors.aliyun.com/kali kali-rolling main non-free contrib

清华大学Kali镜像源

deb http://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/kali kali-rolling main contrib non-free

kali 不同类型软件包安装命令

$ sudo apt install -y kali-linux-default
$ sudo apt install -y kali-linux-large #: Our previous default tools for amd64/i386 images
$ sudo apt install -y kali-linux-everything #: Every metapackage and tool listed here
$ sudo apt install -y kali-desktop-live #: Used during a live session when booted from the image

npm 软件包更换源

nuget 软件包更换源

0

评论 (0)

取消