2023-05-11 09:03:14 +08:00
|
|
|
|
[#]: subject: "How to Set Proxy Settings for APT Command"
|
|
|
|
|
[#]: via: "https://www.linuxtechi.com/set-proxy-settings-for-apt-command/"
|
|
|
|
|
[#]: author: "James Kiarie https://www.linuxtechi.com/author/james/"
|
|
|
|
|
[#]: collector: "lkxed"
|
|
|
|
|
[#]: translator: "geekpi"
|
2023-05-15 18:47:32 +08:00
|
|
|
|
[#]: reviewer: "wxy"
|
|
|
|
|
[#]: publisher: "wxy"
|
|
|
|
|
[#]: url: "https://linux.cn/article-15815-1.html"
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
如何为 APT 命令设置代理
|
|
|
|
|
======
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
![][0]
|
|
|
|
|
|
|
|
|
|
> 在本指南中,你将了解如何在 Ubuntu/Debian Linux 发行版中为 `apt` 命令设置代理。
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
代理服务器是位于请求资源的客户端系统或最终用户与资源本身之间的中间服务器。在大多数情况下,代理服务器充当最终用户和互联网之间的网关。
|
|
|
|
|
|
|
|
|
|
对于组织和企业环境,代理服务器提供了许多好处。它通过阻止被认为会影响员工工作效率的网站来控制互联网的使用。它还通过数据加密增强隐私并提高组织的安全性。
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
有几种方法可以为 `apt` 命令设置代理,让我们直接进入。
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
注意:为了演示,我们将使用 Ubuntu 22.04。
|
|
|
|
|
|
|
|
|
|
### 使用代理文件为 APT 配置代理
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
为 `apt` 命令配置代理的最简单方法是创建一个 `proxy.conf` 文件,如下:
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo vi /etc/apt/apt.conf.d/proxy.conf
|
|
|
|
|
```
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
对于没有用户名和密码的代理服务器,添加以下条目,如下:
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
对于 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/";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
如果你的代理服务器需要用户名和密码详细信息,请按以下方式添加:
|
|
|
|
|
|
|
|
|
|
```
|
2023-05-15 18:47:32 +08:00
|
|
|
|
Acquire::http::Proxy "http://username:password@proxy-IP-address:proxyport";
|
|
|
|
|
Acquire::https::Proxy "http://username:password@proxy-IP-address:proxyport";
|
2023-05-11 09:03:14 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
例子:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cat /etc/apt/apt.conf.d/proxy.conf
|
2023-05-15 18:47:32 +08:00
|
|
|
|
Acquire::http::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";
|
|
|
|
|
Acquire::https::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";
|
2023-05-11 09:03:14 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
完成后,保存更改并退出配置文件。代理设置将在你下次运行 APT 包管理器时生效。
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
例如,你可以更新本地包索引,然后安装 `net-tools` 包:
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo apt update
|
|
|
|
|
$ sudo apt install net-tools -y
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![][1]
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
验证代理服务器日志以确认 `apt` 命令正在使用代理服务器下载包。在代理服务器运行时:
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# tail -f /var/log/squid/access.log | grep -i 192.168.56.240
|
|
|
|
|
```
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
这里 `192.168.56.240` 是我们 Ubuntu 机器的 IP 地址。
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
![][2]
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
完美,上面的输出确认我们的 Ubuntu 系统的 `apt` 命令正在通过代理服务器(192.168.56.102)下载包。
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
### 另一种指定代理详细信息的方法
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
除了第一种方法,你还可以用更简单的方式指定代理详细信息。再次创建一个 `proxy.conf` 文件,如下所示。
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ 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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![][3]
|
|
|
|
|
|
|
|
|
|
对于具有用户名和登录详细信息的代理服务器:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Acquire {
|
2023-05-15 18:47:32 +08:00
|
|
|
|
http::Proxy "http://username:password@proxy-IP-address:proxyport/";
|
|
|
|
|
https::Proxy "http://username:password@proxy-IP-address:proxyport/";
|
2023-05-11 09:03:14 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
保存更改并退出配置文件。提醒一下,当你开始使用 APT 包管理器,这些设置就会立即生效。
|
|
|
|
|
|
|
|
|
|
##### 总结
|
|
|
|
|
|
2023-05-15 18:47:32 +08:00
|
|
|
|
本指南到此结束。在本教程中,我们演示了如何为 Debian/Ubuntu Linux 发行版中使用的 APT 包管理器配置代理设置。本文就到这里了。
|
|
|
|
|
|
|
|
|
|
(题图:MJ/dfb4d5a0-9150-47bd-9f54-c120ddd77046)
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.linuxtechi.com/set-proxy-settings-for-apt-command/
|
|
|
|
|
|
|
|
|
|
作者:[James Kiarie][a]
|
|
|
|
|
选题:[lkxed][b]
|
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2023-05-15 18:47:32 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2023-05-11 09:03:14 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://www.linuxtechi.com/author/james/
|
|
|
|
|
[b]: https://github.com/lkxed/
|
|
|
|
|
[1]: https://www.linuxtechi.com/wp-content/uploads/2023/04/Apt-Install-Package-Proxy-Settings-1024x403.png?ezimgfmt=ng:webp/ngcb22
|
|
|
|
|
[2]: https://www.linuxtechi.com/wp-content/uploads/2023/04/Grep-Proxy-Logs-Ubuntu-Machine-1024x326.png?ezimgfmt=ng:webp/ngcb22
|
2023-05-15 18:47:32 +08:00
|
|
|
|
[3]: https://www.linuxtechi.com/wp-content/uploads/2023/04/Proxy-conf-Apt-Command-Ubuntu.png
|
|
|
|
|
[0]: https://img.linux.net.cn/data/attachment/album/202305/15/184447hh8ac86hkycy6jio.jpg
|