Ugacomp

How to format a disk in Linux?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Formatting a disk in Linux is a fundamental task that may be necessary for various reasons, such as preparing a new disk for use, removing data securely, or fixing disk errors. This article will guide you through the process of formatting a disk in Linux using command-line tools.

Checking Disk Information

Before formatting a disk, it’s essential to identify the disk device and its partitions. You can use the lsblk command to list all block devices connected to your system and their respective partitions:

lsblk

The output of the lsblk command typically looks something like this:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 232.9G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0    30G  0 part /
└─sda3   8:3    0 202.4G  0 part /home
sdb      8:16   0   1.8T  0 disk 
└─sdb1   8:17   0   1.8T  0 part /mnt/data

This output provides information about all block devices connected to the system, including their names, sizes, types, and mount points (if applicable). In this example, sda and sdb are the disk names, with their respective partitions listed beneath them. The SIZE column shows the disk size, RO indicates whether the disk is read-only, and TYPE specifies the type of device (e.g., disk or partition). The MOUNTPOINT column displays where each partition is currently mounted, if applicable.

Knowing disk information before formatting in Linux is crucial to ensure accuracy, safety, and effectiveness. It allows users to identify the correct disk among multiple drives, understand the disk’s layout, check existing data or partitions for preservation, ensure compatibility with formatting commands, and avoid unintended consequences such as data loss. By verifying disk information beforehand, users can make informed decisions and execute the formatting process confidently, minimizing the risk of errors or complications.

Unmounting the Disk

Ensure the disk is unmounted before formatting to prevent data corruption. Use the umount command followed by the disk’s mount point:

sudo umount /dev/sdX

Replace /dev/sdX with the appropriate device identifier for the disk you wish to format.

Unmounting the disk before formatting in Linux is necessary to prevent data corruption and ensure a successful formatting process. Unmounting also ensures that no files or directories are actively being accessed or modified, reducing the risk of data loss or inconsistency.

It prevents the filesystem from being in use, allowing the formatting command to interact directly with the disk without interference. This step is essential for maintaining the integrity of the filesystem and ensuring that the formatting operation completes smoothly without any potential conflicts or errors arising from active disk usage.

Formatting the Disk

Once unmounted, you can proceed to format the disk using the mkfs command followed by the desired filesystem type. For example, to format a disk with ext4 filesystem:

sudo mkfs.ext4 /dev/sdX

Verifying the Format

After formatting, you can verify the disk format using the blkid command, which displays the UUIDs and filesystem types of block devices:

blkid /dev/sdX

Mounting the Disk

Once the disk is formatted, you can mount it back to the filesystem using the mount command:

sudo mount /dev/sdX /mnt/disk

Replace /mnt/disk with the desired mount point.

Automating Formatting with Fstab

To ensure the disk is mounted automatically upon system boot, you can add an entry to the /etc/fstab file. First, obtain the UUID of the formatted disk using blkid, then edit /etc/fstab:

sudo blkid /dev/sdX

Copy the UUID and edit /etc/fstab using a text editor:

sudo nano /etc/fstab

Add a new line in the following format:

UUID=your_UUID /mnt/disk ext4 defaults 0 2

Replace your_UUID with the UUID obtained from blkid, and /mnt/disk with the desired mount point.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.