Linux Format HDD: How to Partition and Format Disk Drives

Linux Format HDD: How to Partition and Format Disk Drives
In case you are wondering how to format HDD in Linux, you will need three basic actions: identify the disk drive with the use of lsblk command, partition the disk with the use of fdisk or parted and build a file system with mkfs. Linux format HDD seems to be something that can be accomplished by means of one command only. However, it is not and the additional actions are necessary in order to protect you, because one wrong device name leads to the total erasure of everything stored on the wrong disk drive.
This article will help you to accomplish all the actions in order to format an HDD on Linux step by step: identify the device, partition, choose one of the Linux disk formats (ext4, XFS, NTFS, FAT32), mount and set up automatic mounting after boot. At the end, you will learn how to securely erase the contents of a drive in order to delete everything that is stored there.
WARNING! All the commands in this tutorial that touch a partition table or filesystem will destroy data on the chosen drive. Double check the device names before you hit Enter. In case you are not sure – stop and double check it again.
Things you need
Linux computer with a connected drive (whether SATA, USB or a virtual disk in VM).
Root access (or sudo) privileges
Backup of all the data on the target disk which is still needed. There is no Undo for mkfs command.
Step 1: Identify the disk
In order to format a disk on Linux, you will need to find out its device name. Making a guess in this stage is how people destroy their system drive.
List block devices with lsblk command
bash
lsblkplain
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 256G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 63.5G 0 part /
└─sda3 8:3 0 8G 0 part [SWAP]
sdb 8:16 0 1.8T 0 diskThe tree should be read upwards: sda drive contains partitions and mount points and thus it is used. sdb drive is an unused 1.8 TB disk with no partitions mounted: it is a new drive. The SIZE column is your best friend.
Check detailed information with fdisk -l command
bash
sudo fdisk -l /dev/sdbYou will see the model, precise size of the disk in bytes and sectors and if the partition table is present on this disk. If fdisk recognizes the model and the capacity of the disk as expected – you have found the right device.
Find out what is mounted with df -h command
bash
df -hdf will list only the mounted filesystems, so you will not see anything on the new, not formatted yet disk. Its purpose here is to protect you: make sure that /dev/sdb is not listed in any of the mount points – if it is, it means that it is used somewhere and you should not format it.
Step 2: Create partition table with fdisk or parted
Partition table is necessary for OS to recognize the start and the end of each partition. If you plan to format a disk bigger than 2 TB (in our case the 1.8 TB is just under this border, but in case of new drives it usually exceeds this value), GPT partition table should be created instead of MBR. Both commands allow creating GPT tables; parted is more reliable than fdisk in case of very large drives and scripting, fdisk is more convenient in interactive mode.
Interactive partitioning with fdisk
bash
sudo fdisk /dev/sdbNotice that we use device name sdb (whole disk), not sdb1 (partition). Inside fdisk:
Type g to create a new GPT table.
Type n to create a new partition. Press enter to accept default partition number, first sector and last sector in order to use the entire disk.
Type p to print the layout of new table and check it.
Type w to apply the table and exit.
Nothing changes until the last step. You can exit without applying any changes with q command without any harm.
Partitioning script with parted
bash
sudo parted /dev/sdb mklabel gpt
sudo parted -a optimal /dev/sdb mkpart primary 0% 100%The -a optimal flag allows to align the partition to sectors of the physical drive, which is important in case of modern 4K-sector drives. In contrast to fdisk, parted applies changes immediately so there is no step to review the table, so be sure before you run it.
In any case, repeat the lsblk command. Now you will see sdb1 partition under sdb disk.
Step 3: Format the partition with mkfs
Partitioning marks the start and the end of the partition; formatting creates a filesystem inside it. Linux disk formatting is a family of commands rather than one: mkfs allows you to create filesystem of every common disk format in Linux, the syntax is always mkfs.<type>
ext4: the default Linux disk format
bash
sudo mkfs.ext4 /dev/sdb1ext4 is a perfect choice in case if the drive will be used only by Linux operating systems: it is journaled, fast, well tested and recognized by every possible installer or rescue system.
XFS: for large files and volumes
bash
sudo mkfs.xfs /dev/sdb1XFS is much better in handling large volumes and high parallel I/O than ext4. That's why Red Hat chose it as the default file system. The price of this is that you cannot decrease the size of XFS partition, only increase.
FAT32 and NTFS: for sharing across multiple OSes
bash
sudo mkfs.vfat -F 32 /dev/sdb1 # FAT32
sudo mkfs.ntfs -Q /dev/sdb1 # NTFS (quick format)If the drive has to work across several OSes, pick FAT32 (universal and old-fashioned but working everywhere, including old devices and EFI partitions, but it has 4 GB limit for each file) or NTFS (more modern and handles large files well, but will work in Linux only with ntfs-3g driver).
Regardless of the choice, mkfs will print the UUID of the created file system. Write it somewhere: you will need it in the following step, and it can be retrieved with sudo blkid /dev/sdb1.
How the result should look like
Successful formatting will not give any output except for the block groups or allocation units, no errors, and UUID at the end. Check it with lsblk -f command, which will give you the additional info:
plain
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
sdb
└─sdb1 ext4 1.0 5f3c8a2e-9b1d-4e7a-a6c2-1d0e8f4b2c91The output above should look similar to yours: in the FSTYPE column you will see the file system that you have created. Your partition should be located under its parent disk. If the FSTYPE field is empty, formatting was unsuccessful or you have formatted the wrong disk. If the partition itself is missing – go back to the previous step. In case of existing partitions, the output will differ, but every old filesystem signature should be gone from the partition you reformatted.
Step 4: Mount the filesystem and automount it with /etc/fstab
Formatting a hard drive on Linux does not allow seeing the new drive in file tree: you should mount it manually.
Mount the drive manually
bash
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/dataMake sure that it works by using df -h /mnt/data command; this mount survives until you unmount it or reboot.
Persistent mount with /etc/fstab
In order to mount the disk automatically at boot, you will need to add a new row to the /etc/fstab file. Use UUID instead of /dev/sdb1, because device letters may vary when you connect or disconnect other disks; but the UUID of the drive will not change.
plain
UUID=5f3c8a2e-9b1d-4e7a-a6c2-1d0e8f4b2c91 /mnt/data ext4 defaults 0 2You should replace ext4 with xfs, vfat or ntfs depending on the format which you have chosen. In order to avoid problems, follow two simple rules here. First of all, test it before reboot:
bash
sudo mount -aThis will notify you about any error in fstab file before you will see an emergency shell at boot. Second of all, before editing /etc/fstab, make a copy of it: sudo cp /etc/fstab /etc/fstab.bak.
Step 5: Securely wipe a disk
Deletion of files or even reformatting allows recovering the data with special software. In case you plan to sell, recycle or repurpose the disk – wipe it completely.
Wipe with shred
bash
sudo shred -v -n 3 /dev/sdbThis command will overwrite the disk 3 times with random data and show you the progress. Use -z parameter in order to finish with zeros and hide the shred procedure from other tools.
Zero fill with dd
bash
sudo dd if=/dev/zero of=/dev/sdb bs=64K status=progress conv=fdatasyncOne-time zero pass is enough in order to destroy the possibility of recovery in standard software, and it allows you to find broken sectors of the drive, because dd stops on them. However, be careful with this command: dd got the nickname of "disk destroyer" because of its dangerousness. Read twice: transpose if and of parameters, or substitute sda instead of sdb and you will overwrite whatever you pointed out without asking.
It is important that both of these commands are not suitable for SSDs. Due to the mechanism of wear leveling, neither of them will guarantee the erasure of the drive. In case of SSDs use blkdiscard or manufacturer's secure erase utility.
Where you can find help
Each of the tools in this tutorial has an embedded man page which is better than any blog post: man fdisk, man parted, man mkfs.ext4 and man fstab will tell you about options which were skipped here. In case of quick reference, fdisk --help and parted --help will list the most popular flags and the tldr command (you can install the tldr package) will give you worked examples for each tool. In case of unexpected errors of any command, paste the error message and the output of lsblk -f into your distribution's forum or Linux question site on StackExchange.
FAQ
What is the best disk format for Linux?
ext4 is better for general use; XFS is more suitable for very large volumes or heavy parallel workload. In case of cross-platform drive, use FAT32 (universal but limits each file to 4 GB) or NTFS (allows large files and works on both Linux and Windows).
Is it possible to format a disk in Linux without loss of data?
No, you will lose it. Formatting implies destruction of existing file system in the partition, so if you need this data – transfer it beforehand, or use resizing/converting tools for particular file systems instead of mkfs.
How can I find the format of the Linux disk?
Use either lsblk -f or sudo blkid command. These commands will print the filesystem type and UUID for every partition.
Do I need to partition my disk before formatting it?
Yes, and it is recommended practice, even for the drive with one partition only, because tools expect the partition table to be present. You can technically put a file system on a raw device with mkfs.ext4 /dev/sdb, but some tools will reject it.
Summary
Whether you searched "linux format HDD", "format disk Linux", "linux format drive", "linux format disk" or "format hard drive Linux" – all the actions will be the same: use lsblk and confirm the drive with fdisk -l, partition it with fdisk or parted, format it with proper mkfs command, mount and bind it in /etc/fstab via UUID.
Get started with Orbit Servers
Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.
Get startedRelated products
Written by
Julius