Linux Command Line Proxy Configuration

Linux Command Line Proxy Configuration

In Linux, many command line tools (such as <span>curl</span>, <span>wget</span>, <span>git</span>, <span>pip</span>, etc.) require network access. When in a restricted network environment, it is often necessary to configure a HTTP/HTTPS/SOCKS5 proxy. This article will systematically introduce how to set up proxies for common tools.

1. Environment Variable Proxy

The most common proxy method in Linux is by setting environment variables:

# HTTP Proxy
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080

# SOCKS5 Proxy
export all_proxy=socks5://127.0.0.1:1080

⚠️ Note:

  • <span>http_proxy</span> and <span>https_proxy</span> are mainly used for tools that support the HTTP(S) protocol;
  • <span>all_proxy</span> allows most programs (like curl, wget) to forward through SOCKS5.

To make the proxy settings persist, you can write them into <span>~/.bashrc</span> or <span>~/.zshrc</span>:

echo 'export http_proxy=http://127.0.0.1:8080' &gt;&gt; ~/.bashrc
echo 'export https_proxy=http://127.0.0.1:8080' &gt;&gt; ~/.bashrc
echo 'export all_proxy=socks5://127.0.0.1:1080' &gt;&gt; ~/.bashrc
source ~/.bashrc

2. Individual Tool Proxy Settings

Not all tools read environment variables by default; some require separate configuration.

2.1 <span>curl</span>

<span>curl</span> supports environment variable proxies by default, but can also specify parameters:

# HTTP Proxy
curl -x http://127.0.0.1:8080 http://example.com

# SOCKS5 Proxy
curl -x socks5://127.0.0.1:1080 http://example.com

2.2 <span>wget</span>

<span>wget</span> also supports environment variables and can specify in the configuration file:

# Temporary setting
wget -e use_proxy=yes -e http_proxy=http://127.0.0.1:8080 http://example.com

# Permanent setting (write to ~/.wgetrc)
echo "http_proxy = http://127.0.0.1:8080" &gt;&gt; ~/.wgetrc
echo "https_proxy = http://127.0.0.1:8080" &gt;&gt; ~/.wgetrc
echo "use_proxy = on" &gt;&gt; ~/.wgetrc

2.3 <span>apt</span> (Ubuntu/Debian)

APT does not read the <span>http_proxy</span> environment variable and needs separate configuration:

sudo tee /etc/apt/apt.conf.d/95proxies &lt;&lt;EOF
Acquire::http::Proxy "http://127.0.0.1:8080";
Acquire::https::Proxy "http://127.0.0.1:8080";
EOF

Verification:

apt-get update

2.4 <span>pip</span> (Python Package Manager)

# Temporary proxy
pip install requests --proxy=http://127.0.0.1:8080

# Permanent configuration (write to ~/.pip/pip.conf)
mkdir -p ~/.pip
tee ~/.pip/pip.conf &lt;&lt;EOF
[global]
proxy = http://127.0.0.1:8080
EOF

2.5 <span>git</span>

# Set global proxy
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

# Unset proxy
git config --global --unset http.proxy
git config --global --unset https.proxy

# Only use proxy for GitHub
git config --global http.https://github.com.proxy http://127.0.0.1:8080

2.6 <span>docker</span>

Docker’s network access needs to be configured in <span>/etc/systemd/system/docker.service.d/proxy.conf</span>:

sudo mkdir -p /etc/systemd/system/docker.service.d

sudo tee /etc/systemd/system/docker.service.d/proxy.conf &lt;&lt;EOF
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:8080"
Environment="HTTPS_PROXY=http://127.0.0.1:8080"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF

Then restart Docker:

sudo systemctl daemon-reexec
sudo systemctl restart docker

2.7 <span>npm</span> (Node.js)

# Set proxy
npm config set proxy http://127.0.0.1:8080
npm config set https-proxy http://127.0.0.1:8080

# View proxy settings
npm config get proxy

# Unset proxy
npm config delete proxy
npm config delete https-proxy

3. Proxy Exclusion (NO_PROXY)

Sometimes we only want to proxy external networks while allowing local or internal networks to connect directly. You can set the <span>NO_PROXY</span> variable:

export NO_PROXY="localhost,127.0.0.1,.example.com"
  • <span>localhost,127.0.0.1</span>: Do not proxy local requests
  • <span>.example.com</span>: Do not proxy <span>example.com</span> and its subdomains

4. Summary

  • Environment Variable Proxy: Suitable for most tools (curl, wget);
  • Independent Proxy Configuration: Some tools (apt, pip, docker, npm, git) require separate settings;
  • NO_PROXY: Can flexibly exclude internal domain names and IPs;
  • Persistent Configuration: It is recommended to write to configuration files rather than just set them temporarily in the terminal.

By properly configuring the proxy, you can ensure that all command line tools in the Linux system can access external resources normally in a restricted environment.

#LinuxCommandLine #ProxyConfiguration

Leave a Comment