how to set proxy for apt
- User
how to set proxy for apt
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
how to set proxy for apt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To set a proxy for apt (Advanced Package Tool) on Debian-based Linux systems (like Ubuntu), you can configure it in multiple ways. Here are the common methods:
Set the proxy environment variables before running apt commands:
export http_proxy="http://proxy-server:port"
export https_proxy="http://proxy-server:port"
export ftp_proxy="http://proxy-server:port"
Replace proxy-server:port with your actual proxy address (e.g., http://192.168.1.1:8080).
Then run apt commands as usual:
sudo apt update
Edit the /etc/environment file to set system-wide proxy settings:
sudo nano /etc/environment
Add the following lines (adjust the proxy address):
http_proxy="http://proxy-server:port"
https_proxy="http://proxy-server:port"
ftp_proxy="http://proxy-server:port"
Save the file (Ctrl+O, then Ctrl+X in nano), and reload the environment:
source /etc/environment
Create or edit the APT configuration file:
sudo nano /etc/apt/apt.conf.d/proxy.conf
Add the following (replace with your proxy details):
Acquire::http::Proxy "http://proxy-server:port";
Acquire::https::Proxy "http://proxy-server:port";
Acquire::ftp::Proxy "http://proxy-server:port";
Save the file and test with:
sudo apt update
If your proxy requires a username and password, use this format:
Acquire::http::Proxy "http://username:password@proxy-server:port";
(Note: Storing passwords in plaintext is insecure. Use this only in trusted environments.)
Check if the proxy is working by running:
env | grep -i proxy
Or test with curl:
curl -I http://example.com
apt traffic.https:// in the URL if required.I want to set a socks proxy and it seems like the env var methods are not working at all
Setting a SOCKS proxy for apt is more complex than an HTTP proxy because apt does not natively support SOCKS proxies. However, you can work around this limitation using one of the following methods:
redsocks to Convert SOCKS to HTTP ProxySince apt only supports HTTP proxies, you can use redsocks to redirect traffic from an HTTP proxy to your SOCKS proxy.
redsockssudo apt update
sudo apt install redsocks
redsocksEdit the configuration file:
sudo nano /etc/redsocks.conf
Add/modify the following (replace 127.0.0.1:1080 with your SOCKS proxy):
redsocks {
local_ip = 127.0.0.1;
local_port = 12345; // Local HTTP proxy port
ip = 127.0.0.1;
port = 1080; // Your SOCKS proxy port
type = socks5;
}
Save (Ctrl+O, Enter, Ctrl+X).
redsockssudo systemctl restart redsocks
apt to Use the Local HTTP ProxyNow, set apt to use the local HTTP proxy (127.0.0.1:12345):
sudo nano /etc/apt/apt.conf.d/proxy.conf
Add:
Acquire::http::Proxy "http://127.0.0.1:12345";
Acquire::https::Proxy "http://127.0.0.1:12345";
Save and test:
sudo apt update
proxychains to Force apt Through SOCKSIf redsocks doesn't work, you can use proxychains to force apt to use a SOCKS proxy.
proxychainssudo apt install proxychains
proxychainsEdit the config file:
sudo nano /etc/proxychains.conf
lets focus on methods to config the apt instead
Since apt does not natively support SOCKS proxies, we need to use workarounds to make it work. Below are the best methods to configure apt to work with a SOCKS proxy, focusing on direct apt configuration rather than external tools like proxychains or redsocks.
apt-transport-socks (Best for SOCKS5)This method involves using a custom apt transport method that supports SOCKS proxies.
apt-transport-sockssudo apt update
sudo apt install apt-transport-socks
apt to Use SOCKS ProxyEdit or create a new apt configuration file:
sudo nano /etc/apt/apt.conf.d/99socks-proxy
Add the following (replace 127.0.0.1:1080 with your SOCKS proxy):
Acquire::socks::Proxy "socks5h://127.0.0.1:1080";
Acquire::http::Proxy "false";
Acquire::https::Proxy "false";
Acquire::ftp::Proxy "false";
socks5h means SOCKS5 with DNS resolution on the proxy side.http, https, and ftp proxies ensures apt uses only the SOCKS proxy.sudo apt update
If it works, apt will now route traffic through the SOCKS proxy.
apt with corkscrew (For SOCKS → HTTP Tunneling)If your SOCKS proxy requires tunneling through an HTTP proxy, you can use corkscrew.
corkscrewsudo apt install corkscrew
apt to Use corkscrewEdit /etc/apt/apt.conf.d/proxy.conf:
sudo nano /etc/apt/apt.conf.d/proxy.conf
Add:
Acquire::http::Proxy "http://127.0.0.1:8080";
Acquire::https::Proxy "http://127.0.0.1:8080";
Add your SOCKS proxy (e.g., socks5 127.0.0.1 1080).
apt Through proxychainssudo proxychains apt update
(Note: This may not work for all apt operations due to permission issues.)
tsocks (Alternative to proxychains)If proxychains fails, try tsocks:
sudo apt install tsocks
Edit /etc/tsocks.conf:
sudo nano /etc/tsocks.conf
Add:
server = 127.0.0.1
server_type = 5 # SOCKS5
server_port = 1080
Then run:
sudo tsocks apt update
http_proxy, https_proxy) Don’t Work for SOCKSapt only respects HTTP proxies, not SOCKS.socks_proxy, apt ignores it.redsocks, proxychains, or tsocks are needed to bridge SOCKS to HTTP.redsocks (most reliable for apt).proxychains (works for some cases but may fail with sudo).(Replace 127.0.0.1:8080 with your HTTP proxy if needed.)
corkscrew to Use SOCKSEdit /etc/corkscrew.conf (create if it doesn’t exist):
sudo nano /etc/corkscrew.conf
Add:
proxy 127.0.0.1:1080 # Your SOCKS proxy
Now, apt will tunnel through corkscrew, which forwards traffic to the SOCKS proxy.
apt with dante-client (For SOCKS4/SOCKS5)If you have a Dante SOCKS server, you can configure apt to use it.
dante-clientsudo apt install dante-client
socksify for aptEdit /etc/apt/apt.conf.d/socks.conf:
sudo nano /etc/apt/apt.conf.d/socks.conf
Add:
Acquire::http::Proxy "socks://127.0.0.1:1080";
Acquire::https::Proxy "socks://127.0.0.1:1080";
(Replace 127.0.0.1:1080 with your SOCKS proxy.)
aptsudo apt update
apt-transport-socks is the cleanest way to make apt use SOCKS directly.corkscrew is useful if you need to tunnel through an HTTP proxy first.dante-client is best if you have a Dante SOCKS server.apt fails, check logs:
sudo tail -f /var/log/apt/term.log
socks5h (for remote DNS) instead of socks5.apt-transport-socks (Method 1).corkscrew (Method 2) if you need HTTP tunneling.dante-client (Method 3).