如何为 APT 命令设置代理
代理 apt
![](/data/attachment/album/202305/15/184447hh8ac86hkycy6jio.jpg)



> 
> 在本指南中，你将了解如何在 Ubuntu/Debian Linux 发行版中为 `apt` 命令设置代理。
> 
> 
> 


代理服务器是位于请求资源的客户端系统或最终用户与资源本身之间的中间服务器。在大多数情况下，代理服务器充当最终用户和互联网之间的网关。


对于组织和企业环境，代理服务器提供了许多好处。它通过阻止被认为会影响员工工作效率的网站来控制互联网的使用。它还通过数据加密增强隐私并提高组织的安全性。


有几种方法可以为 `apt` 命令设置代理，让我们直接进入。


注意：为了演示，我们将使用 Ubuntu 22.04。


### 使用代理文件为 APT 配置代理


为 `apt` 命令配置代理的最简单方法是创建一个 `proxy.conf` 文件，如下：



```
$ sudo vi /etc/apt/apt.conf.d/proxy.conf

```

对于没有用户名和密码的代理服务器，添加以下条目，如下：


对于 HTTP 代理，添加以下条目：



```
Acquire::http::Proxy "http://proxy-IP-address:proxyport/";

```

对 HTTPS 代理执行相同的操作：



```
Acquire::https::Proxy "http://proxy-IP-address:proxyport/";

```

例子：



```
$ cat  /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://192.168.56.102:3128/";
Acquire::https::Proxy "http://192.168.56.102:3128/";

```

如果你的代理服务器需要用户名和密码详细信息，请按以下方式添加：



```
Acquire::http::Proxy "http://username:password@proxy-IP-address:proxyport";
Acquire::https::Proxy "http://username:password@proxy-IP-address:proxyport";

```

例子：



```
$ cat  /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";
Acquire::https::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";

```

完成后，保存更改并退出配置文件。代理设置将在你下次运行 APT 包管理器时生效。


例如，你可以更新本地包索引，然后安装 `net-tools` 包：



```
$ sudo apt update
$ sudo apt install net-tools -y

```

![](/data/attachment/album/202305/15/184633lup6zi6354ghs3v6.jpg)


验证代理服务器日志以确认 `apt` 命令正在使用代理服务器下载包。在代理服务器运行时：



```
# tail -f /var/log/squid/access.log  | grep -i 192.168.56.240

```

这里 `192.168.56.240` 是我们 Ubuntu 机器的 IP 地址。


![](/data/attachment/album/202305/15/184639jdhb73rrbd7lbxl5.jpg)


完美，上面的输出确认我们的 Ubuntu 系统的 `apt` 命令正在通过代理服务器（192.168.56.102）下载包。


### 另一种指定代理详细信息的方法


除了第一种方法，你还可以用更简单的方式指定代理详细信息。再次创建一个 `proxy.conf` 文件，如下所示。



```
$ sudo vi /etc/apt/apt.conf.d/proxy.conf

```

对于没有用户名和密码的代理服务器，如图所示进行定义。



```
Acquire {
  http::Proxy "http://proxy-IP-address:proxyport/";
  https::Proxy "http://proxy-IP-address:proxyport/";
}

```

示例文件如下所示：



```
$ sudo vi /etc/apt/apt.conf.d/proxy.conf

```

![](/data/attachment/album/202305/15/184647x6epqn10l91aq1pf.jpg)


对于具有用户名和登录详细信息的代理服务器：



```
Acquire {
   http::Proxy "http://username:password@proxy-IP-address:proxyport/";
   https::Proxy "http://username:password@proxy-IP-address:proxyport/";
}

```

保存更改并退出配置文件。提醒一下，当你开始使用 APT 包管理器，这些设置就会立即生效。


### 总结


本指南到此结束。在本教程中，我们演示了如何为 Debian/Ubuntu Linux 发行版中使用的 APT 包管理器配置代理设置。本文就到这里了。


（题图：MJ/dfb4d5a0-9150-47bd-9f54-c120ddd77046）




---


via: <https://www.linuxtechi.com/set-proxy-settings-for-apt-command/>


作者：[James Kiarie](https://www.linuxtechi.com/author/james/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this guide, you will learn how to set proxy settings for the APT command in Ubuntu/Debian Linux distributions.

A proxy server is an intermediary server that sits between a client system or end user requesting the resource and the resource itself. In most cases, a proxy server acts as a gateway between end users and the internet.

For organizations and enterprise environments, a proxy server provides a number of benefits. It controls internet usage by blocking sites that are deemed to impact employees’ productivity. It also enhances privacy and improves the organization’s security through data encryption.

There are several ways to set proxy settings for apt command, so let’s jump right in.

Note: For demonstration, we will use Ubuntu 22.04.

## Configure Proxy Setting For APT Using A Proxy file

The easiest way to configure proxy settings for the APT command is by creating a proxy.conf file as shown.

$ sudo vi /etc/apt/apt.conf.d/proxy.conf

For a proxy server without a username and password, add the following entries as shown

For the HTTP Proxy, add the following entry:

Acquire::http::Proxy "http://proxy-IP-address:proxyport/";

Do the same for the HTTPS Proxy:

Acquire::https::Proxy "http://proxy-IP-address:proxyport/";

Example:

$ cat /etc/apt/apt.conf.d/proxy.conf Acquire::http::Proxy "http://192.168.56.102:3128/"; Acquire::https::Proxy "http://192.168.56.102:3128/";

If your Proxy server requires a username and password details, add them as follows

Acquire::http::Proxy "http://username:password@proxy-IP-address:proxyport"; Acquire::https::Proxy "http://username:password@proxy-IP-address:proxyport";

Example:

$ cat /etc/apt/apt.conf.d/proxy.conf Acquire::http::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/"; Acquire::https::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";

Once you are done, Save the changes and exit the configuration file. The Proxy settings will come into action the next time you run the APT package manager.

For example, you can update the local package index and then install net-tools package

$ sudo apt update $ sudo apt install net-tools -y

Verify the proxy server logs to confirm that [apt command](https://www.linuxtechi.com/apt-command-ubuntu-debian-linux/) is using proxy server for downloading packages. On the proxy server run,

# tail -f /var/log/squid/access.log | grep -i 192.168.56.240

Here ‘192.168.56.240’ is the IP address of our Ubuntu machine,

Perfect, output above confirms that apt command of our ubuntu system is downloading the packages via proxy server (192.168.56.102)

## An Alternative Way of Specifying Proxy Details

Apart from the first approach, you can specify the proxy details in a much simpler way. Once again, create a proxy.conf file as follows.

$ sudo vi /etc/apt/apt.conf.d/proxy.conf

For a Proxy server without a username and password, define it as shown.

Acquire { http::Proxy "http://proxy-IP-address:proxyport/"; https::Proxy "http://proxy-IP-address:proxyport/"; }

Sample example file would look like below,

$ sudo vi /etc/apt/apt.conf.d/proxy.conf

For a Proxy server with username and login details:

Acquire { http::Proxy "http://username:password@proxy-IP-address:proxyport/"; https::Proxy "http://username:password@proxy-IP-address:proxyport/"; }

Save the changes and exit the configuration file. Just a reminder that these settings take immediate effect once you start using the APT package manager.

#### Conclusion

This concludes this guide. In this tutorial, we have demonstrated how you can configure proxy settings for the APT package manager which is used in Debian/Ubuntu Linux distributions. That’s all for now. Keep it Linuxechi!

Also Read: [How to Install Go (Golang) on Ubuntu Linux Step-by-Step](https://www.linuxtechi.com/install-go-golang-on-ubuntu-linux/)