Open In App

Configure CGroups in CentOS Linux

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

“CGroups” or “Control Groups” is a feature of the Linux kernel that allows us to control and restrict system resources (like CPU, memory, disk I/O, and network) for individual processes or groups of related processes. One of the main objectives is to manage and control the resource allocation of processes, ensuring fair and efficient usage of resources.

Important Features of CGroups

Administrators can set resource limits for individual processes or groups of related processes using CGroups. This helps avoid system instability or performance degradation caused by a single process or group of processes using up all system resources. It is organized in a hierarchical structure, allowing for the creation of nested groups. This makes it easy to apply different resource constraints at various levels of the hierarchy. It provides different types of resource controllers for managing a specific set of resources. Some common controllers include:

  1. CPU: manages CPU time usage.
  2. memory: controls memory usage.
  3. blkio: manages block I/O (disk I/O) access.
  4. net_cls: tags network packets with a class identifier.

Some Common Uses of CGroups

In a cloud environment:

CGroup is useful in cloud computing environments where multiple users share the same physical infrastructure. For instance, to avoid one customer’s workload adversely affecting others, a cloud provider that hosts virtual machines (VMs) for multiple customers can use CGroups to assign CPU, memory, and other resources to each VM. In this way, it helps ensure fair resource allocation.

Resource scarcity prevention:

It can be explained with an example, let’s suppose a process uses too much CPU or memory, leading to degraded performance for other processes on the system. Administrators can guarantee that no single process monopolizes resources by imposing restrictions on resource usage through CGroups, thereby preserving a more responsive and stable system.

Energy Efficiency in Data Centres:

In Data Centres, power consumption can also be optimized by using CGroups to control CPU frequency and other power-related settings about the workload. This is very helpful for large-scale data centers, where cutting energy costs can result in significant savings.

Steps to Configure CGgroup in CentOS

Configuring CGroups in CentOS typically involves installing CGroup, creating a CGroups configuration file, and starting the cgconfig services. Follow the steps given below to configure CGroup in CentOS.

Step 1: Checking CGroups Support:

The very first thing we have to do is to confirm whether the kernel supports CGroups. Most of the modern kernels do support CGroup. To check whether the CGroup is supported or not type the command given below.

  grep -i cgroup /proc/mounts

If the kernel supports it, we can see entries like cgroup on /sys/fs/cgroup in the output.

Output:

Screenshot-2023-11-24-233046

Checking Support

Step 2: Install the libcgroup Package:

We need to install libcgroup package to manage CGroup. Type the command given below to install the required package.

sudo yum install libcgroup

Output 1:

Screenshot-2023-11-24-233734

Installing CGroup

Output 2:

Screenshot-2023-11-24-233815

Installed Successfully

Step 3: Enabling and Starting the Service:

Now the next step is to start and enable the cgconfig service. It is the responsibility of cgconfig service, to read and implement the requested settings from the CGroups configuration file, which is typically found at /etc/cgconfig.conf. To start the cgconfig service type the command given below.

sudo systemctl start cgconfig

The cgconfig service can be enabled to start automatically at boot time. Systemd will launch the cgconfig service upon system startup, guaranteeing that the CGroups configuration is implemented consistently. To enable the service at boot, type the command given below.

sudo systemctl enable cgconfig

Output:

Screenshot-2023-11-24-234212

Starting and Enabling the Service

Step 4: Creating and Editing CGroup Configuration File:

Now we can also create a configuration file for CGroups. The default configuration file is usually located at /etc/cgconfig.conf. To open the default configuration file type the command given below.

sudo nano /etc/cgconfig.conf

This command will open the Nano text editor, allowing you to edit the cgconfig.conf file

Output:

Screenshot-2023-11-24-234406

Nano Text Editor

Now we can make the necessary changes to the configuration file. We can specify group names, limit resources to groups, and other settings as well. For example, we can write the following in the cgconfig.conf file.

group <group_name> {
cpu {
cpu.shares="512";
}
memory {
memory.limit_in_bytes="4G";
}
}

In this example, we are specifying CPU and memory resource constraints using the “cpu.shares” and “memory.limit_in_bytes” parameters respectively. The amount of CPU time that a CGroup receives in relation to other CGroups on the system is determined by its CPU shares. The group will also be restricted from using more than the specified amount of memory.

Output:

Screenshot-2023-11-25-004156

Editing Configuration File

Once the configuration has been modified, hit Ctrl + O to save the changes to the file then Enter to validate the filename, and then Ctrl + X to close the Nano editor.

Step 5: Restart the CGroups Service:

Once the modification of the configuration file is completed, restart the service. To restart the CGroup service type the command given below.

sudo systemctl restart cgconfig

Step 6: Verifying the CGroup:

To verify the CGroup service “cgexec” command can be used. The cgexec command is used to execute a command within a specified Control Group (CGroup) with specific resource constraints. To run a process within a CGroup type the command given below.

cgexec -g cpu,memory:<group_name> <command>

Replace the <group_name> with the actual group name that we want to use and which must be present in cgconfig.conf file and also replace <command> with the actual command we want to run. For example, we run a simple command.

Output:

Screenshot-2023-12-01-163754

Running process within a CGroup

In this example:

  1. cgexec -g cpu,memory:kaal: Executes the command within the specified CGroup (here kaal) with CPU and memory constraints.
  2. ls /: The command lists the contents of the root directory.

Step 7: Checking the Status:

We can also check the status of CGroup. We can use the “cat” command to show the PIDs currently in the specified CGroup. To check the status type the command given below.

cat /sys/fs/cgroup/cpu/<group_name>/tasks

Just replace the <group_name> with the actual group name (here kaal) that we want to use and which must be present in cgconfig.conf file. The above command is used to display the list of process IDs (PIDs) associated with a specific Control Group (CGroup).

Output:

Screenshot-2023-12-01-165158

Checking the Status

Conclusion:

The configuration of Control Groups (CGroups) involves defining resource constraints for specific groups of processes, allowing for smooth control over system resource allocation. The most important file where CGroup specifications are given is cgconfig.conf, which contains settings for memory and CPU limitations. Since It ensures optimal performance and stability in contexts with various workloads, this level of resource isolation is especially valuable.



Similar Reads

Linux Virtualization : Resource throttling using cgroups
In Linux Virtualization – Chroot Jail article, we discussed about kernel namespaces and process jailing. To understand this article, you may not need to read the earlier one, but I strongly suggest that you go through it once before diving into resource throttling. It should help tremendously in understanding what's going on. What are cgroups? cgro
9 min read
How to configure network interfaces in CentOS?
A network interface is a link between a computer and another network(Private or Public). The network interface is basically a card which is known as NIC or Network Interface Card, this does not necessarily have to be in a physical form instead, it can be inbuilt into the software. If we take the example of some loopback addresses such as 127.0.0.1(
5 min read
Basic CentOS Linux Commands in linux
CentOS is a free and open-source operating system that aims to provide a stable reliable, and community-supported platform for servers and other enterprise applications. In this article, we will be covering CentOS Linux basics commands and functions of CentOS and also we will look into the advanced commands. These CentOS commands allow you to manag
4 min read
How to reset the root password of RedHat/CentOS Linux
RedHat Linux is a worldwide used OS in servers and networking devices. Red Hat lists Adobe, Ampersand and many among the top 500 fortune companies. They list Cisco, Dell, HP, and IBM as partners. Resetting the root password of RHEL is not a difficult task but rather you need to know 4 commands and you are into the root account! Root account in Linu
2 min read
How to Install Cockpit on Linux CentOS 7?
The cockpit is a free, open-source Linux Server Management Tool which provides you the facility to control your server using the mouse on the web-interface. The cockpit can be used to perform any server related task such as start containers, storage administration, and network configuration and much more. It has a very beautiful user interface alon
3 min read
How to Add New Disk in Linux CentOS 7 Without Rebooting
Let's Say we hosted the DataBase on our Server Initially with 500GB given space and as we know Database should run 24/7 because the user's using it. One day it consumes space about 400GB only 100GB left Now we want to Increase the Space at the runtime only and In the case of static partitioning we need to restart the server but If we restart the se
3 min read
How to Upgrade Linux Kernel on CentOS 7
The kernel is a central component of an operating system that manages the operations of the computer and hardware. It basically manages operations of memory and CPU time. It is a core component of an operating system. Kernel acts as a bridge between applications and data processing performed at the hardware level using inter-process communication a
2 min read
How to Compile Linux Kernel on CentOS 7
The kernel is the core of a computer's operating system that has complete control over the system and provides basic services for all other parts. The default kernel available in any distribution cannot be customized. We cannot enable or disable any feature on it. Running a custom-compiled Linux Kernel becomes very helpful in cases when the hardwar
3 min read
How to Audit Linux Process Using ‘autrace’ on CentOS/RHEL
Autrace is a command line tool that is used for the purpose of auditing processes on Linux. The audit rules which are created by autrace are stored in the /var/www/audit/audit.log file. Before autrace can work perfectly all the previous audit logs must be deleted. The syntax of autrace is given below: Syntax: autrace -r program program-args Note: H
3 min read
How to Install CentOS
CentOS is a popular open-source Linux distribution aimed at servers and provides compatibility with Red Hat's RPM package manager. It is built with the goal of providing a stable operating system that provided great compatibility with the upstream RHEL (Red hat enterprise Linux) CentOS is therefore one of the best free Linux distributions to utiliz
3 min read