Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active August 3, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeinbu/ae731e40eda5ef323c04dd107307d9e0 to your computer and use it in GitHub Desktop.
Save aeinbu/ae731e40eda5ef323c04dd107307d9e0 to your computer and use it in GitHub Desktop.

How to add a new disk in Linux

This post takes you through creating a partition, formatting it and finally mount it in the filesystem.

These procedures where tested on Linux Mint 19

List disks and partitions

$ sudo fdisk -l

The result will vary depending on the number of disk you have. On my computer I got these three entries:

  • /dev/sda primary physical disk
  • /dev/sdb secondary physical disk
  • /dev/sdc a USB memory stick currently plugged in
  • etc

Modify partition map for a disk

$ sudo fdisk /dev/sdb

Substitute the physical disk for /dev/sdb.

Inside fdisk you type m to list the available commands. Use them to list free space (F), list partitions (p) or create (n) or delete (d) one.

NOTE: You can specify size, rather than the ending sector numbers when partitioning. (Ie. type +200G to make a 200 Gigabyte partition.)

When you're finished, type w (for write) to perform the changes, or q to quit fdisk without performing the changes.

Format the partition

$ sudo mkfs -t ext4 /dev/sdb1

Substitute the physical disk and partition number for /dev/sdb1 (where sdb is the physical disk, and 1 is the partition number.)

Mount the partition

$ mkdir /my-new-partition
$ sudo mount /dev/sdb1 /my-new-partition

_Substitute the path in the file system where you would like to mount the partition for /my-new-partition. If you forget the leading / you wil mount it at a path relative to you current working directory.

If you want, you can mount the same partitions at multiple paths in the file system.

NOTE: The partition stays mounted only until next reboot. To make the changes persist over reboots, you need to write an entry in the /etc/fstab file. (See below)

You can list all mounted partitions with the df utility, like this:

$ df -h

You unmount a partition with umount:

$ sudo umount /my-new-partition
$ rmdir /my-new-partition

Persisting the mount with /etc/fstab

To make the mount persistent, you must edit the /etc/fstab table. Open it with you favorite text editor to add an entry (a new row)

/dev/sdb1   /my-new-partition   ext4   defaults   0   0

Use space(s) or tab(s) to seperate the columns. (The exact number of spaces or tabs is irrelevant)

NOTE: Since partition names are somewhat dynamic (especially when moving disks to new ports, or USB-connected drives may not always be enumerated in the same order), you could lock down a specific partition in the /etc/fstab file by substituting the partition's UUID for /dev/sdb1. You can use the blkid utility to find a partitions UUID:

$ blkid | grep /dev/sdb1

Copy only the UUID="00000000-0000-0000-0000-000000000000" part from the string and use it like this:

UUID="00000000-0000-0000-0000-000000000000" /my-new-partition   ext4   defaults   0   0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment