Ubuntu Linux下通过代理(proxy)使用git上github.com

版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/loveaborn/article/details/24575659

github.com,作为程序猿的代码仓库,我们常常会用到。但有时候我们不能直接通过网络链接它,仅仅能通过代理。

这里我有一台代理服务器。起初我以为在终端设置了代理环境即可了。其设置为在你的~/.bashrc里增加下面几行:

export http_proxy="http://proxy-server:3128/"
export https_proxy="http://proxy-server:3128/"
export ftp_proxy="http://proxy-server:3128/"

设置好以后。使用下面命令使其启动

source ~/.bashrc

然后測试wget是没有问题的,例如以下:


但使用git clone就不行

 git clone git@github.com:aborn/ulitcs.git 
通过这两篇文章知道了原因:在windows上通过代理訪问github.com 和 Using git over proxy

配制过程分为下面几步:

1. 安装socat,在ubuntu下使用下面命令安装

 sudo apt-get install socat 

2. 编辑一个脚本文件,名字为git-proxy ,内容例如以下

#!/bin/sh
# Use socat to proxy git through an HTTP CONNECT firewall.
# Useful if you are trying to clone git:// from inside a company.
# Requires that the proxy allows CONNECT to port 9418.
#
# Save this file as gitproxy somewhere in your path
# (e.g., ~/bin) and then run
# chmod +x git-proxy
# git config --global core.gitproxy git-proxy
#
#
# Configuration. Common proxy ports are 3128, 8123, 8000.
_proxy=proxy-server
_proxyport=3128
exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport

3. 将git-proxy放到一个文件夹下,如我将它放到/home/lisp/local/bin。并将该文件夹增加到PATH

cp git-proxy /home/lisp/local/bin/
将该文件夹增加到PATH。增加下面内容到~/.bashrc,然后souce ~/.bashrc

export PATH=$PATH:/home/lisp/local/bin

source ~/.bashrc


4. 改动~/.gitconfig,增加下面行和代理

gitproxy = git-proxy
我.gitconfig文件内容例如以下:

[push]
	default = simple
[user]
	name = aborn
	email = loveaborn@foxmail.com
[core]
	editor = emacs
	gitproxy = git-proxy
[https]
	proxy = http://proxy-server:3128
[http]
	proxy = http://proxy-server:3128

5. 下载转换协议文件connect.c,下载地址点击

仅仅要下载connect.c文件即可,然后编译

gcc -o connect connect.c

将编译后的文件connect也复制到/home/lisp/local/bin下


6. 改动~/.ssh/config,增加下面行

 ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p 

我的~/.ssh/config文件内容例如以下:

ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p
Host github.com
User loveaborn@foxmail.com
Port 443
Hostname ssh.github.com

注意这里的connect文件文件夹与第5步放置的文件夹一致。


以上步骤完毕后。即可了。例如以下截图:

git clone git@github.com:aborn/ulitcs.git   


git push



注意:

1. 上面的proxy-server依据你的代理,设置为替换为你的代理服务器的ip地址或者域名

2. 上面的connect.c 文件、编译好的connect文件和git-proxy文件。也能够从这里下载connect.tar.gz 和 git-proxy

3. 我的操作系统为Ubuntu 14.04LTS


【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10918531.html