Friday, June 25, 2010

How to mount and unmount a hard drive in Linux?

I lease multiple Linux servers. One of them has multiple hard drives installed in it. After asking the data center to reload the OS on it, the staff did not mounted the second drive. Surprisingly it is extremely easy for anyone with a root access to mount a hard drive on a Linux-based system. Here I will show you the very basic ways to mount and unmount a hard drive. This particular server is running CentOS 5.

Determine available drives and partitions

To star, you should first find out what hard drives and partitions are available. A quick way to show available hard drives in your system is to run the command fdisk -l. The results will give you some helpful information about the connected drive(s), as well as information on the partitions, and their size.

Fdisk is a partition manipulating program. The letter "l" attribute stand for list, causing fdisk to list the system partitions. You can read more in the fdisk man pages.

[caption id="attachment_1059" align="aligncenter" width="655"]Hard disk list command in Linux. List of hard drives on the system.[/caption]

I should point out that your hard drive or drives will be identified on your system with the letters hd or sd. If you have multiple drives they will be identified by a letter, e.g. hda /hdb, or sda /sdb. The partitions on each drive will be identified numerically, e.g. hda1 /hda2, or sdb1 /sdb2. You can see this in the screenshot above.

How to mount a hard drive in Linux

First thing you should do is create a directory where the drive or partition will be mounted. In my case, I use the second hard drive for storing backups so I created a directory called backups. I set the permissions to 755, although others would recommend 777 - full read and write access for everyone. In my opinion that is a security risk, especially on an Internet accessible server.

You will have to either authenticate as the root user or use elevated privileges via sudo. If you have either authenticated as root or via sudo bash then you can omit the sudo text from the commands below.

The commands to create the directory and set its permissions are:
# Creating the directory and setting its permissions.
# mkdir /path/dir_name
# chmod 755 /path/dir_name

$sudo mkdir /backups
$sudo chmod 755 /backups

Now that we have the target directory created, it is time to mount the hard drive partition. In my case the second hard drive has only one partition. It encompasses the entire hard drive.

The command we are going to use is mount. Look through the mount man pages for detailed overview of the various functions that mount can perform.
# Mounting partition in the directory.
# mount /dev/partitionId /path/dir_name

$sudo mount /dev/sdb1 /backups

Your partition should be permanently mounted now.

How to unmount a hard drive in Linux

To unmount a hard drive or a partition in Linux, the command is the same as when you are mounting the same, except you have to add a u to the front of the command. It becomes umount.
# Un-mounting the drive.
# umount /dev/partitionId /path/dir_name

$sudo umount /dev/sdb1 /backups

Keep in mind that although when we refer to the procedure we use the word unmount, the actual command does not have n in the beginning. The umount man pages contain a list of the various attributes you can use, as well as some of the other functions the program offers.