APT 配置 Proxy
2026/4/19 3:45:46 网站建设 项目流程

方法 1:临时设置 APT 代理

如果只需要在某个终端会话中使用代理,可以临时配置代理。运行以下命令:

sudo apt-get -o Acquire::http::Proxy="http://proxy-server:port/" update

其中proxy-server是代理服务器的地址,port是代理服务器的端口号。如果代理需要认证,可以这样设置:

sudo apt-get -o Acquire::http::Proxy="http://username:password@proxy-server:port/" update

这个方法只是临时生效,执行完一次apt命令后不会保留代理配置。

方法 2:全局设置 APT 代理

要全局配置 APT 使用代理,可以将代理设置写入 APT 的配置文件。

  1. 编辑 APT 的配置文件/etc/apt/apt.conf。如果文件不存在,可以新建该文件:

    sudo nano /etc/apt/apt.conf
  2. 添加以下行:

    Acquire::http::Proxy "http://proxy-server:port/"; Acquire::https::Proxy "http://proxy-server:port/";

    如果代理需要认证,可以这样设置:

    Acquire::http::Proxy "http://username:password@proxy-server:port/"; Acquire::https::Proxy "http://username:password@proxy-server:port/";
  3. 保存并关闭文件。此后,所有的 APT 命令都会通过代理服务器进行。

方法 3:设置环境变量

你还可以通过设置环境变量来全局配置代理。

  1. 编辑/etc/environment文件,添加以下行:

    http_proxy="http://proxy-server:port/" https_proxy="http://proxy-server:port/"

    如果代理需要认证:

    http_proxy="http://username:password@proxy-server:port/" https_proxy="http://username:password@proxy-server:port/"
  2. 保存并关闭文件。

  3. 更新环境变量使其生效:

    source /etc/environment

这会在整个系统范围内设置代理,包括 APT 命令在内的所有 HTTP 和 HTTPS 请求都会通过这个代理。

方法 4:针对特定用户设置代理

如果只想针对当前用户设置代理,可以在用户的.bashrc.bash_profile文件中添加代理设置:

  1. 编辑用户的~/.bashrc~/.bash_profile文件:

    nano ~/.bashrc
  2. 添加以下行:

    export http_proxy="http://proxy-server:port/" export https_proxy="http://proxy-server:port/"

    如果代理需要认证:

    export http_proxy="http://username:password@proxy-server:port/" export https_proxy="http://username:password@proxy-server:port/"
  3. 保存并关闭文件,然后通过以下命令使其生效:

    source ~/.bashrc

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询