5

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,811

7 Answers7

13

cat /etc/*-release should do the trick

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

guettli
  • 3,811
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
  • 477
0

Apart from...

cat /etc/*-release 

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

$ uname -a
chicks
  • 3,915
  • 10
  • 29
  • 37
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
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,491
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,370
-2

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

man-page of /etc/issue

guettli
  • 3,811
Matt
  • 2,781