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.
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
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:
Command | Description |
---|---|
-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 , --offline | Limits the output to offline CPUs. This option only works with arguments -e or -p . |
-b , --online | Allows the output to only online CPUs (default for -p ). This option only works together with option -e or -p . |
-a , --all | Includes online and offline CPU lines in the output (default for -e ). |
-x , -hex | Uses hexadecimal masks for CPU sets. |
-J , --json | Uses JSON output format for the default summary or extended output. |
-C , --caches[=list] | Displays details about CPU caches. |
-B , --bytes | Shows the sizes in bytes rather than in a human-readable format. |
--output-all | Prints all available columns. This option only works with either --extended , --parse , or --caches . |
--hierarchic[=when] | Uses subsections in summary output. |
-s , --sysroot directory | Gathers CPU info for a Linux instance different from the instance from which the lscpu command is issued. |
-y , --physical | Displays 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
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
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
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
Next, print offline CPUs in parsing-friendly mode with:
lscpu -cp
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
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
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
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):”
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):"
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'
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
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 information. It 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
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.