How to Format and Mount a New Drive on Linux

Setting up a new drive on a Linux system is a common task for system administrators and power users. In this guide, we’ll walk through the process of identifying, formatting, and mounting a new drive, as well as setting it up to mount automatically at boot time.

Identifying the Drive

Before you can format and mount your new drive, you need to identify it. The drive can be identified using several command-line utilities, such as lsblk, which lists information about all available block devices. This tool is powerful and provides a clear layout of all the drives and partitions on your system.

  1. Open your terminal.
  2. Enter the following command:
lsblk -f

This will display a list of all block devices, along with their mount points if they have any. You’ll be looking for a drive that does not have a mount point already, which is likely to be your new NVMe drive.

Formatting the Drive

Once you have identified your drive (for our purposes, /dev/[drive-name]), the next step is to format it. We will use the ext4 file system.

  1. Enter the following command:
sudo mkfs.ext4 /dev/[drive-name]

Caution: Ensure you have the correct drive identifier (/dev/[drive-name] in this example), as formatting will erase all data on the drive.

Mounting the Drive

You’ll need to decide where you want to mount your drive. For instance, we’ll use /mnt/mydrive for this example. Replace /mnt/mydrive with the directory where you prefer to mount your drive.

  1. Create a mount point:
sudo mkdir /mnt/mydrive

Skip this step if you’re using an existing directory.
2. Mount the drive:

sudo mount /dev/[drive-name] /mnt/mydrive

Automating the Mount at Boot

To have your drive mount automatically on boot, you’ll edit the /etc/fstab file.

  1. Use the following command to add an entry to fstab:
echo "/dev/[drive-name] /mnt/mydrive ext4 defaults 0 0" | sudo tee -a /etc/fstab

This will append the necessary line to your fstab without having to edit the file manually.

Confirming the Mount

After setting up the fstab, verify the configuration:

  1. Execute:
sudo mount -a

This will attempt to mount all filesystems listed in fstab. If there are no errors, you’re good to go.
2. Additionally, you can run:

sudo mount

to see all currently mounted filesystems.

Conclusion

You’ve successfully identified, formatted, and mounted your new drive on your Linux system, and set it up to mount automatically on boot. As always with system administration tasks, ensure you have backups and double-check commands before executing them to avoid accidental data loss.

Happy computing!