Sub topic 1
Know what you are running: /etc/os-release
Prerequisites
This chapter assumes you can do the following:
- Work in a Linux shell: run commands, read their output, and use
cat,grep, andecho. - Install software with a package manager, even if you have used only one.
- Recognize systemd as the init system and service manager on modern servers.
- Read a short shell script, including variables and a
casestatement.
You do not need to know the differences between Ubuntu Server, Debian, and Rocky Linux yet. The later chapters cover them. This chapter teaches you to find out which one you are on, and what the answer tells you.
Why identification comes first
Almost every command you run on a server depends on the distribution. apt install fails on Rocky Linux, and dnf install fails on Ubuntu. Firewall commands, repository files, and the security framework you debug all differ between them. A runbook that says "open port 443" means something different on each system.
Before you change anything on an unfamiliar host, find out three things:
- Which distribution it is.
- Which release of that distribution it is.
- Which family it belongs to, Debian or RHEL, because the family decides which tool set applies.
The file /etc/os-release answers all three on any current Linux system.
What /etc/os-release is
/etc/os-release is a plain text file of KEY=value lines. The systemd project defines the format, and Ubuntu, Debian, Rocky Linux, and nearly every other mainstream distribution ship it. Systemd defines the file as a shell-compatible list of variable assignments. A shell can load it directly, and other programs can parse it with simple tools.
On most systems the real file is /usr/lib/os-release, and /etc/os-release is a symbolic link to it. The specification says tools should read /etc/os-release first and fall back to /usr/lib/os-release if the first is missing. In practice, reading /etc/os-release is enough.
Here is what the file looks like on three systems. Your minor version numbers will differ.
Ubuntu Server 24.04:
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
UBUNTU_CODENAME=nobleDebian 12:
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debianRocky Linux 9:
NAME="Rocky Linux"
VERSION="9.4 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.4"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.4 (Blue Onyx)"Debian has no ID_LIKE line because it is the root of its lineage. Ubuntu points to debian, and Rocky Linux points to the RHEL family.
The fields that matter
| Field | Meaning | Use it for |
|---|---|---|
| `ID` | Lowercase machine-readable distribution name, such as `ubuntu`, `debian`, or `rocky` | Deciding which distribution you are on |
| `ID_LIKE` | Space-separated list of distributions this one is derived from or closely resembles | Deciding which family you are on |
| `VERSION_ID` | Release number, such as `24.04`, `12`, or `9.4` | Checking support status and compatibility |
| `VERSION_CODENAME` | Release codename, such as `noble` or `bookworm`. Rocky Linux does not set it | Writing repository entries on Debian-family systems |
| `PLATFORM_ID` | Platform tag, such as `platform:el9`, set on RHEL-compatible systems | Spotting a RHEL-compatible major version |
| `NAME`, `PRETTY_NAME` | Human-readable names | Display and logs, not logic |
| `HOME_URL`, `SUPPORT_URL`, `BUG_REPORT_URL` | Project and support links | Finding the vendor's documentation |
Use ID and ID_LIKE in scripts, not NAME or PRETTY_NAME. The names are written for people and can change in wording or spacing. The IDs are stable values meant for programs.
Reading the file with the shell
Because the file is valid shell syntax, you can load it and use its values as variables:
. /etc/os-release
echo "$NAME $VERSION_ID (ID=$ID, ID_LIKE=${ID_LIKE:-none})"The first line runs the file as shell code in your current shell, so every KEY=value line becomes a variable. The ${ID_LIKE:-none} form prints none when ID_LIKE is unset. This matters on Debian, which does not define it.
Expected output on each system:
Ubuntu 24.04 (ID=ubuntu, ID_LIKE=debian)
Debian GNU/Linux 12 (ID=debian, ID_LIKE=none)
Rocky Linux 9.4 (ID=rocky, ID_LIKE=rhel centos fedora)Loading the file into your interactive shell leaves variables like NAME, VERSION, and ID set for the rest of the session. To avoid that, read one value inside a subshell:
distro_id=$(. /etc/os-release && echo "$ID")
echo "$distro_id"The parentheses run the commands in a child shell, so only the printed value comes back.
Reading it from the other side: other places to look
Several other sources report the same information. They are useful to recognize, but each has limits.
hostnamectl
cat /etc/debian_version
cat /etc/redhat-release
uname -r
lsb_release -ahostnamectlis part of systemd. It prints the operating system line, the kernel, and the machine's virtualization type. It reads/etc/os-releaseitself, so it adds no new facts about the distribution./etc/debian_versionexists on Debian and Ubuntu. On Debian it holds the point release, such as12.5, whichVERSION_IDdoes not show. On Ubuntu it holds a codename-style value and is less useful./etc/redhat-releaseexists on RHEL-compatible systems, including Rocky Linux. It is a one-line legacy file. Newer scripts should prefer/etc/os-release.uname -rprints the kernel release. It tells you about the kernel only, not the distribution.lsb_release -acomes from the Linux Standard Base tooling. It is present on Ubuntu Server by default, but a minimal Debian or Rocky install may not have it. Do not depend on it in scripts.
Debian versus Ubuntu: why ID_LIKE exists
Ubuntu is built from Debian. A package that works on Debian will usually install on Ubuntu, and the tools (apt, dpkg) are the same. The ID_LIKE=debian line states that relationship so a script can treat both the same way without listing every derivative.
Rocky Linux lists rhel centos fedora. Rocky is built to be compatible with Red Hat Enterprise Linux (RHEL), so rhel is the key word. AlmaLinux sets a similar list, which means one script can handle several RHEL rebuilds.
The following script maps a host to a family. It checks ID first, then each word of ID_LIKE, and stops at the first match:
#!/usr/bin/env bash
set -eu
. /etc/os-release
family="unknown"
for candidate in "$ID" ${ID_LIKE:-}; do
case "$candidate" in
debian)
family="debian"
break
;;
rhel|fedora|centos)
family="rhel"
break
;;
esac
done
echo "id=$ID version=${VERSION_ID:-unknown} family=$family"The ${ID_LIKE:-} expansion is deliberately left unquoted, so the shell splits the list into separate words. Quoting it would turn rhel centos fedora into one string that matches no case.
Run on the three systems above, it prints:
id=ubuntu version=24.04 family=debian
id=debian version=12 family=debian
id=rocky version=9.4 family=rhelThe diagram shows the same decision.
Version fields behave differently on each system
VERSION_ID does not mean the same thing everywhere, and this causes bugs in scripts.
- Ubuntu:
VERSION_IDis the release year and month, such as24.04. It stays the same for the whole life of that release. Point releases such as 24.04.1 appear inVERSIONandPRETTY_NAMEonly. - Debian:
VERSION_IDis the major number, such as12. The point release (12.5) is not in/etc/os-release. Read/etc/debian_versionfor it. - Rocky Linux:
VERSION_IDincludes the minor version, such as9.4. It changes when you update the system withdnf upgrade. A script that testsVERSION_ID == "9"fails on every real Rocky host. UsePLATFORM_ID(platform:el9) or take the part before the dot with${VERSION_ID%%.*}.
The next lines print the major version safely on any of the three:
. /etc/os-release
major="${VERSION_ID%%.*}"
echo "major release: $major"On Ubuntu this prints 24, on Debian 12, and on Rocky Linux 9.
Debian testing and unstable do not set VERSION_ID. Under set -u, a script that reads $VERSION_ID without a default stops with an unbound variable error. Use ${VERSION_ID:-unknown}, as the family script does.
Containers and virtual machines
Inside a container, /etc/os-release describes the container image, not the host. A Rocky Linux container running on an Ubuntu host reports ID=rocky, but its kernel is the Ubuntu host's kernel. For the kernel, use uname -r. For the host's distribution, run the check on the host itself.
Cloud images behave like normal installs. They carry the same file, and the vendor's image may add extra variables such as VARIANT_ID. A script should ignore keys it does not recognize.
Common errors
- Matching on
NAMEorPRETTY_NAME. The text includes spaces and quote marks, for exampleDebian GNU/Linux. Match onID. - Testing for
ID=ubuntuwhen you mean "any Debian-family system". Use the family loop above so Debian, Ubuntu, and other derivatives all work. - Comparing
VERSION_IDas a whole number on Rocky Linux. Strip the minor version first. - Sourcing the file in an interactive session and then wondering why
$VERSIONor$NAMEis set. Use a subshell. - Trusting
lsb_releaseto exist. It is missing on many minimal installs. - Assuming a container's
/etc/os-releasedescribes the machine you are logged in to.
What this tells you for the rest of the tutorial
Once you know the family, you know the package tools: apt and dpkg for Debian-family systems, dnf and rpm for RHEL-family systems. You also know which mandatory access control system is likely to be active, and which firewall front end is the default. The next chapters build on this split. They explain why the lineages differ, how long each release is supported, and how to choose between them for a given job.
Exercise
A script runs on a Rocky Linux 9.4 host and contains the test [ "$VERSION_ID" = "9" ]. The test fails. Explain why, and give two ways to fix it.
Show answer
On Rocky Linux, VERSIONID includes the minor version, so it is "9.4" and never equals "9". Fix 1: strip the minor part with `major="${VERSIONID%%.*}" and compare $major to 9. Fix 2: test PLATFORM_ID for platform:el9`, which stays the same across minor releases.
Exercise
A host reports ID=ubuntu and IDLIKE=debian. Another host reports ID=rocky and IDLIKE="rhel centos fedora". Which package manager does each use, and why does a family check that tests only ID fail on Ubuntu derivatives?
Show answer
The first host uses apt and dpkg because it is in the Debian family. The second uses dnf and rpm because it is in the RHEL family. A check that tests only ID=debian misses Ubuntu, whose ID is ubuntu. The IDLIKE list names the parent family, so a script that checks both ID and IDLIKE handles Debian, Ubuntu, and other derivatives.
Exercise
You run . /etc/os-release on a Debian 12 host, then print ${ID_LIKE:-none}. What is printed, and why is the :-none part needed under set -u?
Show answer
It prints "none". Debian is the root of its lineage and does not define ID_LIKE. Without a default, set -u would stop the script with an unbound variable error when it expands the unset variable.