Site icon NetworkHunt

How to check how many CPUs in a Linux System

How to check how many CPUs are in a Linux System

There are multiple ways to check the number of CPUs in a Linux system. There are simple commands like lscpu and nproc and more advanced tools like top, htop, and hwinfo. Each method has its features so that you can choose the best one for you.

If you know the number of CPUs (logical processors) in Linux systems then you can optimize performance, manage resources, and troubleshoot.


Table of Contents

  1. Method 1: Using the lscpu Command
  2. Method 2: Using the /proc/cpuinfo File
  3. Method 3: Using the nproc Command
  4. Method 4: Using top or htop Commands
  5. Method 5: Using the hwinfo Command
  6. Method 6: Using the dmidecode Command
  7. Method 7: Using the getconf _NPROCESSORS_ONLN Command

Method-1 (Using lscpu command)

The lscpu command can be used to display a number of CPU’s in a Linux system It provides an overview of the processor, including the number of CPUs, cores, threads, and other hardware-related information.

When you run the lscpu command, you get a summary of CPU architecture details such as:

Use the lscpu command as below to check how many CPUs are there in your system-

$> lscpu

Method-2 (Using the /proc/cpuinfo file)

The /proc/cpuinfo file is part of the /proc virtual filesystem, which provides kernel and system information in a human-readable format. This file contains information about the CPU of the system. It provides low-level information about each CPU core and processor on the system, such as the processor’s model, architecture, speed, cache sizes, and more.

Key information fields included in this file are-

Use the command below to view the contents of /proc/cpuinfo file using nano editor

$> nano /proc/cpuinfo

Method-3 (Using nproc command)

The nproc command is used to display the number of processing units (CPU cores) available to the current process. It displays the number of CPUs or threads that the system can use, based on the system’s configuration and available hardware.

Common options used with nproc command-

–all : Displays the total number of processing units, including those that may not be available due to restrictions like CPU affinity settings, etc.

–ignore=N : Ignores N number of processors from the total count. This is used by the system administrator for resource management or testing.

Use the nproc command as below to check how many CPUs are there in your system-

$> nproc

Method-4 (Using top or htop command)

top: This command is a standard command-line utility used to monitor system processes and resources in real-time. It’s available by default on almost all Linux systems. Because, it provides a dynamic, real-time view of system processes, CPU usage, memory consumption, etc.

htop: It is a more advanced and interactive alternative to the top, with a more user-friendly, color-coded display and additional features for process management. But, it is not available on most of the Linux distributions by default. Thus, to use, htop on a system use the command below to install it on a system.

$> sudo apt install htop

Use the top or  htop command as below to check how many CPUs are there in your system-

$> top
$> htop

Method-5 (Using the hwinfo command)

The hwinfo command is used to provide information about the hardware components of a system. It can give you detailed data on a wide range of hardware, including the CPU, memory, storage devices, graphics, network interfaces, etc.

Common options used with hwinfo command are-

–cpu : Displays detailed information about the CPU.

$> hwinfo --cpu

–memory : Shows information about the system’s RAM.

$> hwinfo --memory

–disk : Lists the storage devices (HDD, SSD, etc.).

$> hwinfo --disk

–network : Displays network interface details (Ethernet, Wi-Fi).

$> hwinfo --network

–gpu : Shows information about the graphics card(s).

$> hwinfo --gpu

Use the hwinfo command as below to check how many CPUs are there in your system-

$> hwinfo --cpu

Method-6 (Using dmidecode command)

dmidecode is a tool used in Linux systems to retrieve detailed system hardware information from the DMI table. This information can be particularly used for diagnosing hardware issues, performing inventory management, or gathering system details in a scriptable format. It’s a must-have tool for system administrators and advanced users

Use the dmidecode command below to check how many CPUs are there in your system-

$> sudo dmidecode -t 2


Method-7 (Using getconf _NPROCESSORS_ONLN command)

The getconf _NPROCESSORS_ONLN command is used to retrieve the number of active processors available on a system. Thus using this command displays the number of CPUs that the system is currently utilizing for processes. This can be used for determining how many processor cores are actively available for running tasks or for system monitoring.

Use the getconf _NPROCESSORS_ONLN command as below to check how many CPUs are there in your system-

$> getconf _NPROCESSORS_ONLN        

There are multiple ways to check how many CPUs are in a Linux system, ranging from simple commands like lscpu and nproc to more advanced tools like htop, hwinfo, and dmidecode. Each method serves a unique purpose, whether you need a quick count of logical processors or a detailed hardware analysis. Choose the one that best fits your requirements for optimizing performance, managing resources, or troubleshooting effectively.

Exit mobile version