How to Use YUM/DNF Command with a Proxy Server

This guide will show you how to set a proxy for YUM/DNF so that you may be able to install and update packages from remote repositories.

Direct internet access is not permitted in the majority of corporate networks. So, if we need to get something from the internet, we must go through some proxy servers.

Suppose we use Linux servers running Red Hat Enterprise Linux, Fedora, AlmaLinux, or RockyLinux, and we need to install new packages or apply updates. In that case, we must configure a proxy for the yum/dnf command

But before that, I want to clarify something. Perhaps some of you are confused about the two commands, yum and dnf, generally do the same thing.

Here is the answer. From Red Hat Enterprise Linux 8.0 onwards, the implementation of the yum command actually calls dnf directly since it is just a symlink to it. The same is true for the configuration files, yum.conf and dnf.conf.

And to make it even more apparent, look at the image below.

YUM is just a symlink to the DNF command

In other words, if you type yum instead of dnf, the command works as expected because yum is an alias to dnf for compatibility.

Related: How to Create an Alias and Use the Alias Command in Linux

In addition, since distros like Fedora, AlmaLinux, and Rocky Linux are Red Hat-based, the above applies to them as well.

How to Enable Proxy Settings for DNF/YUM Command

To configure the dnf/yum operations to use a proxy server, you need to modify the content of the /etc/dnf/dnf.conf file. You have three configuration options available:

  1. proxy=http://SERVER_ADDRESS:PORT/
  2. proxy_username=USERNAME (optional)
  3. proxy_password=PASSWORD (optional)

Of course, if your proxy does not require authentication, you can use only the proxy=http://SERVER_ADDRESS:PORT/ directive.

First, open the dnf.conf file with your preferred text editor:

sudo vim /etc/dnf/dnf.conf

Then, add the following settings to the end of the file:

Enable proxy settings for DNF/YUM command

The settings above instruct the dnf command to connect to port 3128 via the proxy server squid.mycompany.com, using the login user james and the password mysecretpass.

Finally, save and close the file. That’s all! Now you will be able to update and install software using the yum/dnf command passing through a proxy server.

Please keep in mind that setting a proxy server, username, and password in the dnf.conf file means that all users who use the dnf command will connect to the proxy server with those credentials.

Now dnf will use the proxy settings to get updates, install packages, etc.

Use yum/dnf command with a proxy server

But sometimes, however, you may want only the root user, for example, to have proxy settings reconfigured for use with yum/dnf. Here’s how to achieve it.

Enable Proxy Access for a Specific User

So, if you do not want to configure a global proxy through the /etc/dnf/dnf.conf file, you can specify a proxy at the user level by defining it in the user’s .bash_profile file.

Fortunately, the environment variables http_proxy and https_proxy are available in Linux and UNIX-like systems. It enables the connection of text-based sessions and applications via the proxy server.

The syntax follows one of the two forms shown below:

  1. http_proxy=http://SERVER_ADDRESS:PORT/
  2. http_proxy=http://USERNAME:PASSWORD@SERVER_ADDRESS:PORT/

If your proxy does not require authentication, you can use variant 1. Otherwise, use variant 2.

So, the steps are as follows to set the yum/dnf to use a proxy reconfigured only for the root user.

First, switch to root user, cd to its home directory, and open the .bash_profile file with your preferred text editor.

su
cd root
vim .bash_profileCode language: CSS (css)

Then, add the following two lines to the end of the file:

Enable yum/dnf proxy access for a specific user

As a result, this proxy setting will be available to all apps, such as dnf, curl, and wget, that use the system proxy under the root login.

In other words, when an internet connection is required, these apps will connect to port 3128 via the proxy server squid.mycompany.com, using the login user james and the password mysecretpass.

Finally, source the .bash_profile file for the changes to take effect.

source ~/.bash_profile

Conclusion

I hope the instructions in this article to configure a proxy for the yum/dnf command in Linux were useful. So, please share your ideas and feedback in the comments section.

See the DNF Configuration Reference for a complete list of all DNF options and more information.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%