git/pip/easy_install/apt代理/源设置

在公司内网环境中,访问公网往往需要经过公司的代理,对于浏览器、IDE等开发工具,都提供了设置代理的配置,而git、pip、easy_install等CLI工具,则需要通过命令或配置文件进行代理设置;对于Python、Ubuntu、Maven等研发环境,一些公司内网会提供镜像,加快访问速度。相关设置,在此做个笔记,避免每次用到了都去Google。

公司内网的代理,访问时往往需要用户名密码,在URL中带用户名密码的格式如下:

http://user:password@proxy_host:port

git配置代理

设置代理

git config --global http.proxy http://user:password@proxy_host:port
git config --global https.proxy http://user:password@proxy_host:port

给指定域名设置代理
如给github.com设置代理

git config --global http.https://github.com.proxy http://user:password@proxy_host:port

取消代理

git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset http.https://github.com.proxy

git代理常见错误处理

错误1:SSL certificate problem: unable to get local issuer certificate
原因:git默认开启了SSL验证,关闭即可。
解决办法:关闭SSL验证,配合如下。

git config --global http.sslVerify false
git config --global https.sslVerify false

错误2:Received HTTP code 407 from proxy after CONNECT
原理:代理认证错误
解决办法:代理配置正确的用户名密码

git config --global http.proxy http://user:password@proxy_host:port
git config --global https.proxy http://user:password@proxy_host:port

注:为了避免遗漏,最好把http和https的都配上。

pip配置代理

直接在命令行中增加--proxy参数来指定代理
pip install numpy --proxy http://user:password@proxy_host:port
通过pip config set命令设置代理
pip config set global.proxy http://user:password@proxy_host:port

这里要注意下,pip官方帮助文档说的设置方法为 pip config [<file-option>] set name value ,这里的name直接写proxy会报ValueError: not enough values to unpack (expected 2, got 1)错误,name要写为<profile>.<key>格式,如这里的global.proxy
此操作和直接改配置文件效果相同,但在不确定配置文件位置的时候,可以使用此操作来定位配置文件(执行后会提示修改的文件)。

直接修改pip.ini文件进行配置
[global]
proxy=http://user:password@proxy_host:port 

pip配置源

Pip的配置文件为用户根目录下的:~/.pip/pip.conf(Windows路径为:$HOME\pip\pip.ini), 配置内容:

[global]
index-url = http://mirrors.xxx.com/pypi/simple
trusted-host = mirrors.xxx.com
timeout = 120

easy_install配置代理

Windows中

set http_proxy=http://user:password@proxy_host:port
set https_proxy=http://user:password@proxy_host:port

Linux中

export http_proxy=http://user:password@proxy_host:port
export https_proxy=http://user:password@proxy_host:port

easy_install配置源

easy_install的配置文件为用户根目录下的:~/.pydistutils.cfg(Windows路径为:$HOME\pydistutils.cfg), 配置内容:

[easy_install]
index-url = http://mirrors.xxx.com/pypi/simple

Ubuntu apt设置软件源

apt软件源的配置文件为 /etc/apt/sources.list,修改前请先备份。

修改 sources.list文件,将 http://archive.ubuntu.comhttp://security.ubuntu.com替换成 http://mirrors.xxx.com,参考命令:
sudo sed -i "s@http://.*archive.ubuntu.com@http://mirrors.xxx.com@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@http://mirrors.xxx.com@g" /etc/apt/sources.list
执行 apt-get update更新索引