Daniel López Azaña

Theme

Social Media

Blog

GNU/Linux, Open Source, Cloud Computing, DevOps and more...

How to know how many cores and processors has a Linux box

cpu-cores

The simplest and shortest method to measure the number of processors present on a Linux box, which is also widely extended as it’s part of coreutils is:

~$ nproc --all
8

Another way to get the same result which also allows us to obtain additional information from our processor are the lscpu command:

~$ lscpu | grep 'CPU(s)'
CPU(s):                8
On-line CPU(s) list:   0-7
NUMA node0 CPU(s):     0-7

Or we can examine the cpuinfo file from /proc filesystem:

~$ grep processor /proc/cpuinfo | wc -l
8

There are many additional details about these processors in /proc/cpuinfo file, including the CPU model and number of cores :

~$ cat /proc/cpuinfo | grep "model name"
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
model name	: Intel(R) Xeon(R) CPU           E5520  @ 2.27GHz
~$ cat /proc/cpuinfo | grep "cpu cores"
cpu cores	: 4
cpu cores	: 4

As you can see, the machine in our example has 2 processors with 4 cores each.

Recommended reading:
Differences between physical CPU vs logical CPU vs Core vs Thread vs Socket

Due to HyperThreading technology and other processor enhancements, we might need to differentiate between physical and logical CPUs :

# Get number of physical cpus/cores:
~$ lscpu -p | egrep -v '^#' | wc -l
4
# Get number of logical cpus (including those reported by hyperthreading cores):
~$ lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l
8

The equivalent of the above for Mac OS X:

# Get number of physical cpus/cores:
~$ sysctl -n hw.physicalcpu_max
4
# Get number of logical cpus (including those reported by hyperthreading cores):
~$ sysctl -n hw.logicalcpu_max
8

Another portable method that works on old boxes, both Linux and Mac OS X, which don’t have nproc or lscpu commands available is using the getconf command:

~$ getconf _NPROCESSORS_ONLN
8

We also can use alternative tools to get hardware information like dmidecode , but we’ll need root privileges:

~# dmidecode -t 4 | egrep 'Socket Designation|Count'
        Socket Designation: CPUSocket
        Core Count: 8
        Thread Count: 8

If running the above commands you find out that there are multiple CPUs or multiple cores present in your system, you’ll want to also make sure that your current Linux kernel has multiprocessor support (SMP, Symmetric MultiProcessing):

~# uname -a
Linux sasser 2.6.18-028stab092.1 #1 SMP Wed Jul 20 19:47:12 MSD 2011 i686 GNU/Linux
Daniel López Azaña

About the author

Daniel López Azaña

Tech entrepreneur and cloud architect with over 20 years of experience transforming infrastructures and automating processes.

Specialist in AI/LLM integration, Rust and Python development, and AWS & GCP architecture. Restless mind, idea generator, and passionate about technological innovation and AI.

Related articles

fatrace command man page

Fatrace command: how to know in real time which processes are writing to a file

It is usually easy to know which process or processes are writing to a given file in Linux, since we either know its origin and its nature beforehand (for example the Apache access_log), or we can easily find it out with the fuser or lsof commands. However, sometimes it will happen that although we know the role and purpose of a file, there are so many applications accesing it simultaneously that it is very difficult to know which of them is the one that reads/writes the most or does so in a precise moment. Knowing this would be very useful to learn for example why a log file is growing excessively or which application is making an abusive use of system resources, either by mistake or intentionally.

June 23, 2017
linux-penguin-inside-a-box-tar-gz

15 most useful Linux commands for file system maintenance

One of the most common and tedious tasks of a sysadmin is to prevent file systems become completely full, because when a server runs out of space the consequences are unpredictable. Depending on how you structured the root file system and if it is divided into different partitions or volumes, those consequences will be more or less severe, but in any case undesirable.In any case it is always better to be safe than sorry, so use tools that perform automatic log rotation as logrotate and custom scripts to monitor and conduct periodic emptying actions to prevent file systems to get full. However, still using these prevention methods it is for sure it will be many times when you will have to act manually to troubleshoot problems.

October 1, 2014
Ctrl+S

Unlock Linux command line after pressing Ctrl+s in Bash

Since the key combination Control+s is widely used as a shortcut to save files in GUI applications such as text editors, image editors, web browsers, etc. sometimes you are betrayed by your subconscious when you are working from the Linux command line and you use that same key combination when you are for example editing a Vim document when trying to save it. Then you notice that no key answers, the shell is locked and you can no longer do anything else in it.  Even worse, you get a cold sweat because you can’t continue editing your document and you can’t save the changes.

April 27, 2017

Comments

ronald October 28, 2012
Or, in my case: grep -i processor /proc/cpuinfo | wc -l

Submit comment