Open In App

How to Use Linux lscpu Command

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the Linux environment, the lscpu command is an important utility for getting detailed information about the CPU (Central Processing Unit) configuration of our system. When this command is been executed, it provides the overview of the CPU architecture such as the CPU family, model, number of cores, threads, and other characteristics. In this article, we will see detailed information on the lscpu command in Linux along with syntax, installation, and practical examples.

Syntax of the ‘lscpu’ command in Linux

lscpu [options]

‘lscpu’ will retrieve specific information about the CPU configuration. Commonly used options include -b for machine-readable output, -c for displaying online CPUs, and -p for displaying output in a parsable format.

Commonly Used Options in the ‘lscpu’ command in Linux

Options

Description

-b

Generate machine-readable (raw) output.

-c

Display information about online CPUs.

-p

Format output in a parsable format.

-a

Display all information (equivalent to -bcp).

-x

Display extended information (shows flags and other details).

-y

Display NUMA (Non-Uniform Memory Access) information.

-s

Display a summary of the CPU architecture.

-t

Display the thread, socket, and core information.

-o

Display only the specified fields (use with -b or -p for customized output).

How to install lscpu command in Linux?

Follow the below commands to install the ‘lscpu‘ command on different Linux distributions. Although the command is pre-installed on the system, if for some reason the command is not available then we can install it using the package manager for a specific distribution.

1. Debian/Ubuntu-based systems (apt):

Use the following command to install the util-Linux package, which includes lscpu:

sudo apt-get install util-linux

Installation on Debain based system

Installation on Debain based system

2. Red Hat/CentOS-based systems (yum or dnf):

Use one of the following commands depending on your package manager:

  • For systems using yum:
sudo yum install util-linux

  • For systems using dnf:
sudo dnf install util-linux

3. openSUSE (zypper):

Use the following command to install lscpu:

sudo zypper install util-linux

Practical Examples of lscpu command in Linux

Below are some of the practical examples of the lscpu command in Linux with the utilization of various additional options along with the command. These options will retrieve the specific information in terms of the option given as input.

1. Display Complete CPU Architecture Information

lscpu

This command displays comprehensive CPU architecture information, including details about the CPU family, model, number of cores, threads, cache sizes, and more.

Displaying Complete CPU information

Displaying Complete CPU information

2. Display sizes in bytes:

lscpu --bytes

The command in Linux displays CPU information with sizes, such as cache sizes, represented in bytes instead of more human-readable units like kilobytes or megabytes. This option provides detailed and precise numerical values for CPU attributes.

2-(1)

Display size in bytes

3. Display CPU information in human-readable format:

lscpu -e

This command displays CPU information in a human-readable format, including details about the architecture, model name, and various CPU features.

Displaying CPU information in human- readable format

Displaying CPU information in human- readable format

4. Display CPU information in parsable format:

lscpu -p

This command extracts and displays only the CPU model names from the lscpu output in a parsable format.

Displaying CPU information in parse format

Displaying CPU information in parse format

5. Display both online and offline CPUs:

lscpu --all -e

-a or -all option is used to display both online and offline CPUs in the output. You can only use this option with -e or -p.

Displaying both online and offline CPUs

Displaying both online and offline CPUs

6. Display only online CPUs:

lscpu --online -e

Using the –online option with lscpu displays information only about online CPUs, which are actively in use.

Displaying only online CPUs

Displaying only online CPUs

7. Display only offline CPUs:

lscpu --offline -e

The –offline option allows you to view information about CPUs that are currently offline.

Displaying only offline CPUs

Displaying only offline CPUs

8. Display CPU information in JSON format:

lscpu --json

This command presents CPU information in JSON format, making it easier for programmatic access or integration with other tools.

Displaying CPU information in JSON Format

Displaying CPU information in JSON Format

9. Display information about caches:

lscpu --caches

The –caches option in lscpu displays information about CPU caches, including data, instruction, unified caches, their sizes, and more.

Displaying information about caches

Displaying information about caches

10. Use hexadecimal masks:

lscpu --hex

This command extracts the hexadecimal masks of the CPU’s available resources, which can be useful for specific system configuration or analysis.

10-(1)

Use hexadecimal masks

11. Read CPU information from a directory:

lscpu --sysroot /

The –sysroot option allows you to read CPU information from the /sys/devices/system/cpu/ directory, providing more detailed information about individual CPU cores.

11-Reading CPU information from directory(1)

Reading CPU information from directory

12. Display all available columns:

lscpu --output-all -e

The --output-all option is used to view all the available columns for --extended, --parse, and --caches.

Displaying all columns

Displaying all columns

13. Save lscpu output to a text file:

lscpu > cpu_info.txt

This command redirects the output of lscpu to a text file named cpu_info.txt, allowing you to save and review the information at a later time or share it with others.

Saving lscpu output to text file

Saving lscpu output to text file

Conclusion

In conclusion, the lscpu command in Linux is a valuable utility for gaining insights into the CPU architecture of a system. It provides comprehensive information about the number of CPUs, threads, cores, sockets, cache details, and more, aiding in system optimization and hardware understanding. With options for customized output formats, such as bytes, JSON, and parsable data, it offers versatility for various use cases. Whether you’re a system administrator, developer, or simply curious about your system’s CPU configuration, lscpu is a powerful tool that helps you make informed decisions and efficiently manage hardware resources in the Linux environment.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads