4

I want to detect linux distribution and version.

I searched with my favorite search engine and discovered lsb_release.

Unfortunately this tool is not installed by default.

I would like to avoid to install lsb_release first.

Is there no easier way to detect the linux distribution and version in a portable way?

With "portable" I mean different kind of linux distributions. Not *BSD or Windows.

guettli
  • 3,113
  • 14
  • 59
  • 110
  • 4
    Some answers: https://stackoverflow.com/questions/1864734/how-to-know-which-linux-distribution-im-using – Gerald Schneider Oct 19 '17 at 08:12
  • Hi guettli did you try this? uname -a – Agustín Noel Morales Oct 19 '17 at 17:38
  • How many hosts do you need to inventory? You should really use a devops tool like puppet/chef/ansible/salt, all of those tools can gather various facts about the hosts that you are managing. – Danie Oct 25 '17 at 11:02
  • @Danie thank you for your hint. Yes, you are right. But these tools you meanted need a way to discover the distro, too. If they can do it, I should be able to do it, too. Or am I wrong? – guettli Oct 27 '17 at 08:06
  • if you for example use Ansible - it uses ssh to communicate to remote hosts. You don't need agents installed. then you can quickly get a inventory of hosts. check my answer below for a Ansible example – Danie Nov 03 '17 at 08:50

7 Answers7

12

cat /etc/*-release should do the trick

Docs: https://www.freedesktop.org/software/systemd/man/os-release.html

guettli
  • 3,113
  • 14
  • 59
  • 110
Diego Velez
  • 780
  • 1
  • 6
  • 13
  • Thank you, this is an easy to parse format. Is there a spec for this file? – guettli Oct 24 '17 at 10:47
  • systemd usually comes with a easy to parse `/etc/os-release` -file, but other than that it's not very standardized – ptman Oct 24 '17 at 10:57
4

Pick one...

cat /etc/issue
source /etc/os-release && echo "$NAME $VERSION"
cat /etc/os-release
uname -a

I guess most modern and LSB compliant distributions should provide /etc/os-release, but as usual: it's complicated! ;-)

Some further reading material:

https://en.wikipedia.org/wiki/Linux_Standard_Base

http://0pointer.de/blog/projects/os-release.html

Jan Grewe
  • 457
  • 5
  • 8
0

Apart from...

cat /etc/*-release 

...this will tell you kernel version and machine architecture.

$ uname -a
chicks
  • 3,639
  • 10
  • 26
  • 36
0

Your task is a difficult one and you have my sympathy.

I would recommend you use a library rather than repeating the work of others. Python ships with a library called 'platform' which has a function to do this:

python -c 'import platform; print(platform.linux_distribution())'

However this is still not a complete solution, there are simply too many distributions.

Bracken
  • 139
  • 3
0

If you have ruby gem installed in your system, you can install facter using gem install facter or if you have puppet installed, you can use facter

facter os
{
  architecture => "x86_64",
  family => "RedHat",
  hardware => "x86_64",
  name => "CentOS",
  release => {
    full => "7.2.1511",
    major => "7",
    minor => "2"
  },
  selinux => {
    config_mode => "enforcing",
    config_policy => "targeted",
    current_mode => "permissive",
    enabled => true,
    enforced => false,
    policy_version => "28"
  }
}
c4f4t0r
  • 5,149
  • 3
  • 28
  • 41
  • I am sorry, I can't follow you. I don't have ruby gem installed and I don't have puppet installed. I can do use ssh to connect to the host. – guettli Oct 27 '17 at 08:08
0

Install Ansible on a host that is able to reach all the hosts you want to scan. Follow the instructions: http://docs.ansible.com/ansible/latest/intro_installation.html

Then set up a Hosts file /etc/ansible/hosts

[all]
overlord
10.10.10.2

[all:vars]
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
ansible_user=root
ansible_ssh_pass=RootPassword
#ansible_become=True
#ansible_become_method='sudo'
#ansible_become_pass=sudopassword

It can be ip address or dns names, as long as it is resolvable. If you need to use sudo uncomment and change the ansible_user / ansible_ssh_pass

Run setup module to extract information from hosts

# ansible -i hosts all -m setup 

for all facts and you can filter the facts using

# ansible -i hosts overlord -m setup -a 'filter=ansible_distribution*'
overlord | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "OracleLinux",
        "ansible_distribution_major_version": "7",
        "ansible_distribution_release": "NA",
        "ansible_distribution_version": "7.3"
    },
    "changed": false
}
Danie
  • 1,350
  • 10
  • 12
-2

in addition to the options available withuname you can also check /etc/issue

man-page of /etc/issue

guettli
  • 3,113
  • 14
  • 59
  • 110
Matt
  • 2,711
  • 1
  • 13
  • 20
  • Yes, I could parse the output of `uname` or `/etc/issue`. But for me using regexs feels like eating rubbish. – guettli Oct 24 '17 at 08:39
  • /etc/issue is intended for local information - if your distro happens to include version information then that's pretty much coincidental. Uname only gives you information about the kernel version - not the distribution nor its version. – symcbean Oct 24 '17 at 12:09
  • pretty much coincidental meaning for a standard install from the media found on the websites of the top two(at least) most popular server distributions. Various versions CentOS, RHEL, Ubuntu, and Debian from the last three years all had distro information in `/etc/issue`. So while some administrators may know enough to customize this to give more useful information about the OS Image loaded on a system, many users will never encounter such a system. `uname` was my bad, though the kernel version can give useful information about the OS. In a pinch try these, but there are better methods – Matt Oct 25 '17 at 07:42
  • OS in /etc/issue might not compliant and therefore removed. – bbaassssiiee Oct 26 '17 at 06:06