Cyberithub

How to Use Proxy Server to Update Packages from YUM Repository in Linux

In this article, I will show you how to use proxy server to update packages from YUM Repository in Linux. Many times we face a situation where Linux Server currently residing in a private network which does not have any access to public network or Internet needs to be updated from YUM Repository on a urgent basis. This can be easily done by using Proxy Server as a gateway to reach out the Internet. I am going to show you all the steps required to use proxy server in Linux machines.

What is Proxy Server

A proxy server is an intermediary server which acts as a gateway to Internet or sometimes to a different network. SQUID Proxy Server could be one such example in Linux. There are other proxy servers that one can use to route its traffic to reach Internet.

How to Use Proxy Server to download packages from YUM Repository in Linux

How to Use Proxy Server to Update Packages from YUM Repository in Linux

Also Read: How to List all the Installed Python Modules in Linux{2 Easy Methods}

Firstly you need to check if the proxy server is already set using environment variables http_proxy and https_proxy in Linux. You can use env command to display all the set environment variables as you can see below. Once confirmed that it is not set you can follow through the steps as describe below.

[root@localhost ~]# env
XDG_SESSION_ID=1
HOSTNAME=server1.example.com
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.0.101 51709 22
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PATH=/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
MAIL=/var/spool/mail/root
PWD=/root
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOME=/root
SHLVL=1
LOGNAME=root
SSH_CONNECTION=192.168.0.101 51709 192.168.0.103 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
DISPLAY=:0
_=/usr/bin/env

Step 1: Prerequisites

a) You need to have a Running Linux Server.

b) You should have a running Proxy Server. In this article we are using a SQUID Proxy Server. You can check How to Install and Configure SQUID Proxy Server on RHEL/CentOS 7/8.

c) Your Server should have access to a running Proxy Server. We are using below Servers for our demo purpose.

Host : 192.168.0.106
Proxy Server: https://192.168.0.103:3128

d) You should have root or sudo access to run privileged commands. Please Check How to Add User to Sudoers to know more about providing sudo access to the User.

Step 2: Set http_proxy environment variable

You need to set http_proxy variable and export it from the terminal using export http_proxy=https://192.168.0.103:3128 command. Please note that this setting is a temporary setting which will only work till the current session is active. Once you open a new session or close the current session this setting will be gone. So if you are preparing to make it permanent then do it through Step 4 or 5.

[root@localhost ~]# export http_proxy=https://192.168.0.103:3128

Step 3: Set https_proxy envoironment variable

Then set and export https_proxy variable using export https_proxy=https://192.168.0.103:3128 command as shown below. Like the previous step the setting of https_proxy variable through terminal is also a temporary setting which will not work if you open a new session or close the current session. So if you want to add proxy server information permanently then do it through Step 4 or 5.

[root@localhost ~]# export https_proxy=https://192.168.0.103:3128

Step 4: Enabling Proxy Globally through /etc/profile 

If you want to permanently enable proxy server globally for all the users then you need to set the proxy server information in the /etc/profile file. Here you need open the file using vi editor and add the proxy server info at the end of the file and then save and exit the file by Pressing Esc and then :wq!

[root@localhost ~]# vi /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done

unset i
unset -f pathmunge

export http_proxy=https://192.168.0.103:3128
export https_proxy=https://192.168.0.103:3128

Step 5: Enabling Proxy for Specific User 

Similarly if you want to permanently enable the proxy for a specific User then you need to add the proxy server information in the User Profile. For example: Here we are enabling proxy for User centos so we will add the proxy server information at the end of /home/centos/.bash_profile file.

[root@localhost ~]# vi /home/centos/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export http_proxy=https://192.168.0.103:3128
export https_proxy=https://192.168.0.103:3128

Step 6: Configure /etc/yum.conf file

Next step is to add the proxy server information into /etc/yum.conf file. You can open the file using vi editor and then add proxy=https://192.168.0.103:3128 configuration at the end of the file as shown below. Then save and exit by Pressing Esc and then :wq!

[root@localhost ~]# vi /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

# This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
exclude=kernel*,*.i?86
proxy=https://192.168.0.103:3128

Step 7: Configure YUM Repository

After adding the proxy server information you need to configure yum repository by creating a new repo under /etc/yum.repos.d directory path. Here we are creating a repo file known as centos.repo using vi editor and adding below configuration. You can also check How to Setup Local YUM Repository on CentOS 7 Using 8 Easy Steps to know more about downloading packages from local YUM Repository.

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vi centos.repo
[centos-server]
name=CentOS 7
baseurl=http://mirror.centos.org/centos/7/os/x86_64/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1
gpgcheck=1

name : Name of the repository.

baseurl : URL of CentOS Repository. If you are using local mount point then path of that mount point(for example: /mnt/iso)

gpgkey : Path of the gpgkey. file:/// denotes the local path.

enabled : Value of 1 enables the repository and 0 disables it.

gpgcheck : Value of 1 enables the GPG check and 0 disables it.

Step 8: Update Packages from YUM Repository

Now run yum clean all command to clean up all the packages from cache and then enable the repolist using yum repolist enabled command. If everything goes well you can now update the Server by using yum --disablerepo=* --enablerepo=centos-server update command. This command will disable all the repo and will enable only centos-server repo to run the update through proxy server. More on yum command Man Page.

[root@localhost ~]# yum clean all
[root@localhost ~]# yum repolist enabled
[root@localhost ~]# yum --disablerepo=* --enablerepo=centos-server update

Step 9: Unset Variables and Remove Proxy

Once you are done with the yum update you can unset all the proxy variables and remove the proxy information from /etc/yum.conf file to come back to original configuration. You need to use unset http_proxy to remove the proxy from http_proxy environment variable and unset https_proxy to remove the proxy server from https_proxy environment variable. Then open /etc/yum.conf file using vi editor and remove the entry of proxy=https_proxy://192.168.0.103:3128 which you added in the earlier steps and then save and exit the file by Pressing Esc and :wq!

[root@localhost ~]# unset http_proxy
[root@localhost ~]# unset https_proxy
[root@localhost ~]# vi /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


# This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

 

 

 

 

 

 

Popular Recommendations:-

Python3: ModuleNotFoundError: No Module Named “prettytable” in Linux 

Solved: ModuleNotFoundError: No Module Named “requests” in Python 3

How to Install and Enable EPEL Repository on RHEL/CentOS 7/8{Simple and Easy Steps}

How to Properly Search PHP Modules Using YUM tool in Linux(RHEL/CentOS 7/8)

Solved: FATAL: Authentication Helper Program /usr/lib/squid/basic_ncsa_auth: (2) No Such File or Directory

How to Install and Configure Squid Proxy Server on RHEL/CentOS 7/8

5 Best Ways to Become root user or Superuser in Linux (RHEL/CentOS/Ubuntu)

Leave a Comment