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 - Type
nto create a new partition. - Choose the partition type (type
pfor primary) and define the partition number (usually1). - Press Enter on the starting and ending sector prompts to use all available disk space.
- Type
wto save the new partition table and exit the utility.
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. 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. The fdisk prompts are n, p, Enter, Enter, w:
sudo fdisk /dev/sdb
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.
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.