How to Partition, Format, and Mount a Disk on Ubuntu 20.04 Using Command Line

Share This:

Mount Disk Ubuntu

If you run an Ubuntu 20.04 server, or even a workstation, at some point you’ll probably need to connect an external drive and partition it, format it, and mount it. Using the Ubuntu GUI is straightforward, but if you’re running a Linux server, you’ll need to do this process using the command line. You can use a new disk or a used disk that you’re wanting to erase. For the purposes of this article, we’ll be formatting the disk using the EXT4 filesystem.

How to Partition, Format, and Mount a Disk on Ubuntu 20.04 Using Command Line

After attaching your disk to the machine, you’ll want to get a list of the disks and find the disk identifier (i.e. /dev/sda). To get the disk identifier, run:

sudo fdisk -l

In the following commands, replace /dev/sda with the disk identifier of your drive!

If this is a used drive, you’ll want to delete all existing partitions. It is good practice to do this on new drives too, just to verify it is a clean drive:

sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Use the d option to delete the partitions:

Command (? for help): d
Partition number (1-2): 1

Command (? for help): d
Using 2

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y

Run GDisk again:

sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Create a new partition using the n option and selecting partition number 1:

Command (? for help): n
Partition number (1-128, default 1): 1

Select all the default options to create a partition using the whole disk:

First sector (34-11721045134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-11721045134, default = 11721045134) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):

Use the w option to write the changes to the disk:

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

Next, we’ll need to format the partition using EXT4. Assuming this is the only partition on your disk, you’ll want to use /dev/sda1 (or whatever your disk identifier is):

sudo mkfs.ext4 /dev/sda1

This step can take a few minutes to complete depending on the size of the disk.

You’ll need to create the mount point that you’ll use to mount your disk. An example is:

sudo mkdir /mnt/external1

To mount the disks now and after a reboot, we’ll need to get the UUID of the drive:

sudo blkid
/dev/sda1: LABEL="external1" UUID="745ae867-1613-4f9b-a428-4e04b8184ee5"
  TYPE="ext4" PARTLABEL="primary" PARTUUID="f853b5d0-ee6d-4eb2-9f95-9d4b6c1ce285"

Now we’ll want to edit our fstab file and create a reference for the drive using the UUID and mount point. For ease of use, I used the nano editor:

sudo nano /etc/fstab

Here’s an example of the fstab file. There are quite a few options you can use in fstab, but these are the basics. Be careful not to delete any existing disks, and only add a new line for the new disk:

/etc/fstab
# storage
UUID=745ae867-1613-4f9b-a428-4e04b8184ee5 /mnt/external1 ext4 defaults 0 0

Finally, mount your drives:

sudo mount -a

You should be able to change directory (cd) to /mnt/external1 and access your /dev/sda1 drive now. Since you edited the /etc/fstab file, the disk should automount after a reboot.


Share This:

 

2 Comments

  1. Alexandru Ivanov

Leave a Reply