Install Hard Drive on Linux via Terminal
Introduction
In some scenarios, we will have to install a new hard drive on a computer with Linux system without graphical user interface.
In this blog post, I quickly documented the installation protocol of a new hard drive using commands on Ubuntu 18.04 LTS.
Installation Protocol
Determine Drive Information
Once we have plugin the new hard drive to the computer, we have to first determine the logical name of the drive.
1 | $ sudo lshw -C disk |
In my case, the new hard drive is the 1TB hard drive and its logical name is /dev/sdb
.
We could also query the type of the hard drive using the following command.
1 | $ cat /sys/block/sdb/queue/rotational |
Here, 1 means HDD and 0 means SSD.
Command Line Partitioning
The next step is to partition the hard drive. I never partition the hard drive into multiple partitions unless it is very necessary. So I partition the entire entire hard drive as one partition.
1 | $ sudo parted /dev/sdb |
Once it is done, we could confirm the partitions of the disk using the following command.
1 | $ lsblk |
We could see that the only partition for our new hard drive is sdb1
.
Command Line Formatting
The next step is to format the partitions. This can be done easily using the following command. Make sure the device partition name is the desired one.
1 | $ sudo mkfs -t ext4 /dev/sdb1 |
Mount Hard Drive File System
So far we have prepared a new hard drive with partitions. However, its file system is not visible on our operating system because it has not been mounted. To mount the new hard drive file system, we have to create an empty directory for mounting first.
1 | $ sudo mkdir /media/panda |
To mount and unmount manually, we could run the following commands.
1 | $ sudo mount /dev/sdb1 /media/panda/ |
1 | $ sudo umount /media/panda |
To automatically mount the new hard drive file system, we have to edit the /etc/fstab
file.
We add the following line to the /etc/fstab
file.
1 | /dev/sdb1 /media/panda ext4 defaults 0 2 |
To know what exactly those values means in the /etc/fstab
file, please check fstab.
To activate the mounting immediately without rebooting the computer, we could run the following command.
1 | $ sudo mount -a |
Add Write Privilege
With the settings mentioned above, we could read the data from the new hard drive, and write to the new hard drive with sudo
. To add write privilege to user, we could run the following command.
1 | # sudo chown -R USERNAME:USERNAME /media/panda |
Quick Test
We could run a quick read and write test on the new hard drive.
1 | $ echo "Hello Underworld!" > /media/panda/hello.txt |
References
Install Hard Drive on Linux via Terminal