lscpu Command in Linux

November 30, 2023

Introduction

The lscpu command, short for list CPU, provides a detailed list of information about the CPUs in the system. This includes the CPU number, architecture, vendor, family, model, and CPU caches.

The following text explains the lscpu command.

lscpu command in Linux

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to the terminal.

lscpu Command Syntax

The lscpu syntax is:

lscpu [options]

Running lscpu without invoking arguments produces this output:

lscpu
lscpu terminal output

In this case, the output details the CPU architecture, cores, threads, CPU family, model, etc. The exact output varies depending on the specific system and its configuration. However, use arguments to filter out the info and get a tailored output.

lscpu Command Options

The lscpu command has several arguments that allow users to customize what they see in the output. The following table presents the most important lscpu options:

CommandDescription
-e, --extended[=list]Displays the CPU information in human-readable format. Displays only one column of details when used with the column name.
-p, --parse[=list]Optimizes the command output for easy parsing.
-c, --offlineLimits the output to offline CPUs. This option only works with arguments -e or -p.
-b, --onlineAllows the output to only online CPUs (default for -p). This option only works together with option -e or -p.
-a, --allIncludes online and offline CPU lines in the output (default for -e).
-x, -hexUses hexadecimal masks for CPU sets.
-J, --jsonUses JSON output format for the default summary or extended output.
-C, --caches[=list]Displays details about CPU caches.
-B, --bytesShows the sizes in bytes rather than in a human-readable format.
--output-allPrints all available columns. This option only works with either --extended, --parse, or --caches.
--hierarchic[=when]Uses subsections in summary output.
-s, --sysroot directoryGathers CPU info for a Linux instance different from the instance from which the lscpu command is issued.
-y, --physicalDisplays physical IDs for all columns with topology elements (core, socket, etc.)

lscpu Command in Linux: Examples

Due to its versatility, lscpu has plenty of use cases. The following text presents practical lscpu usage examples.

Example 1: Display CPU Data in Readable Format

To see CPU-related data in a human-readable format, run lscpu with the -e argument.

For instance:

lscpu -e
lscpu -e terminal output

The -e argument modifies the output, making it structured and easy to read. The output includes a header line with clear column labels, such as CPU, NODE, SOCKET, CORE, L1d:L1i:L2:L3, and ONLINE. The data is also aligned under each column, enhancing readability.

The -e argument has another function. When run with lscpu and a column name, it prints human-readable info about the specified column.

The syntax is:

lscpu -e=[column_name]

For instance, print only details about the column CPU with:

lscpu -e=cpu
lscpu -e=cpu terminal output

Example 2: Printing Info in Parsing-Friendly Mode

To get a parsing-friendly input designed to be quickly processed and extracted by scripts or programs, use the argument -p.

Run this command:

lscpu -p
lscpu -p terminal output

In the output, values are comma-separated in an easily parsed format. Moreover, each CPU's information is presented in a single line, and the output doesn't include headers or comments.

Example 3: Limiting Output to Online or Offline Mode

The lscpu command in Linux provides options to limit its output specifically to online or offline CPUs. This is useful when focusing on the system's active or inactive CPU cores. The relevant options for this purpose are -b for online CPUs and -c for offline CPUs.

Note: When using these options, combining them with either -e or -p is necessary.

For instance, show all online CPUs in human-readable form with:

lscpu -be
lscpu -be terminal output

Next, print offline CPUs in parsing-friendly mode with:

lscpu -cp
lscpu -cp terminal output

In this case, the output doesn't show any CPUs, as there are no inactive ones.

Example 4: Display Both Online and Offline CPUs

To show both online and offline CPUs, run the lscpu with the -a argument.

Note: The -a argument only works together with -e or -p.

For instance, print all online and offline CPUs in a human-readable format with:

lscpu -ae
lscpu -ae terminal output

Example 5: Using Hexadecimal Masks for CPU Sets

The lscpu command in Linux, when executed without any options, displays its output in a list format. However, the -x option allows users to print CPU sets using a hexadecimal mask.

A hexadecimal mask represents processor affinity using hexadecimal (base-16) numbers. It helps users understand how CPU cores are organized and associated with specific tasks or processes.

For instance, enter:

lscpu -x
lscpu -x terminal output

The f represents a hexadecimal mask. Each digit in the mask corresponds to a CPU or core. The f in hexadecimal translates to binary as 1111, and in this case, it indicates all CPUs (0 to 3 in this case) are online or part of NUMA node0.

Example 6: Display Output in JSON Format

The lscpu command in Linux with the -J argument generates the output in JSON format. This lightweight data-interchange format is easy for humans to read and write and for machines to parse and generate.

For example, run this command:

lscpu -J
lscpu -J terminal output

This output is useful for scripting or processing CPU details in applications or system management tools that consume JSON data.

Example 7: Finding Specific Info

The lscpu command provides details about various CPU-related components. To find specific information, such as the CPUs or sockets number, utilize grep to filter and extract the information from the output.

The syntax is:

lscpu | grep "search-term"

For example, find the number of CPUs with:

lscpu | grep "CPU(s):”
lscpu | grep "CPU(s):” terminal output

The output indicates the total number of CPUs in the system. The second line provides additional information about the CPUs associated with NUMA node0. The range "0-3" indicates CPUs 0 through share a common memory domain associated with NUMA node0.

The same command works for finding other components. For example, find the number of sockets with:

lscpu | grep "Socket(s):"
lscpu | grep "Socket(s):" terminal output

The output shows one socket.

Another way to pipe lscpu with grep is with this syntax:

lscpu | grep -i 'search_term'

The argument -i makes the search case-insensitive and matches any line in the lscpu output that includes the search_term, even if it's part of a larger word.

For instance, run:

lscpu | grep -i 'cpu'
lscpu | grep -i 'cpu' terminal output

The output is more extensive and includes every cpu instance in the text marked in red.

Example 8: Printing Info about Caches

To view info about caches in an extended readable format, run lscpu with the -C argument.

lscpu -C
lscpu -C terminal output

The output details different cache levels (L1, L2, L3), sizes, associativity, and other relevant parameters.

Example 9: Show Detailed Output About All Available Columns

Use the --output-all argument to display more detailed CPU-related informationIt includes additional details such as cache sizes, NUMA information, and extended CPU feature flags.

Note: The --output-all argument only works with -e, -p, or -C options.

For instance, print more details about CPUs in a human-readable format with:

lscpu --output-all -e
lscpu --output-all -e terminal output

Conclusion

After reading this text, you know how to use the lscpu command and its options. Test our use-case examples to get familiar with the command.

Next, learn how to check Linux CPU usage.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Check CPU Temperature on Linux
March 10, 2021

High temperatures can damage the sensitive components in your machine. Read our tutorial to learn how to monitor CPU, GPU and HDD temperatures on machines running...
Read more
What is a vCPU and How to Calculate vCPU Requirements?
December 15, 2022

A vCPU is a virtual central processing unit, created and used by virtual machines. This article explains the vCPU concept and shows how to calculate vCPU requirements...
Read more
CPU Vs. GPU: A Comprehensive Overview
December 9, 2021

With the growing popularity of fields such as deep learning, 3D modeling/rendering, gaming, and crypto mining, modern computing requirements have skyrocketed. The hardware...
Read more
Dual-Core vs. Quad-Core CPU: What's the Difference?
April 19, 2023

The CPU (central processing unit) is a key component of the computer. This article discusses the differences between a dual-core and quad-core CPU, helping you choose the right processor for your...
Read more