In this blog we will learn How to configure proxy in Linux Operating System using the command line method. We will go through different way of using proxy as system env variable as well as configuring it for their package managers.
Introduction
An intermediary server that stands in between a client computer and the internet is called a proxy server. It is typically utilized in internal networks to guard against intrusions and allow for unforeseen access. It is also utilized for bandwidth management, content banning and filtering, and internet access control. To access the internet, you must configure a proxy in your web browser or network proxy configuration if your home or workplace network is protected by a proxy server. Typically we see proxy usage in the corporates where they where the IT infra is running behind a protected network and to connect to the outside world we typically have their proxy servers.
Pre-requisite
- Any versions of Ubuntu or Debian OS installed on the PC/Desktop/Server Hardware.
- Any version of RHEL, CentOS Stream, Rocky , Alma Linux installed on the PC/Desktop/Server Hardware.
- User with sudo privileges
You may go through our previous blog post on How To Install Rocky Linux
Proxy for Specific session/User
You might not want to always utilize a proxy in some situations. If so, you can use the terminal to temporarily set up a proxy for the session and once you logout or switch to any other user the setting will lost. The syntax would look like below :
export http_proxy=username:passwordhttp://[proxy-server-ip-or-FQDN]:[Port-Number]
export http_proxy=http://[proxy-server-ip-or-FQDN]:[Port-Number]
Example , Use the below command for setting HTTP_PROXY
export HTTP_PROXY=username:password@http://proxy.example.com:8080
Use the below command for setting HTTPS_PROXY
export HTTPS_PROXY=username:password@http://proxy.example.com:8080
If the proxy server is not protected by any user credential you can simply use as below
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080
If required to set a permanent proxy for a specific User you can do it by editing the ~/bashrc for that user
nano ~/.bashrc
Add the below lines to the above file
export http_proxy=username:password@http://proxy.example.com:8080
export https_proxy=username:password@http://proxy.example.com:8080
Or Add the below if without any user credentials
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
Save and Exit the file. Activate the changes using the below command :
source ~/.bashrc
Proxy for All User
If required to set proxy for the whole system to be always connected to the Internet/outside world for all the user on the system you will need to use the file /etc/environment
nano /etc/environment
Add the below lines to the above file
export http_proxy=username:password@http://proxy.example.com:8080
export https_proxy=username:password@http://proxy.example.com:8080
or
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
Save and exit the file. Activate the changes
source /etc/environment
Proxy for Apt repo in Debian and Ubuntu
Installing the package from the Ubuntu/Debian repository will need you to set up a proxy for APT. Making a new configuration file at /etc/apt/apt.conf.d/ will allow you to accomplish this:
nano /etc/apt/apt.conf.d/proxy.conf
Add the below content to the file
Acquire::http::Proxy "username:password@http://proxy.example.com:8080/";
Acquire::https::Proxy "username:password@http://proxy.example.com:8080/";
Or
Acquire::http::Proxy "http://proxy.example.com:8080/";
Acquire::https::Proxy "http://proxy.example.com:8080/";
Save and exit the file. Now you can install the packages using the apt repository. Example,
sudo apt-get install <package_name> -y
Proxy for yum and dnf repo in RHEL and its derivatives
Installing the package from the RHEL based operating system repository will need you to set up a proxy for YUM or DNF.
Making a new configuration file at /etc/yum.conf will allow you to accomplish this:
echo "proxy=http://proxy.example.com:8080" >> /etc/yum.conf
This should work fine . If your proxy server is password protected add the below two lines to the file /etc/yum.conf
proxy_username=<Proxy-User-Name>
proxy_password=<Proxy-Password>
Save and exit the file. Test the proxy by installing any packages
sudo yum install -y <package_name>
Conclusion
In this tutorial we have successfully configured proxy for a linux system for a single session/user as well as for all the system user. Later we also learnt how we can configure the proxy for APT ( Debian & Ubuntu system) and YUM/DNF for RHEL based system to install packages via their package manager. You may also go through the wiki to learn more on Proxy Servers