You get a bonus - 1 coin for daily activity. Now you have 1 coin

Adding a Second HDD in Debian and Ubuntu

Practice



In this article I'll show you how to connect a second hard drive to a computer running Debian or Ubuntu (-Server). We'll assume the hard drive is new, or that the data on it isn't needed, so we'll use the whole disk.

So,

1) First we need to figure out its name under /dev. Every hard drive in Linux has its own individual name, such as sda, sdb, xvda, and so on.

For example, run the command:

$ ls /dev | grep sd

In the output you'll see several devices. For example, like this:

sda
sda1
sda2

Here we see one physical disk (sda) and two partitions on that disk (sda1, sda2).

Now turn off the computer, connect the hard drive, and start it up. Let's run the command above again:

$ ls /dev | grep sd
sda
sda1
sda2
sdb

We see the second hard drive (sdb). This is the one we'll be working with.

Note. You can get more detailed information with the fdisk -l command

# fdisk -l

First, let's create partitions on it. For this we'll need the cfdisk utility, which is included in the standard Debian package even in the netinst distribution.

So, let's run this command.

# cfdisk /dev/sdb

In response, the hard drive management utility starts up. The main window shows the existing partitions on the HDD (if any), and a list of commands is at the bottom. If the disk already has partitions, delete them.

Now create a new partition and assign it type 82 (Linux).

Select [ New ], set the size (by default the utility suggests using all the free space), set the partition type to 82, then write the information to disk [ Write ] and exit the utility [ Quit ].

Looking in the /dev directory, we'll now see that a partition has appeared on the second HDD:

$ ls /dev | grep sd
sda
sda1
sda2
sdb
sdb1

However, that's not all — we still need to create a filesystem on this partition (format it).

The mkfs command is used for this. Let's use it:

# mkfs -t ext3 /dev/sdb1

If your version of Debian supports Ext4 and you want to use it, specify ext4 instead of ext3 as the FS type.

The partition is now ready for use and mounting.

First, let's check that everything is fine. Let's mount this disk to the /mnt directory:

# mount /dev/sdb1 /mnt
# cd /mnt
# touch testfile
# ls
testfile
# rm testfile

If everything went well (the testfile appeared after the touch command), it means the second disk is formatted and ready for you to write data to it.


Automatically mounting the disk at system startup

However, on the very next reboot you'll have to mount this disk again, manually. And what if it's used by background services/daemons (for example, if it hosts websites used by apache and mysql)? Let's add the mount operation for this disk to a special file, so it gets attached automatically every time the system starts.

To do this we need to edit the /etc/fstab file. This file contains a table that tells the operating system which disks to mount, and in what order, at startup. We need to add a line at the very end of the file.

Let's open the file and see in what format the disks for mounting are specified there. If something like /dev/sda1 is specified, then we add a line of the form:

/dev/sdb1 /mnt ext3 errors=remount-ro 0 2

But if in the file you see entries like "UUID=blah-blah-blah", then it's better to use the partition's unique identifier (UUID) for adding it. This is better because no matter how you rearrange the disks, or which SATA port you plug them into, Linux will never get it wrong — it will always mount the correct disk to the correct folder.

To find out the UUID of your second disk, reboot and run the command:

$ ls -l /dev/disk/by-uuid

Among others, you'll see your disk (sdb1) and its corresponding UUID there.

Write it down, open the /etc/fstab file for editing, and add a line of the form:

UUID=blah-blah-blah /mnt ext3 errors=remount-ro 0 2

ATTENTION! These are two DIFFERENT ways of specifying a partition for auto-mounting. Do NOT use both of them at the same time by specifying both /dev/sdb1 and uuid= !

That's it — now after a reboot the disk will automatically mount to /mnt

You can create a directory in a location convenient for you, for example, /disk2

# mkdir /disk2

And instead of /mnt, specify this directory in the /etc/fstab file.

Comments

Noname 03-12-2020
Как примонтировать в определенную папку? Например в opt?
Noname2 15-09-2022
Там же написано

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "LINUX operating system"

Terms: LINUX operating system