lscpu command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

Introduction to lscpu command

lscpu is a command-line utility to display information about the CPU architecture. It reads the CPU architecture information from sysfs and /proc/cpuinfo files and prints in the terminal. The information includes the number of CPUs, threads, cores, sockets, and Non-Uniform Memory Access (NUMA) nodes. It also displays CPU caches and cache sharing, family, model, bogoMIPS, byte order, and stepping.

 

How to install lscpu

Most Linux distributions have lscpu pre-installed in the system. In case if it is not found, you can install lscpu from the default package management repositories. The lscpu command is part of the util-linux package.

To install lscpu on CentOS, Fedora and RHEL

$ sudo yum install util-linux

To install lscpu on Ubuntu and Debian

$ sudo apt-get install util-linux

 

Columns in lscpu command output

The following columns are only displayed when lscpu is used with option -e or -p.

CPU: The logical CPU number of a CPU as used by the Linux kernel.

CORE: The logical core number. A core can contain several CPUs.

SOCKET: The logical socket number. A socket can contain several cores.

BOOK: The logical book number. A book can contain several sockets.

NODE: The logical NUMA node number. A node may contain several books.

DRAWER: The logical drawer number. A drawer may contain several nodes.

CACHE: Information about how caches are shared between CPUs.

POLARIZATION: It shows CPU dispatching mode on virtual hardware. The polarization can be:

  • horizontal: The workload is spread across all available CPUs.
  • vertical: The workload is concentrated on a few CPUs.

ADDRESS: The physical address of a CPU.

CONFIGURED: It shows if the hypervisor has allocated the CPU to the virtual hardware.

ONLINE: This shows whether the Linux instance currently makes use of the CPU.

MAXMHZ: It shows the maximum MHz of the CPU.

MINMHZ: It shows the minimum MHz of the CPU.

 

Different examples to use lscpu command

The syntax of lscpu command is:

$ lscpu [options]

Some available options in lscpu command are:

  • -a: print both online and offline CPUs
  • -b: print online CPUs only
  • -c: print offline CPUs only
  • -e: print in an extended readable format
  • -p: print CPU information in parsable format

 

 

1. lscpu command to display complete CPU architecture information

You can use lscpu command without any options to view the complete information of CPU architecture in the system. On some distributions, you can also use --physical argument to print the same information.

$ lscpu

Sample Output:

lscpu command to display CPU architecture information

 

2. lscpu command to display sizes in bytes

-b or --bytes option prints size in bytes rather than in human-readable format.

$ lscpu -b

OR

$ lscpu --bytes

Sample Output:

lscpu command to print sizes=

 

3.  Display the CPU information in human-readable format with lscpu command

You can use -e or --extended option to print the CPU information in an extended human-readable format.

$ lscpu -e

OR

$ lscpu --extended

Sample Output:

ubuntu@golinux:~$ lscpu -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE
  0    0      0    0 0:0:0:0          yes

If you want to limit the columns of the above output, you can use the following syntax. list is the name of the columns.

$ lscpu -e=list

OR

$ lscpu --extended=list

Sample Output:

ubuntu@golinux:~$ lscpu -e=cache
L1d:L1i:L2:L3
0:0:0:0
ubuntu@golinux:~$ lscpu -e=cpu,core
CPU CORE
  0    0

 

4. lscpu command to display CPU information in parsable format

-p or --parse option optimizes the lscpu command output for easy parsing. The parsable format can be fed to other programs.

$ lscpu -p

OR

$ lscpu --parse

Sample Output:

ubuntu@golinux:~$ lscpu -p
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core,Socket,Node,,L1d,L1i,L2,L3
0,0,0,0,,0,0,0,0

You can also limit the columns using the following syntax.

$ lscpu -p=list

OR

$ lscpu --parse=list

Sample Output:

ubuntu@golinux:~$ lscpu -p=cpu,core
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core
0,0

 

5. Display both online and offline CPUs with lscpu command

-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.

$ lscpu -a [-e or -p]

OR

$ lscpu --all [-e or -p]

Sample Output:

~]# lscpu --all -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE
0   0    0      0    0:0:0:0       yes
1   0    1      1    1:1:1:1       yes
2   0    2      2    2:2:2:2       yes
3   0    3      3    3:3:3:3       yes
4   0    4      4    4:4:4:4       yes
5   0    5      5    5:5:5:5       yes
6   0    6      6    6:6:6:6       yes
7   0    7      7    7:7:7:7       yes
8   0    8      8    8:8:8:8       yes
9   0    9      9    9:9:9:9       yes
10  0    10     10   10:10:10:10   yes
11  0    11     11   11:11:11:11   yes
12  0    12     12   12:12:12:12   yes
13  0    13     13   13:13:13:13   yes
14  0    14     14   14:14:14:14   yes
15  0    15     15   15:15:15:15   yes

 

6. Display only online CPUs with lscpu command

You can limit the output to online CPUs with options -b or --online. This option should be used with -e or -p.

$ lscpu -b [-e or -p]

OR

$ lscpu --online [-e or -p]

Sample Output:

~]# lscpu --online -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE
0   0    0      0    0:0:0:0       yes
1   0    1      1    1:1:1:1       yes
2   0    2      2    2:2:2:2       yes
3   0    3      3    3:3:3:3       yes
4   0    4      4    4:4:4:4       yes
5   0    5      5    5:5:5:5       yes
6   0    6      6    6:6:6:6       yes
7   0    7      7    7:7:7:7       yes
8   0    8      8    8:8:8:8       yes
9   0    9      9    9:9:9:9       yes
10  0    10     10   10:10:10:10   yes
11  0    11     11   11:11:11:11   yes
12  0    12     12   12:12:12:12   yes
13  0    13     13   13:13:13:13   yes
14  0    14     14   14:14:14:14   yes
15  0    15     15   15:15:15:15   yes

 

7. lscpu command to display only offline CPUs

You can use -c or --offline option to display only offline CPUs in the output. This option also works with -e or -p only.

$ lscpu -c [-e or -p]

OR

$ lscpu --offline [-e or -p]

Sample Output:

There are no offline CPUs.

ubuntu@golinux:~$ lscpu --offline -e
ubuntu@golinux:~$ 

 

8. lscpu command to display CPU information in JSON format

You can use -J or --json option to print the CPU information in JSON format.

$ lscpu -J

OR

$ lscpu --json

Sample Output:

lscpu command to display cpu information in json format

 

9. lscpu command to display information about caches

-C or --caches option prints the information about caches in an extended readable format.

$ lscpu -C

OR

$ lscpu --caches

Sample Output:

ubuntu@golinux:~$ lscpu --caches
NAME ONE-SIZE ALL-SIZE WAYS TYPE        LEVEL
L1d       32K      32K    8 Data            1
L1i       32K      32K    8 Instruction     1
L2       256K     256K    4 Unified         2
L3         8M       8M   16 Unified         3

Here,

  • NAME: cache name
  • ONE-SIZE: size of one cache
  • ALL-SIZE: size of all system caches
  • WAYS: ways of associativity
  • TYPE: cache type
  • LEVEL: cache level

 

10. Use hexadecimal masks with lscpu command

You can use hexadecimal masks to print CPU sets (for example, 0x3) using -x or --hex option. By default, lscpu prints the sets in list format (for example, 0,1).

$ lscpu -x

OR

$ lscpu --hex

Sample Output:

lscpu command to use hexadecimal masks

 

11. lscpu command to read CPU information from directory

-s or --sysroot option can gather CPU information from the directory, which is the system root of the Linux system.

$ lscpu -s directory

OR

$ lscpu --sysroot directory

Sample Output:

lscpu command to gather information from directory

 

12. Display all available columns with lscpu command

You can use --output-all option to view all the available columns for --extended, --parse, and --caches.

$ lscpu --output-all -e

Sample Output:

lscpu command to output all columns

 

13. Save lscpu command output to a text file

We can also save the output of lscpu command into a text file by appending the content using > sign or tee command:

 ~]# lscpu | tee /tmp/cpu.txt

OR

 ~]# lscpu > /tmp/cpu.txt

Verify the content of /tmp/cpu.txt file:

 ~]# cat /tmp/cpu.txt

 

Conclusion

In this article, you have learned about the lscpu command and its usages in the Linux system. lscpu is a useful tool to gather CPU architecture information. If you still have any confusion, let us know in the comment section.

 

What's Next

20+ lshw command examples in Linux [Cheat Sheet]

 

Further Reading

man page for lscpu command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment