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.
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.
$ sudo lshw -C disk *-disk description: ATA Disk product: Samsung SSD 850 physical id: 0.0.0 bus info: scsi@0:0.0.0 logical name: /dev/sda version: 2B6Q serial: S2RANX0HA26171R size: 465GiB (500GB) capacity: 465GiB (500GB) capabilities: 15000rpm gpt-1.00 partitioned partitioned:gpt configuration: ansiversion=6 guid=19af133c-b0ce-45ab-ab5d-9f3d22a9e606 logicalsectorsize=512 sectorsize=512 *-disk description: SCSI Disk product: 2115 vendor: ASMT physical id: 0.0.0 bus info: scsi@9:0.0.0 logical name: /dev/sdb version: 0 serial: EF9987654321 size: 931GiB (1TB) configuration: ansiversion=6 logicalsectorsize=512 sectorsize=512 *-cdrom description: DVD-RAM writer product: CDDVDW SU-208GB vendor: hp physical id: 0.0.0 bus info: scsi@5:0.0.0 logical name: /dev/cdrom logical name: /dev/cdrw logical name: /dev/dvd logical name: /dev/dvdrw logical name: /dev/sr0 version: HN00 capabilities: removable audio cd-r cd-rw dvd dvd-r dvd-ram configuration: ansiversion=5 status=nodisc
In my case, the new hard drive is the 1TB hard drive and its logical name is
We could also query the type of the hard drive using the following command.
$ cat /sys/block/sdb/queue/rotational 1
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.
$ sudo parted /dev/sdb GNU Parted 3.2 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt (parted) unit TB (parted) mkpart Partition name? ? panda File system type? [ext2]? ext4 Start? 0 End? 1 (parted) print Model: ASMT 2115 (scsi) Disk /dev/sdb: 1.00TB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 0.00TB 1.00TB 1.00TB ext4 panda (parted) quit Information: You may need to update /etc/fstab.
Once it is done, we could confirm the partitions of the disk using the following command.
$ lsblk 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:16 0 931.5G 0 disk └─sdb1 8:17 0 931.5G 0 part sr0 11:0 1 1024M 0 rom
We could see that the only partition for our new hard drive is
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.
$ sudo mkfs -t ext4 /dev/sdb1 mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 244175218 4k blocks and 61046784 inodes Filesystem UUID: 39f29f93-9289-47d2-9004-b9b5583567ba Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
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.
$ sudo mkdir /media/panda
To mount and unmount manually, we could run the following commands.
$ sudo mount /dev/sdb1 /media/panda/ $ df -h | grep /dev/sdb1 /dev/sdb1 916G 77M 870G 1% /media/panda
$ sudo umount /media/panda $ df -h | grep /dev/sdb1
To automatically mount the new hard drive file system, we have to edit the
We add the following line to the
/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.
$ sudo mount -a $ sudo umount /media/panda $ df -h | grep /dev/sdb1 /dev/sdb1 916G 77M 870G 1% /media/panda
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.
# sudo chown -R USERNAME:USERNAME /media/panda $ sudo chown -R leimao:leimao /media/panda
We could run a quick read and write test on the new hard drive.
$ echo "Hello Underworld!" > /media/panda/hello.txt $ cat /media/panda/hello.txt Hello Underworld! $ rm /media/panda/hello.txt