Skip to content

Instantly share code, notes, and snippets.

@kkyucon
Last active May 24, 2020 13:26
Show Gist options
  • Save kkyucon/a0d2b64b727870bd180e47c3762b539b to your computer and use it in GitHub Desktop.
Save kkyucon/a0d2b64b727870bd180e47c3762b539b to your computer and use it in GitHub Desktop.
Ubuntu Server Add Hard Drive

Partition, Format and Mount Hard Drive in Ubuntu

For the purpose of this tutorial you must have "sudo" privileges.

We are going to add a second drive to our system, partition it according to our needs, format and finally mount to a created directory.

Partition the drive

Assuming the drive is physically attached to the system and detected by the bios, let's see how Ubuntu has assigned the disk by typing the following command:
sudo lshw -C disk

In most cases, Ubuntu has assigned the system drive "logical name" as "/dev/sda" and the second drive as "/dev/sdb". If there is a third drive it would be "/dev/sdc" and so on. Take note of the "logical name" of the drive you wish to add. We will be using "/dev/sdb" for this tutorial.

From the output below of command sudo lshw -C disk we can see that there are 2 disks attached to my system and our target drive has the "logical name" of "/dev/sdb".

       *-disk
       description: SCSI Disk
       product: 00AAKS-00V1A0
       vendor: WDC WD50
       physical id: 0.0.0
       bus info: scsi@3:0.0.0
       logical name: /dev/sdb
       serial: 152D20337A0C
       size: 465GiB (500GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=2 logicalsectorsize=512 sectorsize=512 signature=33284869
  *-disk
       description: ATA Disk
       product: ST500LT012-1DG14
       vendor: Seagate
       physical id: 0.0.0
       bus info: scsi@0:0.0.0
       logical name: /dev/sda
       version: SDM1
       serial: SBY7218P
       size: 465GiB (500GB)
       capabilities: gpt-1.00 partitioned partitioned:gpt
       configuration: ansiversion=5 guid=583b84cd-5418-4932-acb8-0c4f253d1035 logicalsectorsize=512 sectorsize=4096

If partitions already exist on the drive type sudo fdisk /dev/sdb followed by d to delete the partitions and w to write the changes. Be carefull as this will delete any data on the drive.

To partition and format a drive 2TB and lower continue using "fdisk". This will ultimately partition the drive using "mbr" partition table. If your kernel supports "gpt"(and most modern kernels do) regardless of size, continue on with Parted.

Type:
sudo parted /dev/sdb

Let's set the partition table to "gpt", type:
mklabel gpt

Now we create the primary partition using the entire disk:
mkpart primary 0GB 500GB

Now exit Parted by typing:
quit

To confirm all went well, type:
sudo fdisk -l

The following output should confirm the "Disklabel type" and that the primary partition is "/dev/sdb1":

Disk /dev/sdb: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D3C27E53-F077-4774-99C8-99276CD8F9A2

Device     Start       End   Sectors   Size Type
/dev/sdb1   2048 976773119 976771072 465.8G Linux filesystem

Formatting

Since we are using the most modern Ubuntu system for this tutorial we are going to use the "ext4" filesystem. You can use a different filesystem like "ntfs" if this drive is going to be shared with a Windows/Ubuntu network using Samba.

Now let's use the "mkfs" command to format the disk partition:
sudo mkfs.ext4 /dev/sdb1

This could take some time depending on the size of the drive. When done, you should see this output:

mke2fs 1.44.1 (24-Mar-2018)
/dev/sdb1 contains a ext4 file system
	last mounted on /target on Sun Dec 23 11:33:15 2018
Proceed anyway? (y,N) y
Creating filesystem with 122096384 4k blocks and 30531584 inodes
Filesystem UUID: 14523919-682c-4668-8004-d5c9aeb2b2b7
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done     

Mounting

Mounting in Ubuntu is much different then Windows. It uses a file system instead of drive letter association. Therefore, each drive must be mounted within a directory.

Ubuntu(server and desktop) already has the "/mnt" directory created. So to keep things simple we are going to use this directory and create a sub-directory. This sub-directory can have any name you choose. I am going to be using this drive for media files, so "/media" it is.

We use the "mkdir" command to create our sub-directory like so:
sudo mkdir /mnt/media

We need to remove all permissions for this sub-directory:
sudo chown nobody:nogroup /mnt/media

Now that we have created our mount point and removed all permissions, we need to give our sub-directory the necessary permissions so it can be readable, writable and executable by any user on the system. Type:
sudo chmod 777 /mnt/media

Note: An essential learning process in Ubuntu/Linux is most definitely "permissions". Get to learn this aspect and know who you share data with, or it could be the last time you see your vacation photos! So make a backup.

Now let's mount our drive:
sudo mount /dev/sdb1 /mnt/media

Check if mounted:
lsblk

From the output we can see "/dev/sdb1" mounted in "/mnt/media":

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 465.3G  0 part /
sdb      8:32   0 465.8G  0 disk 
└─sdb1   8:33   0 465.8G  0 part /mnt/media

Our drive is now temporarily mounted until next reboot. This is fine if we are using a USB key. But what we want is a drive that is automatically mounted at each reboot. To do this we need to edit a configuration file called "fstab".

To permanently mount our drive open the "fstab" file using "vi" editor:
sudo vi /etc/fstab

The file should look like this:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=852b0aa1-0f9b-4fe2-a676-2054836509ba /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=A6D3-9D83  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0
~
~
~
"/etc/fstab" 12L, 665C                                        1,1           All

To begin editing this file press the i key. Use the "Arrow" keys to navigate to the end of the last line and press enter. Type this line:
/dev/sdb1 /mnt/media ext4 defaults 0 2

The file should look like this after typing line:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=852b0aa1-0f9b-4fe2-a676-2054836509ba /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=A6D3-9D83  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0
/dev/sdb1 /mnt/media ext4 defaults 0 2
~                                                                               
~                                                                               
"/etc/fstab" 13L, 704C                                        13,38         All

Now hit the Esc key followed by :wq to write and quit the file.

Now the drive is permanently mounted and can be used by any user on the system.

Conclusion

You may wish to share this drive to the network. If this being the case please follow my NFS tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment