The fdisk
command in Linux is used for disk partitioning. It allows you to create, modify, and delete partitions on a hard disk. The fdisk
command is typically run with administrative privileges (e.g., as root or using sudo
) and requires caution as it directly affects the disk’s partition table.
Here’s an overview of how to use the fdisk
command:
- Selecting the Disk:
To start usingfdisk
, specify the disk device as an argument to the command. For example, to work with the first hard disk (/dev/sda
), use:
sudo fdisk /dev/sda
- Interactive Command Prompt:
Once you’re in thefdisk
utility, you’ll be presented with an interactive command prompt. You can issue various commands to manage the partitions on the selected disk. - Common
fdisk
Commands:
p
: Print the partition table, showing the existing partitions on the disk.n
: Create a new partition.d
: Delete an existing partition.t
: Change the partition type.l
: List the available partition types.w
: Write the changes and exit.q
: Quit without saving changes.
- Creating a New Partition:
To create a new partition, use then
command followed by the desired partition number, starting sector, and size. You can specify the size in sectors, cylinders, or using a specific size unit (e.g.,M
for megabytes orG
for gigabytes). For example:
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
- Deleting a Partition:
To delete an existing partition, use thed
command followed by the partition number. For example:
Command (m for help): d
Partition number (1-4): 1
- Changing the Partition Type:
To change the type of a partition, use thet
command followed by the partition number. You’ll be prompted to select the partition type from a list. For example:
Command (m for help): t
Partition number (1-4): 1
Hex code or GUID (L to show codes, Enter = 8300): L
- Saving Changes and Exiting:
Once you have made the desired changes, use thew
command to write the changes to the disk’s partition table and exitfdisk
. For example:
Command (m for help): w
- Examples:
- Create a new primary partition on
/dev/sda
:
sudo fdisk /dev/sda
n
p
[default options]
w
Delete an existing partition on /dev/sda
:
sudo fdisk /dev/sda
d
[partition number]
w
Change the type of a partition on /dev/sda
:
```
sudo fdisk /dev/sda
t
[partition number]
[desired partition type]
w