check linux version

The Linux OS has many Distributions and various versions with slight modifications in the kernel versions, a major and minor version identification is crucial to practical administration of a linux server. Linux being open source, the release cycles are continuous resulting in many changes with Longterm release and short term release cycles in some most known and popular linux Distributors like Centos/Debian.. A special mentioning of the generic Desktop PC/laptop based versions of fedora/Ubuntu/Arch/OpenSuse Leap-Tumbleweed Linux where fan following is heavy, the Release cycles are very aggressive with new versions releasing once in for every 2-3 weeks.

In this Section we will see how to check linux version, on most popular Distributions.
This will be handy during the kernel patch process to identify the current linux version.
Lets list our some practical linux commands and scenarios

Checking the Linux OS Version using /etc/os-release

[vamshi@node02 ]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

Checking the Linux Version from the file /etc/issue

This file is being relinquished in latest Systemd variants of Linux, But tends to gives a less adequate information just lying around for aged linux user’s familiarity with systemd versions.

vamshi@node03:/$ cat /etc/issue
Debian GNU/Linux 10 \n \l

Check Linux Version using lsb_release:

The lsb_release prints the (Linux Standard Base) and Distribution information on the Linux host.

[vamshi@node02 cp-command]$ lsb_release -a
LSB Version:    :core-4.1-ia32:core-4.1-noarch
Distributor ID:    CentOS
Description:    CentOS Linux release 7.7.1908 (Core)
Release:    7.7.1908
Codename:    Core

How to check the running kernel version information ?

Check Linux Version information with uname command

[vamshi@node02 cp-command]$ uname -a
Linux node02.linuxcent.com 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Checking the Linux Version using hostnamectl

The command hostnamectl gives the complete information of the underlying architecture, kernel version and the Linux OS name

vamshi@node03:/$ hostnamectl 
Static hostname: node03
Icon name: computer-vm
Chassis: vm
Machine ID: b4adcdb84c724856b577524ebbfa0003
Boot ID: 1a2db0a0ae8b4c5ba86b390c68af7024
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-5-amd64
Architecture: x86-64

How to create Temporary filesystem on Linux

The greatest advantage with a tmpfs is that you can use the faster access Volatile memory to store files instead of Secondary storage system.

Lets head off to the Demonstration using tmpfs.

Create a Directory named test-docs in /mnt

[vamshi@SERVER02 mnt]$ sudo mkdir /mnt/test-docs

We will be using the dd command to dump the data and create a file with given Block size, as below and compare the write speeds on tmpfs (Linux temporary Filesystem) vs conventional storage filesystem.

[vamshi@SERVER02 mnt]$ sudo mount -t tmpfs -o size=1G tmpfs /mnt/test-docs

Output from the mount command:

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G     0  1.0G   0% /mnt/test-docs

Now add the entry to /etc/fstab to make the mount persistent across reboots(We should write How to create an fstab entry in another article)

tmpfs       /mnt/webdocs tmpfs   nodev,nosuid,noexec,nodiratime,size=1G   0 0

With these changes it is going to perform faster system read writes during the runtime.

Please advise caution as this uses the RAM.

Now to test the setting Temporary Filesystem vs Storage Filesystem write speeds

We will demonstrate see about the temporary(tmpfs) filesystem on linux in action and practical examples.

Writing a 500MB file on the same server to a Secondary storage vs Writing it to tmpfs.

Lets us write a single 500MB file and measure its write speed and time it’s operation

[vamshi@SERVER02 mnt]$ time sudo dd if=/dev/zero of=/mnt/test-docs/dump.txt bs=1k count=500000
500000+0 records in
500000+0 records out
512000000 bytes (512 MB) copied, 0.815591 s, 628 MB/s

real    0m0.871s
user    0m0.103s
sys    0m0.760s

 

This operation finished under 0.8 seconds time which is around 100 milliseconds lesser than 1 second and write operation completed with 628MB/s.

Lets check on the file size on the tmpfs mountpoint /mnt/test-docs/

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G 489M  536M 48% /mnt/test-docs

Now lets run the same write operation to the secondary storage filesystem

[vamshi@SERVER02 mnt]$ time sudo dd if=/dev/zero of=/mnt/dump.txt bs=1k count=500000
500000+0 records in
500000+0 records out
512000000 bytes (512 MB) copied, 1.38793 s, 369 MB/s

real    0m1.510s
user    0m0.028s
sys    0m1.227s

The write operation to the disk took twice the time at a write speed of 369MB/s

The space usage on the [/code]/mnt/test-docs/[/code] after the operation is 489MB out of 1024MB.

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G 489M  536M 48% /mnt/test-docs

CONCLUSION:

The write/read speeds to tmpfs were substantially faster compared to the secondary storage block device mount points, The fact of the matter is tmpfs although the clear winner is tmpfs, it cannot be used for persistent data storage as its just sits on top of the memory and in no way offers long term data storage persistence.

So what is a practical usage of tmpfs in linux ?

The practical usage can be storing of the static web content to serve the images,css and js to fasten the request speeds.
The secondary storage as we know for straight persistence and long term data storage capabilities.

Setting hostname in Linux

In the systemd Environment there has been an architectural change and the systemd daemon controlling all the essential processes

root 1 0 0 Apr15 ? 00:00:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 32

We can make use of the hostnamectl command to immediately reflect the system hostname change.


Command to get the current hostname on Linux:

[vamshi@server02 ~]$ sudo hostnamectl 
   Static hostname: server02.linuxcent.com
   Pretty hostname: SERVER02
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2338f55840d640689fc5ac4b356b160c
           Boot ID: 418256281d2f4e11822809dde7c1b09e
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.18.1.el7.x86_64
      Architecture: x86-64

As you can see the current hostname is set to SERVER02

The file /etc/hostname also used to have the same effect but on the cloud systems this is dynamically generated and doesn’t hold true on cloud and tends to get overwritten after reboot.
On the general DataCenter or PC environment we can use the static files to populate the hostnames and make them permanent:

$ cat /etc/sysconfig/network
HOSTNAME=node2.linuxcent.com

But only /etc/hostname file is given importance since the systemd version and updating this ensures persistence.

$ cat /etc/hostname 
node02.linuxcent.com

 

Process to set or change the hostname in Linux Commandline:

The systemd provides a sophisticated command hostnamectl to set the hostname, It will also update the static file /etc/hostname and ensure the changes are permanent across reboots.

$ sudo hostnamectl set-hostname node02.Linuxcent

To get the hostname along with FQDN

[vamshi@server02 ~]$ hostname --fqdn
node02.linuxcent.com
[vamshi@node02 ~]$ sudo hostnamectl status 
   Static hostname: node02.linuxcent.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2338f55840d640689fc5ac4b356b160c
           Boot ID: 33619e39ea4c4900bd848e13d2a6239e
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1062.18.1.el7.x86_64
      Architecture: x86-64

Changing hostname is ubuntu and Debian systems can be done through hostnamectl command

vamshi@debian:~$ hostnamectl 
Static hostname: debian
Icon name: computer-vm
Chassis: vm
Machine ID: b4adcdb84c724856b577524ebbfa0003
Boot ID: 67e1bf27946a4770b8e939f2420d06fd
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-5-amd64
Architecture: x86-64

Control Structures in Linux BASH

The Linux BASH provides many operators to check against test keyword
We are going to explain the Conditional comparison Operators for the Keyword parameter totest or [

For a detailed explanation of the Keyword test or [ Please refer to our Control Structure: Bash If then Else.

 

File Comparison Operators
Condition Explanation
-d “$file” Returns true if the file exists and is a directory
-e “$file” Returns true if the file exists.
-f “$file” Returns true if the file exists and is a regular file
-h “$file” Returns true if the file exists and is a symbolic link

 

String Comparators Condition  Explanation
“$str” = “$str2” True if string $str is equal to string $str2. Not best for integers.
“$str” != “$str2” True if the strings are not equal
-z “$str” True if length of string is zero
-n “$str” True if length of string is non-zero

 

Integer Comparators Condition Explanation
“$int1” -eq “$int2” True if the integers are equal
“$int1” -ne “$int2” True if the integers are not equals
“$int1” -gt “$int2” True if $int1 is greater than $int2
“$int1” -ge “$int2” True if $int1 is greater than or equal to $int2
“$int1” -lt “$int2” True if $int1 is less than $int2
“$int1” -le “$int2” True if $int1 is less than or equal to $int2

 

How to Remove Files and Directories in Linux using Command line

The Remove command in Unix/Linux ecosystem is very powerful and effective one time operation as it is unrecoverable.

To delete different kinds of content various options can be used with the rm command in linux.

In this section , we will demonstrate how to use the go about rm, unlink, and rmdir commands to remove files and directories in Linux

The Linux system treats the files and Directories by identifying them with Inodes.

Deleting a file is a simple operation but the user has to advise a lot of caution.

Sample Syntax

$ rm filename

Now lets Demonstrate deleting a file

Let’s use the -v flag to print the verbose information onto the screen.

[vamshi@linuxcent delete-dir]$ rm -v file1 file2 file3

removed ‘file1’

removed ‘file2’

removed ‘file3’

Using -i option asks the user for interaction and deletes the file upon accepting a “y” as user input.

[vamshi@linuxcent delete-dir]$ rm -i file3
rm: remove regular file ‘file3’? y

Deleting multiple files from the command line.

Using the wildcard * to delete similar extension filenames

[vamshi@linuxcent delete-dir]$ rm -v ./*.pdf

removed ‘./samplePDFfile1.pdf’

removed ‘./samplePDFfile2.pdf’

removed ‘./samplePDFfile3.pdf’

It’s a good practice to use (-v) verbose mode while running rm command.

Now let’s focus on the best practice

[vamshi@linuxcent ]$ alias rm="rm -iv"

Ensure to export the changes to your profile as demonstrated here(Exporting the changes to user login profile permanently)

[vamshi@linuxcent ]$ rm samplefilename.txt
rm: remove regular empty file ‘file10’? y
removed ‘samplefilename.txt’

But One has to always ensure to see the file permissions and if they are currently being used by any user or a program.(fuser and what it does)

You can long list the filename to see the permissions(long listing the files on Linux system)

How to Remove the Directory/s from Linux system?

A command-line utility rmdir exists on Linux to delete the directories. But, it deletes only the Empty directory(ies).

The command rm -d or --dir is also preferred to delete an empty directory

The rm command-line utility also lets you delete the directories and its contents recursively with -r option.

Sample Command Syntax:

[vamshi@linuxcent delete-dir]$ rmdir  sampleDIR1

We can use the -v option to print the verbose information:

[vamshi@linuxcent delete-dir]$ rmdir -v sampleDIR1

rmdir: removing directory, ‘sampleDIR1’

Deleting multiple subdirectories that are empty can be done with -p option, its demonstrated as follows:

[vamshi@linuxcent ]$ rmdir -v -p sampleDIR/subdir/
rmdir: removing directory, ‘sampleDIR/subdir/’
rmdir: removing directory, ‘sampleDIR’

Deleting multiple files from the command line.

[vamshi@linuxcent ]$ rmdir sampleDIR1 sampleDIR2 sampleDIR3

Now let’s shift our focus to rm command which offers the ability to delete the directories and its contents recursively.

[vamshi@linuxcent ]$ rm -v -r sampleDIR3/
removed directory: ‘sampleDIR3/subdir3’
removed ‘sampleDIR3/samplefile3.txt’
removed directory: ‘sampleDIR3/’
[vamshi@linuxcent delete-dir]$ rm -v -r sampleDIR6 sampleDIR7 sampleDIR8
removed directory: ‘sampleDIR6/subdir6’
removed directory: ‘sampleDIR6’
removed directory: ‘sampleDIR7/subdir7’
removed directory: ‘sampleDIR7’
removed directory: ‘sampleDIR8/subdir8’
removed directory: ‘sampleDIR8’

Please also see our post on How to make a file undeletable on Linux