Set Proxy for APT on Ubuntu/Debian

PowerADM.com / Linux / Debian / Set Proxy for APT on Ubuntu/Debian

Proxy servers (such as the Squid proxy) are often used in corporate networks to provide access to the Internet for internal devices. In order for your internal Ubuntu/Debian hosts to be able to install and update deb packages from external repositories, you need to configure the proxy settings for the APT (Advanced Packaging Tool) package manager. This article describes how to configure proxy connection settings for APT.

You can specify global proxy settings for APT in the /etc/apt/apt.conf file.

$ sudo touch /etc/apt/apt.conf
$ sudo mcedit /etc/apt/apt.conf

Add the following configuration lines here:

Acquire::http::proxy "http://192.168.0.14:3128/";
Acquire::https::proxy "http://192.168.0.14:3128/";
Acquire::ftp::proxy "ftp://192.168.0.14:3128/";
Acquire::::Proxy "true";

use proxy for apt on ubuntu and debian

  • 192.168.0.14 – your proxy server IP or DNS name
  • 3128 – proxy server port

You can set a username and password if authentication is required to connect to a proxy server:

Acquire::http::Proxy "http://user1:password@192.168.0.14:3128";
Acquire::https::Proxy "http://user1:password@192.168.0.14:3128";
Acquire::ftp::Proxy "ftp://user1:password@192.168.0.14:3128";

The next time you use apt or apt-update, a proxy server will be used to connect to the repositories.

If you have deployed your own local deb repositories, you can set apt to access them directly, rather than through a proxy. Add the following exclusion list to your apt.conf file:

Acquire::http::Proxy {
    localrepo1.poweradm.com DIRECT;
    elastic.poweradm.com DIRECT;
};

To check whether a proxy connection was used to install or update an Ubuntu package using apt, run the command:

$ cat /var/log/apt/history.log

The automatic keyword indicates that a proxy server was used to download these packages.

apt/history.log

The above proxy settings for APT are global and apply to all users. You can use environment variables if you only want to use the APT proxy for the current user.

To do this, add the following lines to the end of the user’s ~/.profile:

export https_proxy=http://User:Password@192.168.0.14:3128/
export http_proxy=http://User:Password@192.168.0.14:3128/
export ftp_proxy=http://User:Password@192.168.0.14:3128/

If you have specified a proxy server that is not available, you will see the following apt errors:

W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease Could not connect to 192.168.0.14:3128 (192.168.0.14). - connect (111: Connection refused)
W: Some index files failed to download. They have been ignored, or old ones used instead.

If the credentials (username and password) used to connect to the proxy server are not correct, an error message will be displayed:

407 Proxy Authentication Required

Leave a Reply

Your email address will not be published. Required fields are marked *