AAtsushi's Blog
Infrastructure

Repositories in Linux: yum or dnf

→ 日本語版を読む

In Linux, a repository is a server that hosts packages (.rpm, .deb, etc.) for download.

For example, Nifcloud places various packages (like aajohan-comfortaa-fonts.noarch) in repositories (servers) such as rhui-rhel-8-for-x86_64-baseos-rhui-rpms and rhui-rhel-8-for-x86_64-appstream-rhui-rpms, making them available for users to download. By the way, .noarch means "architecture-independent," indicating that the package creator has made it usable on any architecture such as i386 (32-bit) or x86_64 (64-bit).

If you don't add a repository to your Linux machine, you can't download packages hosted in that repository.

In RHEL, adding a repository is done with the following commands:

yum repolist    # List repositories
yum-config-manager --add-repo repository_url    # Add a repository
yum-config-manager --enable repository    # Enable a repository

Added repositories are saved as files (.repo) in the /etc/yum.repos.d/ directory.

If you look at the contents of a .repo file, it is written in ini format. enabled=1 means the repository is enabled. gpgkey is the path to the public key file, and if the encrypted signature contained in the RPM can be decrypted with the public key file, it confirms that the RPM file is genuine. With gpgcheck=1, the GPG signature is checked for all packages in the repository to verify package authenticity.

/etc/yum.repos.d/vscode.repo

[code]
name=Visual Studio Code
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

To disable a repository, use --disable.

yum-config-manager --disable repository    # Disable a repository

To delete an added repository, delete the .repo file from the /etc/yum.repos.d/ directory.

To install or update packages from a repository, run the following commands:

yum list # List installable packages
yum list installed # List installed packages
yum install [package name] # Install a package

yum check-update # List installed packages that have updates available
yum update [package name]

Actually Adding a Repository and Installing a Package from It

Let's try adding a repository and installing a package using a CentOS Stream9 Docker image. Note that the Stream9 image is on quay.io, not Docker Hub.

docker pull quay.io/centos/centos:stream9
docker run --name centos_stream9 -it quay.io/centos/centos:stream9 /bin/bash

Since yum-config-manager is not installed, install yum-utils.

# yum install -y yum-utils

I want to try installing VScode. Looking at the official documentation, the repository is https://packages.microsoft.com/yumrepos/vscode. After adding the repository, packages.microsoft.com_yumrepos_vscode.repo is created under /etc/yum.repos.d/.

# yum-config-manager --add-repo https://packages.microsoft.com/yumrepos/vscode
# ls /etc/yum.repos.d/
centos-addons.repo  centos.repo  packages.microsoft.com_yumrepos_vscode.repo

The contents of the .repo file look like this:

# cat /etc/yum.repos.d/packages.microsoft.com_yumrepos_vscode.repo
[packages.microsoft.com_yumrepos_vscode]
name=created by dnf config-manager from https://packages.microsoft.com/yumrepos/vscode
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1

You could install VScode with yum install code now, but the .repo filename is a bit ugly and gpgcheck is not configured, so let's rename the file and add gpgcheck and gpgkey. Like this:

/etc/yum.repos.d/vscode.repo

[vscode]
name=Visual Studio Code
baseurl=https://packages.microsoft.com/yumrepos/vscode
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc

You can also manually configure the .repo file like this and install packages with yum install. For example, the official gcloud CLI installation instructions have you manually create a .repo file as follows:

sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-cli]
name=Google Cloud CLI
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el9-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM

For the record, the official VScode installation method doesn't require manually creating a .repo file.

This time I used yum, but there is also a command called dnf. This is the successor to yum and can use the same subcommands and options as yum. In practice, both yum and dnf are symbolic links pointing to dnf-3, so they both end up executing the dnf-3 command.

$ ls -l /usr/bin/yum /usr/bin/dnf
lrwxrwxrwx 1 root root 5 Oct 26 05:16 /usr/bin/dnf -> dnf-3
lrwxrwxrwx 1 root root 5 Oct 26 05:16 /usr/bin/yum -> dnf-3

That's it for today.

References

https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/6/html/deployment_guide/sec-managing_yum_repositories

https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/6/html/deployment_guide/sec-configuring_yum_and_yum_repositories

https://xtech.nikkei.com/it/article/COLUMN/20061031/252339/

https://onoredekaiketsu.com/yum-command-and-repository/#toc36

https://eng-entrance.com/linux-package-yum

https://qiita.com/sksmnagisa/items/05a6f8a707010b8bea56

https://atmarkit.itmedia.co.jp/ait/articles/2001/31/news006.html

https://blog.apar.jp/linux/16100/