公司使用代理,github不能使用ssh管理git倉庫,只能通過https由用戶名密碼登陸,網搜了一下發現了兩個方法,過程相似。但是使用nc命令的方法一沒有運行成功,因為對該命令不熟,沒有深究,改用方法二成功。
一般通過http代理,只能訪問外網的443和80端口,github現在本身就支持https的方式提交代碼,這里講的是如何通過默認方式即git協議提交代碼,我們知道git是基於ssh的,端口是22,github這個服務器的ssh端口我們又不能像自己的服務器一樣改成443,怎么辦呢。在基於http做好的ssh代理之上,再連接github,已經開啟的ssh代理是不受端口限制的。
方法一
配置一個 proxy-wrapper
腳本
cat > $HOME/bin/proxy-wrapper
#!/bin/bash
nc -x 127.0.0.1:7080 -X5 $*
給它增加一個可執行權限
$ chmod +x $HOME/bin/proxy-wrapper
配置 .ssh/config
, 對 github.com 設置一個代理命令
Host github github.com
Hostname github.com
User git
ProxyCommand $HOME/bin/proxy-wrapper '%h %p'
必須全部走ssh協議
$ git clone git@github.com:meshinestar/HelloGit.git
以上,參考:http://fengmk2.cnpmjs.org/github-proxy.html。
但是感覺這個方法不如這個詳細:http://fungo.me/linux/tunneling-ssh-over-http-proxy.html,只是沒有嘗試。
方法二
配置 .ssh/config
, 對 github.com 設置一個代理命令
Host github.com
ProxyCommand ~/.ssh/ssh-https-tunnel %h %p ~/.ssh/proxyauth
Port 443
Hostname ssh.github.com
這里你可以看到第二行最后有一個~/.ssh/proxyauth
。這是因為我單位的代理有口令,所以要再生成一個proxyauth
文件,格式就是:username:password
。如果你沒有,去掉它就行了。
同時Hostname
的目的是為了創建一個別名,其實我們使用的是ssh.github.com
,但是因為平時都使用git@github.com
,所以為了不進行修改,創建一個別名。
下載ssh-https-tunnel,可以從 http://zwitterion.org/software/ssh-https-tunnel/ssh-https-tunnel ,保存到你的git的~/.ssh目錄下,同時要打開這個文件進行修改,將:
my $proxy = "";
my $proxy_port = ;
改成你的實際的代理服務器地址。
使用ssh git@github.com 來測試
The authenticity of host 'github.com (192.38.252.124)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.38.252.124' (RSA) to the list of known hosts.
Permission denied (publickey).
以上,參考:http://blog.chinaunix.net/uid-22627501-id-3050090.html