Ubuntu的代理设置备忘录

在Ubuntu安装后,以下是需要为使用代理环境进行设置的列表(除apt外,这些都是每个用户的设置)。

环境设置

首先,查找代理设置的方法。

printenv http_proxy https_proxy

如果什么都没有出现,为了向 ~/.bashrc 添加配置,请执行以下操作。

echo -e "\n## proxy setting"  >> ~/.bashrc
echo 'export http_proxy="http://proxy_server:port/"' >> ~/.bashrc
echo 'export https_proxy=$http_proxy' >> ~/.bashrc

APT配置

在apt.conf文件中添加代理设置
在shell中执行以下操作

sudo nano /etc/apt/apt.conf

通过在此打开的apt.conf文件中写入以下内容并保存(保存快捷键是Ctrl+O,退出是Ctrl+X)。

Acquire::http::Proxy "http://proxy_server:port";
Acquire::https::Proxy "http://proxy_server:port";

Git的配置

git的代理设置

使用HTTPS

git config --global http.proxy http://proxy_server:port
git config --global https.proxy http://proxy_server:port

如果使用ssh的话

在 ~/.ssh/config 文件中进行以下设置
可以参考下面等相关资料来生成密钥
GitHub SSH连接步骤:从密钥对的生成开始~

Host github
  User git
  HostName ssh.github.com
  IdentityFile ~/.ssh/id_rsa
  ProxyCommand nc -X connect -x proxy_server:port %h %p

连接测试

ssh -T github

Docker配置

运行 sudo systemctl edit docker,并进行以下环境变量的设置。

[Service]
Environment = 'http_proxy=http://proxy_server:port' 'https_proxy=http://proxy_server:port'

接下来,为了客户端的配置,请在~/.docker/config.json文件中写入以下内容。

{
  "proxies": {
    "default": {
      "httpProxy": "http://proxy_server:port",
      "httpsProxy": "http://proxy_server:port"
    }
  }
}

参考:在使用代理的环境中执行Docker的方法

wget的设置。

请在 ~/.wgetrc 文件中写下以下内容。

http_proxy=http://proxy_server:port/
https_proxy=http://proxy_server:port/

在使用add-apt-repository时应用代理

使用sudo命令的-E选项可以继承环境变量。
参考【为了我们在代理环境中不能出去】如何在代理环境中使用sudo add-apt-repository【以及sudo和http_proxy的一些事情】。
以下是git的示例。

sudo -E add-apt-repository ppa:git-core/ppa

在使用apt-key时应用代理

在安装CUDA工具包等时,可能会使用apt-key。在这种情况下,由于可能会出现网络错误,因此需要添加–keyserver-option来进行代理设置。

sudo apt-key adv --keyserver-option http-proxy=http://proxy_server:port

注意:需要注意的是,作为选项的顺序中,–keyserver-option应位于选项的开头。