apt-get在代理后面不起作用

我的公司使用HTTP代理,因此我们需要正确配置各种Ubuntu 12.04服务器,即将\etc\apt\apt.conf.d\80proxy为:

 Acquire::http::Proxy "http://proxy.mycompany.com:80"; Acquire::http::No-Cache true; 

现在,几天后,这种方法突然停止工作:我遇到了总和不匹配错误。 我已经尝试了在stackoverflow或网络上发现的所有常用技巧 ,其中包括:

 sudo rm -fR /var/lib/apt/lists/* 
 sudo apt-get clean 

但似乎没有任何效果。 我甚至没有运气就切换到了FTP服务器。 什么是这个问题的根本解决方案? 代理服务器可能存在某种问题吗? 会是什么呢?

使用Ubuntu 12.04

要通过代理使用apt-get,我会执行以下操作 – 您确实需要能够访问互联网(例如通过Firefox等浏览器):

 sudo apt-get --print-uris install PROGRAM 

这将打印执行安装所需的包的URL(以及md5sums等其他信息),以便您可以下载它们。 例如,使用supertux

 wilf@comp:~$ sudo apt-get install --print-uris supertux Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: supertux-data The following NEW packages will be installed supertux supertux-data 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 59.4 MB of archives. After this operation, 80.0 MB of additional disk space will be used. Do you want to continue [Y/n]? Y 'http://gb.archive.ubuntu.com/ubuntu/pool/universe/s/supertux/supertux-data_0.3.3-6_all.deb' supertux-data_0.3.3-6_all.deb 58590640 MD5Sum:68bd36f2c262f7caed1b5c947977202a 'http://gb.archive.ubuntu.com/ubuntu/pool/universe/s/supertux/supertux_0.3.3-6_i386.deb' supertux_0.3.3-6_i386.deb 804782 MD5Sum:a49c6c3c918bae2c968b3da6ac725b06 

然后从给定链接(最好是空文件夹),通过代理等工作的浏览器下载.deb文件,然后使用软件中心安装它们; 或者在终端中使用cd /FOLDER/WITH/DOWNLOADED-DEB-FILES和其中一个命令

 dpkg -i *.deb gdebi *.deb 

这有点慢和恼人,但似乎在HTTP代理上工作。 您也可以从http://packages.ubuntu.com/获取包裹

我发现以下对我有用,就单独使用终端而言:

  1. /etc/apt/apt.conf保留为空,以便apt回退到$*_proxy环境变量。
  2. 确保正确设置了环境变量:例如,您可以添加.bashrc:

     http_proxy="http://username:password@proxyserver:port" # And so on for other proxy settings like https_proxy and ftp_proxy 

    如果您的用户名或密码包含任何特殊字符,则可能需要对其进行URL编码 。

  3. 让sudo使用你的环境变量,而不是它自己的。 这是通过编辑/etc/sudoers文件来完成的。 这样做时要小心! 仅使用sudo visudo命令编辑文件; 任何错误都可能让您无法重新进入sudo模式! 添加以下内容:

     Defaults env_keep+="http_proxy https_proxy ftp_proxy socks_proxy" 

    这确保sudo在执行sudo apt-get install ...时保留这些变量sudo apt-get install ...等。

我从Ubuntu的apt-get howto中找到了这个。

请告诉我它是否有效:)

要添加到Wilf,我运行以下命令以通过Chrome自动下载。 我理解的Firefox可以更简单。

 yes | sudo apt-get --print-uris install PROGRAM-NAME-HERE | grep http | awk '{print $1 }' | tr -d \' | while read -r line; do google-chrome "$line"; done 

编辑:所以整个答案将在一个post,一旦下载完成简单运行

 cd /FOLDER/WITH/DOWNLOADED-DEB-FILES; dpkg -i *.deb 

我不知道你是否尝试过这个但是怎么样:

 export http_proxy=proxy.mycompany.com:80 

您可以在以后查看:

 echo $http_proxy 

这里的一个陷阱是,如果您将代理设置为未经授权的用户后运行

 sudo apt-get install REQUIRED_PACKAGE 

其中REQUIRED_PACKAGE是你想安装的软件,你仍然看到它在你的代理机器之外联系机器时它会挂起,那么你可能会将-E传递给sudo命令,如下所示……

 sudo -E apt-get install REQUIRED_PACKAGE 

这具有使用当前环境以root身份运行命令的效果(代理环境设置)