• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Enable/Disable CPUs (Limiting CPU count) in CentOS / RHEL

by admin

Question

What is the procedure to disable/enable processors (on the fly) on systems with multiprocessors? How to restrict server to “N” number of CPU’s only ?

Answer

There are three ways to restrict number of CPU’s in CentOS/RHEL.

  • Using maxcpus parameter (RHEL/CentOS 6)
  • Using nr_cpus parameter (RHEL/CentOS 6,7)
  • Disabling CPU Online (RHEL/CentOS 6,7)

1. Using maxcpus parameter

This method works with RHEL/CentOS 6 systems. It may fail if you use it in RHEL/CentOS 7 systems. Although in newer version of RHEL 7 system this bug has been fixed.

You can add the kernel parameter maxcpus=N in /boot/grub/grub.conf or to the kernel line at boot time. For example to limit the server to use only 2 CPU’s use the below entry in the file

# vi /boot/grub/grub.conf
...
title Red Hat Enterprise Linux Server (2.6.18-238.el5)
    root (hd0,0)
    kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet maxcpus=3
    initrd /initrd-2.6.18-238.el5.img
NOTE: It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.

When maxcpus is used, it will take the CPUs from all the available physical CPUs. For example on a system with two dual core CPUs, maxcpus=2 will take one CPU from each physical CPUs. To know the physical CPU IDs in use:

# cat /sys/devices/system/cpu/cpu*/topology/physical_package_id

2. Using nr_cpus parameter

a. For CentOS/RHEL 6
Add kernel parameter nr_cpus=N in /boot/grub/grub.conf or to the kernel line at boot time. For example, Below entry will restrict server to only 2 CPU’s.

# vi /boot/grub/grub.conf
title Red Hat Enterprise Linux Server (2.6.18-238.el5)
    root (hd0,0)
    kernel /vmlinuz-2.6.18-238.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet nr_cpus=2
    initrd /initrd-2.6.18-238.el5.img

b. For CentOS/RHEL 7
1. For RHEL 7 systems add the nr_cpus=N parameter to the “GRUB_CMDLINE_LINUX” line in “/etc/sysconfig/grub” as shown below.

# cat /etc/default/grub 
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="serial console"
GRUB_SERIAL_COMMAND="serial --speed=115200"
GRUB_CMDLINE_LINUX="console=ttyS0,115200 console=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2"
GRUB_DISABLE_RECOVERY="true"

2. Use the grub2-mkconfig command to regenrate the /boot/grub2/grub.cfg file.

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.21.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.21.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.17.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.17.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.11.6.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.11.6.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.11.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.11.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-f9afeb75a5a382dce8269887a67fbf58
Found initrd image: /boot/initramfs-0-rescue-f9afeb75a5a382dce8269887a67fbf58.img
done

3. Verify the entry of the nr_cpu parameter in the grub configuration file.

# grep linux16 /boot/grub2/grub.cfg
	linux16 /boot/vmlinuz-3.10.0-693.21.1.el7.x86_64 root=UUID=0f790447-ebef-4ca0-b229-d0aa1985d57f ro console=ttyS0,115200 console=tty0 vconsole.font=latarcyrheb-sun16 crashkernel=auto nr_cpus=2 
...

3. Disabling CPU Online

Disabling CPU cores
1. At runtime it is possible to disable cpu cores with the following commands. For example for a 4 core system, we can disable 3 CPUs as shown below.

# echo 0 > /sys/devices/system/cpu/cpu3/online
# echo 0 > /sys/devices/system/cpu/cpu2/online
# echo 0 > /sys/devices/system/cpu/cpu1/online

2. To verify if you have disable 3 cores and left with only 1 core enabled, use the below command.

# grep "processor" /proc/cpuinfo
processor	: 0

Enabling CPU cores back
1. The cpu cores can be reactivated again by below command.

# echo 1 > /sys/devices/system/cpu/cpu3/online
# echo 1 > /sys/devices/system/cpu/cpu2/online
# echo 1 > /sys/devices/system/cpu/cpu1/online

2. Verify again for 4 core enabled cores in /proc/cpuinfo.

# grep "processor" /proc/cpuinfo
 processor       : 0
 processor       : 1
 processor       : 2
 processor       : 3
NOTE: These settings are not persistent across reboot.

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. Linux OS Service ‘avahi-daemon’
  2. viewnior Command Examples in Linux
  3. How to Install NTP Service and Client in CentOS/RHEL 8
  4. lvs: command not found
  5. updatedb: command not found
  6. keyctl: command not found
  7. ansible-pull – Pull ansible playbooks from a VCS repo and executes them for the local host
  8. Oracle Database Environment Variables and Their Functions
  9. retroarch Command Examples in Linux
  10. wifi-menu: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright