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

本文详细介绍如何在Ubuntu系统中配置Git代理,通过socat实现HTTP CONNECT防火墙下的Git仓库克隆及推送。步骤包括安装socat、创建git-proxy脚本、配置.gitconfig文件等。

配制过程分为以下几步:

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


  1. sudo apt-get install socat  
sudo apt-get install socat

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

  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= 172.26.100.238  
  14. _proxyport=64000  
  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= 172.26.100.238
_proxyport=64000
exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport

 3. 将git-proxy放到一个目录下,如我将它放到/home/wilsonke/local/bin,并将该目录加入到PATH

  1. cp git-proxy /home/wilsonke/local/bin/  
cp git-proxy /home/wilsonke/local/bin/

将该目录加入到PATH,加入以下内容到~/.bashrc,然后souce ~/.bashrc

  1. export PATH=$PATH:/home/wilsonke/local/bin  
export PATH=$PATH:/home/wilsonke/local/bin

  1. source ~/.bashrc  
source ~/.bashrc

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

  1. [push]  
  2.     default = simple  
  3. [user]  
  4.     name = wilsonke77  
  5.     email = 275156430@qq.com  
  6. [core]  
  7.     editor = emacs  
  8.     gitproxy = git-proxy  
  9. [https]  
  10.     proxy = http://wilson_ke:password@172.26.100.238:64000  
  11. [http]  
  12.     proxy = http://wilson_ke:password@172.26.100.238:64000  
[push]
	default = simple
[user]
	name = wilsonke77
	email = 275156430@qq.com
[core]
	editor = emacs
	gitproxy = git-proxy
[https]
	proxy = http://wilson_ke:password@172.26.100.238:64000
[http]
	proxy = http://wilson_ke:password@172.26.100.238:64000

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

  1. gcc -o connect connect.c  
gcc -o connect connect.c

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


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

  1. ProxyCommand /home/wilsonke/local/bin/connect -H 172.26.100.238:64000 %h %p  
  2. Host github.com  
  3. User 275156430@qq.com  
  4. Port 443  
  5. Hostname ssh.github.com  
ProxyCommand /home/wilsonke/local/bin/connect -H 172.26.100.238:64000 %h %p
Host github.com
User 275156430@qq.com
Port 443
Hostname ssh.github.com

7.完成并测试

  1. git clone https://github.com/facebook/wangle  
git clone https://github.com/facebook/wangle

如果能正常clone下来,则表示成功。


后记:很多开源项目同时可能还会用到wget来下载代码,同样,wget也要设置代理服务器


创建用户的~/.wgetrc文件中,添加如下内容:

  1. http_proxy = http://172.26.100.238:64000/  
  2. ftp_proxy = http://172.26.100.238:64000/  
  3. --proxy-user=wilson_ke  
  4. --proxy-passwd=password  
http_proxy = http://172.26.100.238:64000/
ftp_proxy = http://172.26.100.238:64000/
--proxy-user=wilson_ke
--proxy-passwd=password

完成了上述配置后,就可以成功编译facebook的项目proxygen


参考资料

http://blog.youkuaiyun.com/loveaborn/article/details/24575659

当用户遇到 `fatal: git@github.com:xiaowu111111/learngit.git does not appear to be a git repository` 错误时,这通常意味着 Git 无法找到指定的远程仓库。该问题可能由多种原因引起,包括但不限于仓库地址错误、SSH 配置问题或网络连接问题。 ### 1. 检查仓库地址 确保你使用的仓库地址是正确的。如果你使用的是 SSH 地址(如 `git@github.com:xiaowu111111/learngit.git`),请确认以下几点: - 仓库名称和用户名是否正确无误。 - 是否在 GitHub 上创建了该仓库,并且已经推送了内容。 - 如果你是通过复制粘贴获得的地址,请检查是否有拼写错误或多余的字符。 ### 2. 验证 SSH 配置 如果你使用的是 SSH 协议进行 Git 操作,需要确保你的本地环境已正确配置 SSH 密钥并将其添加到 GitHub 账户中。 #### 生成 SSH 密钥(如果尚未生成) ```bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 默认情况下,密钥将保存在 `~/.ssh/id_rsa` 和 `~/.ssh/id_rsa.pub` 中。 #### 添加 SSH 密钥到 GitHub 将公钥(`.pub` 文件)的内容复制并粘贴到 GitHub 的 SSH 密钥设置页面: 1. 打开终端并运行 `cat ~/.ssh/id_rsa.pub`(Linux/macOS)或使用文本编辑器打开文件(Windows)。 2. 复制输出内容。 3. 登录 GitHub,进入 **Settings > SSH and GPG keys**,点击 **New SSH key** 并粘贴密钥内容。 #### 测试 SSH 连接 运行以下命令以测试与 GitHub 的连接: ```bash ssh -T git@github.com ``` 如果一切正常,你会看到类似 `Hi username! You've successfully authenticated, but GitHub does not provide shell access.` 的消息。 ### 3. 使用 HTTPS 替代 SSH 如果你不想使用 SSH 或者遇到问题,可以改用 HTTPS 地址克隆仓库。例如: ```bash git clone https://github.com/xiaowu111111/learngit.git ``` 使用 HTTPS 时,Git 会提示你输入用户名和密码(或 Personal Access Token)。如果你之前设置了凭据缓存,可以通过以下命令清除旧凭据: ```bash git credential-cache exit ``` 或者在 Windows 上使用: ```bash git config --global --unset credential.helper ``` 然后再重新设置: ```bash git config --global credential.helper wincred ``` ### 4. 检查网络连接和代理设置 如果你所在的网络环境需要通过代理访问外部服务,请确保 Git代理配置正确。你可以使用以下命令查看当前的代理设置: ```bash git config --global --get http.proxy git config --global --get https.proxy ``` 如果发现代理配置不正确,可以按照以下方式取消代理设置: ```bash git config --global --unset http.proxy git config --global --unset https.proxy ``` ### 5. 更新 Git 客户端 确保你使用Git 版本是最新的。旧版本的 Git 可能存在兼容性问题或 bug,导致某些操作失败。可以通过以下命令更新 Git: - **Windows**: 下载最新版本的 [Git for Windows](https://git-scm.com/download/win)。 - **macOS**: 使用 Homebrew 更新: ```bash brew update && brew upgrade git ``` - **Linux (Debian/Ubuntu)**: ```bash sudo apt-get update && sudo apt-get upgrade git ``` ### 6. 检查仓库是否存在 最后,确保你尝试访问的仓库确实存在于 GitHub 上,并且你有权限访问它。如果你不确定仓库是否存在,可以在浏览器中直接访问 `https://github.com/xiaowu111111/learngit` 来验证。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值