fdisk Command in Linux with Examples
fdisk Command
fdisk Command stands for Fixed-disk or Format-disk. Fdisk command is used to create partition or delete partition after the initial partitions is set during OS installation. It also allows you to format the disk.
Only the root user can utilize the fdisk command. When the fdisk command is used, it creates ony 4 default partitions and any further partition is only considered as extended.
Syntax
fdisk < options> < partition-name>
To display all available partitions
To view the partitions that created during OS installation use the following fdisk command.
Example
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ed6bd
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 1951 15360000 83 Linux
/dev/sda3 1951 2206 2048000 82 Linux swap / Solaris
Here, it shows the default partition we made at the initial stage.
To isolate the partition
If we have many partitions i.e if we have attached many hard disks it will be viewed as many partitions. So we can view the specific partition alone using fdisk command.
Example:
[root@localhost ~]# fdisk /dev/sdb
Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00012c78
Device Boot Start End Blocks Id System
/dev/sdb1 1 121602 976760832 83 Linux
So, now we can view the selected partition alone.
Creating the new partition
To create the new partition in the respective hard-disk just follows the steps below.
Since the default partitions are 4, the new partition should be created only in extended area.
To view all the available fdisk command just type ' m' in the fdisk search column.
Command (m for help): m
Command action
a - toggle a bootable flag
b - edit bsd disk label
c - toggle the dos compatibility flag
d - delete a partition
l - list known partition types
m - print this menu
n - add a new partition
o - create a new empty DOS partition table
p - print the partition table
q - quit without saving changes
s - create a new empty Sun disk label
t - change a partition' s system id
u - change display/entry units
v - verify the partition table
w - write table to disk and exit
x - extra functionality (experts only)
Example
[root@localhost ~]# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000727e9
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 25561 204800000 83 Linux
/dev/sda3 25561 27601 16384000 82 Linux swap / Solaris
/dev/sda4 27601 60801 266687008+ 5 Extended
Command (m for help): n
First cylinder (27601-60801, default 27601):
Using default value 27601
Last cylinder, +cylinders or +size{K,M,G} (27601-60801, default 60801): +10G
Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000727e9
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 25561 204800000 83 Linux
/dev/sda3 25561 27601 16384000 82 Linux swap / Solaris
/dev/sda4 27601 60801 266687008+ 5 Extended
/dev/sda5 27601 28906 10490388+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
To delete the existing partition
To create the partition that we created before can be made very simple with following fdisk command.
Example
[root@localhost ~]# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000727e9
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 25561 204800000 83 Linux
/dev/sda3 25561 27601 16384000 82 Linux swap / Solaris
/dev/sda4 27601 60801 266687008+ 5 Extended
/dev/sda5 27601 28906 10490388+ 83 Linux
Command (m for help): d
Partition number (1-5): 5
Command (m for help): p
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000727e9
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 25561 204800000 83 Linux
/dev/sda3 25561 27601 16384000 82 Linux swap / Solaris
/dev/sda4 27601 60801 266687008+ 5 Extended
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
To Format the partition
Formatting process is recommended normally for pen-drives and some other devices. It should be used with proper view, because it will lead to lose some data. In this example we have formatted for 10 GB.
Example
[root@localhost ~]# mkfs.ext4 /dev/sda6
mke2fs 1.41.12 (17-May-2010)
/dev/sda6 alignment is offset by 2560 bytes.
This may result in very poor performance, (re)-partitioning suggested.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=1 blocks, Stripe width=0 blocks
610800 inodes, 2441872 blocks
122093 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2503999488
75 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
To find size of particular partition
There may be several extended partitions created according to our disk capability. So to view the particular partitions size ' -s ' option is to be used in fdisk command.
Example
[root@localhost ~]# fdisk -s /dev/sda2
204800000
To toggle the boot partition
It means that the boot flag can be toggled to some other partitions available. By default it tells the master boot record to boot from this partition (i.e.) boot flagged partition. If it is changed to some other partition then it will boot from that partition,
Example
[root@localhost ~]# fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ed6bd
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 1951 15360000 83 Linux
/dev/sda3 1951 2206 2048000 82 Linux swap / Solaris
Command (m for help): a
Partition number(1-4) : 2
Command (m for help): p
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ed6bd
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 * 39 1951 15360000 83 Linux
/dev/sda3 1951 2206 2048000 82 Linux swap / Solaris
Now, the sda2 partition is assigned with boot flag. But the default flag still will be sda1 until next reboot of your system.
List of partition types
Fdisk allows us to create many partitions according to disk availability. But it will support only for recommended partition types.
Example
[root@localhost ~]# fdisk /dev/sda
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 < 32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext' d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 < 3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Here, ' l ' option will show you what are the partitions it will support.
Expert commands
In fdisk these are the commonly used options but still more options are available in expert area. But this area should be used only by the experts. It will edit the partition table to the core level. So it’ s not recommended for others.
Example
[root@localhost ~]# fdisk /dev/sda
Command (m for help): x
Expert command (m for help): m
Command action
b move beginning of data in a partition
c change number of cylinders
d print the raw data in the partition table
e list extended partitions
f fix partition order
g create an IRIX (SGI) partition table
h change number of heads
i change the disk identifier
m print this menu
p print the partition table
q quit without saving changes
r return to main menu
s change number of sectors/track
v verify the partition table
w write table to disk and exit
These are the list of expert fdisk command in fdisk system.