4 ways to find the number of CPU cores in Linux

In my previous post, we discussed how to find a list of the most consumed processes in Linux. We will now discuss how to find the number of CPU cores in Linux.

Question: How to find the number of CPU cores in Linux?

  • Using the Proc Filesystem

You can use the proc filesystem to find the number of CPU cores in Linux. This routine is a pseudo-filesystem that monitors the execution environment.

In order to get cpu information, you just need to cat “/ proc / cpuinfo” in proc. This gives detailed information about the processor such as VENDOR_ID, CPU family, model, CPU MHz, etc. as shown below:

[email protected]:~# cat /proc/cpuinfo                                                                                                                            
processor       : 0                                                                                                                                           
vendor_id       : GenuineIntel                                                                                                                                
cpu family      : 6                                                                                                                                           
model           : 13                                                                                                                                          
model name      : QEMU Virtual CPU version 2.5+                                                                                                               
stepping        : 3                                                                                                                                           
microcode       : 0x1                                                                                                                                         
cpu MHz         : 2394.454                                                                                                                                    
cache size      : 4096 KB                                                                                                                                     
physical id     : 0                                                                                                                                           
siblings        : 1                                                                                                                                           
core id         : 0                                                                                                                                           
cpu cores       : 1                                                                                                                                           
apicid          : 0                                                                                                                                           
initial apicid  : 0                                                                                                                                           
fpu             : yes                                                                                                                                         
fpu_exception   : yes                                                                                                                                         
cpuid level     : 4                                                                                                                                           
wp              : yes                                                                                                                                         
flags           : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pse36 clflush mmx fxsr sse sse2 syscall nx lm rep_good nopl pni cx16 hypervisor la
hf_lm abm                                                                                                                                                     
bugs            :                                                                                                                                             
bogomips        : 4788.90                                                                                                                                     
clflush size    : 64                                                                                                                                          
cache_alignment : 64                                                                                                                                          
address sizes   : 46 bits physical, 48 bits virtual                                                                                                           
power management:                                                                                                                                                                                                                                                                                      
-----OUTPUT TRUNCATED---------------------------

Hence, to find the number of CPU cores in Linux with exact details, use the following command:

[email protected]:~# cat /proc/cpuinfo|grep processor                                                                                                             
processor       : 0                                                                                                                                           
processor       : 1                                                                                                                                           
processor       : 2                                                                                                                                           
[email protected]:~# cat /proc/cpuinfo|grep processor|wc -l                                                                                                       
3

In this case, we got 3 processors, a number in the range from 0 to 2.

  • Using the lscpu command

You can also find the number of CPU cores in Linux with the lscpu command.

[email protected]:~# lscpu                                                                                                                                        
Architecture:          x86_64                                                                                                                                 
CPU op-mode(s):        32-bit, 64-bit                                                                                                                         
Byte Order:            Little Endian                                                                                                                          
CPU(s):                3                                                                                                                                      
On-line CPU(s) list:   0-2                                                                                                                                    
Thread(s) per core:    1                                                                                                                                      
Core(s) per socket:    1                                                                                                                                      
Socket(s):             3                                                                                                                                      
NUMA node(s):          1                                                                                                                                      
Vendor ID:             GenuineIntel                                                                                                                           
CPU family:            6                                                                                                                                      
Model:                 13                                                                                                                                     
Model name:            QEMU Virtual CPU version 2.5+                                                                                                          
Stepping:              3                                                                                                                                      
CPU MHz:               2394.454                                                                                                                               
BogoMIPS:              4788.90                                                                                                                                
Hypervisor vendor:     KVM                                                                                                                                    
Virtualization type:   full                                                                                                                                   
L1d cache:             32K                                                                                                                                    
L1i cache:             32K                                                                                                                                    
L2 cache:              4096K                                                                                                                                  
NUMA node0 CPU(s):     0-2                                                                                                                                    
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pse36 clflush mmx fxsr sse sse2 syscall nx lm rep_good nopl pni cx16 hypervis
or lahf_lm abm

The above example clearly mentions the number of processors 3 opposite the heading “CPU (s)”.

  • Using the NPROC command

NPROC command can help find the number of CPU cores in Linux directly without grep or calculation, and shown below.

[email protected]:~# nproc                                                                                                                                        
3

  • Using the dmidecode command

The dmidecode command also provides processor information along with other hardware information such as system information, fan information. To get exactly or find the number of CPU cores in Linux using the dmidecode command, you need to grep with the word CPU as shown below:

[email protected]:~# dmidecode |grep -i CPU                                                                                                                       
        Socket Designation: CPU 1                                                                                                                             
        Socket Designation: CPU 2                                                                                                                             
        Socket Designation: CPU 3

Sidebar