Linux - How to Check How Many Cores / CPUs
A common thing that comes up when working with Linux systems is the need to check how many cores or CPUs a system has installed. This is going to be a day to day task if you are working as a systems administrator but is also something you might need for personal use.
To check how many cores / CPUs a Linux system has just run this:
lscpu | egrep 'Model name|Socket|Thread|NUMA|CPU\(s\)'
The above will give you really useful output. The key things in the output are:
- “CPU(s)”: logical cores = threads per core * cores per socket * sockets
- “Thread(s) per core”
- “Socket(s)”: number of physical CPUs
You can determine the total number of cores by dividing the logical cores by the threads per core.
I have another page that is very similar to this one but gives a more general overview here: Linux - How to Get CPU Info.
The dmidecode command is incredibly useful. When combined with egrep ( or just normal grep ) you can extract the exact information you are looking for including the number of cores and threads.
sudo dmidecode -t 4
sudo dmidecode -t 4 | egrep -i 'core (count|enabled)|thread count|Version'
You can use the lscpu command to get information about CPU sockets, threads, and more:
lscpu
lscpu | egrep 'Model name|Socket|Thread|NUMA|CPU\(s\)'
Using a “-p” will give you easily parsable output which is great for scripting.
lscpu -p
Checking the contents of the cpuinfo file can be helpful:
cat /proc/cpuinfo
more /proc/cpuinfo
You can use grep to find the exact information you are looking for:
echo "CPU threads: $(grep -c processor /proc/cpuinfo)"
grep 'cpu cores' /proc/cpuinfo | uniq
Top is usulaly used more for monitoring performance but you can also show what processors are available. Just type ‘1’ after startting top.
top
1
The htop command can be considered even more useful than top. You will likely need to install it first though.
htop
Another tool that can be used is the nproc command.
Number of processing units available:
nproc # available to current process
nproc --all # all units
Another useful command is hwinfo. Using the right options it can give you a summary of cpu info.
hwinfo --cpu # detailed info
hwinfo --cpu --short # short summary
It is worth mentioning the getconf command even though it probably isn’t the first thing I would use.
getconf _NPROCESSORS_ONLN