• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / fdisk command in linux with examples

fdisk command in linux with examples

The fdisk command in Linux is used to create, delete, modify, and manage disk partitions. It is one of the most commonly used tools for partitioning storage devices.

Table of Contents

Toggle
  • Summary of fdisk Commands
  • 1. Display Partition Table (-l option)
    • Example Output:
  • 2. Enter fdisk Interactive Mode
  • 3. View Current Partition Table
  • 4. Create a New Partition
    • Step 1: Enter fdisk
    • Step 2: Create a New Partition
    • Step 3: Save and Exit
  • 5. Delete a Partition
  • 6. Change Partition Type
  • 7. Format the New Partition
  • 8. Mount the New Partition
  • 9. Resize a Partition (Requires resize2fs)

Summary of fdisk Commands

Command Description
fdisk -l List partitions
fdisk /dev/sdb Open partition manager for /dev/sdb
p Print partition table
n Create a new partition
d Delete a partition
t Change partition type
w Write changes and exit
q Quit without saving

The fdisk command is powerful for managing disk partitions. However, be cautious while modifying partitions to avoid data loss.


1. Display Partition Table (-l option)

To list all available disks and their partitions:

sudo fdisk -l

Example Output:

Disk /dev/sda: 500GB
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 976773119 974673920 499G 83 Linux

๐Ÿ”น Explanation:

  • Device column shows the partitions (/dev/sda1, /dev/sda2).
  • Boot column (*) marks the bootable partition.
  • Size column shows partition sizes.

2. Enter fdisk Interactive Mode

To modify partitions, open fdisk for a specific disk:

sudo fdisk /dev/sdb

๐Ÿ”น This opens an interactive mode where you can create, delete, and modify partitions.


3. View Current Partition Table

Inside fdisk, press:


๐Ÿ”น Displays partition details of /dev/sdb.


4. Create a New Partition

Step 1: Enter fdisk

sudo fdisk /dev/sdb

Step 2: Create a New Partition

Press n โ†’ Enter (for new partition).

๐Ÿ”น It will ask for the partition type:

  • Press p for Primary partition.
  • Press e for Extended partition.

Then, specify the partition number, starting sector, and size (e.g., +10G for a 10GB partition).

Step 3: Save and Exit

Press:


๐Ÿ”น This writes the changes and exits fdisk.


5. Delete a Partition

Inside fdisk:

  1. Press d โ†’ Enter (Deletes a partition).
  2. Enter the partition number to delete.
  3. Press w to save and exit.

6. Change Partition Type

Inside fdisk:

  1. Press t โ†’ Enter (Change partition type).
  2. Enter the partition number.
  3. Enter the type code (e.g., 82 for Swap, 83 for Linux).
  4. Press w to save changes.

7. Format the New Partition

After creating a partition, format it before use:

sudo mkfs.ext4 /dev/sdb1

๐Ÿ”น Formats /dev/sdb1 with ext4 filesystem.

For Swap Partition:

sudo mkswap /dev/sdb1
sudo swapon /dev/sdb1

8. Mount the New Partition

To use the new partition, mount it:

sudo mount /dev/sdb1 /mnt

To make it permanent, add to /etc/fstab:

echo "/dev/sdb1 /mnt ext4 defaults 0 2" | sudo tee -a /etc/fstab

9. Resize a Partition (Requires resize2fs)

If you extend a partition, resize the filesystem:

sudo resize2fs /dev/sdb1

๐Ÿ”น This resizes the filesystem to match the new partition size.


Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners ๐Ÿš€
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet โ€“ Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) โ€“ Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

ยฉ 2025 - All Rights Reserved Disclaimer & Privacy Policy