How to Partition, Format, and Mount a New Disk in Linux With fdisk and parted
Learn to partition, format, and mount a new disk in Linux with fdisk and parted: build a safe practice disk, format with ext4 and xfs, fix a real stale filesystem signature error, and add a...
Every Linux server, VM, and cloud instance eventually needs more than one filesystem: a separate volume for a database, extra space attached after the fact, or a second disk you want to use for backups. Before Linux can store a single file on that new disk, the disk has to be divided into partitions and each partition has to be formatted with a filesystem. This tutorial teaches that whole process from scratch, using the two tools every Linux administrator eventually has to know: fdisk and parted.
Table Of Content
- Prerequisites
- Understanding Partition Tables: MBR vs GPT
- Step 1: Build a Safe Practice Disk
- Step 2: Identify Your Target Disk
- Step 3: Create a Partition With fdisk
- Verify the Partition Exists
- Step 4: Format the Partition
- Step 5: Mount the Partition and Test It
- Step 6: Unmount the Partition
- Step 7: Create a Partition With parted Instead
- The Gotcha: parted’s fs-type Argument Doesn’t Actually Format Anything
- Step 8: Handle a Stale Filesystem Signature
- Step 9: Get the Partition’s UUID and Add a Persistent Mount
- Step 10: Delete a Partition Safely
- Step 11: Clean Up Your Practice Environment
- Common Mistakes and Troubleshooting
- “Device or resource busy” when writing the partition table
- Partition created but not visible in lsblk
- “mkfs: appears to contain an existing filesystem”
- Choosing MBR when you meant GPT (or the reverse)
- How to Verify Everything Worked
- Next Steps
A partition is a logical section of a physical disk that the operating system treats as its own independent storage area, the same way a whole disk would be treated. A partition table is a small structure written to the very start of the disk that records where each partition begins and ends, and what type it is. Before a disk can hold any partitions, it needs one of these tables, and Linux systems today use one of two formats: the older MBR (Master Boot Record) or the modern GPT (GUID Partition Table). A partition on its own is just an empty labeled section of the disk; it cannot store files until you also format it with a filesystem such as ext4 or xfs, which is the layer that actually organizes files and directories on the storage.
By the end of this tutorial, you will have created a partition table, created and deleted partitions with both fdisk and parted, formatted partitions with two different filesystems, mounted them by hand and persistently through /etc/fstab, and hit (and fixed) a real, easy-to-miss mistake that trips up even experienced administrators. Every command in this guide was executed while writing it, on the exact tool versions named below, so the output shown is real captured output, not a paraphrase of the manual. The task scope follows DigitalOcean’s own fdisk and parted tutorial, with every command independently re-run, verified against the official fdisk manual and the GNU parted manual, and extended with a safe disposable practice disk and a real stale-filesystem gotcha neither source covers.
Prerequisites
- A Linux system with a bash shell and root or
sudoaccess. This guide was tested on Ubuntu 26.04 LTS, withfdiskfrom util-linux 2.41.3, GNU parted 3.6, mke2fs 1.47.2 (forext4), and xfsprogs 6.18.0 (forxfs). The concepts and commands are the same on any modern distribution; only the package manager used to installpartedorxfsprogschanges. - Comfort running commands in a terminal. You do not need any prior partitioning experience: every command below is explained before you run it.
- No spare physical disk is required. Step 1 shows you how to build a safe, disposable practice disk out of an ordinary file, so you can follow every step on a laptop or a cloud VM with a single disk, without any risk to your real data.
A warning before you start: partitioning and formatting are destructive operations. Writing a new partition table, or running a format command on the wrong device, permanently destroys whatever data was there. Every command in this tutorial targets a disposable loopback file you create yourself in Step 1, specifically so you can make mistakes safely while you learn. When you later repeat these steps on a real disk, always confirm the exact device name with lsblk first, and back up anything you cannot afford to lose.
Understanding Partition Tables: MBR vs GPT
Before creating anything, it helps to know what you are choosing between.
MBR is the original IBM PC partitioning scheme. It stores the partition table in a single 512-byte sector at the start of the disk, which limits it to addressing about 2.2 TB of space (2,199,023,255,040 bytes, using 512-byte sectors) and to exactly four primary partitions. If you need more than four, one primary partition can be turned into an extended partition that holds additional logical partitions inside it, a workaround GPT does not need.
GPT is the modern replacement defined as part of the UEFI specification. It removes the 2.2 TB ceiling entirely and, as you will see directly from fdisk itself in Step 3, supports up to 128 partitions by default with no primary/extended distinction. Unless you are working with old BIOS-only hardware or a legacy dual-boot setup that specifically requires MBR, GPT is the right default for new disks today, and it’s what this tutorial uses.
Step 1: Build a Safe Practice Disk
Instead of risking a real disk while you learn, you can create an ordinary file, tell the Linux kernel to treat it as a block device using a loop device, and partition that instead. Everything you do to a loop device is fully reversible: delete the file and it is as if the disk never existed.
# Context: any Linux system with root access, working in a scratch directory.
# Purpose: create a 1 GiB file to act as a disposable practice disk.
mkdir -p ~/partition-practice && cd ~/partition-practice
dd if=/dev/zero of=test-disk.img bs=1M count=1024 status=none
ls -lh test-disk.img
Expected output: -rw-r--r-- 1 root root 1.0G ... test-disk.img. The dd command copies 1024 blocks of 1 MiB of zeros from /dev/zero into the file, giving you an exactly 1 GiB file of empty space.
# Purpose: attach the file to the kernel's loop-device driver so it behaves like a real disk.
sudo losetup -fP test-disk.img
sudo losetup -a | grep test-disk
Expected output: something like /dev/loop14: [2049]:772829 (/root/partition-practice/test-disk.img). The -f flag tells losetup to pick the first free loop device automatically, and -P tells the kernel to scan the file for partitions once one exists. Note the device name it printed, such as /dev/loop14; you will use that exact name for the rest of this tutorial. Every reference to /dev/loop14 below should be replaced with whatever loop device your system assigned.
Step 2: Identify Your Target Disk
Before you partition anything, real disk or practice file, get in the habit of confirming exactly which device you are about to modify. Two commands do this:
lsblk
sudo fdisk -l /dev/loop14
lsblk (“list block devices”) shows every disk and partition the kernel currently knows about, in a tree view, without needing root. fdisk -l shows more detail about one specific device, including its size and current partition table, if any. Running both before you make changes is the single most effective habit for avoiding an accidental wipe of the wrong disk, since it is the last chance to notice you have the wrong device name.
Step 3: Create a Partition With fdisk
fdisk is an interactive, menu-driven program: you launch it against a device, then type single-letter commands. Launch it now:
sudo fdisk /dev/loop14
You will land on a Command (m for help): prompt. From here, type the following commands one at a time, pressing Enter after each:
g: create a new, empty GPT partition table on the disk.n: create a new partition. fdisk will then ask for a partition number, a first sector, and a last sector; press Enter three times to accept the defaults (partition 1, using all remaining free space).w: write the new partition table to disk and exit. Nothing is actually written until this command runs; everything before it only changes fdisk’s in-memory copy.
Here is exactly what that session looks like, captured from a real run:
Command (m for help): g
Created a new GPT disklabel (GUID: 2B01E059-C304-4B45-8311-4E13236CC5FE).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-2097118, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097118, default 2095103):
Created a new partition 1 of type 'Linux filesystem' and of size 1022 MiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Two details are worth pausing on. First, notice fdisk’s own prompt: Partition number (1-128, default 1). That is GPT’s 128-partition default limit, confirmed directly by the tool itself, exactly as described above. Second, if this is a genuinely blank device, fdisk may print Device does not contain a recognized partition table and silently stage a temporary DOS (MBR) label in memory before you even type your first command; this is harmless and gets replaced the moment you run g, but it can be surprising to see mentioned before you have typed anything.
Verify the Partition Exists
lsblk /dev/loop14
Expected output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop14 7:14 0 1G 0 loop
└─loop14p1 259:0 0 1022M 0 part
The new partition shows up as loop14p1 (loop devices append p1, p2, and so on; a normal disk like /dev/sdb would instead show sdb1, sdb2). If you do not see the partition here, see the Troubleshooting section below before continuing.
Step 4: Format the Partition
A partition is just an empty, labeled region of disk until you format it with a filesystem. This tutorial uses ext4 first, the default filesystem on Ubuntu, Debian, and many of their derivatives, well-tested and a safe general-purpose choice. Later in this tutorial you will format a different partition as xfs, the default on RHEL-family distributions since RHEL 7 and a common choice for very large files or heavy parallel I/O.
# Purpose: create an ext4 filesystem on the new partition, with a human-readable label.
sudo mkfs.ext4 -L testdata /dev/loop14p1
Expected output (abridged): mke2fs 1.47.2 (1-Jan-2025) followed by lines about discarding device blocks, creating the filesystem, allocating group tables, and writing inode tables, ending with Writing superblocks and filesystem accounting information: done. The -L flag sets a volume label, which makes the partition easier to identify later with tools like lsblk -f or blkid.
Step 5: Mount the Partition and Test It
Formatting creates the filesystem; mounting attaches it to a directory (called a mount point) so normal file operations can reach it.
# Purpose: create a mount point, attach the partition to it, and confirm it works.
sudo mkdir -p /mnt/test-partition
sudo mount /dev/loop14p1 /mnt/test-partition
df -h /mnt/test-partition
echo "hello from sxz test" | sudo tee /mnt/test-partition/hello.txt
cat /mnt/test-partition/hello.txt
Expected output:
Filesystem Size Used Avail Use% Mounted on
/dev/loop14p1 988M 276K 921M 1% /mnt/test-partition
hello from sxz test
df -h confirms the partition is mounted and shows how much space is used; writing and reading back hello.txt confirms the filesystem genuinely works, not just that the mount command returned success.
Step 6: Unmount the Partition
sudo umount /mnt/test-partition
Always unmount a filesystem before you repartition or reformat the underlying device. Trying to modify a partition that is still mounted is one of the most common sources of the Device or resource busy error covered in Troubleshooting below.
Step 7: Create a Partition With parted Instead
fdisk is interactive by nature, which is great for learning but awkward to put in a script. parted can do the same job non-interactively, one command per line, which is why it shows up in automation and provisioning scripts. First, remove the existing partition so you have a clean disk to work with again:
sudo fdisk /dev/loop14
At the prompt, type d (delete the partition, it will be selected automatically since there is only one), then w (write and exit). Now recreate the layout with parted in a single pair of non-interactive commands:
# Purpose: create a new GPT label and a partition spanning the whole disk, without an interactive prompt.
sudo parted -s /dev/loop14 mklabel gpt
sudo parted -s /dev/loop14 mkpart primary ext4 0% 100%
sudo parted -s /dev/loop14 print
The -s flag means “script mode”: run non-interactively and don’t ask for confirmation. mkpart primary ext4 0% 100% reads as “create a partition named primary, hinted as ext4, spanning from the very start to the very end of the disk.” Expected output from print:
Model: Loopback device (loopback)
Disk /dev/loop14: 1074MB
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 1073MB 1072MB ext4 primary
The Gotcha: parted’s fs-type Argument Doesn’t Actually Format Anything
That File system: ext4 column is misleading if you are new to parted. The ext4 in mkpart primary ext4 0% 100% is only a type hint stored in the partition table, it does not run mkfs for you. You can confirm this yourself:
sudo blkid /dev/loop14p1
If the partition table still contains a filesystem signature from a previous format at those same sectors (as it will if you are following this tutorial from Step 4 onward on the same practice disk), blkid reports the old filesystem, since parted never touched the actual data blocks. This leads directly into the next step.
Step 8: Handle a Stale Filesystem Signature
This is a real mistake that even experienced administrators run into, reproduced here exactly as it happened while writing this tutorial. After deleting the ext4 partition in Step 7 and recreating it at the same sectors with parted, formatting it as xfs failed:
$ sudo mkfs.xfs -L testxfs /dev/loop14p1
mkfs.xfs: /dev/loop14p1 appears to contain an existing filesystem (ext4).
mkfs.xfs: Use the -f option to force overwrite.
Why this happens: deleting a partition with fdisk (or recreating one with parted) only removes the entry from the partition table. It does not erase the filesystem superblock that mkfs.ext4 wrote to the underlying disk blocks earlier. When a new partition lands on the same sectors, tools like blkid and mkfs can still see the old filesystem’s signature and, correctly, refuse to silently destroy it. This is a safety feature, not a bug, but it surprises people who assume deleting a partition wipes its contents.
The fix is to explicitly wipe old signatures before formatting:
# Purpose: erase filesystem/RAID/LVM signatures so mkfs can format cleanly.
sudo wipefs -a /dev/loop14p1
Expected output: /dev/loop14p1: 2 bytes were erased at offset 0x00000438 (ext4): 53 ef, the exact two magic bytes that identify an ext4 superblock. With the stale signature cleared, formatting succeeds cleanly:
sudo mkfs.xfs -L testxfs /dev/loop14p1
sudo mkdir -p /mnt/test-partition
sudo mount /dev/loop14p1 /mnt/test-partition
df -hT /mnt/test-partition
Expected output:
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop14p1 xfs 958M 51M 908M 6% /mnt/test-partition
The alternative to wipefs, forcing the format directly with mkfs.xfs -f, works too, but running wipefs first is clearer about what is happening and is the safer habit to build, especially on a real disk where you want to be certain about what you are erasing before you do it.
Step 9: Get the Partition’s UUID and Add a Persistent Mount
Everything so far is mounted only until the next reboot. To make a mount permanent, Linux uses /etc/fstab, a table of filesystems the system mounts automatically at boot. Persistent entries should reference a partition’s UUID (a unique identifier baked into the filesystem itself) rather than a device name like /dev/loop14p1, because device names can shift between boots, especially with multiple disks, while the UUID never changes.
sudo blkid /dev/loop14p1
Expected output: /dev/loop14p1: LABEL="testxfs" UUID="0aad2b05-b7c0-48fd-8c90-b2edb4eafe44" BLOCK_SIZE="512" TYPE="xfs" .... Copy the UUID value, then add a line like this to /etc/fstab (use sudo visudo-style caution: edit with sudo nano /etc/fstab or a similar editor, never overwrite the file):
UUID=0aad2b05-b7c0-48fd-8c90-b2edb4eafe44 /mnt/test-partition xfs defaults,nofail 0 2
Each field matters: the UUID identifies the partition, /mnt/test-partition is where it mounts, xfs is the filesystem type, defaults uses standard mount options, 0 disables legacy dump backups, and 2 tells fsck to check this filesystem after the root filesystem at boot. The nofail option is a safety net worth adding to any non-critical entry: without it, a missing or failed disk at boot time can drop the whole machine into an emergency recovery shell; with it, the boot simply continues without that mount.
Before you reboot, always validate a new fstab line with:
sudo mount -a
This mounts everything listed in /etc/fstab that isn’t already mounted, using the same parsing the boot process uses, so any typo or syntax error shows up immediately in a terminal you control, instead of during the next unattended reboot.
Step 10: Delete a Partition Safely
To remove a partition entirely (again, only on your disposable practice disk), unmount it first, then use fdisk:
sudo umount /mnt/test-partition
sudo fdisk /dev/loop14
At the prompt: d to delete (fdisk auto-selects the only partition when there is just one; with multiple partitions it asks which number), then w to write and exit. Deleting a partition does not erase its data, as you saw firsthand in Step 8; it only removes the partition table entry. If you actually need the underlying data gone, follow the deletion with wipefs -a or, for genuinely sensitive data, an overwrite tool, since a deleted partition’s contents are still recoverable until something overwrites those blocks.
Step 11: Clean Up Your Practice Environment
# Purpose: detach the loop device and remove the practice file, leaving your system exactly as it was.
sudo losetup -d /dev/loop14
rm ~/partition-practice/test-disk.img
This detaches the loopback device and deletes the backing file. Nothing about your real disks was ever touched.
Common Mistakes and Troubleshooting
“Device or resource busy” when writing the partition table
This means the kernel cannot re-read the partition table because something is still using the disk, most often a filesystem that is still mounted, or a swap partition that is still active. Run sudo umount on any mounted partitions from that disk (or sudo swapoff for swap) and try again.
Partition created but not visible in lsblk
Occasionally the kernel’s in-memory view of a disk’s partitions falls out of sync after changes, particularly on disks that were already in use. Force a rescan with sudo partprobe /dev/loop14 (or sudo partx -u /dev/loop14 if partprobe is not installed) before assuming something went wrong.
“mkfs: appears to contain an existing filesystem”
Covered in depth in Step 8: this is mkfs correctly refusing to silently destroy a leftover filesystem signature from a previous format at the same sectors. Run sudo wipefs -a <device> first if you are certain you want to proceed, or investigate before doing so if you did not expect a signature to be there.
Choosing MBR when you meant GPT (or the reverse)
If a disk needs to boot on very old BIOS-only hardware, or is part of an existing MBR-based dual-boot setup, you may specifically need MBR (fdisk’s o command instead of g). For any new disk on modern hardware, default to GPT: it has no practical size limit at today’s disk capacities and no four-partition ceiling to work around.
How to Verify Everything Worked
Before you consider a real-disk partitioning job finished, confirm all of the following: lsblk -f shows the expected partition, filesystem type, and label; df -h shows it mounted at the expected path with sensible free space; a test file written to the mount point survives an umount and re-mount; and, if you added an /etc/fstab entry, sudo mount -a completes with no errors. Only after all four checks pass should you trust the partition with real data or reboot the machine to confirm the fstab entry mounts automatically.
Next Steps
From here, natural directions to keep learning include LVM (Logical Volume Manager), which adds a flexible layer on top of partitions so you can resize and snapshot volumes without unmounting them; using parted resizepart or growpart to expand a partition that has run out of space; and setting up software RAID with mdadm across multiple disks for redundancy. All three build directly on the partition and filesystem fundamentals you just practiced.








No Comment! Be the first one.