VOOZH about

URL: https://www.geeksforgeeks.org/linux-unix/configure-cgroups-in-centos-linux/

⇱ Configure CGroups in CentOS Linux - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Configure CGroups in CentOS Linux

Last Updated : 19 Nov, 2025

CGroups is a Linux kernel feature used to limit, manage, and monitor system resources for processes. It helps ensure fair resource usage and prevents any single process from overloading the system.

  • Works for individual processes or grouped processes
  • Ensures stable and efficient system performance
  • Supports nested grouping through a hierarchical structure

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.

Command:

 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.

Command:

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.

Command:

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.

Command:

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.

Command:

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.

Command:

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.

Command:

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:

  • cgexec -g cpu,memory:kaal: Executes the command within the specified CGroup (here kaal) with CPU and memory constraints.
  • 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

Important Features of CGroups

1. Resource Limiting

  • Allows setting limits on CPU, memory, and I/O
  • Prevents resource exhaustion by any single process

2. Hierarchical Structure

  • Organizes processes in nested groups
  • Enables applying constraints at multiple levels

3. Resource Controllers

  • CPU: Manages CPU time allocation
  • memory: Controls and restricts RAM usage
  • blkio: Regulates disk I/O operations
  • net_cls: Assigns class identifiers to network packets

Common Uses of CGroups

These are the common use cases of CGroups in modern computing environments:

  • Cloud Environments: Allocates CPU, memory, and I/O to each VM, ensuring fair resource usage and preventing one tenant from affecting others.
  • Resource Scarcity Prevention: Stops processes from overusing CPU or memory, maintaining system stability and preventing performance issues.
  • Energy Efficiency in Data Centres: Controls CPU frequency and power settings based on workload to reduce energy consumption and operational costs.
  • Container Resource Management: Ensures containers (Docker, Podman, Kubernetes) receive controlled and predictable resources for smooth orchestration.
  • Multi-User Systems: Prevents individual user applications from consuming excessive resources, ensuring fair usage across all logged-in users.
Comment

Explore