Storage management is one of the most important tasks in Linux server administration. Whether you need to add a new backup volume or expand the disk space of a VPS, knowing the correct commands prevents accidental data loss.
In this article, we present a practical step-by-step guide to managing disks and partitions via the terminal.
1. Identifying Connected Disks
Before making any changes, we need to list the available disks and verify what partitions already exist.
Essential commands:
lsblk: Shows the block device tree (disks and partitions) connected and their mount points.df -h: Displays the free and occupied space of all partitions currently mounted in the operating system.
Identify the disk you want to manipulate. Generally, SATA/Virtual disks appear as /dev/sdb, /dev/sdc, or /dev/vdb (in VPS environments).
2. Creating a Partition with fdisk
The fdisk utility is the classic tool for partition table manipulation.
The process:
- Access the disk using superuser privileges:
sudo fdisk /dev/sdb - Check the label with
sudo fdisk -l /dev/sdb.Disklabel type: gptis the usual case on a VPS.dosis MBR. - Type
nto create a new partition. On MBR,pselects primary. GPT does not ask that: confirm the number (usually1) and the first and last sectors with Enter to use the whole disk. - Type
wto write and exit.fdiskis interactive. Do not paste it in the same block asmkfs: the shell waits for the prompts and the later commands never reach the partition table.
3. Formatting the Filesystem
Creating the partition is not enough; you must format it so the system can read and write files. The most common and stable default filesystem in Linux is ext4.
How to format:
Run the mkfs command pointing to the created partition (and not to the entire disk): sudo mkfs.ext4 /dev/sdb1
4. Mounting the Partition (mount and fstab)
To start using the partition, we need to connect it to a folder in the system (mount point).
Temporary mount:
sudo mkdir /mnt/data sudo mount /dev/sdb1 /mnt/data
Persistent mount (after reboots):
If the server is restarted, the temporary mount will be lost. To make it permanent, add the entry in the /etc/fstab file:
- Get the unique identifier (UUID) of the disk partition:
sudo blkid /dev/sdb1 - Open the
/etc/fstabfile with a text editor (like nano or vi):sudo nano /etc/fstab - Add the line at the end of the file:
UUID=your-uuid-here /mnt/data ext4 defaults 0 2
5. The full sequence, with a read-only check first
mkfs erases the partition's contents. The first command only reads. If /dev/sdb shows a mountpoint, stop.
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT
df -h
sudo fdisk -l /dev/sdb
New partition, filesystem, and mount. On GPT, the fdisk prompts are n, Enter (number), Enter (first sector), Enter (last sector), w. On MBR, p comes after n, and only then the number. Exit with w before the commands below. This block is safe to paste:
sudo mkfs.ext4 /dev/sdb1
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
sudo blkid /dev/sdb1
Copy the UUID that blkid printed into /etc/fstab. The last field 2 asks for a filesystem check at boot, after the root filesystem:
UUID=paste-the-uuid-here /mnt/data ext4 defaults 0 2
sudo findmnt --verify
df -h /mnt/data
findmnt --verify reads fstab without mounting again. An error here is the boot that would drop into emergency mode. The system disk, usually /dev/sda or /dev/vda, stays out of this sequence.
When the partition does not mount
sudo findmnt --verifypoints at the fstab line with the wrong UUID. Copy theblkid /dev/sdb1output again, without quotes.lsblkwith nosdb1meansfdisknever wrote the table, or the disk was a different one. Do not runmkfsagain.sudo dmesg | tailafter the mount showswrong fs typewhenmkfs.ext4did not run on that partition.
Conclusion
Managing disks in Linux requires attention to detail, especially in typing device names correctly. Mastering these steps ensures complete flexibility to manage the growth of databases and media of your enterprise applications.