A CPU having multiple cores outstandingly performs better as compared to a single-core. In Linux, knowing information about the number of cores is beneficial for the user to optimize the performance of the system while multitasking. Linux provides various methods to know the number of cores of a system such as “top”, “nproc”, “lscpu”, and “hwinfo” command line tools.
In today’s post, the methods to check the number of cores of the Linux operating system will be demonstrated.
- Through /proc/cpuinfo File
- Through nproc Utility
- Through top Utility
- Through lscpu Utility
- Through hwinfo Utility
- Through getconf _NPROCESSORS_ONLN
Method 1: Through /proc/cpuinfo File
The “/proc.cpuinfo” is the file that holds the CPU information, the user can print the content of this file to get the CPU cores information. Display the content of the file “/proc/cpuinfo” using the cat command:
$ cat /proc/cpuinfo
There are two processors “processor 0” and “processor 1” and each processor has two cores.
Method 2: Through nproc Utility
The “nproc” is the built-in command of Linux that displays the number of processing units available in the system. Run the “nproc” command in the terminal as shown:
$ nproc
The two processing units are available in the system which indicates that the CPU has 2 cores.
Method 3: Through top Utility
Another way to know the number of cores of the system is through the built-in “top” utility that displays the real-time view of the system processes. Run the top command in the terminal and press “1” for getting the information regarding CPU cores:
$ top
The CPU has 2 cores (cpu0 and cpu1).
Method 4: Through lscpu Utility
The “lscpu” is the command that is utilized for displaying the CPU information that ultimately gives information about the CPU cores. Run the “lscpu” command as shown:
$ lscpu
The CPU has 2 cores as shown above.
Total number of cores = no of Threads per core * no of sockets.
Method 5: Through hwinfo Utility
The “hwinfo” is the tool that is used for getting the hardware information of the system that is helpful for the users to get the CPU cores information. Let’s see how it can be used:
Install hwinfo on Linux
It is not pre-installed in the operating system, run the below-given command based upon Linux distro:
$ sudo apt install hwinfo #For Ubuntu/Debian
$ sudo yum install hwinfo #For CentOS/RHEL
$ sudo dnf install hwinfo #For Fedora
$ sudo pacman -s hwinfo #For Arch and Manjora
Get the Number of Cores Using hwinfo
To get the CPU information through “hwinfo”, execute the command as shown:
$ hwinfo --cpu --short
The CPU has 2 cores as shown in the above figure.
Method 6: Through getconf _NPROCESSORS_ONLN
The “getconf _NPROCESSORS_ONLN” is a variable in Linux that stores the number of cores of the system. Use the echo statement to print the variable for displaying the number of cores:
$ echo "Number of CPU/cores available $HOSTNAME is: $(getconf _NPROCESSORS_ONLN)"
The output shows that the CPU has 2 cores. Here, “$HOSTNAME” holds the hostname (ubuntu).
Conclusion
To get the number of CPU cores in a system, consider the “proc/cpuinfo” file’s output or the “nproc”, “top”, “lscpu”, or “hwinfo” utility. The user can also use the “getconf _NPROCESSORS_ONLN” variable to display the number of cores.
This post has illustrated all the possible methods to know the number of cores of a system in Linux.