网络配置

Git

Git 的网络配置包括基于 http 协议和 ssh 协议,两者有不同的配置方法。

1. http 协议

通过git config命令进行。假设代理地址为http://proxy.exmaple.com:8080,配置命令如下:

git config --global http.proxy http://proxy.example.com:8080

2. ssh 协议

SSH 配置稍微复杂一点,通常通过ssh-agent转发 SSH 密钥,或者通过设置代理服务器使用。最常见的方法是通过ProxyCommand配置来设置代理。

a. 使用ssh-agent转发 SSH 密钥

首先,启动ssh-agent并添加 SSH 密钥:

eval $(ssh-agent -s)

然后在 Git 配置中在代理主机。

b. 配置.ssh/config文件使用代理

~/.ssh/config文件中,配置以下内容:

Host github.com
  Hostname github.com
  User git
  ProxyCommand nc -X connect -x proxy.example.com:8080 %h %p

ProxyCommand使用nc(netcat)工具通过 HTTP/SOCKS 代理连接到 Git 服务器。-X connect表示使用 HTTP 代理,可以将connect替换为5或者4,分别表示使用 SOCKET5 和 SOCKET4 代理。-x proxy.example.com:8080指定代理地址和端口。

Wget

为 Wget 配置端口代理,只需要在用户空间创建对应的配置文件即可。

配置文件创建

在用户空间中,创建 Wget 的配置文件.wgetrc

touch $HOME/.wgetrc

配置文件内容

输入以下内容,即可配置启用代理,并通过example.com:port端口连接。

use_proxy=on
http_proxy=http://example.com:port
https_proxy=http://example.com:port

Apt

为 Apt 工具配置端口代理,用于更方便下载 GitHub 之类的程序。

在路径 /etc/apt/apt.conf.d/ 下,创建配置文件。需要注意,配置文件名称任意,但需要以.conf结尾。

键入以下内容:

Acquire {
  http::Proxy "http://example.com:port";
  https::Proxy "http://example.com:port";
}

如果需要指定默认直连,某个网址走端口代理,可以如下配置:

Acquire::http::Proxy {
  default "DIRECT";
  "proxy.site" "http://example.com:port";
}

Acquire::https::Proxy {
  default "DIRECT";
  "proxy.site" "http://example.com:port";
}