When compiling software from source code or configuring a hypervisor for virtual machines, you need to know exactly how many processing cores are available on your Linux server. If you allocate more virtual cores to a container than your physical CPU actually possesses, you will cause severe performance bottlenecks.
While you can read the verbose /proc/cpuinfo file to gather hardware data, finding a single, clean integer representing your total core count can be tedious. The fastest method is to use the dedicated nproc utility.
How to Use the nproc Command
The nproc (Number of Processors) command is part of the GNU coreutils package, meaning it is pre-installed on virtually every Linux distribution in existence, from Ubuntu to CentOS.
- Open your terminal.
- Type the following exact command:
nproc
- Press Enter.
The terminal will output a single, raw number (e.g., 16). This number represents the total number of processing units available to your current operating system environment.
Physical Cores vs. Logical Threads
It is crucial to understand that the number nproc returns is the number of logical threads, not physical cores, assuming your CPU supports hyper-threading (which almost all modern Intel and AMD processors do).
If you have an 8-core CPU with hyper-threading enabled, nproc will return 16, because the operating system addresses each physical core as two separate logical processors.
How to Ignore Hyper-Threading
If you need to know the exact number of physical silicon cores (ignoring the virtual threads), nproc cannot help you. Instead, you should use the lscpu command.
- Execute the following command to filter the hardware data:
lscpu | grep "Core(s) per socket"
This will return a line like Core(s) per socket: 8, confirming the true physical layout of your processor.