25

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?

w128
  • 353
  • 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. – ddnomad Mar 15 '17 at 09: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. – xDaizu Mar 15 '17 at 11:18
  • @xDaizu I'm running scripts on remote hosts to pull various info about them. – w128 Mar 15 '17 at 11:52
  • @w128 in that case, can't you check if the "feature" (/etc/os-release) exists (and fallback to other methods if not) instead of relying in a static mutable list of distributions where it works? – xDaizu Mar 15 '17 at 11:55
  • 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. – w128 Mar 15 '17 at 11:59
  • 1
    Relevant: http://unix.stackexchange.com/q/92199/22222 – terdon Mar 15 '17 at 15:24

1 Answers1

23

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.

Stephen Kitt
  • 434,908