In this tutorial, we’ll look at ways to check the number of processors or cores available in a Linux machine. We’ll take a closer look at some of the Bash utilities and system files that can be used for this purpose.
2. The lscpu Utility
First, let’s check the default output of the lscpu command:
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 39 bits physical, 48 bits virtual CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 142 Model name: Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz ...
Here, the CPU(s) value indicates the number of logical cores, which is equal to 8 in our output.
The number of logical cores is equal to “Thread(s) per core” × “Core(s) per socket” × “Socket(s)” and the number of physical cores on a machine equals “Core(s) per socket” × “Socket(s)”.
Based on the above output, we can infer that our machine has 4 physical cores, and since hyperthreading is enabled (“Thread(s) per core” equals 2), we get 8 logical cores.
Let’s also check the command to directly get the number of physical cores on a machine:
$ lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l 4
Here, we’re using the -p option to parse only the “Core” and “Socket” columns. Alongside that, we’re also using the -b option to limit the output to online CPUs only.
3. The /proc/cpuinfo File
The lscpu utility derives its information from the /proc/cpuinfo file, which contains processor-wise information for all the logical cores or processors:
$ cat /proc/cpuinfo | egrep "processor|core id" processor : 0 core id : 0 processor : 1 core id : 0 processor : 2 core id : 1 processor : 3 core id : 1 processor : 4 core id : 2 processor : 5 core id : 2 processor : 6 core id : 3 processor : 7 core id : 3
Therefore, let’s get the number of logical cores on a machine based on the contents of the /proc/cpuinfo file:
$ grep --count ^processor /proc/cpuinfo 8
Additionally, we can also count the number of unique core ids to get the number of physical cores on a machine:
$ grep ^"core id" /proc/cpuinfo | sort -u | wc -l 4
4. Other Alternatives
There are a few other commands we can use to get the processor information. Let’s take a look.
4.1. The nproc Command
Let’s check the number of processors available on the system using the nproc utility:
$ nproc --all 8
Here, we used here the –all option to print the number of installed processors.
4.2. Querying Using getconf
We can query the configured processor values using the getconf command:
$ getconf -a | grep PROCESSORS _NPROCESSORS_CONF 8 _NPROCESSORS_ONLN 8
In this case, the -a option lists all the configuration variables of the system and their values. Alternatively, we can also query for just the processor-related parameters. So, let’s check the number of configured processors:
$ getconf _NPROCESSORS_CONF 8
And, the number of processors online:
$ getconf _NPROCESSORS_ONLN 8
In this article, we looked at different ways to get the number of cores on a Linux machine.
First, we introduced the lscpu utility of Bash. Additionally, we learned to interpret its output to calculate the number of physical and logical cores. We also checked the commands to directly get the physical and logical processor count on a machine.
Then, we looked at ways to derive the same information from the /proc/cpuinfo file. Finally, we also examined ways to use nproc and getconf commands to get the CPU core information.