Configuring a proxy server in Linux allows terminal applications to access network resources through an intermediary. This setup is useful when working within restricted networks or when you need to conceal your IP address. It also provides the benefit of routing traffic securely through HTTP and HTTPS proxies.
In Linux, you can configure terminal applications to use a proxy server by setting environment variables. This method is efficient as it doesn't require configuring each application individually. Tools like curl, wget, and package managers can use these proxy settings directly from the environment variables.
To ensure consistent network routing, it's important to set these variables correctly. You can configure the proxy for a single session or make the settings persistent across all sessions. Understanding the syntax and correct implementation will help manage your network connections effectively.
Steps to configure HTTP and HTTPS proxy on Linux:
- Set up a proxy server if you don't already have one.
Related: How to create an SSH SOCKS proxy
Related: How to create SOCKS proxy on Windows - Open the terminal on your Linux system.
- Check if the terminal is currently using any HTTP or HTTPS proxy settings (optional).
$ curl https://ipinfo.io { "ip": "132.197.229.20", "city": "Boston", "region": "Massachusetts", "country": "US", "loc": "42.3584,-71.0598", "org": "AS6984 Verizon Data Services LLC", "postal": "02108", "timezone": "America/New_York", "readme": "https://ipinfo.io/missingauth" }
Related: How to install cURL on Ubuntu
For connection to the Internet, the output should display the gateway's IP address details.
Example of supported tools are curl, wget, ftp, ssh, yum and apt.
- Verify if any HTTP or HTTPS proxy is currently set for the current terminal session.
$ echo $http_proxy $https_proxy $HTTP_PROXY $HTTPS_PROXY
No proxy if set if the command returns empty output
http_proxy and https_proxy are the standard though HTTP_PROXY and HTTPS_PROXY are also commonly used. There's also ftp_proxy environment variable that you can use to route your FTP traffic through a proxy.
- If no proxy is set, configure the http_proxy and https_proxy environment variables with the appropriate details.
$ export https_proxy=username:password@https://192.168.1.111:8443
Format as the followings:
<USERNAME>:<PASSWORD>@<TYPE>://<IP/HOSTNAME>:<PORT>
Omit username and password if not required, like so:
$ export https_proxy=https://192.168.1.111:8443
Some supported proxy server protocols are http, https, socks4 and socks5
Configure http_proxy environment variable to route HTTP traffic trough a proxy and configure https_proxy for HTTPS traffic.
- Configure the environment variable for every new terminal session for the current user (optional).
$ echo 'export https_proxy="https://192.168.1.111"' >> ~/.bashrc
~/.bashrc is a script executed on every new Bash terminal session. ~/.zshrc is the correshponding file for Zsh.
- Configure the environment variable for every new terminal session for all system users (optional).
$ echo 'export https_proxy="https://192.168.1.111"' | sudo tee -a /etc/profile
- Confirm the environment variables are correctly set.
$ echo $https_proxy $http_proxy https://192.168.1.111:8443
Launch new terminal if you were adding the proxy setting in any of the configuration files for the change to take effect.
- Test the connection to ensure the terminal applications are using the proxy server.
$ curl https://ipinfo.io { "ip": "52.36.74.55", "hostname": "ec2-52-36-74-55.us-west-2.compute.amazonaws.com", "city": "Boardman", "region": "Oregon", "country": "US", "loc": "45.8399,-119.7006", "org": "AS16509 Amazon.com, Inc.", "postal": "97818", "timezone": "America/Los_Angeles", "readme": "https://ipinfo.io/missingauth" }
The output should show the IP address of the proxy server.
- Remove the proxy settings when they are no longer needed by unsetting the environment variables.
$ unset http_proxy https_proxy
This removes the proxy settings from the current session. Remove the environment variables added to the shell's configuration file (.bashrc or .zshrc if configured.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.