Find Distro and Kernel Information
Sometime, you may find that you are working in a Linux environment you are not familiar with, especially if you are logging in remotely and are only seeing a terminal. It can therefore be useful to be able to find some information on the system you are working on.
The /etc folder contains information on the release but the actual names of these files can vary depending on the distro. However, they normally end in release so we can find out what their names are with the command
$ls -lah /etc/*release
Here, we are using the wildcard to see files with any characters but that end in release and on our CentOS VM we can see four files as follows:
/etc/centos-release /etc/os-release /etc/redhat-release -> centos-release /etc/system-release -> centos-release
These are two files in fact, with two hard links to the file centos-release. This means that we could so something like
$cat /etc/centos-release
or we could do either of
$cat /etc/redhat-release
$cat /etc/system-release
and the result in each case would be the same. We see that the result is that we can see we are using CentOS Linux release 7.7.1908 (Core).
The file, os-release, contains some additional information and we can display the contents of this file with a cat command or we could simply display all of the relevant information by making use of the wildcard again with
$cat /etc/*release
Note that the second file may appear in the /usr folder rather than in the /etc folder but if that is the case, we would still expect to see at least one hard link in the /etc folder pointing to that file. This means that the command we used above to see all of the release information would still work.
If we want to know the version of the kernel, we can get this with the uname command (with the a option for all)
$uname -a
This displays the name of the distro (with less detail than we saw before), the kernel version (which in this case is 3.10.0-1602.1.1.el7.x86_64) and some system architecture information.
Just as an aside, I don’t know how easy it is to upgrade the kernel but the latest version is 4.14 and we are using 3.10.0 so I am curious to know if a yum update would have any effect on this so I am running the command
$sudo yum upgrade
Now, I will run uname again and the version of the kernel is still 3.10.0. In fact, it turns out that the process for upgrading the kernel is more complex and is detailed on the How to Forge website. After finishing this course, I will go ahead and upgrade the kernel and detail the process in Appendix B.
Find System Hardware and Disk Information
As in the previous section, the chances are that if you are working on your own Linux system or VM, you will have a good idea of its hardware resources. If you are working on a remote system, you might not have this information so it can be useful to be able to find out what resources it has.
We will start by looking at how much RAM is available which we can do that with the free command and we will use the -h option to get the results in a human-readable format.
$free -h
This tells me that my CentOS VM has 3.7GB available with 2GB reserved for the swap file, 739MB in use and 1.1GB free.
Next, we want to take a look at the processor and this information is stored in a file called cpuinfo which is in the /proc folder so we can display this with a simple cat command
$cat /proc/cpuinfo
This shows quite a lot of information about each of the available processors (2 in this case). This shows me that the CPU is an Intel i7-4790 running at 3.60GHz and with 2 cpu cores.
We can use the df command for information on disk usage, again we will use the -h option for human readable results.
$df -h
This gives us data on several different volumes but the most interesting is probably the root folder (or /) since this represents the file system which you would most likely be using space on when installing software such as applications or updates. We can also see where the volume is mounted which can be quite useful information.
We can also use du to show how much space is taken up by various users and folders.
$sudo du / hd1
We are using sudo here because as an ordinary user, I can’t see into all of the folders at the root level. The options are h for human readable and d for the number of levels to go down to. If we use 1 for the depth, we will only see usage of the folders in root but not any of the subfolders within these folders.
If we increase the depth, even to 2, there is a dramatic increase in the results we get so we probably wouldn’t go above 1 if we are looking at the root folder.
A common usage of du is to look at the space used by a particular user and which of their folders is taking up most space so we would do something like
$du home/philip -hd2
Note that I didn’t use the ~ character to represent the home folder although in this case
$du ~ -hd2
would have exactly the same effect. This is because I am the only user on this machine but as an administrator, you would be more likely to be investigating the disk usage of a user other than yourself so you can use /home/username where you would replace the username with the appropriate name of the user.
In all likelihood, you would use d1 and then when you have found which folder is using most space, you can navigate to it or add its name to the command in order to check the size of folders within that folder.
A procedure may go something like this.
• Navigate to the home folder (with cd ~). • Use du . hd1. This will show a list of users (or rather, user directories). In my case, there is only one with my username which is philip and it is using 204MB of space. • Navigate to this folder with cd philip and run the command again. Here I can see that the documents folder is using 4.2MB of space. • We can continue this digging deeper into the folder structure as required and don’t forget, you can always amend the path in the command rather than navigating to the folder so we can run the command at step 3 as du ./philip -hd1 rather than navigating to the folder.
It is also worth noting that I didn’t use sudo here since access is not a problem but if you are looking at folders belonging to another user, sudo would most likely be required.
We can also explore what hardware the system has with the lshw (list hardware command) which produces a lot of output which you would probably want to pipe into either a command like less or to a file. In this case, we will pipe it to less:
$lshw | less
If we want some information on networking, we can use the ip command with the a option, the correct usage is
$ip a
This shows the ip address for each network adapter.
Managing Software with Packages
Distros based on or derived from Debian use the APT package manager. Distros based on or derived from RedHat (including CentOS) use the YUM package manager. There are other package managers, Feodra uses DNF, SUSE uses YaST and Arch uses Pacman.
Most package managers work more or less the same way and allow you to do things like search for a package, install or uninstall a package or update a package.
In the case of YUM, a cheat sheet is available from RedHat.
A similar cheat sheet for APT can be found here.
The course video has been using Ubuntu which uses the APT package manager whereas I have been using CentOS which uses YUM so I will follow the examples on an Ubuntu VM and then try to recreate them on CentOS.
For this example, we will locate and install a package called tree. We will assume that we do not know the exact name of the package and so we will search for it with
$apt search tree
This generates a lot of results so we need to scroll through them and we find that there is a package called tree and alongside the package name we see some details about the version (this package is fir Bionic because we are using the Bionic version of Ubuntu) and a brief description which helps us to be sure this is the right package.
We can also use the show command to get more information on the package
$apt show tree
Before we install the package, we might try to run it first – it may already be installed!
$tree
As well as confirming that the package is not installed, this can also generate suggestions for installation so is often a useful tactic.
The next step is to update our package repositories with
$sudo apt update
Note that we need sudo access for the actual installation of software. Once the update is complete, we can install the package we want with
$sudo apt install tree
We can then confirm that the package has installed successfully by running it! Now we will try to do the same thing on CentOS using the YUM package manager.
Trying to run the tree command does not work but also doesn’t offer any hints so we will try a search with
$yum search tree
This yields fewer results than we saw on Ubuntu but we can see the package name is the same. There is less info for the package as well so we can get more info using the yum equivalent of the show command which is
$yum info tree
Next, we will update our packages with
$sudo yum update
Finally, we will install the package with
$sudo yum install tree
In addition to installing the software, this command will also install additional items such as the man pages.
It is worth noting that on the Ubuntu system, the apt update is part of a two step process and has to be followed by apt upgrade to actually download and install the updated packages whereas yum update does the download and install without needing a second step.