前言
公司里面基于安全的原因封锁了外网的接口,普通程序只能通过代理来访问外网。我配置的Ubuntu的虚拟机想要访问外网就遇到了问题,需要对其进行代理的配置使其能平滑的使用互联网上的一些功能。
A. 在ubuntu下专供apt-get 使用的代理
在终端下编辑/etc/apt/apt.conf并加入下面这行*
Acquire {
Retries "0";
HTTP {
Proxy "http://user:passwd@proxy_ip_address:port";
};
};
B. 使每次启动时均能自动配置代理服务器
1.配置~/.bashrc*
export http_proxy="http://user:passwd@proxy_ip_address:port"
export https_proxy="http://user:passwd@proxy_ip_address:port"
2.安装socat
sudo apt-get install socat
3.编辑一个脚本文件,名字为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=proxyaddress
_proxyport=port
_proxyauth=username:passwd
exec socat STDIO PROXY:$_proxy:$1:$2,proxyport=$_proxyport,proxyauth=$_proxyauth
cp git-proxy ~/bin/
sudo chmod +x git-proxy
5.修改~/.gitconfig,加入以下行和代理
[core]
gitproxy = git-proxy
[user]
name = LI Pan
email = pan***@gmail.com
6. 下载转换协议文件connect.c(https://bitbucket.org/gotoh/connect/src/ee1fbc21da4b?at=default) 并编译
gcc -o connect connect.c
7. 拷贝至~/bin,并添加可执行权限
sudo chmod +x connect
8. 创建并修改~/.ssh/config,加入以下行
ProxyCommand /home/panlis/bin/connect -H username:passwd@proxyaddress:port %h %p
9. 然后你就已可以运行下列命令来查看代理是否可以工作
panlis@ubuntu:~/.ssh$ git clone git@github.com:***/learngit.git
Cloning into 'learngit'...
The authenticity of host 'github.com (<no hostip for proxy command>)' can't be established.
RSA key fingerprint is xx:xx:xx....
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
10. 如果需要让配置立即生效,还需要运行
source ~/.bashrc
C. 备注
/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.~/.bash_pro每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.
~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件.
*若无用户名密码可直接使用http://proxy_ip_address:port