On a Linux server or inside a container there is no system proxy dialog; there are three mechanisms, and which one you need depends on how well-behaved the software is. Environment variables (http_proxy, https_proxy, no_proxy) are honoured by curl, wget, pip, apt, git, most language HTTP libraries and Docker itself — and silently ignored by some tools. Per-process wrappers such as proxychains force a single program through a proxy at the library level. Transparent redirection with iptables and a helper like redsocks captures every outbound TCP connection from the host or container regardless of what the program thinks it is doing. This guide covers all three, the traps in each, and how to check the result.

Level 1: environment variables

The convention, older than Linux itself: if http_proxy / https_proxy are set, use them; skip hosts listed in no_proxy.

export http_proxy="http://user:pass@proxy.example.com:8080"
export https_proxy="http://user:pass@proxy.example.com:8080"
export no_proxy="localhost,127.0.0.1,::1,.internal,10.0.0.0/8"

curl https://api.ipify.org          # goes through the proxy
curl http://localhost:9000/health   # direct, thanks to no_proxy

Points that cost people hours:

  • Case. Set both lower-case and upper-case (HTTP_PROXY, HTTPS_PROXY, NO_PROXY); tools disagree about which they read. Note that https_proxy takes an http:// URL — it names the proxy for HTTPS targets, and the proxy itself is normally plain HTTP with CONNECT.
  • Special characters in the password must be percent-encoded.
  • Scope. The variables apply to processes started after they are set, in that shell. Services started by systemd do not see your shell; put them in the unit's Environment= or an EnvironmentFile. Cron jobs likewise have a bare environment.
  • sudo strips the environment by default; use sudo -E or configure env_keep.
  • Who ignores them. Go programs honour them (via net/http's ProxyFromEnvironment); Python's requests honours them; Node's built-in fetch and many Node HTTP clients do not unless a helper is used; Java needs -Dhttp.proxyHost JVM flags; databases and custom TCP protocols never look at them. If a program's traffic still shows your real IP after setting the variables, it is one of these — move to level 2 or 3.

Package managers have their own knobs

# apt
echo 'Acquire::http::Proxy "http://user:pass@proxy.example.com:8080";'  | sudo tee /etc/apt/apt.conf.d/95proxy
echo 'Acquire::https::Proxy "http://user:pass@proxy.example.com:8080";' | sudo tee -a /etc/apt/apt.conf.d/95proxy
# pip
pip config set global.proxy http://user:pass@proxy.example.com:8080
# git
git config --global http.proxy http://user:pass@proxy.example.com:8080
# npm
npm config set proxy http://user:pass@proxy.example.com:8080
npm config set https-proxy http://user:pass@proxy.example.com:8080

Level 1b: Docker specifically

Docker has two separate proxy scopes, and confusing them is the classic container mistake.

The daemon (pulling images)

Image pulls are done by the Docker daemon, a systemd service — it does not see your shell. Configure it in a drop-in:

sudo mkdir -p /etc/systemd/system/docker.service.d
cat <<'EOF' | sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://user:pass@proxy.example.com:8080"
Environment="HTTPS_PROXY=http://user:pass@proxy.example.com:8080"
Environment="NO_PROXY=localhost,127.0.0.1,registry.internal"
EOF
sudo systemctl daemon-reload && sudo systemctl restart docker

Containers (traffic from your applications)

Processes inside a container have their own environment. Either pass the variables at run time:

docker run -e HTTP_PROXY=http://user:pass@proxy.example.com:8080 \
           -e HTTPS_PROXY=http://user:pass@proxy.example.com:8080 \
           -e NO_PROXY=localhost,127.0.0.1 myimage

or set defaults for all containers you start from this client in ~/.docker/config.json:

{ "proxies": { "default": {
    "httpProxy":  "http://user:pass@proxy.example.com:8080",
    "httpsProxy": "http://user:pass@proxy.example.com:8080",
    "noProxy":    "localhost,127.0.0.1" } } }

The client injects these into every container's environment (and into builds). Inside the container the same caveats apply: the application must be one that honours the variables. In Compose, use the environment: block per service, and remember that services talking to each other by service name must be in NO_PROXY or every internal call will try to leave through the proxy and fail.

Builds

docker build gets the proxy from ~/.docker/config.json or from --build-arg HTTP_PROXY=…. Do not bake credentials into the image with ENV; they end up in the layer history for anyone who pulls it.

Level 2: per-process wrappers

For a program that ignores the environment, proxychains (the maintained fork is proxychains-ng, package proxychains4) preloads a shared library that intercepts the program's socket calls and routes them through a proxy chain you define:

sudo apt install proxychains4
# /etc/proxychains4.conf, at the bottom:
#   [ProxyList]
#   http  proxy.example.com 8080 user pass
#   or: socks5 proxy.example.com 1080 user pass
proxychains4 -q ./my-binary --that-ignores-env

Set proxy_dns in the config so hostname lookups also go through the proxy (the DNS-leak issue from How to test a proxy). Limits: it works only on dynamically linked binaries that use the standard socket library — statically linked Go programs and anything doing raw sockets slip past it. It is a tool for one process at a time, ideal for testing and for the odd misbehaving binary, not for a whole host.

Alternatives in the same class: tsocks, graftcp (uses ptrace, so it also catches static binaries, at a performance cost), and for containers running the process under one of these as the entrypoint.

Level 3: transparent redirection with iptables and redsocks

When you need everything from a host or container to exit through a proxy — including software you cannot configure — redirect at the kernel. iptables can rewrite outbound TCP connections to a local port; a small daemon listening there wraps each connection in the proxy protocol and forwards it. redsocks is the standard such daemon.

sudo apt install redsocks
# /etc/redsocks.conf
base { log_debug = off; log_info = on; daemon = on; redirector = iptables; }
redsocks {
    local_ip = 127.0.0.1; local_port = 12345;
    ip = PROXY_IP; port = 8080;
    type = http-connect;          # or socks5
    login = "user"; password = "pass";
}
sudo systemctl restart redsocks

# Redirect all outbound TCP except the proxy itself and local/private ranges
sudo iptables -t nat -N REDSOCKS
sudo iptables -t nat -A REDSOCKS -d PROXY_IP -j RETURN
sudo iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
sudo iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345
sudo iptables -t nat -A OUTPUT -p tcp -j REDSOCKS        # this host's own traffic
# for traffic from containers/other hosts routed through this box, use PREROUTING instead

Three things to know before you do this. The RETURN rule for the proxy's own IP is not optional — without it, redsocks' connection to the proxy is itself redirected back to redsocks, and the box loops. UDP is not redirected by this recipe: DNS will still go direct unless you also redirect port 53 to a resolver you trust or use redsocks' redudp section; and QUIC/HTTP/3 will simply not work through the proxy (see HTTP/3 through proxies), which is fine because clients fall back to TCP. Test the rules with a timeout (sudo timeout 60 iptables … is not a thing, but keeping a second SSH session open and being ready to iptables -t nat -F is) — a wrong rule can cut you off from a remote server.

The same pattern works for a container by running the iptables rules inside it (needs --cap-add=NET_ADMIN) or, more cleanly, on the host with the container's traffic routed through a dedicated network namespace. Kubernetes users will recognise this as exactly how service-mesh sidecars capture traffic.

Which level for which situation

SituationUseWhy
Scripts, CLI tools, package managers, well-behaved servicesEnvironment variables (+ tool-specific config)Simple, reversible, per-process scope
Docker image pullsDaemon drop-inThe daemon has its own environment
Applications inside containers~/.docker/config.json or -eInjected into the container's env
One binary that ignores the environmentproxychains / graftcpIntercepts at socket level; no host-wide change
Everything from a host or namespace, unconditionallyiptables + redsocksKernel-level; nothing can opt out
Different proxies for different destinationsPAC-like logic: env no_proxy, or iptables rules per destination rangeRoute by target

Verify, always

curl -s https://api.ipify.org          # honours env: should show the proxy IP
curl -s --noproxy '*' https://api.ipify.org   # bypasses env: shows the proxy IP ONLY if level 3 is in place
python3 -c "import urllib.request;print(urllib.request.urlopen('https://api.ipify.org').read())"
dig +short myip.opendns.com @resolver1.opendns.com   # which IP does DNS see you from?

The second line is the useful trick: with --noproxy '*' curl ignores the environment, so if it still reports the proxy's IP, transparent redirection is working; if it reports your real IP, only the environment level is active and non-compliant software will leak. Also check DNS separately — it is the most common leak on servers, since resolvers are configured system-wide and nothing in the three levels above redirects UDP unless you make it.

For choosing the proxy type behind all this — datacenter for most server work, ISP for a stable identity, residential where the target demands it — see the proxy types comparison; provider-side notes for Linux workloads are in our Linux proxy ranking.

Frequently asked questions

How do I set a proxy for the whole Linux server?

For compliant software, export http_proxy/https_proxy/no_proxy system-wide (e.g. /etc/environment) and configure services via systemd Environment=. For truly everything, use iptables redirection to redsocks.

How do I make Docker use a proxy?

Two places: the daemon (for pulls) via a systemd drop-in with HTTP_PROXY, and containers via ~/.docker/config.json proxies or -e variables. They are independent; set both if you need both.

Why does my program ignore http_proxy?

Not all software reads it — Node's fetch, Java, databases and custom protocols do not. Use a tool-specific setting, a wrapper like proxychains, or transparent redirection.

What is redsocks?

A daemon that accepts TCP connections redirected to it by iptables and forwards them through an HTTP CONNECT or SOCKS proxy, giving you a transparent proxy for any application without configuring the application.

Does transparent redirection handle DNS and UDP?

Not by the basic recipe — it redirects TCP. Redirect DNS explicitly (or point the resolver at a trusted server) and expect UDP-based protocols like QUIC to fall back to TCP.

Should containers use SOCKS5 or HTTP proxies?

HTTP with CONNECT covers HTTPS targets and is what environment variables expect; SOCKS5 is needed only for non-HTTP TCP traffic, and then via proxychains or redsocks in socks5 mode.