1. Introduction
For administration tasks, we often need detailed information about our system. Thus in the Linux case, the knowledge about kernel version and distribution is very basic for us.
Moreover, we need information about the physical storage available. Consequently, in the Linux context, we need to list disk partitions.
We’re going to look through Linux commands helpful to achieve this goal.
2. The uname Command for Kernel Data
The uname command returns a bunch of information concerning our kernel. So, let’s check the kernel name:
$ uname
Linux
2.1. Options to uname
We should use options to get more specific information. Therefore, for all fields, let’s use the –a switch:
$ uname -a
Linux 10.0.2.15 5.14.10-300.fc35.x86_64 #1 SMP Thu Oct 7 20:48:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Now, let’s check each kernel-related piece of information with its own switch.
Thus, for the kernel name, let’s use -s (or just nothing):
$ uname -s
Linux
Then, for the kernel release, let’s use -r:
$ uname -r
5.14.10-300.fc35.x86_64
Finally, for the kernel version, we should use -v:
$ uname -v
#1 SMP Thu Oct 7 20:48:44 UTC 2021
Let’s notice that, the kernel-related data are just compiled during the build of the kernel. In the runtime, they are obtained with the uname system call.
2.2. Availability
The command is a part of the sh-utils or coreutils package. Therefore, we should find it in almost all distributions.
3. Distribution Information With lsb_release
Now we’re going to learn about the Linux distribution details with the lsb_release command:
$ lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: Fedora
Description: Fedora release 35 (Thirty Five)
Release: 35
Codename: ThirtyFive
So, we see that our operating system is Fedora 35.
Furthermore, let’s notice that the command is built around the Linux Standard Base (LSB) concept.
Thus, in the case of LSB compliant distributions, the command gathers information from /etc/lsb-release and /etc/distrib-release files. In addition, it parses the names of files in the /etc/lsb-release.d folder.
However, in our Fedora 35 example, only the folder /etc/lsb-release.d exists.
In certain distributions, the command is not available out of the box. It comes with the lsb_release package.
4. Parsing the os-release File
We can achieve a similar effect as using lsb_release by parsing the content of the /etc/os-release file. So, let’s cat it:
$ cat /etc/os-release
NAME="Fedora Linux"
VERSION="35 (Workstation Edition)"
ID=fedora
VERSION_ID=35
VERSION_CODENAME=""
PLATFORM_ID="platform:f35"
PRETTY_NAME="Fedora Linux 35 (Workstation Edition)"
# more output skipped ...
Now, let’s be more specific and display only PRETTY_NAME with the help of grep:
$ grep '^PRETTY_NAME' /etc/os-release
PRETTY_NAME="Fedora Linux 35 (Workstation Edition)"
This feature is a part of the systemd manager, so we should find it in most Linux distributions. However, not all fields need to be filled.
5. The hostnamectl Command
The hostnamectl command manages the hostnames of the system. It accepts commands related to different features of the system.
Thus, with the status command, we’re going to obtain information on kernel and distribution:
$ hostnamectl status
Static hostname: n/a
Transient hostname: 10.0.2.15
Icon name: computer-vm
Chassis: vm
Machine ID: 588ce19ba22340289d4e9f9ad3c5df63
Boot ID: 5a4584a74a1f4b1596ac4b6d15c96e01
Virtualization: oracle
Operating System: Fedora Linux 35 (Workstation Edition)
CPE OS Name: cpe:/o:fedoraproject:fedora:35
Kernel: Linux 5.14.10-300.fc35.x86_64
Architecture: x86-64
Hardware Vendor: innotek GmbH
Hardware Model: VirtualBox
Although some other commands narrow the hostnamectl‘s output, no one returns sole kernel information.
The hostnamectl command comes with the systemd-services package.
6. Listing Block Devices With lsblk
We’re going to obtain information about the system’s block devices with the lsblk command:
$ sudo lsblk -o NAME,KNAME,FSTYPE,MOUNTPOINT,SIZE
NAME KNAME FSTYPE MOUNTPOINT SIZE
sda sda 78,1G
├─sda1 sda1 ext2 /boot 1,5G
├─sda2 sda2 1K
├─sda5 sda5 ext4 / 5,5G
├─sda6 sda6 ext4 /usr 12,5G
├─sda7 sda7 ext4 /home 54,7G
└─sda8 sda8 swap 4G
sr0 sr0 1024M
We should use -o options to provide the list of column names we want to display. In our example, we used:
- NAME – device name
- KNAME – internal device name
- FSTYPE – the type of filesystem
- MOUNPOINT – the mounting point of the device
- SIZE – device’s size
We can obtain the complete list of available columns using issuing lsblk -h.
In addition, we can change units to bytes with the -b switch.
Finally, we don’t need the root privilege to run this command. However, without root access or sudo, some columns aren’t available, e.g., FSTYPE.
The command is distributed as a part of the util-linux package.
7. Listing Disk Partitions With fdisk
Let’s use the well-known fdisk command to list the disk’s partition:
$ sudo fdisk -l
Disk /dev/sda: 83.9 GB, 83886080000 bytes
255 heads, 63 sectors/track, 10198 cylinders, total 163840000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000031b3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 3106815 1552384 83 Linux
/dev/sda2 3108864 163839999 80365568 5 Extended
/dev/sda5 3110912 14583807 5736448 83 Linux
/dev/sda6 14585856 40724479 13069312 83 Linux
/dev/sda7 40726528 155449343 57361408 83 Linux
/dev/sda8 155451392 163839999 4194304 82 Linux swap / Solaris
The size of partitions is given in 512-byte blocks. Unfortunately, we can’t change the unit shown on the list.
7.1. Getting the Partition’s Size in GB
Let’s display partition details, passing its name as an argument to fdisk:
$ sudo fdisk -l /dev/sda7
Disk /dev/sda7: 58.7 GB, 58738081792 bytes
255 heads, 63 sectors/track, 7141 cylinders, total 114722816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sda7 doesn't contain a valid partition table
In this way, we can read the partition size in GB.
7.2. Security Remarks and Availability
We should be aware that providing the partitions list is only a small part of the fdisk‘s capabilities.
Primarily, it’s an interactive tool to create, delete, and modify partitions.
So, if by accident we call sudo fdisk /dev/sda (without -l), then we get into a shell with all fdisk‘s commands available.
Then, if we don’t want to change the partition table, we should type q to quit.
The command is included in the util-linux package.
8. parted to Cope With Big Partitions
Yet another command to manipulate partitions is parted. So, let’s show the usual list of partitions:
$ sudo parted -l
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 83,9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1591MB 1590MB primary ext2 boot
2 1592MB 83,9GB 82,3GB extended
5 1593MB 7467MB 5874MB logical ext4
6 7468MB 20,9GB 13,4GB logical ext4
7 20,9GB 79,6GB 58,7GB logical ext4
8 79,6GB 83,9GB 4295MB logical linux-swap(v1)
Let’s notice that the partitions are identified by number, not by the device name. In addition, the size is neatly formatted with the appropriate unit.
The parted command is an interactive tool similar to fdisk.
However, its remarkable feature is the ability to deal with partitions greater than 2 TB.
We need to install this utility from the equally named package parted.
9. Conclusion
In this article, we presented ways to provide information about the Linux operating system. We started with retrieving the compiled-in kernel version.
Next, we learned how to check the Linux distribution details by applying the LSB concept or using systemd data. We also retrieved similar information by means of the hostnames managing tool.
Finally, we examined a variety of tools destined to query the disk’s partitions system.