How to Configure Proxy in Debian/CentOS/RHEL/Fedora
What is a Proxy Server
A proxy server is a server that acts as an intermediary for requests from clients seeking resources on the internet or an external network. Think of it as a go-between who makes requests on behalf of the client, ensuring that anyone outside of your network does not know the details of the requesting host.
Like every Linux distribution, proxy settings can be set using environment variables. There are a number of variables available to use, ranging from HTTP traffic to FTP traffic.
Proxy settings can be either persistent by setting them in your profile, or non-persistent by setting them from the shell session.
Variable | Description |
---|---|
http_proxy | Proxy server for HTTP traffic |
https_proxy | Proxy server for HTTPS traffic |
ftp_proxy | Proxy server for FTP traffic |
no_proxy | Patterns for IP addresses or domain names that shouldn’t use the proxy |
Verify if proxy is set on the server
This command will show if there is a proxy server configured on the system:
~] echo $http_proxy
Setting Proxy for Command line programs
The http_proxy environment variable is used to specify proxy settings to client programs such as curl and wget . Below are the various examples of using proxy for commandline programs:
** 1. No username and password required: **
To configure proxy without username and password:
~] export http_proxy=http://SERVER:PORT/
2. Username and password authentication:
To configure the proxy server with username and password authentication:
~] export http_proxy=http://USERNAME:PASSWORD@SERVER:PORT/
3. Domain, username and password required:
To configure proxy with username/password authentication along with the Domain name:
~] export http_proxy=http://DOMAIN\\USERNAME:PASSWORD@SERVER:PORT/
Special character handling
Literal backslash characters \
need to be doubled escape them as shown below.
~] export http_proxy=http://DOMAIN\\USERNAME:PASSWORD@SERVER:PORT/
When the username or password uses the @
symbol, add a backslash \
before the @
for example:
~] export http_proxy=http://DOMAIN\\USERN\@ME:PASSWORD@SERVER:PORT
or
~] export http_proxy=http://DOMAIN\\USERNAME:P\@SSWORD@SERVER:PORT
Configuring Proxy in Debian/CentOS/RHEL permanently (for processes without shell)
Define the environment variables in /etc/environment file if you want to add a permanent proxy
~] echo "http_proxy=http://proxy.example.com:3128/" > /etc/environment
Configuring proxy for processes with SHELL
For bash and sh users, add the export line given above into a new file called /etc/profile.d/http_proxy.sh file:
~] echo "export http_proxy=http://proxy.example.com:3128/" > /etc/profile.d/http_proxy.sh
For csh and tcsh users, use the following command to set the http_proxy variable in a new file called /etc/profile.d/http_proxy.csh file:
~] echo "setenv http_proxy http://proxy.example.com:3128/" > /etc/profile.d/http_proxy.csh
/etc/environment file vs /etc/profile file
/etc/environment is not part of POSIX , it belongs to PAM (Pluggable Authentication Module) , and only programs compiled with PAM support are able to use it (primarily login systems, which subsequently start the shell or user environment). This means it isn't even read by your shell. The file only accepts variable=value.
You can see the programs using /etc/environment with
~] grep -l pam_env /etc/pam.d/*
So /etc/environment is used for setting variables for programs which are usually not started from a shell.
Setting proxu for other programs
Yum
To configure yum behind proxy, modify the /etc/yum.conf with the following:
proxy=http://proxy.example.com:3128
proxy_username=yum-user
proxy_password=qwerty