Home FAQ Find Linux System Details Using Python
Find Linux System Details Using Python

Find Linux System Details Using Python

By sk
577 Views

Python is one of the popular programming language and It comes pre-installed with many Linux operating systems by default.  Did you know we can find the information about a Linux system using Python? Yes! This brief guide explains how to find Linux system details using Python. Python has module named Platform which allows us to find various system information such as OS version, distribution name, machine architecture, Kernel, OS code name, Python version etc.

Find Linux System Details Using Python

As stated above, most Linux distributions ships with Python by default. If it is not installed already, you can install it using the distribution's package manager. Here I have included the steps to install latest Python 3.6.x version on some Linux distributions.

On Arch Linux and its variants:

$ sudo pacman -S python

On Fedora:

$ sudo dnf install python36

On CentOS 7, we can install Python 3.6 from IUM (Inline with Upstream Stable) repository like below.

$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum install python36u

Ubuntu 17.10 and newer versions, Python 3.6 is available by default. So don't bother with installation.

On Ubuntu 16.10 and 17.04, Python3 is available in the [Universe] repository. Make sure you have enabled [Universe] repository and install Python 3.6 as shown below.

$ sudo add-apt-repository universe
$ sudo apt-get install python3.6

On Ubuntu 14.04 or 16.04, Python 3 is not available in the Universe repository. So, use the following PPA to install Python 3.

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6

Once Python is installed, check its version using command:

$ python3 --version

Or,

$ python3.6 -V

You may see an output something like below.

Python 3.6.8

Now it is time to find what OS we are running on.

Display OS name, version and code name:

$ python3 -c 'import platform; print(platform.linux_distribution())'

If you have Python 2.x on your system, simply use:

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

Sample output from Ubuntu 18.04 Desktop:

('Ubuntu', '18.04', 'bionic')

Display machine architecture:

$ python3 -c 'import platform; print(platform.architecture())'
('64bit', 'ELF')

Display machine type:

$ python3 -c 'import platform; print(platform.machine())'
x86_64

Display computer's network name:

$ python3 -c 'import platform; print(platform.node())'
ostechnix

Display underlying platform:

$ python3 -c 'import platform; print(platform.platform())'
Linux-5.0.0-32-generic-x86_64-with-Ubuntu-18.04-bionic

Display processor's name:

$ python3 -c 'import platform; print(platform.processor())'
x86_64

Display system's release details:

$ python3 -c 'import platform; print(platform.release())'
5.0.0-32-generic

Display system/OS name:

$ python3 -c 'import platform; print(platform.system())'
Linux

Display system's release version:

$ python3 -c 'import platform; print(platform.version())'
#34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019

Display all details, such as system, node, release, version, machine, and processor

$ python3 -c 'import platform; print(platform.uname())'
uname_result(system='Linux', node='ostechnix', release='5.0.0-32-generic', version='#34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019', machine='x86_64', processor='x86_64')

It is equivalent to "uname -a" command.

Like I already mentioned earlier, we can get the installed Python version as well.

For Python 2.x, run:

$ python -c 'import platform; print(platform.python_version())'
2.7.15+

For Python 3.x:

$ python3 -c 'import platform; print(platform.python_version())'
3.6.8

Find Linux System Details Using Python Platform module

Create simple Python script to display Linux system details

We can create a simple Python script to display the information about a Linux system by putting all functions together in a text file and view the complete details in one go.

Let us create a text file named os_info.py:

$ nano os_info.py

Add the following lines:

import platform

print('Uname:', platform.uname())

print()
print('Distribution :', platform.linux_distribution())
print('Machine :', platform.machine())
print('Node :', platform.node())
print('Processor :', platform.processor())
print('Release :', platform.release())
print('System :', platform.system())
print('Version :', platform.version())
print('Platform :', platform.platform())

Python script to find Linux system details

Save and exit the file.

Now run the following command to find your Linux system information:

$ python os_info.py

Or,

$ python3 os_info.py

Sample output would be:

Uname: uname_result(system='Linux', node='ostechnix', release='5.0.0-32-generic', version='#34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019', machine='x86_64', processor='x86_64')

Distribution : ('Ubuntu', '18.04', 'bionic')
Machine : x86_64
Node : ostechnix
Processor : x86_64
Release : 5.0.0-32-generic
System : Linux
Version : #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019
Platform : Linux-5.0.0-32-generic-x86_64-with-Ubuntu-18.04-bionic

Find Linux System Details Using Python script

All of the above commands have been tested with Python 3.6.8 on Ubuntu 18.04 desktop edition.

Find Linux system details using Python 3.8.0

Please note that the platform.linux_distribution() function is depreciated since version 3.5, and it will be removed in 3.8. If you're using most recent Python version, which is 3.8.0, you must use the alternative like the distro package.

You can install distro package using PiP.

$ pip install distro

Once distro package is installed, simply run the following command to find what OS you're running on:

$ distro

Sample output:

Name: Ubuntu 18.04.2 LTS
Version: 18.04 (bionic)
Codename: bionic

To display little more details, use -j flag:

$ distro -j

Sample output:

{
    "codename": "bionic",
    "id": "ubuntu",
    "like": "debian",
    "version": "18.04",
    "version_parts": {
        "build_number": "",
        "major": "18",
        "minor": "04"
    }
}

Find Linux System Details Using distro package

There are also plenty of other ways and applications available to find the details of a Linux machine. We have already covered some of them and linked them here for your reference.

Hope this helps.

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More