- How to Partition and Format Disk Drives on Linux
- #What is disk formatting in Linux?
- #What is partitioning in Linux?
- #How to Partition and Format Disk Drives on Linux
- #Prerequisites
- #How to view disks in Linux
- #How to view existing partitions in Linux
- #How to Partition a Disk in Linux
- #How to format a disk in Linux
- #How to mount a disk in Linux
- #Conclusion
- Mantas Levinas
- Join Cherry Servers Community
- How to Format Disk Partitions in Linux
- Checking the Partitions
- Formatting Disk Partition in Linux
- Formatting Disk Partition with ext4 File System
- Formatting Disk Partition with FAT32 File System
- Formatting Disk Partition with NTFS File System
- Mounting the Disk Partition in Linux
- Understanding the Linux File System
How to Partition and Format Disk Drives on Linux
Formatting and partitioning disks is a key aspect of Linux administration. You can use formatting and partitioning to address use cases like prepping storage media for use, addressing space issues with existing disks, or wiping a filesystem.
This article will walk you through how you can partition and format disks to complete common Linux administration tasks.
#What is disk formatting in Linux?
Disk formatting is the process that prepares a storage partition for use. Formatting deletes the existing data on the partition and sets up a filesystem.
Some of the most popular filesystems for Linux include:
- Ext4 — Ext4 is a common default filesystem on many modern Linux distributions. It supports file sizes up to 16TB and volumes up to 1EB. It is not supported on Windows by default.
- NTFS — NTFS is a popular filesystem developed by Microsoft. It supports 8PB max volume and file sizes. The Linux kernel added full support for NTFS in version 5.15.
- FAT32 — Is an older filesystem, but you may still see it used in the wild. It supports a 4GB max file size and a 2TB max volume size. Many *nix and Windows operating systems support FAT32.
#What is partitioning in Linux?
Partitioning is the process of creating logical boundaries on a storage device. Common examples of storage devices include hard disk drives (HDDs), solid-state drives (SSDs), USB flash drives, and SD cards. Creating a partition on a drive logically separates it from other partitions. This logical separation can be useful for a variety of scenarios, including limiting the growth of a filesystem and installing multiple operating systems on a single drive.
#How to Partition and Format Disk Drives on Linux
Now let’s dive into partitioning and formatting disks on a Linux system.
#Prerequisites
Before we begin, you’ll need:
- Access to the terminal of a Linux system. We’ll use Ubuntu 22.04 LTS.
- sudo/root privileges
- An available disk you want to format and partition. We are going to use a server with custom partitioning layout from Cherry Servers.
- Backups of any data you don’t want to lose (optional)
#How to view disks in Linux
To view available disks in Linux, run this command:
Output should look similar to:
The fsdisk output above included loop devices which are logical pseudo-devices, but not real disks. If you need a more refined view of your disks, use the lsblk -I 8 -d command. «-I 8» specifies a the kernel device number for block devices and the -d excludes partitions.
The output should look similar to:
If you need more information to properly identify your drives, use lshw -class disk . The output will include additional identifying information such as the product, size, vendor, bus, and logical name (the device’s path), similar to this:
#How to view existing partitions in Linux
Before you create a new partition, you may want to view your existing partitions. To view existing partitions in Linux, use the lsblk command. The output should look similar to:
Partitions have a TYPE of part and are nested under their disks in the output like sda1 in our example.
If you want to see information like file system types, disk labels and UUIDs, use the command lsblk -f . The output should look similar to:
#How to Partition a Disk in Linux
There are several ways to partition disks in Linux, including parted and gparted , but we’ll focus on the popular fdisk utility here. For our case, we’ll assume our disk is mounted on /dev/sda . We will create a primary partition and use the default partition number, first sector, and last sector that fdisk selects. You can modify these options based on your requirements.
Note: If you’re partitioning a disk that is currently mounted, first unmount it with the command `umount .
To begin, we’ll open our drive in fdisk with this command:
That will launch the interactive fdisk utility and you should see output similar to:
At the Command (m for help): prompt, type n to create a new partition. The output should look similar to:
It shows that the disk that is mounted on /dev/sda directory has one primary partition that is formatted and being used at the moment.
We’ll press enter to select the default and create a new primary partition. Then, we’ll be prompted to give a partition number.
We’ll use the default of 2 and then get prompted for a sector number.
We’ll press enter to accept the default first sector, and then get prompted for a last sector.
Again, we’ll press enter to accept the default and fdisk will create the partition. Note that if we wanted to create a smaller partition, we could use a smaller gap between our first and last block. This would enable us to create multiple partitions on the drive.
The full output looks like this:
You may enter p to see a partition table and make sure your changes are correct:
As you can see, we now have two partitions on the /dev/sda disk. At the Command (m for help): prompt, input a w to write the changes to the Linux system. The output should look similar to:
fdisk will then exit and you’ll be back at Linux shell. We can see our newly created partition sda by running the command lsblk /dev/sda . The output should look similar to:
#How to format a disk in Linux
Now that our disk is fully partitioned, we can format the newly created sda2 partition. The general syntax for formatting a disk partition in Linux is:
For example, to format our newly created /dev/sda2 partition, we can use this command:
The output should look similar to:
To use an NTFS filesystem instead, the command is:
To use a FAT32 filesystem instead, the command is:
The -F parameter specifies the FAT-TYPE , which determines if the file allocation tables are 12, 16, or 32-bit.
#How to mount a disk in Linux
Once a disk is partitioned and formatted, we can mount the filesystem in Linux.
First, if your mount point doesn’t already exist, created it with the mkdir command. The general command syntax is:
For example, to make our mount point /var/cherry , use this command:
Next, we mount our partition using the mount command. The general command structure to mount a disk partition in Linux is:
Note: If you omit the -t option, the mount command will default to auto and attempt to guess the correct filesystem type.
For example, to mount our /dev/sda2 (which has an Ext4 filesystem) to /var/cherry in read/write mode, we can use this command»
mount -t ext4 -o rw /dev/sda2 /var/cherry
If there are no errors, the command will not return any output.
You can confirm your partitions mount point is correct with the lsblk /dev/sda command. The output should include a new mountpoint /var/cherry for your newly formatted /dev/sda2 device:
Finally, to ensure the disk automatically mounts when your Linux system boots, you need to add it to /etc/fstab .
⚠️ Warning: Be careful! Errors in /etc/fstab can cause your system not to boot!
The general format for an /etc/fstab partition entry is
Paraphrasing Ubuntu’s Fstab File Configuration, enables or disables backups using the command dump. It can be set to 1 (enabled) or 0 (disabled) and is generally disabled. determines the order fsck checks the partition for errors when the system boots. Generally, a system’s root device is 1 and other partitions are 2. 0 disables the fsck check on boot.
To edit /etc/fstab , open it in a text editor like nano or vim and make the changes. For our /dev/sda2 partition mounted at /var/cherry , we’ll use this configuration:
/dev/sda2 /var/cherry ext4 rw 0 0
Save the changes and close your text editor when you’re done.
#Conclusion
That’s it! Now you know the basics of how to partition and format disks on Linux. For a deeper dive on the topic of partitioning, formatting, and mounting drives, we recommend reading the man pages for the specific tools we used here like the mkfs. utilities (e.g. mkfs.ext4), fdisk, mount, and fstab.
Mantas Levinas
Helping engineers learn 💡 about new technologies and ingenious IT automation use cases to build better systems 💻
Join Cherry Servers Community
Get monthly practical guides about building more secure, efficient and easier to scale systems on an open cloud ecosystem.
How to Format Disk Partitions in Linux
A disk partition must be formatted and mounted before use. The formatting process can also be done for several other reasons, such as changing the file system, fixing errors, or deleting all data.
In this tutorial, you will learn how to format and mount disk partitions in Linux using ext4, FAT32, or NTFS file system.
- A system running Linux
- A user account with sudo or root privileges
- Access to a terminal window / command line (Activities >Search >Terminal)
Checking the Partitions
Before formatting, locate a partition you wish to format. To do so, run the lsblk command that displays block devices. Block devices are files that represent devices such as hard drives, RAM disks, USB drives, and CD/ROM drives.
The terminal prints out a list of all block devices as well as information about them:
- NAME – Device names
- MAJ:MIN – Major or minor device numbers
- RM – Whether the device is removable (1 if yes, 0 if no)
- SIZE – The size of the device
- RO – Whether the device is read-only
- TYPE – The type of the device
- MOUNTPOINT – Device’s mount point
We will use the /dev/sdb1 partition as an example.
The lsblk command without additional options does not display information about the devices’ file systems.
To display a list containing file system information, add the -f option:
The terminal prints out the list of all block devices. The partitions that do not contain information on the file system in use are non-formatted partitions.
Note: Consider learning how to create partitions in Linux.
Formatting Disk Partition in Linux
There are three ways to format disk partitions using the mkfs command, depending on the file system type:
The general syntax for formatting disk partitions in Linux is:
mkfs [options] [-t type fs-options] device [size]
Formatting Disk Partition with ext4 File System
1. Format a disk partition with the ext4 file system using the following command:
2. Next, verify the file system change using the command:
The terminal prints out a list of block devices.
3. Locate the preferred partition and confirm that it uses the ext4 file system.
Formatting Disk Partition with FAT32 File System
1. To format a disk with a FAT32 file system, use:
2. Again, run the lsblk command to verify the file system change and locate the preferred partition from the list.
Formatting Disk Partition with NTFS File System
1. Run the mkfs command and specify the NTFS file system to format a disk:
The terminal prints a confirmation message when the formatting process completes.
2. Next, verify the file system change using:
3. Locate the preferred partition and confirm that it uses the NFTS file system.
Mounting the Disk Partition in Linux
Before using the disk, create a mount point and mount the partition to it. A mount point is a directory used to access data stored in disks.
1. Create a mount point by entering:
2. After that, mount the partition by using the following command:
sudo mount -t auto /dev/sdb1 [mountpoint]
Note: Replace [mountpoint] with the preferred mount point (example: /usr/media ).
There is no output if the process completes successfully.
3. Verify if the partition is mounted using the following command:
Understanding the Linux File System
Choosing the right file system before formatting a storage disk is crucial. Each type of file system has different file size limitations or different operating system compatibility.
The most commonly used file systems are:
Their main features and differences are:
File System | Supported File Size | Compatibility | Ideal Usage |
FAT32 | up to 4 GB | Windows, Mac, Linux | For maximum compatibility |
NTFS | 16 EiB – 1 KB | Windows, Mac (read-only), most Linux distributions | For internal drives and Windows system file |
Ext4 | 16 GiB – 16 TiB | Windows, Mac, Linux (requires extra drivers to access) | For files larger than 4 GB |
Note: Refer to our Introduction to Linux File System article to learn more about the evolution and features of different Linux file systems.
After following this tutorial, you should be able to format and mount a partition in Linux in various file systems. Partition manipulation is essential for efficient data management, and next, we recommend learning how to delete a partition in Linux.