How to Format a Linux Disk

For a desktop Linux system, there are many graphical tools available for disk partitioning and formatting. The most famous are GParted and QTParted. Nevertheless, there are many instances in which you might need to format a hard drive from the command line, particularly if you are running a server. Once you are finished, you will have a partitioned hard drive with a Linux file system ready for use. The example used in this article will create a single partition with an Ext4 Linux file system. Follow each command by pressing "Enter".

Image Credit: BananaStock/BananaStock/Getty Images

Set Up a New Partition

Step 1

Open a terminal window and log in as root:

$ su (or "sudo su" on some distributions)

Step 2

Find out the name of your hard drive. To do this, enter the following from the command prompt as root:

fdisk -l

Step 3

Choose your newly installed drive after fdisk lists the current drives. The current hard drive will already have partitions assigned, and the output will look like this:

Disk /dev/sda: 16.1 GB, 16139354112 bytes /dev/sda1 * 1 1874 15052873+ 83 Linux

/dev/sda2 1875 1962 706860 5 Extended

/dev/sda5 1875 1962 706828+ 82 Linux swap / Solaris

Your new hard drive will not have partitions, so all you will see will be a line such as:

Disk /dev/sdb: 16.1 GB, 16139354112 bytes

Step 4

Check to see if your disk is already mounted with this command:

df

Step 5

Unmount your drive if it is mounted using this command:

umount /dev/sdb

Step 6

Create a new partition by typing this command:

fdisk /dev/sdb

Step 7

The fdisk prompt will open. Press "n" to make the new partition, and then press "Enter."

Step 8

Press "p" to create a primary partition, and then press "Enter."

Step 9

Press "1" to create the first partition followed by "Enter."

Step 10

Enter the default for both the first and last cylinders when your computer asks for this information. This will make the partition cover the entire disk rather than just part of it.

Step 11

Press "t" to change the file system type followed by "Enter."

Step 12

Type "L" to see a list of known types followed by "Enter."

Step 13

Type "83" for "Linux" followed by "Enter."

Step 14

Press "w" to write the partition to the disk (this cannot be undone), and then press "Enter."

Format the New Partition

Step 1

Format the new partition that you created with the following command:

mkfs.ext4 /dev/sdb1

Step 2

Create a new directory and mount the new drive with these commands:

mkdir /media/newdrive (or whatever name you prefer)

mount /dev/sdb1 /media/newdrive

Step 3

Edit your fstab file so that the new drive will be mounted at boot. Fstab is the Linux file system configuration file to mount partitions at boot. You can edit /etc/fstab with the "nano" command or "vi" depending on which editor you prefer.

nano /etc/fstab or # vi /etc/fstab

Step 4

Add the following line to the end of fstab:

/dev/sdb1 /media/newdrive ext4 defaults 1 2

Step 5

Save the fstab file.

Things You'll Need

  • Computer running Linux

  • Newly installed hard drive or removable drive

Tip

If this is your first time formatting with Linux, try it on an empty drive that will not be used for anything critical.

Warning

Formatting in Linux is a relatively quick process, but you should pay close attention to the details because most changes of this magnitude are irreversible.

references & resources