The Linux Command — fdisk

Create and manage hard drive partitions from the Linux command line

fdisk is a command-line partition editor that allows you to create, edit, and manage the partitions on your computer's hard drives from the command line. There are other similar tools, such as cfdisk and parted, but fdisk is easily the most universal and probably the simplest.

Even if you don't plan on using it regularly, it's good for any Linux user to have a general idea of how to work with fdisk, since it's a great fallback when graphical options aren't available, and it's probably already on your Linux system.

Empty office cubicles
Jetta Productions/Blend Images/Getty Images

Partition a Drive With fdisk

This guide is going to walk you through the basics of partitioning a hard drive with fdisk. This is a simple but powerful tool that offers a lot of options to manage your drives. For a more complete technical breakdown, refer to the technical manual at the end of the guide.

Partitioning your hard drive will erase all data on it. Be sure to back up your data or work with a blank drive.

  1. Start by finding out the location of your hard drive. You can try:

    ls /dev

    Then, try to figure out which drive it is. Your primary drive is probably /dev/sda, but you'd need to make sure.

    You can also take a look at /etc/fstab to see which drives are mounted where. If it gives you UUID numbers, you can search for each partition's UUID, and see which ones match.

    sudo blkid /dev/sda1
  2. Next, list out the current partition table on the drive using the -l flag. Replace "/dev/sda" with your actual drive.

    sudo fdisk -l /dev/sda
    Linux fdisk list partitions

    If you're working with a blank drive, there shouldn't be much there.

  3. Run the fdisk command again. This time, without any flags. This will bring you into the fdisk console.

    sudo fdisk /dev/sda
    Linux fdisk console
  4. Once you're in the fdisk console, you can start working with your partitions. First, if you want to list the partitions like before, you can enter the p key.

    Linux fdisk list partitions from console
  5. Then, use the n key to create a new partition.

  6. The prompt will then ask if you want to make a primary or extended partition. Choose p(primary).

    Linux fdisk set partition type
  7. Choose a partition number. The default is 1, for the first partition, so use that.

    Linux fdisk set partition number
  8. Then, it'll ask you to choose the first sector. Don't worry too much about this. Just press Enter for the default.

    Linux fdisk set first sector
  9. The next prompt will let you choose the size of your partition. You can specify the partition size in either megabytes(M) or gigabytes(G). Assuming that you're setting up a drive to install Linux, 512M is a good place to start for a boot partition.

    Linux fdisk set partition size
  10. Once that's done, you can view your new partition again with the p key.

    Linux fdisk partition created
  11. If you plan on using your new partition as a boot partition, you'll need to toggle the bootable flag on. Do that by pressing a. Then, if asked, enter the partition number. If this is the only one on your drive, it's 1.

  12. Now, you can create the other partitions on your drive. Start by entering n again.

  13. Press 1 again for a primary partition.

  14. Use the default partition number. If you're starting fresh, it's 2.

  15. Use the default first sector too. It will start immediately after the previous partition. Press Enter to continue.

  16. Once again, choose the size of your drive. If you're looking to set up a root partition with a separate home partition, something like +20G will work well. If you want to occupy the rest of the available space, just hit Enter.

    Linux fdisk create remaining partitions
  17. Repeat these steps for each new partition that you want to create on the drive. When you're done, press w to write the partition table to the drive.

  18. Now, you can use the mkfs command to format the partitions for use. For most Linux systems, the EXT4 filesystem is what you want, so you can run the following set of commands to set up your partitions. This assumes that you're working on /dev/sda and that you set up separate home and root partitions.

    sudo mkfs.ext4 /dev/sda1
    sudo mkfs.ext4 /dev/sda2
    sudo mkfs.ext4 /dev/sda3
    Linux format partitions
  19. Your newly partitioned and formatted drive is ready to use. If you've been following along, you can install and run a Linux distribution like Arch Linux off of the partitions you just created.

Fdisk Synopsis

Here's a technical breakdown of fdisk and its options and capabilities:

fdisk [-u] [-b sectorsize] [-C cyls] [-H heads] [-S sectsdevice

fdisk -l [-u] [device ...]

fdisk -s partition ...

fdisk -v

Fdisk and Partitions

Hard disks can be divided into one or more logical disks called partitions. This division is described in the partition table found in sector 0 of the disk.

In the BSD world, one talks about 'disk slices' and a 'disklabel.'

Linux needs at least one partition, namely for its root file system. It can use swap files and/or swap partitions, but the latter is more efficient. So, usually one will want a second Linux partition dedicated as a swap partition.

On Intel-compatible hardware, the BIOS that boots the system can often only access the first 1024 cylinders of the disk. For this reason, people with large disks often create a third partition, just a few MB large, typically mounted on /boot, to store the kernel image and a few auxiliary files needed at boot time, so as to make sure that this stuff is accessible to the BIOS.

There may be reasons of security, ease of administration and backup, or testing, to use more than the minimum number of partitions.

How Fdisk Works

fdisk (in the first form of invocation) is a menu-driven program for the creation and manipulation of partition tables. It understands DOS-type partition tables and BSD or SUN-type disklabels.

The device is usually one of the following: 

  • /dev/hda /dev/hdb /dev/sda /dev/sdb
  • (/dev/hd[a-h] for IDE disks, /dev/sd[a-p] for SCSI disks, /dev/ed[a-d] for ESDI disks, /dev/xd[ab] for XT disks).

(A device name refers to the entire disk.)

The partition is a device name followed by a partition number. For example, /dev/hda1 is the first partition on the first IDE hard disk in the system. Disks can have up to 15 partitions. See also/usr/src/linux/Documentation/devices.txt.

BSD/SUN-Type Disklabel

A BSD/SUN type disklabel can describe 8 partitions, the third of which should be a 'whole disk' partition. Do not start a partition that actually uses its first sector (like a swap partition) at cylinder 0, since that will destroy the disklabel.

RIX/SGI-Type Disklabel

An IRIX/SGI type disklabel can describe 16 partitions, the eleventh of which should be an entire 'volume' partition, while the ninth should be labeled 'volume header.' The volume header will also cover the partition table, i.e., it starts at block zero and extends by default over five cylinders. The remaining space in the volume header may be used by header directory entries.

No partitions may overlap with the volume header. Also do not change its type and make some file system on it, since you will lose the partition table. Use this type of label only when working with Linux on IRIX/SGI machines or IRIX/SGI disks under Linux.

DOS-Type Partition Table

A DOS-type partition table can describe an unlimited number of partitions. In sector 0 there is room for the description of 4 partitions (called 'primary'). One of these may be an extended partition; this is a box holding logical partitions, with descriptors found in a linked list of sectors, each preceding the corresponding logical partitions. The four primary partitions, present or not, get numbers 1-4. Logical partitions start numbering from 5.

In a DOS type partition table the starting offset and the size of each partition are stored in two ways: as an absolute number of sectors (given in 32 bits) and as a Cylinders/Heads/Sectors triple (given in 10+8+6 bits). The former is OK; with 512-byte sectors, this will work up to 2 TB. The latter has two different problems. First of all, these C/H/S fields can be filled only when the number of heads and the number of sectors per track is known.

Secondly, even if we know what these numbers should be, the 24 bits that are available do not suffice. DOS uses C/H/S only, Windows uses both, Linux never uses C/H/S.

Disk Geometry

If possible, fdisk will obtain the disk geometry automatically. This is not necessarily the physical disk geometry (indeed, modern disks do not really have anything like a physical geometry, certainly not something that can be described in simplistic Cylinders/Heads/Sectors form), but is the disk geometry that MS-DOS uses for the partition table.

Usually, all goes well by default, and there are no problems if Linux is the only system on the disk. However, if the disk has to be shared with other operating systems, it is often a good idea to let an fdisk from another operating system make at least one partition. When Linux boots, it looks at the partition table and tries to deduce what (fake) geometry is required for good cooperation with other systems.

Consistency Check

Whenever a partition table is printed out, a consistency check is performed on the partition table entries. This check verifies that the physical and logical start and endpoints are identical and that the partition starts and ends on a cylinder boundary (except for the first partition).

Some versions of MS-DOS create a first partition that does not begin on a cylinder boundary, but on sector 2 of the first cylinder. Partitions beginning in cylinder 1 cannot begin on a cylinder boundary, but this is unlikely to cause difficulty unless you have OS/2 on your machine.

A sync() and a BLKRRPART ioctl() (reread partition table from disk) are performed before exiting when the partition table has been updated. Long ago it used to be necessary to reboot after the use of fdisk. I do not think this is the case anymore; indeed, rebooting too quickly might cause a loss of not-yet-written data. Note that both the kernel and the disk hardware may buffer data.

Dos 6.x Warning

The DOS 6.x FORMAT command looks for some information in the first sector of the data area of the partition and treats this information as more reliable than the information in the partition table. DOS FORMAT expects DOS FDISK to clear the first 512 bytes of the data area of a partition whenever a size change occurs. DOS FORMAT will look at this extra information even if the /U flag is given — we consider this a bug in DOS FORMAT and DOS FDISK.

The bottom line is that if you use cfdisk or fdisk to change the size of a DOS partition table entry, then you must also use dd to zero the first 512 bytes of that partition before using DOS FORMAT to format the partition. For example, if you were using cfdisk to make a DOS partition table entry for /dev/hda1, then (after exiting fdisk or cfdisk and rebooting Linux so that the partition table information is valid) you would use the command "dd if=/dev/zero of=/dev/hda1 bs=512 count=1" to zero the first 512 bytes of the partition.

Be extremely careful if you use the dd command, since a small typo can make all of the data on your disk useless.

For the best results, you should always use an OS-specific partition table program. For example, you should make DOS partitions with the DOS FDISK program and Linux partitions with the Linux fdisk or Linux cfdisk program.

fdisk Options

-b sectorsize: Specify the sector size of the disk. Valid values are 512, 1024, or 2048. (Recent kernels know the sector size. Use this only on old kernels or to override the kernel's ideas.)

-C cyls: Specify the number of cylinders of the disk. We have no idea why anybody would want to do so.

-H heads: Specify the number of heads of the disk. (Not the physical number, of course, but the number used for partition tables.) Reasonable values are 255 and 16.

-S sects: Specify the number of sectors per track of the disk. (Not the physical number, of course, but the number used for partition tables.) A reasonable value is 63.

-l: List the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that exists) are used.

-u: When listing partition tables, give sizes in sectors instead of cylinders.

-s partition: The size of the partition (in blocks) is printed on the standard output.

-v: Print version number of fdisk program and exit.

Was this page helpful?