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:
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:
1. Access the disk using superuser privileges: `sudo fdisk /dev/sdb` 2. Type `n` to create a new partition. 3. Choose the partition type (type `p` for primary) and define the partition number (usually `1`). 4. Press *Enter* on the starting and ending sector prompts to use all available disk space. 5. Type `w` to 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: 1. Get the unique identifier (UUID) of the disk partition: `sudo blkid /dev/sdb1` 2. Open the `/etc/fstab` file with a text editor (like nano or vi): `sudo nano /etc/fstab` 3. Add the line at the end of the file: `UUID=your-uuid-here /mnt/data ext4 defaults 0 2`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.