Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux fdisk Command: Partition Hard Disk

fdisk is a command-line utility in Linux that is used to manage disk partitions. It allows you to create, delete, and modify partitions on hard drives, solid-state drives, and other disk devices.

Here's a tutorial on the Linux fdisk command:

  1. Display available disk devices:

    Use the lsblk or fdisk command to list all available disk devices:

    lsblk
    

    or

    sudo fdisk -l
    
  2. Start fdisk on a specific disk device:

    To work with a specific disk device, run fdisk as the root user. Replace /dev/sdX with the device identifier for your disk (e.g., /dev/sda, /dev/sdb):

    sudo fdisk /dev/sdX
    

    This will start the fdisk utility in interactive mode for the specified disk.

  3. Common fdisk commands:

    • p: Print the current partition table
    • n: Create a new partition
    • d: Delete a partition
    • t: Change a partition's type
    • w: Write the changes to the disk and exit
    • q: Quit without saving changes
  4. Create a new partition:

    • Type n and press Enter to create a new partition
    • Choose the partition type (primary or extended)
    • Specify the partition number (1-4 for primary partitions)
    • Set the first and last sectors (use the default values to use the entire available space)
    • Type w and press Enter to write the changes to the disk
  5. Delete a partition:

    • Type d and press Enter to delete a partition
    • Specify the partition number you want to delete
    • Type w and press Enter to write the changes to the disk
  6. Change a partition's type:

    • Type t and press Enter to change the partition type
    • Specify the partition number you want to change
    • Enter the new partition type's hexadecimal code (e.g., 83 for Linux, 82 for Linux swap)
    • Type w and press Enter to write the changes to the disk
  7. Exit fdisk without saving changes:

    If you made a mistake or want to cancel the changes, type q and press Enter to quit fdisk without saving any changes.

Remember that modifying partitions can lead to data loss, so always ensure that you have a backup of your data before using fdisk. By understanding the fdisk command and its various options, you can effectively manage disk partitions on your Linux system.

  1. How to use fdisk command in Linux: The fdisk command is used for disk partitioning. To use fdisk:

    sudo fdisk /dev/sdX
    

    Replace /dev/sdX with the appropriate disk identifier.

  2. Formatting partitions after using fdisk: After partitioning, format the partitions using a file system. For example, to format a partition as ext4:

    sudo mkfs.ext4 /dev/sdXY
    

    Replace /dev/sdXY with the appropriate partition identifier.