知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

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

发布时间:2014-09-05 17:40:52作者:知识屋

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

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

 

[python] view plaincopyprint?在CODE上查看代码片派生到我的代码片
export http_proxy="http://proxy-server:3128/"  
  • export https_proxy="http://proxy-server:3128/"  
  • export ftp_proxy="http://proxy-server:3128/"  
    export%20http_proxy="http://proxy-server:3128/"export%20https_proxy="http://proxy-server:3128/"export%20ftp_proxy="http://proxy-server:3128/"设置好以后,使用以下命令使其启动

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片
    source ~/.bashrc  
    source%20~/.bashrc然后测试wget是没有问题的,如下:

     

    但使用git%20clone就不行

     

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

     

    配制过程分为以下几步:

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

     

    [python]%20view%20plaincopyprint?派生到我的代码片sudo apt-get install socat   %20sudo%20apt-get%20install%20socat%202.%20编辑一个脚本文件,名字为git-proxy%20,内容如下

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片#!/bin/sh%20  
  • # Use socat to proxy git through an HTTP CONNECT firewall.%20  
  • # Useful if you are trying to clone git:// from inside a company.%20  
  • # Requires that the proxy allows CONNECT to port 9418.%20  
  • #%20  
  • # Save this file as gitproxy somewhere in your path%20  
  • # (e.g., ~/bin) and then run%20  
  • # chmod +x git-proxy%20  
  • # git config --global core.gitproxy git-proxy%20  
  • #%20  
  • #%20  
  • # Configuration. Common proxy ports are 3128, 8123, 8000.%20  
  • _proxy=proxy-server  
  • _proxyport=3128  
  • exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport  #!/bin/sh#%20Use%20socat%20to%20proxy%20git%20through%20an%20HTTP%20CONNECT%20firewall.#%20Useful%20if%20you%20are%20trying%20to%20clone%20git://%20from%20inside%20a%20company.#%20Requires%20that%20the%20proxy%20allows%20CONNECT%20to%20port%209418.##%20Save%20this%20file%20as%20gitproxy%20somewhere%20in%20your%20path#%20(e.g.,%20~/bin)%20and%20then%20run#%20chmod%20+x%20git-proxy#%20git%20config%20--global%20core.gitproxy%20git-proxy###%20Configuration.%20Common%20proxy%20ports%20are%203128,%208123,%208000._proxy=proxy-server_proxyport=3128exec%20socat%20STDIO%20PROXY:$_proxy:$1:$2,proxyport=$_proxyport3.%20将git-proxy放到一个目录下,如我将它放到/home/lisp/local/bin,并将该目录加入到PATH

     

     

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

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片export PATH=$PATH:/home/lisp/local/bin  export%20PATH=$PATH:/home/lisp/local/bin[python]%20view%20plaincopyprint?派生到我的代码片source ~/.bashrc  source%20~/.bashrc

     

     

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

     

    [python]%20view%20plaincopyprint?派生到我的代码片gitproxy = git-proxy  gitproxy%20=%20git-proxy我.gitconfig文件内容如下:

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片[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  
  • [push] default%20=%20simple[user] name%20=%20aborn email%20=%20loveaborn@foxmail.com[core] editor%20=%20emacs gitproxy%20=%20git-proxy[https] proxy%20=%20http://proxy-server:3128[http] proxy%20=%20http://proxy-server:3128

     

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

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

     

    [python]%20view%20plaincopyprint?派生到我的代码片gcc -o connect connect.c  gcc%20-o%20connect%20connect.c将编译后的文件connect也拷贝到/home/lisp/local/bin下

     

     

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

     

    [python]%20view%20plaincopyprint?派生到我的代码片ProxyCommand /home/lisp/local/bin/connect -H proxy-server:3128 %h %p   %20ProxyCommand%20/home/lisp/local/bin/connect%20-H%20proxy-server:3128%20%h%20%p%20我的~/.ssh/config文件内容如下:

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片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  ProxyCommand%20/home/lisp/local/bin/connect%20-H%20proxy-server:3128%20%h%20%pHost%20github.comUser%20loveaborn@foxmail.comPort%20443Hostname%20ssh.github.com注意这里的connect文件目录与第5步放置的目录一致。

     

     

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

     

    [python]%20view%20plaincopyprint?派生到我的代码片git clone git@github.com:aborn/ulitcs.git     git%20clone%20git@github.com:aborn/ulitcs.git%20%20%20

     

     

    [python]%20view%20plaincopyprint?派生到我的代码片git push  git%20push
    (免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
    收藏
    • 人气文章
    • 最新文章
    • 下载排行榜
    • 热门排行榜