Skip to main content

General Documents

Configure Proxy on Linux Server

A proxy server acts as an interface for clients seeking resources on the internet or an external network. This provides additional security to the host machine by masking the host details from the external networks.

This article describes how to configure a proxy server on Linux machines.

Before you Start

Ensure that you have:

  • Access to a command line or terminal window

  • Sudo or root privileges

Run the following command to verify if the proxy is already configured on the server:

printenv | grep proxy

Steps

Configure Proxy on Linux command line or Terminal

In a Linux Like operating system, the http_proxy, ftp_proxy, and https_proxy environment variables are used to specify proxy settings through which you can access the internet via proxy on a Linux console. All of these environment variables can be used with client programs, such as elinks, wget, lynx, rsync, and more.

The following list describes various ways of configuring a proxy for a specific session:

  • To access HTTP-based sites via proxy, run the following command:

    export http_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]

    For example, export http_proxy=http://proxy.cyware.com:8080.

  • To protect the proxy settings using a username and password combination, run the following command:

    export http_proxy=http://USERNAME:PASSWORD@[proxy-server-ip-or-dns-name]:PORT

    For example, export http_proxy=http://john:password@proxy.cyware.com:8080.

  • To access HTTPS-based secure sites via proxy, run the following command:

    export https_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]

    For example, export https_proxy=http://proxy.cyware.com:8080.

  • To access FTP-based sites via proxy, run the following command:

    export ftp_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]

    For example, export ftp_proxy=http://proxy.cyware.com:8080.

Configure Proxy Permanently

To permanently configure a proxy on a Linux server, you must update the environment variables in the bash.bashrc file.

To update the bash.bashrc file with the environment variables, do the following:

  1. To open the bash.bashrc file, run the following command:

    vi /etc/bash.bashrc
  2. Update the file with the following environment variables:

    export http_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]
    export ftp_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]
    export https_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]
  3. Save and exit.

Configure Proxy in CentOS or RHEL 7 Permanently

You can configure proxy in CentOS or RHEL 7 for the processes with and without a shell.

For Processes without Shell

To configure proxy for the process without a shell, you must define the environment variables in the /etc/environment file. To update the environment variables, run the following command:

echo "http_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]/"> /etc/environment

For Processes with Shell

For bash and sh users, add the environment variable to a new http_proxy.sh file in the /etc/profile.d/ directory. To update the environment variables, run the following command:

echo "export http_proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number]/" > /etc/profile.d/http_proxy.sh

For csh and tcsh users, add the environment variable to a new http_proxy.csh file in the /etc/profile.d/ directory. To update the environment variables, run the following command:

echo "setenv http_proxy http://[proxy-server-ip-or-dns-name]:[Port-Number]/" > /etc/profile.d/http_proxy.csh

Note

The extension of these files determines which type of shell will read them. The commands are not interchangeable.

Configure Proxy for Yum

To configure yum behind a proxy, you must update the yum.conf file with the environment variables. To update the yum.conf file, do the following:

  1. To open the yum.conf file, run the following command:

    vi /etc/yum.conf
  2. Update the file with the following details:

    proxy=http://[proxy-server-ip-or-dns-name]:[Port-Number] 
    proxy_username=[USERNAME] 
    proxy_password=[PASSWORD]
  3. Save and exit.

Configure wget to Download Files via Proxy

The wget program allows you to download files from web URLs. You must update the wget configuration files to download files via proxy. Following are the wget configuration files as per their priorities:

  • ~/.wgetrc: User startup file.

  • /etc/wgetrc: Default location of the global startup file.

  • Set proxy variables in the shell for the current pseudo-terminal.

  • ~/.bash_profile: User-specific environment.

  • /etc/profile: System-wide environment.

Note

If a higher priority configuration is not set, then the next priority configuration is used.

Update the configuration files with the following details:

  • For ~/.wgetrc and /etc/wgetrc:

    http_proxy = http://[Proxy_Server]:[port]
    https_proxy = http://[Proxy_Server]:[port]
    ftp_proxy = http://[Proxy_Server]:[port]
  • For a shell:

    $ export http_proxy=http://[Proxy_Server]:[port]
    $ export https_proxy=$http_proxy
    $ export ftp_proxy=$http_proxy
  • For ~/.bash_profile and /etc/profile:

    # export http_proxy=http://[Proxy_Server]:[port]
    # export https_proxy=http://[Proxy_Server]:[port]
    # export ftp_proxy=http://[Proxy_Server]:[port]