Using a Proxy With DNF

By | 2020-11-27

Using a proxy with DNF is straightforward. You can edit /etc/dnf/dnf.conf, set the relevant environment variable, or use the shell sub command. DNF supports both HTTP and SOCKS proxies and several authentication methods.

Keep in mind a few things when caching package repositories:

  • Package repositories running on HTTPS cannot be cached without something like Squid’s SSL bump. If you opt for plain text protocols to ensure caching, be leery of accepting new signing keys; verify the keys before accepting them.
  • Using mirror lists will prevent caching. CentOS does this by default. I recommend configuring your repositories to use a specific mirror.

Editing The Configuration File

Below is an example /etc/dnf/dnf.conf with the proxy options added. The only required option is proxy, which is used to define the proxy server’s URL.

[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=True

proxy=http://proxy.tylersguides.com:3128
#proxy=socks5://proxy.tylersguides.com:1080
#proxy_username=dnf
#proxy_password=password
#proxy_auth_method=basic

The options shown are all of the options available. See the DNF documentation for more details. If your proxy server doesn’t require authentication, they can be omitted.

Setting Environment Variables

DNF uses the same environment variables as curl for proxy server usage. Set the variable with the proxy URL and credentials as you would with curl.

# export ALL_PROXY=socks5://proxy.tylersguides.com:1080
# export http_proxy='username:password@http://proxy.tylersguides.com:3128'

If you have a proxy server configured in /etc/dnf/dnf.conf and you define a variable, the proxy configuration in the configuration file will be used.

Sometimes you may wish to ignore the proxy server configuration in the config file. To do this, use -c.

# export http_proxy='username:password@http://proxy.tylersguides.com:3128'
# dnf -c /dev/null makecache

In the example, the configuration file is set to /dev/null. When dnf reads it, it will be zero size and use the defaults for everything. This forces it to use the environment variable for the proxy configuration instead of the file.

Using The Shell

Another way to use a proxy server is the shell feature.

# dnf shell
Last metadata expiration check: 0:03:09 ago on Fri 27 Nov 2020 01:25:27 PM EST.
> clean all
20 files removed
> config proxy http://proxy.tylersguides.com:123
> makecache
CentOS-8 - AppStream                                                                                                    0.0  B/s |   0  B     00:00    
Error: Failed to synchronize cache for repo 'AppStream'
> config proxy http://proxy.tylersguides.com:3128
> makecache
CentOS-8 - AppStream   
CentOS-8 - AppStream   
CentOS-8 - Base
CentOS-8 - Base
CentOS-8 - Extras
CentOS-8 - Extras
Metadata cache created.
> exit
Leaving Shell

In the example above, I set the proxy configuration to use a non existent server. As expected, my attempt to update the cache failed. When I fixed the setting to use a working server, it worked as expected.

The shell config options are the same as those in the file. If you need to set credentials or change the authentication type, set the variables with config the same way you would in the file.

# dnf shell
Last metadata expiration check: 0:06:56 ago on Fri 27 Nov 2020 01:29:37 PM EST.
> config proxy http://proxy.tylersguides.com:3128
> config proxy_username myusername
> config proxy_password supersecret
> config proxy_auth_method digest
> makecache
CentOS-8 - AppStream   
CentOS-8 - AppStream   
CentOS-8 - Base
CentOS-8 - Base
CentOS-8 - Extras
CentOS-8 - Extras
Metadata cache created.
> exit
Leaving Shell

When you exit the shell, you lose any configuration variables set. You must set them again if you enter another shell session.

References

See Also