How to Format, Mount, and Persist a Drive on Linux
Here’s how to add a new drive to your Linux server.
- List block devices before plugging in the drive
- Plug in the drive, list it again to identify it
- Check if there’s an existing filesystem
- Format the drive if needed
- Create a mount point
- Verify the mount
- Get the drive’s UUID
- Add the entry to fstab
- Test fstab without rebooting
IDENTIFYING THE STORAGE DRIVE AND FILESYSTEM
Before plugging in the drive, use this command to list the hard drives and partitions:
lsblkNow plug in the drive and run the command again. Take note of what the new drive is (this is just a USB):
lsblkVerify if there is a filesystem in the new volume:
sudo file -s /dev/[disk-name]sudo file -s /dev/sdc1If the output has ‘data’ (/dev/sdc: data) then it means there’s no filesystem.
In my case, it was xfs. It’s up to you if you want to reformat the filesystem.
REFORMATTING THE DRIVE
IMPORTANT: Verify that you are formatting the correct drive first. You do not want to risk erasing the data from the wrong drive.
sudo mkfs.[filesystem] -f /dev/[disk-name]sudo mkfs.xfs -f /dev/sdc1CREATING A MOUNT POINT
Create the mount point directory:
mkdir [ FOLDER YOU WANT TO MOUNT IT TO ]mkdir /test-folderOptional — Change the ownership:
sudo chown [ USER-OWNER ]:[ GROUP-OWNER ] [ MOUNT POINT ]sudo chown $USER:$USER /test-folderThis makes the account that you’re logged in the owner of the directory.
Mount the disk to the new directory:
sudo mount /dev/[disk-name] /[mountpoint]sudo mount /dev/sdc1 /test-folderConfirm that it’s been added to the mount point:
df -hENSURE THESE SETTINGS PERSIST AFTER A REBOOT
We’ll have to edit the /etc/fstab file, which is responsible for telling the OS how to automatically mount disk partitions and filesystems during the boot process.
Get Lumenyx’s stories in your inbox
Join Medium for free to get updates from this writer.
First we have to find the UUID for the new filesystem:
sudo blkidNow we have to edit the /etc/fstab file:
sudo vim /etc/fstabAppend /etc/fstab with:
UUID=[UUID-you-copied] [mountpoint] [type-of-filesystem] [configuration-options] [dump] [pass]In my case, the command is:
UUID=eea4436d-154c-4bde-b885-940d7ed42971 /test-folder xfs defaults,nofail 0 0What is nofail?
Specifying
nofailmeans that if the drive fails to mount at boot, then the OS will continue to boot without it.By default, not specifying
nofailcan cause the boot process to hang waiting for the device.Specifying
nofailis important for removable drives like USB sticks as they may not always be plugged in.
Run daemon-reload:
sudo systemctl daemon-reloadTest out your changes without rebooting by telling the OS to manually mount everything listed in /etc/fstab with this command:
sudo mount -aYou’re all done.








