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.
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.
6 responses to “How to mount and unmount a hard drive in Linux?”
mount: you must specify the filesystem type
No, specifying the file system type (FST) is no a “must”. Mount is capable of auto detecting the file system type if it is a standard/known FST to the kernel*. You will have to specify the FST if you are using an old not supported FST or some other special circumstances.
Those who would like to know how to specify a file system type use the following command
If you want mount to automatically try to guess the file system type follow the instructions from the articles, or append -t auto.
* Known file system types are – adfs, affs, autofs, cifs, coda, coherent, cramfs, debugfs, devpts, efs, ext, ext2, ext3, ext4, hfs, hfsplus, hpfs, iso9660, jfs, minix, msdos, ncpfs, nfs, nfs4, ntfs, proc, qnx4, ramfs, reiserfs, romfs, squashfs, smbfs,sysv, tmpfs, ubifs, udf, ufs, umsdos, usbfs, vfat, xenix, xfs, xiafs. There might be others. As the kernel evolves support for some FSTs might be dropped.
Thanks a lot for the overview, found it a helpful refresher.
However, do you have to use chmod -R 755 to give the folder permissions in the first step?
Hi Ab,
Chmod does not need the -R recursive flag because the “backups”, or whatever you choose to name your directory you are going to use to mount the partition, is empty.
$sudo mount /dev/sda /oldhd
mount: you must specify the filesystem type
$sudo mount -t msdos /dev/sda /oldhd
mount: wrong fs type, bad option, bad superblock on /dev/sda,
missing codepage or helper program, or other error
In some cases useful info is found in syslog – try
dmesg | tail or so
$sudo mount -t auto /dev/sda /oldhd
mount: you must specify the filesystem type
If specifying the filesystem type is not actually required, my copy of mount hasn’t got the memo.
Your copy of mount did get the “memo”. Please have a look through the mount man pages!
You, on the other hand, are doing it wrong.
You need to identify the partition by its number. Which is also probably why you are unable to mount a known file system type (msdos) when using the full command.
Assuming your sda hard drive has only one partition, then you will be mounting sda1. Your mount command will be:
$sudo mount -t auto /dev/sda1 /oldhd
or
$sudo mount -t msdos /dev/sda1 /oldhd