How to Set Proxy in Linux Terminal: The Ultimate 2025 Guide
In the world of server administration, development, and ethical hacking, configuring a proxy server is a fundamental skill. Whether you are securing traffic on an enterprise network, scraping data anonymously, or routing traffic through a secure tunnel, knowing how to manipulate environment variables in the Linux terminal is non-negotiable.
This guide provides a deep dive into configuring HTTP, HTTPS, and SOCKS proxies in Linux, covering temporary sessions, persistent settings, and package manager configurations.
---
Understanding Proxy Environment Variables
The Linux kernel and network stack do not inherently know about a "proxy." Instead, it is the individual applications (clients) that must be told to use one. To avoid configuring every single application separately, Linux relies on standard environment variables that compliant applications (like curl, wget, python, and git) automatically check.
Standard Variables
-
http_proxy: Routes HTTP traffic. -
https_proxy: Routes HTTPS traffic. -
all_proxy: Routes all traffic (if supported by the application). -
no_proxy: A comma-separated list of domains that should bypass the proxy.
> Note: Linux environment variables are case-insensitive in practice for most tools, but the lowercase standard (http_proxy) is widely preferred for consistency, though uppercase (HTTP_PROXY) is often supported for legacy reasons.
---
Part 1: Temporary Proxy Settings (Session-Specific)
This is the most common method for troubleshooting or short-term tasks. These settings will reset as soon as you close the terminal window.
1. Basic Setup
Open your terminal and use the export command.
Syntax
export http_proxy="http://proxy-ip:port" export https_proxy="http://proxy-ip:port"
Example for a local proxy (e.g., Squid or Burp Suite)
export http_proxy="http://127.0.0.1:8080" export https_proxy="http://127.0.0.1:8080"
2. Proxy with Authentication
If your corporate proxy or scraping service requires a username and password:
export http_proxy="http://username:password@proxy-ip:port"
export https_proxy="http://username:password@proxy-ip:port"
Security Warning: While valid, typing credentials in plain text in the terminal saves them to your shell history (.bash_history). To avoid this, prepend the command with a space (if your shell is configured to ignore space-prefixed commands) or clear your history immediately after.
3. Excluding Local Addresses (No Proxy)
You almost always want to bypass the proxy for localhost and internal network ranges to avoid unnecessary latency.
export no_proxy="localhost,127.0.0.1,10.0.0.0/8,192.168.0.0/16"
---
Part 2: Permanent Proxy Settings
To survive a reboot, these variables must be written into a configuration file.
Option A: User-Wide Settings (Recommended)
This applies the proxy only to the specific user. Edit your shell's profile file. For most users on Ubuntu/Debian, this is ~/.bashrc or ~/.profile. For Zsh users, it is ~/.zshrc.
1. Open the file in a text editor:
nano ~/.bashrc
2. Scroll to the bottom and add the lines:
# Permanent Proxy Configuration
export http_proxy="http://10.0.0.1:3128" export https_proxy="http://10.0.0.1:3128" export no_proxy="localhost,127.0.0.1"
3. Save and apply changes immediately:
source ~/.bashrc
Option B: System-Wide Settings
This sets the proxy for all users, including those created in the future. This requires root privileges.
1. Edit /etc/environment:
sudo nano /etc/environment
2. Add the variables in strict syntax (do not use the export keyword here):
http_proxy="http://10.0.0.1:3128"
https_proxy="http://10.0.0.1:3128" no_proxy="localhost,127.0.0.1"
3. Reboot or log out and back in.
---
Part 3: Configuring Package Managers (APT & YUM)
Even if terminal variables are set, package managers often ignore them due to security contexts or require explicit configuration files.
Debian/Ubuntu (APT)
APT will not always use the environment variables effectively. It is best practice to create a dedicated proxy configuration file.
1. Create a proxy file in apt.conf.d:
sudo nano /etc/apt/apt.conf.d/proxy.conf
2. Insert the following:
Acquire::http::Proxy "http://10.0.0.1:3128";
Acquire::https::Proxy "http://10.0.0.1:3128";
3. Save the file. No restart is needed; it will apply on the next apt update.
RHEL/CentOS/Fedora (YUM/DNF)
For older systems using yum, edit the main configuration file or create a conf file in /etc/yum.conf.d/.
1. Edit the main config or a sub-directory file:
sudo nano /etc/yum.conf
# OR sudo nano /etc/dnf/dnf.conf
2. Add the following line in the [main] section:
proxy=http://10.0.0.1:3128
proxy_username=your_username proxy_password=your_password
---
Part 4: SOCKS Proxies (SSH & Privacy)
HTTP proxies handle web traffic. SOCKS proxies (usually SOCKS5) handle *all* TCP traffic, making them ideal for SSH tunneling and secure browsing.
Setting up a SOCKS Proxy via SSH
This is a standard developer workflow to tunnel traffic securely.
-D creates a dynamic application-level port forwarding
-f sends ssh to background
-N means no remote commands
ssh -D 1080 -f -C -q user@remote_server
Now, tell your terminal to use this SOCKS proxy:
export all_proxy="socks5://127.0.0.1:1080"
Configuring curl for SOCKS
Sometimes curl requires specific flags if the environment variable is not set correctly:
curl --socks5 127.0.0.1:1080 https://api.ipify.org
---
Part 5: Python and Scripting Automation
When writing scripts to scrape data or interact with APIs, you should not rely on global shell variables. Instead, define proxies within the code for portability.
Python Requests Example
The requests library is the industry standard. You can pass the proxies dictionary to the get or post method.
import requests
proxies = { 'http': 'http://10.0.0.1:3128', 'https': 'http://10.0.0.1:3128', }
try: response = requests.get('https://api.ipify.org?format=json', proxies=proxies) print(f"Public IP: {response.json()['ip']}") except requests.exceptions.ProxyError as e: print(f"Proxy connection failed: {e}")
Environment Variables in Python
You can also dynamically set them within the script using os.environ:
import os
os.environ['HTTP_PROXY'] = "http://user:pass@10.0.0.1:3128" os.environ['HTTPS_PROXY'] = "http://user:pass@10.0.0.1:3128"
Subsequent requests will use these settings automatically
---
Comparison: Methods vs. Scope
| Method | Scope | Persistence | Best Use Case | | :--- | :--- | :--- | :--- | | export | Current Shell | Temporary (Session) | Testing, quick tasks, debugging. | | ~/.bashrc | Single User | Permanent | Personal developer workstations. | | /etc/environment | System-Wide | Permanent | Enterprise servers, all users. | | apt.conf | APT Only | Permanent | System updates on Debian/Ubuntu. | | yum.conf | YUM/DNF Only | Permanent | System updates on RHEL/CentOS. |
---
Troubleshooting & Verification
How do you know it worked?
1. Verification via curl
curl -I https://www.google.com
2. Check your Public IP
This confirms traffic is actually routing through the proxy.
curl https://api.ipify.org
If the returned IP matches your proxy server's IP, the configuration is correct. If it matches your local machine's IP, the proxy is not active.
3. The echo Check
Simply verify the variable is set:
echo $http_proxy
Conclusion
Configuring a proxy in the Linux terminal ranges from a simple one-line export command to editing system configuration files for persistence. For modern DevOps and scraping workflows, relying on environment variables (http_proxy, https_proxy) provides the best compatibility. Always ensure you configure the no_proxy variable to keep local traffic fast and unrestricted, and remember that package managers (APT/YUM) often require their own specific configuration directives to function correctly behind a corporate firewall.