当前位置: 首页 > article >正文

Linux lsblk 命令详解:查看磁盘和分区信息 (中英双语)

Linux lsblk 命令详解:查看磁盘和分区信息

在 Linux 系统中,管理磁盘设备和分区是日常运维工作的重要部分。而 lsblk 命令是一个强大的工具,它用于列出系统中的块设备(block devices)信息,可以帮助我们快速查看磁盘、分区、挂载点等内容。相比 fdisk -ldf -hlsblk 提供了更加直观的树状结构,能够更清晰地展示磁盘及其分区关系。

在本文中,我们将详细介绍 lsblk 命令的基本用法、高级用法、常见参数,并通过示例展示如何使用它来高效管理 Linux 服务器中的磁盘设备。


1. lsblk 命令概述

lsblkList Block Devices 的缩写,它用于列出系统中的块设备(block devices),包括硬盘、SSD、U 盘等存储设备的信息。这个命令主要用于:

  • 查看磁盘分区结构
  • 检查磁盘挂载情况
  • 获取设备的 UUID、文件系统类型等信息

基本语法

lsblk [选项]

其中,[选项] 用于指定不同的显示方式,例如显示详细信息、过滤特定设备等。


2. lsblk 命令的基本用法

2.1 显示所有块设备

lsblk

示例输出

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  500G  0 disk
├─sda1   8:1    0  100G  0 part /
├─sda2   8:2    0  200G  0 part /home
└─sda3   8:3    0  200G  0 part /data
sdb      8:16   0  1T    0 disk
└─sdb1   8:17   0  1T    0 part /mnt/storage
sr0     11:0    1  4.3G  0 rom  /mnt/cdrom

结果解析

  • NAME:设备名称,例如 sda 代表第一个磁盘,sdb 代表第二个磁盘。
  • MAJ:MIN:主设备号和次设备号。
  • RM(Removable):是否为可移动设备(1 代表可移动,如 U 盘,0 代表固定磁盘)。
  • SIZE:磁盘或分区的大小。
  • RO(Read-Only):是否为只读设备(1 代表只读,0 代表可读写)。
  • TYPE:设备类型:
    • disk:物理磁盘
    • part:分区
    • rom:光驱
  • MOUNTPOINT:分区的挂载点。

2.2 以详细格式显示

使用 -o 选项可以指定显示的字段,例如:

lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT,UUID

示例输出

NAME   MAJ:MIN RM   SIZE RO FSTYPE MOUNTPOINT UUID
sda      8:0    0  500G  0
├─sda1   8:1    0  100G  0 ext4   /
│                               123e4567-e89b-12d3-a456-426655440000
├─sda2   8:2    0  200G  0 xfs    /home
│                               123e4567-e89b-12d3-a456-426655440001
└─sda3   8:3    0  200G  0 ext4   /data
                                123e4567-e89b-12d3-a456-426655440002

参数解析

  • FSTYPE:文件系统类型(如 ext4xfs)。
  • UUID:文件系统的唯一标识符(用于 fstab 挂载)。

3. lsblk 命令的高级用法

3.1 仅显示磁盘(不包括分区)

lsblk -d

示例输出

NAME   MAJ:MIN RM   SIZE RO TYPE
sda      8:0    0  500G  0 disk
sdb      8:16   0  1T    0 disk
sr0     11:0    1  4.3G  0 rom

-d 选项仅显示磁盘,不包括分区信息。


3.2 仅显示已挂载的分区

lsblk -o NAME,MOUNTPOINT | grep -v "^\s*$"

示例输出

NAME    MOUNTPOINT
sda1    /
sda2    /home
sda3    /data
sdb1    /mnt/storage

3.3 显示设备的 UUID

lsblk -o NAME,UUID

示例输出

NAME    UUID
sda1    123e4567-e89b-12d3-a456-426655440000
sda2    123e4567-e89b-12d3-a456-426655440001
sdb1    123e4567-e89b-12d3-a456-426655440002

UUID 在 /etc/fstab 中用于自动挂载。


3.4 结合 grep 过滤特定设备

如果想查找某个特定的磁盘(如 sda),可以使用:

lsblk | grep sda

4. lsblk 与其他磁盘管理命令对比

命令作用
lsblk显示磁盘、分区信息,树状结构,简洁直观
fdisk -l显示磁盘详细分区信息,适用于磁盘分区管理
blkid获取设备 UUID、文件系统类型等
df -h查看已挂载设备的存储使用情况
parted -l显示分区信息,适用于 GPT 磁盘

5. lsblk 命令应用场景

5.1 查看新插入的 U 盘或硬盘

当你插入 U 盘或新的硬盘时,可以使用 lsblk 来检查设备:

lsblk

然后可以用 mount 命令手动挂载它。


5.2 检查文件系统类型

有时候需要知道磁盘的文件系统类型,比如 ext4、xfs、ntfs:

lsblk -o NAME,FSTYPE

5.3 确定磁盘是否已挂载

使用 lsblk -o NAME,MOUNTPOINT 可以快速确认某个分区是否已被挂载。


6. 总结

lsblk 是 Linux 系统中用于查看块设备信息的重要工具,它相比 fdisk -ldf -h 更加直观,尤其适用于:

  • 检查磁盘分区结构
  • 查看挂载状态
  • 获取设备 UUID 及文件系统类型

常用命令总结

命令作用
lsblk显示所有块设备
lsblk -d仅显示磁盘,不包括分区
lsblk -o NAME,MOUNTPOINT查看已挂载的设备
lsblk -o NAME,FSTYPE,UUID显示文件系统类型和 UUID

希望这篇文章能帮助你更好地理解 lsblk 命令,提升 Linux 磁盘管理能力!🚀

Comprehensive Guide to the lsblk Command in Linux

In Linux, managing disk devices and partitions is a crucial task for system administrators. The lsblk command is a powerful tool that lists information about block devices, including hard drives, SSDs, USB drives, and other storage media. Compared to commands like fdisk -l or df -h, lsblk presents a tree-like structure that makes it easier to understand disk and partition relationships.

This article provides a complete guide to lsblk, covering its basic and advanced usage, common options, and practical scenarios where it is useful.


1. Overview of the lsblk Command

lsblk stands for List Block Devices. It is used to display information about block devices, including their partitions, sizes, mount points, and file system types.

Basic Syntax

lsblk [options]
  • The [options] modify the output to display specific details such as file system types, UUIDs, or mounted status.

2. Basic Usage of lsblk

2.1 List All Block Devices

lsblk

Example Output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0  500G  0 disk
├─sda1   8:1    0  100G  0 part /
├─sda2   8:2    0  200G  0 part /home
└─sda3   8:3    0  200G  0 part /data
sdb      8:16   0  1T    0 disk
└─sdb1   8:17   0  1T    0 part /mnt/storage
sr0     11:0    1  4.3G  0 rom  /mnt/cdrom

Explanation of Columns

  • NAME: Device name (sda for the first disk, sdb for the second, etc.).
  • MAJ:MIN: Major and minor device numbers.
  • RM (Removable): Whether the device is removable (1 for removable, 0 for fixed storage).
  • SIZE: The size of the disk or partition.
  • RO (Read-Only): Whether the device is read-only (1 for yes, 0 for no).
  • TYPE: Device type:
    • disk: Physical disk
    • part: Partition
    • rom: Optical disk (CD/DVD)
  • MOUNTPOINT: Directory where the device is mounted.

2.2 Display Detailed Information

To include additional details such as file system type and UUID, use:

lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT,UUID

Example Output:

NAME   MAJ:MIN RM   SIZE RO FSTYPE MOUNTPOINT UUID
sda      8:0    0  500G  0
├─sda1   8:1    0  100G  0 ext4   /
│                               123e4567-e89b-12d3-a456-426655440000
├─sda2   8:2    0  200G  0 xfs    /home
│                               123e4567-e89b-12d3-a456-426655440001
└─sda3   8:3    0  200G  0 ext4   /data
                                123e4567-e89b-12d3-a456-426655440002
  • FSTYPE: File system type (e.g., ext4, xfs, ntfs).
  • UUID: Unique identifier of the partition (used for /etc/fstab mounting).

3. Advanced Usage of lsblk

3.1 Display Only Physical Disks (Exclude Partitions)

lsblk -d

Example Output:

NAME   MAJ:MIN RM   SIZE RO TYPE
sda      8:0    0  500G  0 disk
sdb      8:16   0  1T    0 disk
sr0     11:0    1  4.3G  0 rom

The -d flag hides partitions, displaying only disks.


3.2 Show Only Mounted Partitions

lsblk -o NAME,MOUNTPOINT | grep -v "^\s*$"

Example Output:

NAME    MOUNTPOINT
sda1    /
sda2    /home
sda3    /data
sdb1    /mnt/storage

3.3 Display Device UUIDs

lsblk -o NAME,UUID

Example Output:

NAME    UUID
sda1    123e4567-e89b-12d3-a456-426655440000
sda2    123e4567-e89b-12d3-a456-426655440001
sdb1    123e4567-e89b-12d3-a456-426655440002

UUIDs are used for persistent device mounting in /etc/fstab.


3.4 Filter Specific Devices Using grep

To find information about a specific disk (e.g., sda):

lsblk | grep sda

4. Comparison with Other Disk Management Commands

CommandDescription
lsblkLists block devices with a clear tree structure
fdisk -lDisplays detailed partition information (good for partition management)
blkidRetrieves file system type and UUID
df -hShows mounted devices and their disk usage
parted -lLists partition details, useful for GPT disks

5. Practical Use Cases for lsblk

5.1 Checking a Newly Inserted USB Drive or Hard Disk

When you insert a USB drive or a new hard disk, use lsblk to check if it is recognized:

lsblk

Then, use the mount command to access the device.


5.2 Identifying File System Type

To determine whether a partition is formatted as ext4, xfs, or ntfs:

lsblk -o NAME,FSTYPE

5.3 Checking If a Partition Is Mounted

Use lsblk -o NAME,MOUNTPOINT to quickly verify if a partition is currently mounted.


6. Summary

The lsblk command is a crucial tool for managing block devices in Linux. Compared to fdisk -l or df -h, it provides a more user-friendly and structured output, making it ideal for:

  • Checking disk and partition structures
  • Viewing mounted storage devices
  • Retrieving file system types and UUIDs

Commonly Used lsblk Commands

CommandDescription
lsblkList all block devices
lsblk -dShow only physical disks, excluding partitions
lsblk -o NAME,MOUNTPOINTDisplay only mounted partitions
lsblk -o NAME,FSTYPE,UUIDShow file system type and UUID

Mastering lsblk will greatly improve your ability to manage storage devices in Linux efficiently. 🚀

后记

2025年2月22日20点16分于上海,在GPT4o大模型辅助下完成。


http://www.kler.cn/a/558024.html

相关文章:

  • 区块链相关方法-波士顿矩阵 (BCG Matrix)
  • 《论模型驱动架构设计方法及其应用》审题技巧 - 系统架构设计师
  • Ubuntu 查看mysql用户和数据库
  • Qwen2.5-VL Technical Report!!! 操作手机电脑、解析化学公式和乐谱、剪辑电影等,妥妥六边形战士 !...
  • Jmeter HTTP代理服务器录制压力脚本
  • MySQL 架构
  • 理解 logits_to_keep = logits_to_keep + 1 在 _get_per_token_logps 中的作用
  • JAVA中 BigInteger类的构造与常见使用方法的简述
  • Java数据结构第十二期:走进二叉树的奇妙世界(一)
  • MyBatis 中 SqlMapConfig 配置文件详解
  • Docker-技术架构演进之路
  • 遥感影像目标检测:从CNN(Faster-RCNN)到Transformer(DETR)
  • 51单片机-按键
  • 紧随“可信数据空间”政策风潮,数造科技正式加入开放数据空间联盟
  • 系统验收文档(验收交付全套资料集)
  • 鸿蒙NEXT开发-学生管理系统小案例
  • 西安电子科技大学计算机科学与技术学院考研复试笔试、机试分数情况
  • Spring MVC 与 Spring Boot:从“手动挡”到“自动驾驶”的进化论,兼谈前后端分离的哲学
  • Linux 文件的三个时间:Access、Modify 和 Change
  • 14.7 LangChain Experimental 模块解析:解锁 Auto-GPT 开发新范式