- 3 Ways to check CPU Cores in Linux
- Methods to get number of CPU cores in Linux
- Understanding CPU cores in Linux
- Understanding CPU cores and hyper threading in Linux
- Understanding threads per core in Linux
- Check CPU cores with lscpu command in Linux
- Check CPU cores from /proc/cpuinfo File in Linux
- Check CPU cores with dmidecode command in Linux
- How to Find How Many Cores There are in Linux Systems
- The lscpu Command
- Check the Number of Cores in Linux With the nproc Command
- The hwinfo Command
- Finding the Number of CPUs in Linux Using the Top Command
- Checking How Many Cores Using the htop Command
- Reveal Number of Cores in Linux by Reading /proc/cpuinfo
- Conclusion
- About the author
- David Adams
- How to find the number of CPU cores including virtual?
- 6 Answers 6
3 Ways to check CPU Cores in Linux
In this blog post, we will discuss 3 different ways to check the number of cpu cores in Linux. This is useful information to have if you are trying to optimize your system for performance.
Each method has its own advantages and disadvantages, so be sure to read through all of them before choosing the one that is best for you. Let’s get started!
Methods to get number of CPU cores in Linux
The following commands can be used to check the number of CPU cores in Linux.
- cat /proc/cpuinfo
- lscpu
- sudo dmidecode -t 4
- nproc
- hwinfo
- getconf _NPROCESSORS_ONLN
Understanding CPU cores in Linux
When you are trying to optimize your system for performance, it is important to understand the concept of CPU cores. In Linux, a CPU core is essentially a processor.
This means that, if you have multiple CPU cores, your system can process multiple tasks at the same time. This can result in a significant increase in performance.
But the number of CPU cores is not the same as the number of processors.
Understanding CPU cores and hyper threading in Linux
Hyper threading is a feature that was first introduced in Intel processors. It allows a single CPU core to process multiple tasks at the same time by creating virtual cores.
This can be useful if you are trying to optimize your system for performance, as it can increase the number of tasks that can be processed simultaneously.
However, it is important to note that not all processors support hyper threading.
Understanding threads per core in Linux
In Linux, threads per core is a way of measuring the number of threads that can be processed simultaneously on a single CPU core. This is different than the number of CPU cores, as it measures the number of tasks that can be processed at the same time. There are 2 threads per core in most of the current CPU architecture.
- The number of CPU cores = sockets * Core per socket
- The number of vCPU = CPU cores * threads per core
Check CPU cores with lscpu command in Linux
The best way to check the number of CPU cores in Linux is using the lscpu command. Open the terminal and run this command lscpu. It gives you a lot of information about cpu, including the number of cores, the vendor_id, model name etc.
echo «Cores = $(( $(lscpu | awk ‘/^Socket\(s\)/< print $2 >‘) * $(lscpu | awk ‘/^Core\(s\) per socket/< print $4 >‘) ))»
It will list the number of CPU cores on your system.
The number of CPU cores = sockets * Core per socket
sockets=$(lscpu | awk '/^Socket\(s\)/< print $2 >') Core per socket=$(lscpu | awk '/^Core\(s\) per socket/< print $4 >')
We can use this command to get the number of CPU cores.
echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/< print $2 >') * $(lscpu | awk '/^Core\(s\) per socket/< print $4 >') ))"
The lscpu command displays either one single CPU family or all families detected by querying sysfs (on Linux kernels with CONFIG_SYSFS). It supports Intel x86, AMD Family 16h/18h, and ARM processors.
In the following examples, the number of CPU cores is 1 * 2=2
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 2 Core(s) per socket: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 85
From this example, we can get that the number of CPU cores are 2 * 8 =16
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\(' CPU(s): 32 Thread(s) per core: 2 Core(s) per socket: 8 Socket(s): 2
Check CPU cores from /proc/cpuinfo File in Linux
Another way to check the number of cpu cores in Linux is by looking at the /proc/cpuinfo file. Open the terminal and run this command: cat /proc/cpuinfo. It will list a lot of information about the cpu, including the number of CPU cores on your system.
This file also contains CPU vendor_id, model name etc. To view it, just type the following command into your terminal:
cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 85 model name : Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz stepping : 7 microcode : 0x500320a cpu MHz : 3109.590 cache size : 36608 KB physical id : 0
check the physical number of CPU:
cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
cat /proc/cpuinfo |grep "processor"|wc -l
Check CPU cores with dmidecode command in Linux
dmidecode command can be used to check the number of cpu cores in Linux. It is a command-line tool to retrieve device data that the Linux kernel exposes through MCU. To run it, just type the following command into your terminal:
$ dmidecode -t processor | grep "Core Count"
This command is very useful if you want to get more information about your hardware info, such as CPU or memory.
dmidecode --type processor dmidecode --type memory
As you can see, there are three different ways to check the number of cpu cores in Linux. Each method has its own advantages and disadvantages, so be sure to choose the one that is best for you.
If you want a simple way to check the number of cpu cores, then I recommend using the /proc/cpuinfo file. If you want more information about your system’s cpu, then I recommend using the lscpu command. Whichever method you choose, be sure to use it to optimize your system for performance. Thanks for reading
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.
How to Find How Many Cores There are in Linux Systems
After reading this article, you will be able to check the number of cores through different available commands for all Linux systems. Some of the explained commands are also useful in BSD systems.
All instructions described in this tutorial include screenshots for every user to understand easily, independently of his knowledge level.”
To check the number of CPUs in Linux, this tutorial covers the following commands:
The lscpu Command
The lscpu command reads both the sysfs file system and the /proc/cpuinfo file. By executing lscpu, we can see how many cores there are in our Linux system. Additionally, this command shows the information on CPU caches, model, and sockets, among other useful information.
This section explains how to use lscpu to print all CPU information and how to show the number of CPU cores specifically.
To display all CPU information, run lscpu without additional flags, as shown in the figure below.
By using egrep, you can print only the number of cores, as shown below.
The lscpu command is the most widely used to check information on CPUs.
Check the Number of Cores in Linux With the nproc Command
Another simple to use tool to reveal the number of cores in your Linux system is the nproc command.
By default, without flags, this command displayed the core number. Just run the command as shown below for it to return the number of cores in the system.
As you can see, the output is extremely simple.
The hwinfo Command
The hwinfo Linux command is another option to check the number of cores on the system.
The hwinfo command is not included by default on Linux systems. To install it on Debian-based Linux distributions, use the apt packages manager as shown in the screenshot below.
Once installed, run the hwinfo command followed by the flags shown in the following figure.
As you can see, the number of cores is returned line by line.
The hwinfo command can be used to print information about all hardware in the system.
Among its features, you can export the output to a file. To export the number of cores to a file, use the following syntax.
Note: The exported file name is arbitrary; you can name it as you want.
You can use the less command to read the exported file.
As you can see below, the file content is the same as the previous output.
To show all system information with hwinfo, run it as shown in the following figure.
As shown in the screenshot above, the hwinfo command is not user-friendly when displaying information on all the hardware. For a more user-friendly view, use the –short flag to specify the hardware you want to check, as shown in the first example of the hwinfo section.
Finding the Number of CPUs in Linux Using the Top Command
The Linux top (table of processes) command is a task manager that prints real-time Memory and CPU resource usage.
This command prints executing processes and memory and CPU consumption. When used with the -1 flag, it also shows the number of cores in the system.
Run the following command to start the top utility showing how many cores are present in the system.
As you can see, the top command shows 4 cores: 0,1,2, and 3.
Additionally, you also can run the top command without flags and then press 1; the result is the same.
Checking How Many Cores Using the htop Command
The htop command is similar to the top command previously explained. But it has some advantages over the top. The htop user interface is colorful, therefore more user friendly, htop also supports scrolling,
Contrary to the top, to see how many cores are in the system, you don’t need to use flags. Run the command below to see how htop displays the cpu information.
Reveal Number of Cores in Linux by Reading /proc/cpuinfo
Another way to see the number of cores in Linux is by reading the /proc/cpuinfo file. You can use different commands to read this file, like less, cat, more, etc.
To read the cpuinfo file, run the command shown below.
The number of cores is shown in the siblings line, as pointed out by the white arrow in the screenshot below.
You also can specifically show the number of cores by running the command below.
As you can see below, there are 4 cores in the system.
That’s all; with all commands described above, you have different ways to check the number of cores.
Conclusion
As shown in this tutorial, under Linux, there are many methods to check the number of cores on the system. All methods are easy to apply by any user, independently of the expertise level. The above commands are universal and available for all Linux distributions, even when not installed by default with the system. Learning the number of cores is important, for example, when assigning processor resources to virtual machines. Every Linux user must know how to gather information about the hardware.
Thank you for reading this tutorial explaining how to find how many cores there are in the system. Keep following LinuxHint for more Linux professional tutorials and tips.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How to find the number of CPU cores including virtual?
How do I find out the number of cores my CPU has, including virtual cores (hyper threading cores) using the command line?
6 Answers 6
cat /proc/cpuinfo | grep processor | wc -l
To check the number of cores !
cat /proc/cpuinfo | grep 'core id' core id : 0 core id : 1
Or lscpu will show you all output:
lscpu Architecture: i686 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 2 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 15 Model: 4 Stepping: 7 CPU MHz: 2792.992 BogoMIPS: 5585.98 L1d cache: 16K L2 cache: 1024K
nproc is also useful in scripts depending on the number of cores available to it. E.g. make -j$(nproc) .
To add to the existing answers, you can determine information about Intel’s HyperThreading by looking at the «siblings» line in /proc/cpuinfo. The example below is from a 2 socket machine. It shows the CPU has 6 cores but 12 «siblings». On Intel CPUs this means HyperThreading is enabled and there are 6 physical cores.
processor : 23 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2430 v2 @ 2.50GHz stepping : 4 microcode : 0x428 cpu MHz : 1599.707 cache size : 15360 KB physical id : 1 siblings : 12 core id : 5 cpu cores : 6 apicid : 43 initial apicid : 43 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 5005.20 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management:
dmidecode is also useful for determining what hardware a Linux system is running on.