How to find the total number of CPUs in a Linux server?

The Central Processing Units (CPUs) are the physical hardware components that carry out instructions in a computer system. In a Linux server, the term "number of CPUs" corresponds to the count of physical processors installed in the system. Each CPU generally encompasses one or more processing cores, which serve as the individual processing units within the CPU. Certain systems may feature dual-core, quad-core (4 cores), or, in the latest generations, as many as 18 cores.

There are numerous scenarios where you must determine the number of CPUs in a Linux system. This could be necessary when assessing the available cores before creating a virtual machine or to gauge your system's processing power.

How to Verify the Number of CPUs in Your Linux System
You can utilize specific command utilities to access and view the CPU details and relevant information.

  1. lscpu command
  2. /proc/cpuinfo
  3. nproc command
  4. top or htop command
  5. dmidecode command
  6. getconf _NPROCESSORS_ONLN command

lscpu command
The lscpu command serves as the principal tool for presenting information about the CPU, including details like CPU operating mode, vendor ID, model name, CPU family, and more. It retrieves the CPU architecture's information from sysfs and /proc/cpuinfo.

It comes pre-installed on the majority of Linux systems and can be used without the need for any additional options.

lscpu

As you can observe, it presents a plethora of additional information that may only be necessary for some users's needs. In such instances, you can utilize the egrep command in the following manner to filter the output:

lscpu | egrep 'Model name|Socket|Thread|NUMA|CPU\(s\)'

By accessing the contents of the /proc/cpuinfo file

When we execute the "lscpu" command, it retrieves information from both sysfs and /proc/cpuinfo. Hence, it's convenient to obtain all the details directly by running this command in the terminal.
cat /proc/cpuinfo

You can also explicitly display the number of CPUs by executing the following command.
less /proc/cpuinfo | grep processor | wc -l

Determine the Number of CPUs in Linux Using the nproc Command.
Another user-friendly tool for discovering the number of CPUs in your Linux system is the nproc command. By default, without any additional flags, this command provides the CPU count. Execute the command as depicted below to obtain the system's core count.

nproc

Discovering the Number of CPUs in Linux with the Top/htop Command
The Linux "top" command, a process table utility, provides real-time information on memory and CPU resource utilization.

When executed, this command displays running processes and their memory and CPU usage. Execute the "top" command without flags and press the "1" key.

top

You can observe that the "top" command displays four cores: 0, 1, 2, and 3.

Moreover, You can run top with the "-1" flag, like top -1, and it also reveals the number of cores in the system; the result is identical.

htop

By Employing the dmidecode Command
The dmidecode command accesses your system's DMI (Desktop Management Interface) table to fetch information about your hardware. Since it provides an extensive amount of additional information when executed without options, it is recommended to utilize specific flags to obtain the desired output:

sudo dmidecode -t 4 | egrep -i 'core (count|enabled)|thread count|Version'

As observed, it provides the count of physical cores (Core Count) and the total number of cores, including threads. This makes it an ideal output for most users and a reliable method for checking the CPU count in Linux.

By using the "getconf _NPROCESSORS_ONLN" Command:
The "getconf" command is employed to fetch system configuration data, including the number of processors accessible on the system. The "_NPROCESSORS_ONLN" option is used to acquire the count of processors that are presently online and available for use. To utilize the "getconf" command for determining the number of processors on the system, open a terminal window and enter the following command:

getconf _NPROCESSORS_ONLN

This command reveals the CPU cores actively utilized by the Linux operating system.

Conclusion

As demonstrated in this tutorial, Linux offers a variety of approaches for determining the number of cores in your system. These techniques are user-friendly and suitable for individuals of all skill levels. The provided commands are versatile, applicable to all Linux distributions, and can be utilized even if not pre-installed.

Being aware of the number of cores is essential for enhancing system performance and making efficient use of resources. Proficiency in gathering CPU information empowers users to manage and optimize their Linux servers efficiently.


Was this answer helpful?

« Back

chat