多平台GitHub配置代理

本地拉取github老是失败,可通过全局配置代理,或只给github.com配置代理

前言:本地拉取github老是失败,可通过全局配置代理,或只给github.com配置代理

git 配置代理

windows 设置代理

http || https协议

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//设置全局代理
git config --global https.proxy http://127.0.0.1:7897
git config --global https.proxy https://127.0.0.1:7897
// socks
git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
//只对github.com使用代理,其他仓库不走代理
git config --global http.https://github.com.proxy socks5://127.0.0.1:7897
git config --global https.https://github.com.proxy socks5://127.0.0.1:7897
//取消github代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
//取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy

SSH协议

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//对于使用git@协议的,可以配置socks5代理
//在~/.ssh/config 文件后面添加几行,没有可以新建一个
//socks5
Host github.com
User git
ProxyCommand connect -S 127.0.0.1:7897 %h %p
//http || https
Host github.com
User git
ProxyCommand connect -H 127.0.0.1:7897 %h %p

WSL2设置代理

在 Ubuntu 子系统中,通过 cat /etc/resolv.conf 查看 DNS 服务器 IP

1
2
3
4
5
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 172.23.64.1

其实上面地址就是windows下面这个ip地址

win11->设置->网络和Internet->高级网络设置->硬件和连接属性

名称:vEthernet (WSL)

ipv4地址:172.23.64.1/20

我们要为WSL配置ssh代理和http代理

1
2
3
4
5
6
7
touch ~/.ssh/config
vim ~/.ssh/config
Host github.com
    HostName github.com
    User git
    # 走 socks5 代理
    ProxyCommand nc -v -x 172.28.32.1:7897 %h %p

使用下面命令检查ssh代理配置是否成功

1
2
3
ssh -T github.com
Connection to github.com 22 port [tcp/ssh] succeeded!
Hi wq-zhijun! You've successfully authenticated, but GitHub does not provide shell access.

可以将上面ip地址配置代理写入到.bashrc文件中,这样就可以自己用户开机永久生效,

另外apt要单独在/etc/apt/apt.conf设置代理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
export u_host=`cat /etc/resolv.conf|grep nameserver|awk '{print $2}'`
sed -i "/.*ProxyCommand*/c\     ProxyCommand nc -v -x $u_host:7897 %h %p" ~/.ssh/config
proxy () {
    export ALL_PROXY="http://$u_host:7897"
    export all_proxy="http://$u_host:7897"
    export {http,https,ftp}_proxy=$ALL_PROXY
    export {HTTP,HTTPS,FTP}_PROXY=$ALL_PROXY
    echo -e "Acquire::http::Proxy \"http://$u_host:7897\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
    echo -e "Acquire::https::Proxy \"http://$u_host:7897\";" | sudo tee -a /etc/apt/apt.conf > /dev/null
    curl ip.gs
}
proxy # default call
proxy noproxy () {
    unset ALL_PROXY
    unset all_proxy
    sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf
    sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf
    curl ip.gs
}

7897端口 是clash for windows端口,另外将 Allow LAN 打开。

还需要将防火墙打开

win+r 输入 control到如下目录

控制面板\系统和安全\Windows Defender 防火墙

放行clash*

git push失败或者卡住不动问题

原因是有的节点22端口被服务端封锁了,要么改服务端端口要么改自己本地更换443端口(~/.ssh/config)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 可以先使用如下测试下
ssh -T -p 443 git@ssh.github.com
# Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
# 更换端口后再测试下
ssh -T git@github.com
# Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
# .ssh目录的config文件添加如下内容
vim ~/.ssh/config
Host github.com
    HostName github.com
    User git
    # set socks5 proxy
    ProxyCommand nc -v -x 172.23.64.1:7897 %h %p

修改配置文件的方式

  • windows系统的配置 文件在 C:\Users\admin\.gitconfig 配置文件如下
1
2
3
4
[http "https://github.com"]
	proxy = http://127.0.0.1:7897
[https "https://github.com"]
	proxy = https://127.0.0.1:7897
Buy me a coffee~
Fred 支付宝支付宝
Fred 微信微信
0%