If you work on Linux servers, you need to check the disk, CPU, and memory usage on your system. You can easily monitor these resources with simple terminal commands. However, you must decide whether you want to monitor CPU usage, memory usage, or disk usage. All of these terms can be different from each other. As a Linux system administrator, you are responsible for monitoring the performance of your system. In this article, we will learn how to monitor the CPU, RAM, and disk usage of your Linux system.
We have tested all the commands in this article on Ubuntu 22.04. So let’s get started!
Monitor Hard disk Usage on a Linux system
The following command-line tools are used to monitor the hard disk usage:
Open the terminal by pressing ‘Ctrl+Alt+t’ and execute one by one the above-mentioned commands on it.
Use of df command
The ‘df’ is used to report how much disk space is used in a Linux system. When we use a filename with the ‘df’ command, it shows the free space on the disk partition where that file is saved. When the -h attribute is used with this command, it shows you the file and folders lists through which you can calculate the available disk space. Type the following command on the terminal to check available disk space:
Use of du command
The ‘du‘ command is also used for Linux disk usage-related purposes. This command is slightly different from the df command. It displays the disk space that is already consumed by files on a disk instead of displaying available space. The following command can be used on the terminal to check the total used space:
Use of ls command
ls command is so simple and similar to the du command Linux can be used to list all directory contents along with the file size of each.
$ ls -l -h
Check CPU and Memory Usage on a Linux system
The following commands are used to monitor CPU utilization:
Use of top command
The top command is pre-installed on most of the latest Linux distributions that give you deep information about CPU utilization of your system. The top command gives you the live view of your total running services on your system. This command is specifically used to get information about how much memory is using each running process. It also provides you the complete details about CPU and memory utilization. Like a free command, it also displays the cache and buffer’s information. Type the following command to display the live information about CPU and memory:
Use ‘Ctrl+C’ to stop the running process.
Use of htop command
Htop command is not installed by default on the Linux system. Therefore, you can install it by running the following command on the terminal:
$ sudo apt install htop
Type ‘Ctrl + C’ to quit the running process.
Use of mpstat command
Before running mpstat command, you need to install sysstat packages on your system. For this purpose, type the following command that will install the required packages on your system:
$ sudo apt install sysstat
Mpstat command is used to report each available processor activity. If no activity is selected, then the complete CPU utilization summary in the form of a report to be displayed on your Linux system screen. Type the following command to check the all global average CPU activities:
Use of vmstat command
The vmstat command is used to report information about memory, processes, block IO, paging, traps, and CPU activity. Type the following command on the terminal to monitor CPU usage:
Use of sar command
Once the sysstat package is installed, you can also use the ‘sar’ command that is also included in this package. The ‘sar’ command is used to check the CPU utilization after a specified time interval.
Let’s explain with an example, you want to monitor CPU usage after every 5 seconds then, run the below-mentioned command on the terminal:
$ sar 5
Type ‘Ctrl + C’ keys to stop the running process. The average CPU usage will be displayed on the terminal.
You can also limit the command to a specific number of iterations as follows:
$ sar 2 3
In the above command, you will monitor the CPU usage after every 2 seconds for 3 iterations. The following output will show on the terminal:
In this article, we have shown how to monitor the memory, CPU, and Hard disk utilization on Ubuntu 22.04 Linux system. We have implemented different terminal commands on our system that can be used for resource monitoring.