方法 1临时设置 APT 代理如果只需要在某个终端会话中使用代理可以临时配置代理。运行以下命令sudo apt-get -o Acquire::http::Proxyhttp://proxy-server:port/ update其中proxy-server是代理服务器的地址port是代理服务器的端口号。如果代理需要认证可以这样设置sudo apt-get -o Acquire::http::Proxyhttp://username:passwordproxy-server:port/ update这个方法只是临时生效执行完一次apt命令后不会保留代理配置。方法 2全局设置 APT 代理要全局配置 APT 使用代理可以将代理设置写入 APT 的配置文件。编辑 APT 的配置文件/etc/apt/apt.conf。如果文件不存在可以新建该文件sudo nano /etc/apt/apt.conf添加以下行Acquire::http::Proxy http://proxy-server:port/; Acquire::https::Proxy http://proxy-server:port/;如果代理需要认证可以这样设置Acquire::http::Proxy http://username:passwordproxy-server:port/; Acquire::https::Proxy http://username:passwordproxy-server:port/;保存并关闭文件。此后所有的 APT 命令都会通过代理服务器进行。方法 3设置环境变量你还可以通过设置环境变量来全局配置代理。编辑/etc/environment文件添加以下行http_proxyhttp://proxy-server:port/ https_proxyhttp://proxy-server:port/如果代理需要认证http_proxyhttp://username:passwordproxy-server:port/ https_proxyhttp://username:passwordproxy-server:port/保存并关闭文件。更新环境变量使其生效source /etc/environment这会在整个系统范围内设置代理包括 APT 命令在内的所有 HTTP 和 HTTPS 请求都会通过这个代理。方法 4针对特定用户设置代理如果只想针对当前用户设置代理可以在用户的.bashrc或.bash_profile文件中添加代理设置编辑用户的~/.bashrc或~/.bash_profile文件nano ~/.bashrc添加以下行export http_proxyhttp://proxy-server:port/ export https_proxyhttp://proxy-server:port/如果代理需要认证export http_proxyhttp://username:passwordproxy-server:port/ export https_proxyhttp://username:passwordproxy-server:port/保存并关闭文件然后通过以下命令使其生效source ~/.bashrc