Understanding Disk Partitioning
Before a newly attached hard drive can be used on a Linux server, it typically needs to be partitioned. Partitions divide a physical disk into logical sections that can later be formatted with file systems and mounted for use by the operating system.
Linux provides several partitioning utilities, but one of the most widely available tools is fdisk. It is included with most Linux distributions and allows administrators to create, bewerken, and manage partition tables directly from the command line.
Because fdisk works at a very low level, it is important to use it carefully. Incorrect changes can result in permanent data loss.
Important Warning
Voordat u wijzigingen aanbrengt:
- Verify the correct disk device.
- Ensure the disk is not actively in use.
- Creëren back-ups of any important data.
- Confirm you are not modifying your operating system disk by mistake.
Modifying the wrong partition table can make data inaccessible and may require recovery procedures.
Identifying the Target Disk
Before launching fdisk, identify the device name assigned to the new drive.
WordPress-webhosting
Vanaf $ 3,99/maandelijks
Loop:
fdisk -l
You can also use:
lsblk
Voorbeelduitvoer:
NAME SIZE TYPE
sda 500G disk
├─sda1 100G part
├─sda2 200G part
└─sda3 200G part
sdb 100G disk
In this example:
/dev/sdb
is a new unpartitioned disk.
Starting fdisk
Launch fdisk using the target disk:
Goedkope VPS-server
Vanaf $ 2,99/maandelijks
fdisk /dev/sdb
On a brand-new drive, you may see a message indicating that no partition table exists yet.
Voorbeeld:
Device contains neither a valid DOS partition table nor GPT partition table.
This is normal for a new disk.
Displaying Available Commands
At any time, display available commands:
m
Common commands include:
| Command | Function |
|---|---|
| m | Display help menu |
| n | Create partition |
| d | Delete partition |
| p | Print partition table |
| t | Change partition type |
| w | Write changes and exit |
| q | Quit without saving |
| g | Create GPT partition table |
| o | Create DOS/MBR partition table |
Creating a GPT Partition Table
Modern systems commonly use GPT instead of the older MBR format.
Windows VPS-hosting
Toegang op afstand en volledig beheer
Create a GPT partition table:
g
Benefits of GPT include:
- Support for disks larger than 2 TB
- More partitions
- Verbeterde betrouwbaarheid
- Modern UEFI compatibility
For most current deployments, GPT is recommended.
Choose the partition-table type before creating partitions. Running g after adding partitions starts a new empty table and discards the pending layout. Use it only on the verified empty target disk.
Creating a New Partition
To create a partition:
n
With GPT, select the partition number and accept the default start sector. There are no primary or extended partition types. On an older MBR/DOS disk, fdisk may instead ask for p (primary) or e (extended), and primary partition numbers are limited to 1–4.
Choosing the First Sector
fdisk will suggest the default starting sector:
First sector:
In most situations, simply press Enter to accept the default.
Voorbeeld:
Using default value
Specifying Partition Size
You can define partition size using:
- Sectors
- Megabytes
- Gigabytes
Voorbeelden:
+500M
+10G
+100G
To create a 10 GB partition:
+10G
fdisk then creates the partition.
Viewing the Current Partition Layout
Display the partition table:
p
Voorbeeld:
Device Start End Sectors Size Type
/dev/sdb1 2048 20973567 20971520 10G Linux
This confirms the first partition has been created successfully.
Creating Additional Partitions
To create another partition:
n
Select the next partition number:
2
Accept the default first sector and choose the desired size.
Voorbeeld:
+50G
Repeat the process until all desired partitions are created.
Using Remaining Free Space
To create a partition that consumes all remaining disk space:
- Create a new partition.
- Accept the default first sector.
- Accept the default ending sector by pressing Enter.
Voorbeeld:
Using default value
The partition will automatically use all remaining available space.
Example Partition Layout
After creating two partitions, the output may resemble:
Device Start End Sectors Size Type
/dev/sdb1 2048 20973567 20971520 10G Linux
/dev/sdb2 20973568 209715199 188741632 90G Linux
Changing a Partition Type
Standaard, fdisk usually assigns:
Linux
If another partition type is required:
t
You can list available partition types:
L
Voorbeelden zijn onder meer:
- Linux
- Linux Swap
- EFI System
- Linux LVM
Saving Changes
Once the partition layout is correct, save the configuration:
w
fdisk writes the partition table to disk and exits.
Voorbeelduitvoer:
The partition table has been altered.
Syncing disks.
Exiting Without Saving
If you made a mistake and want to cancel all pending changes:
q
This exits fdisk without modifying the disk.
Refreshing Partition Information
Sometimes Linux does not immediately reload the updated partition table.
You can force a refresh:
partprobe
Or:
partx -u /dev/sdb
In some situations, a reboot may still be required.
Creating a Filesystem
Formatting destroys existing data on the selected partition. Choose exactly one of the following formats; do not run all three commands. The later mount and fstab example assumes ext4. A swap partition uses swapon and a swap fstab entry instead of mounting on /data.
Partitioning alone does not make the partition usable.
Create a filesystem:
EXT4
mkfs.ext4 /dev/sdb1
XFS
mkfs.xfs /dev/sdb1
Linux Swap
mkswap /dev/sdb1
Mounting the Partition
Create a mount point:
mkdir -p /data
Mount the partition:
mount /dev/sdb1 /data
Verifiëren:
df -h
Voorbeeld:
/dev/sdb1 9.8G 200M 9.1G 3% /data
Configuring Automatic Mounting
To mount automatically after reboot:
Retrieve the UUID:
blkid
Voorbeeld:
UUID="12345678-abcd-1234-abcd-123456789abc"
Add an entry to:
/etc/fstab
Voorbeeld:
UUID=12345678-abcd-1234-abcd-123456789abc /data ext4 defaults 0 2
Test the configuration:
mount -a
Common Troubleshooting
Device Does Not Appear
The scan command below applies only to the correct SCSI host, which may not be host0. It is not a generic fix for NVMe or every virtual-disk bus. Check the provider attachment status and the actual device bus first.
Rescan storage devices:
echo "- - -" > /sys/class/scsi_host/host0/scan
Or reboot the server.
Partition Table Not Reloaded
Loop:
partprobe
If unsuccessful, reboot the server.
Incorrect Device Selected
Never save changes if you notice the wrong disk was selected.
Exit immediately:
q
before writing the partition table.
Best Practices
- Use GPT for modern systems.
- Verify disk names before modifying partitions.
- Create backups before partitioning.
- Use UUIDs in
/etc/fstab. - Leave room for future growth when planning partitions.
- Avoid modifying active production disks without maintenance planning.
Final Notes
fdisk remains one of the most reliable and widely available Linux disk partitioning tools. Whether you are preparing additional storage on a speciale server, creating separate partitions for applications, or configuring new data volumes, understanding how to use fdisk safely is an essential Linux administration skill.
Once partitions are created, formatted, and mounted, the new storage becomes available for applications, databases, back-ups, and general server operations.
