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


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

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

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. export http_proxy="http://proxy-server:3128/"  
  2. export https_proxy="http://proxy-server:3128/"  
  3. export ftp_proxy="http://proxy-server:3128/"  
export http_proxy="http://proxy-server:3128/"
export https_proxy="http://proxy-server:3128/"
export ftp_proxy="http://proxy-server:3128/"

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

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. source ~/.bashrc  
source ~/.bashrc

然后测试wget是没有问题的,如下:

 

\

但使用git clone就不行

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. git clone git@github.com:aborn/ulitcs.git   
 git clone git@github.com:aborn/ulitcs.git 
通过这两篇文章知道了原因:在windows上通过代理访问github.com 和 Using git over proxy

 

配制过程分为以下几步:

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

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install socat   
 sudo apt-get install socat 

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

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #!/bin/sh   
  2. # Use socat to proxy git through an HTTP CONNECT firewall.   
  3. # Useful if you are trying to clone git:// from inside a company.   
  4. # Requires that the proxy allows CONNECT to port 9418.   
  5. #   
  6. # Save this file as gitproxy somewhere in your path   
  7. # (e.g., ~/bin) and then run   
  8. # chmod +x git-proxy   
  9. # git config --global core.gitproxy git-proxy   
  10. #   
  11. #   
  12. # Configuration. Common proxy ports are 3128, 8123, 8000.   
  13. _proxy=proxy-server  
  14. _proxyport=3128  
  15. exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport  
#!/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

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. cp git-proxy /home/lisp/local/bin/  
cp git-proxy /home/lisp/local/bin/
将该目录加入到PATH,加入以下内容到~/.bashrc,然后souce ~/.bashrc

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. export PATH=$PATH:/home/lisp/local/bin  
export PATH=$PATH:/home/lisp/local/bin

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. source ~/.bashrc  
source ~/.bashrc

 

 

4. 修改~/.gitconfig,加入以下行和代理

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. gitproxy = git-proxy  
gitproxy = git-proxy
我.gitconfig文件内容如下:

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. [push]  
  2.     default = simple  
  3. [user]  
  4.     name = aborn  
  5.     email = loveaborn@foxmail.com  
  6. [core]  
  7.     editor = emacs  
  8.     gitproxy = git-proxy  
  9. [https]  
  10.     proxy = http://proxy-server:3128  
  11. [http]  
  12.     proxy = http://proxy-server:3128  
[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文件即可,然后编译

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. gcc -o connect connect.c  
gcc -o connect connect.c

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

 

 

6. 修改~/.ssh/config,加入以下行

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p   
 ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p 

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

 

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p  
  2. Host github.com  
  3. User loveaborn@foxmail.com  
  4. Port 443  
  5. Hostname ssh.github.com  
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步放置的目录一致。

 

 

以上步骤完成后,就行了,如下截图:

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. git clone git@github.com:aborn/ulitcs.git     
git clone git@github.com:aborn/ulitcs.git   

 

\

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. git push  
git push

\

相关内容