How to add storage to Linux

wolfmath

Aaron Wolf

Posted on November 19, 2020

How to add storage to Linux

Intro

Maybe you have a home server that you'd like to have some more storage space on. Maybe you have a desktop or laptop that needs more storage. In this tutorial I will show you how to add an HDD or SDD (or even a flash drive) to a Linux computer.

I'm doing this exercise with a blank flash drive. If you'd also like to practice with a blank flash drive make sure it is unformatted.

Step 1: Find out the name of your new drive.

Prior to installing the new drive, open the terminal and run sudo fdisk -l. There you will see all of the drives and their names. I have a single NVME drive on my laptop, so the output looks like this...

Alt Text

I have 1 SSD named /dev/nvme0n1, with two partitions /dev/nvme0n1p1 and /dev/nvme0n1p2 (ignore any Disk /dev/loop things you may see).

Physically install the new drive then run sudo fdisk -l again to see what's new. Again, ignore any loops you may see. The new drive that wasn't there before is what you should be looking out for.

Alt Text

My new drive is called sdc. So all of my actions henceforth will be on sdc. This drive is completely blank, it is not formatted at all, so I need to format the drive.

Step 2: Format the Drive (optional if your drive is already formatted)

Warning: This step will wipe anything that might be on the drive. Skip this step if your drive already has information that you'd like to be available upon mounting.

In the terminal run sudo fdisk /dev/sdc replacing sdc with the name of your drive if it's different. You will get the following...

Alt Text

Feel free to type m to see all the options. First we will create a new partition table. Select g. You may get a warning if you're writing over an old partition table.

Alt Text

Set up a new partition with selection n. The prompt asks how many partitions you want and sector sizes. I'm leaving everything to the defaults. If fdisk prompts you if you want to remove a signature type y.

Alt Text

Then press p to see all your changes, and w to write your changes to the disk.

Alt Text

If you run sudo fdisk -l again you'll see that your changes were written to the disk. Notice how there's a 1 at the end of sdc. This indicates the partition. We will need that for the next step.

Alt Text

Lastly we will make an ext4 partition.

sudo mkfs.ext4 /dev/sdc1

Alt Text

Step 3: Mount the Drive

Most of the time drives are mounted to /mnt, but you can really mount this disk wherever you want. If you want more space in your home directory you'd mount to /home/[username]/, replacing [username] with whatever user you want to add storage to. I'm (theoretically) working on a server, so I'm mounting my drive to a directory in /mnt that I've named newdir. I personally think it's good practice to have a directory specific to the drive you want to mount.

This is really easy as all we need to do is...
sudo mount /dev/sdc1 /mnt/newdir
or more generally sudo mount [drive name] [location]

If we df -h we'll see the change.
Alt Text

To unmount the drive we use the umount command.
sudo umount /mnt/newdir. The directory will still be there, but the drive and it's contents will be gone.

Again df -h will show the change.

Step 4: Making mounting permanent

If you reboot your machine (assuming you didn't use the umount command above) you'll notice that the drive is no longer there. In order to make this a permanent mount you'll need to edit the configuration file etc/fstab. This file is read at boot so it knows which drives to mount where. You'll see the root file system already there.

sudo nano /etc/fstab. Feel free to use vi if you're a snob.

Alt Text

Add a new line. You can use spaces or tabs between the values. The first value is the device. The second is the location it should be mounted. The third value is the filesystem type. I'm not entirely sure what defaults does, but it always works for me. Google it ¯_(ツ)_/¯ . The next two numbers refer to read order. 0 0 or 0 1 work best.
/dev/sdc1 /mnt/newdir ext4 defaults 0 0

Alt Text

WARNING: If you make a mistake with the root filesystem your machine will not boot again

Make sure everything works with the following command
sudo mount -a

Alt Text

If you get no output you're good to go. If however you get a warning make sure to go back and check your work.

Now you're done.

Enjoy your new expanded storage.

💖 💪 🙅 🚩
wolfmath
Aaron Wolf

Posted on November 19, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to add storage to Linux
linux How to add storage to Linux

November 19, 2020