30

I'm trying to find a way to determine Linux distribution name and version that would work on most (or ideally, all) modern distributions. I noticed that /etc/os-release contains the info I need on the distributions I tried (CentOS, Debian), but how safe is it to rely on the presence of it? Commands such as uname -a don't really contain the same info, and lsb_release is apparently not present on e.g. minimal CentOS.

Is there a quick way to find out exactly what distros come with /etc/os-release? Moreover, is /etc/os-release guaranteed to contain NAME, VERSION and PRETTY_NAME fields?

6
  • 1
    Ubuntu, Debian, Arch for sure. Anyway I would omit detecting distro that way. Last time I need to distinguish distros I was checking a presence of particular package managers (i. e. pacman -> Arch, apt-get & no pacman -> either Ubuntu or Debian). It's kinda tricky task and I would also like to know whether there is a better solution. Commented Mar 15, 2017 at 9:23
  • 3
    One wonders why do you need to know the distribution name. This sounds like a case of Browser detection when you should be doing Feature detection. Commented Mar 15, 2017 at 11:18
  • @xDaizu I'm running scripts on remote hosts to pull various info about them. Commented Mar 15, 2017 at 11:52
  • 1
    @xDaizu falling back to "other methods" may involve extra work that may prove unnecessary if I can guarantee the simple solution proposed in the question to be reliable on the several major distros I need to support, so a mutable list of supported distros is fine in this case. Commented Mar 15, 2017 at 11:59
  • 2
    Relevant: unix.stackexchange.com/q/92199/22222 Commented Mar 15, 2017 at 15:24

2 Answers 2

27

Any system running systemd should have /etc/os-release, which is specified as part of systemd. Some systems without systemd might have it too (e.g. Debian 8 where systemd is optional but /etc/os-release is installed in all cases).

According to the specification, all fields are optional, and some have defaults ("Linux" for NAME and PRETTY_NAME).

You’ll find more background in the /etc/os-release announcement.

2
  • There are also some up-to-date distros which don't include systemd but have /etc/os-release, like Artix Linux I mentioned in the comment to the other answer. Commented May 29, 2024 at 18:11
  • I didn’t intend for this to be anything like an exhaustive list of non-systemd distributions with /etc/os-release ;-). Bear in mind that Debian 8 was up-to-date when I wrote this answer. Commented May 31, 2024 at 8:32
0

A quick oneliner for use in scripts could be: cat /etc/os-release | grep ^ID= | sed -s "s/ID=//g"

Judging from this list it should work just fine https://github.com/chef/os_release

5
  • 3
    Or, equivalently, sed -n 's/^ID=//p' /etc/os-release Commented May 29, 2024 at 16:54
  • That repository is incomplete. For example, it doesn't list Artix Linux, which has /etc/os-release. Commented May 29, 2024 at 17:56
  • @Vilinkameni please open a pull request to add the Artix Linux os-release. Commented May 31, 2024 at 6:51
  • @StephenKitt That was just one example, showing that such lists shouldn't be taken as definitive sources for this classification. Commented May 31, 2024 at 8:23
  • @Vilinkameni of course — at least, such lists should only be used positively (i.e. a distribution present in the list has /etc/os-release), not negatively (it would be incorrect to conclude that a distribution doesn’t have /etc/os-release if it’s not in the list). My point is that you have the power to improve the situation ever so slightly, with something you seem to care about (Artix Linux). Obviously it’s not a demand, just a friendly suggestion. Commented May 31, 2024 at 8:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.