By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,301 Members | 3,690 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to mount partitions under Linux / Unix

Nepomuk
Expert 2.5K+
P: 3,112
In most modern distributions of Linux and Unix (at least ones with graphical environments like KDE, Gnome or XFCE), you get to mount your partitions easily from the desktop. In some cases however, it isn't that easy.

Note, that all of these commands have to be run in a terminal as root user. To open a terminal (sometimes called a console), either press ALT+F2 and type xterm (alternatives could be konsole or terminal) or press CRTL+ALT+F1 to change to a terminal.
However the latter will take you away from your graphical environment (or your desktop), so to get back to that, use CRTL+ALT+F7 or CRTL+ALT+F5, depending on your system.

To switch to root user once a terminal window is open, type
Expand|Select|Wrap|Line Numbers
  1. su
and enter the root password. To "log out" root after you've finished, enter
Expand|Select|Wrap|Line Numbers
  1. exit
Alternatively (if installed), you can use
Expand|Select|Wrap|Line Numbers
  1. sudo
before every command.

First of all, you can check what partitions your system recognises with
Expand|Select|Wrap|Line Numbers
  1. fdisk -l
(That is a small 'L', not the number '1'.)
The output will look something like this:
Expand|Select|Wrap|Line Numbers
  1. Disk /dev/hda: 40.0 GB, 40060403712 bytes
  2. 255 heads, 63 sectors/track, 4870 cylinders
  3. Units = cylinders of 16065 * 512 = 8225280 bytes
  4.  
  5. Device Boot      Start         End      Blocks   Id  System
  6. /dev/hda1               1          64      514048+  82  Linux swap / Solaris
  7. /dev/hda2   *          65        1402    10747485   83  W95 FAT32
  8. /dev/hda3            1403        3978    20691720   83  Linux
  9. /dev/hda4            3979        4870     7164990   83  Linux
or
Expand|Select|Wrap|Line Numbers
  1. Disk /dev/sda: 40.0 GB, 40060403712 bytes
  2. 255 heads, 63 sectors/track, 4870 cylinders
  3. Units = cylinders of 16065 * 512 = 8225280 bytes
  4.  
  5. Device Boot      Start         End      Blocks   Id  System
  6. /dev/sda1               1          64      514048+  82  Linux swap / Solaris
  7. /dev/sda2   *          65        1402    10747485   83  W95 FAT32
  8. /dev/sda3            1403        3978    20691720   83  Linux
  9. /dev/sda4            3979        4870     7164990   83  Linux
The devices called hdx (or sdx, x being something or the other; here found in line 1) in the directory /dev are your harddrives, the devices called hdx1, hdx2, etc (or sdx1, sdx2, etc) are your partitions.

As you can see, in my case the partition hda1 or sda1 is a SWAP-Partition, hda3 and hda4 (sda3 and sda4) are Linux partitions of some kind (like ext2, ext3, reiserfs or xfs) and hda2 (sda2) is FAT32 (a windows filesystem). If you have an NTFS drive, make sure the correct drivers are installed!

Next, check which partitions are already mounted with
Expand|Select|Wrap|Line Numbers
  1. mount
The output will look something like this:
Expand|Select|Wrap|Line Numbers
  1. /dev/hda4 on / type ext3 (rw,errors=remount-ro)
  2. proc on /proc type proc (rw,noexec,nosuid,nodev)
  3. /sys on /sys type sysfs (rw,noexec,nosuid,nodev)
  4. varrun on /var/run type tmpfs (rw,noexec,nosuid,nodev,mode=0755)
  5. varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
  6. procbususb on /proc/bus/usb type usbfs (rw)
  7. udev on /dev type tmpfs (rw,mode=0755)
  8. devshm on /dev/shm type tmpfs (rw)
  9. devpts on /dev/pts type devpts (rw,gid=5,mode=620)
  10. lrm on /lib/modules/2.6.17-12-generic/volatile type tmpfs (rw)
  11. /dev/hda3 on /home type ext2 (rw)
In this case, hda4 and hda3 are mounted, but hda2 isn't.

Now, choose where the device is going to be mounted to. Normally, it's in a subdirectory of either /mnt or /media. Check, that the subdirectory exists (e.g. /mnt/c or /media/hdb2). If it doesn't, create it by using
Expand|Select|Wrap|Line Numbers
  1. cd /media
  2. mkdir hdb2
(or appropriate).

With the above information, you should be able to choose the right partition and use something like
Expand|Select|Wrap|Line Numbers
  1. mount /dev/hdb2 -t vfat /media/hdb2
if you're mounting hdb2 and it's a FAT16 or FAT32 partition.

The most commonly used partition formats are:
  • autofs (automatically try to determine format)
  • ext2
  • ext3
  • iso9660 (for CD-ROMs)
  • ntfs
  • reiserfs
  • smbfs (for SAMBA drives)
  • udf (for DVD-ROMs)
  • usbfs (for some USB devices)
  • vfat (for FAT16 and FAT32)
  • xfs
(More information about various file systems are to be found beneath the main article.)
Now, the drive will be accessible, however only root will have full access.

To make things easier, if you want to mount that drive more often, you can put it into /etc/fstab. Just open it in an editor (as root!) and add the following lines at the bottom:
Expand|Select|Wrap|Line Numbers
  1. # added manually
  2. /dev/hdb2 /media/hdb2 vfat defaults,users,noauto 0 0
(Of course, you'll have to change the device, type and mount directory to what you need.) This will allow anyone to mount that partition.
If you want the drive to be mounted automatically on startup, change noauto to auto.

Then save the file and in future you'll just have to do
Expand|Select|Wrap|Line Numbers
  1. mount /dev/hdb2
or
Expand|Select|Wrap|Line Numbers
  1. mount /media/hdb2
from any account to mount the drive.

More information about mounting can be found in the mount help
Expand|Select|Wrap|Line Numbers
  1. mount -h
or on the manpage of mount, which you can find by entering
Expand|Select|Wrap|Line Numbers
  1. man mount
or alternatively
Expand|Select|Wrap|Line Numbers
  1. info mount
into a terminal window.


Various File Systems
Here are some short descriptions of the most common file systems:
  • ext2 is a file system, which was very common on linux systems for a long time. Nowadays, it is mostly replaced by ext3.
    Use the format type ext2 to mount.
  • ext3 is a so called journaling file system. Other than that, there are very few differences to ext2.
    Use the format type ext3 to mount.
  • ext4 is a file system, which is currently under development. It is supposed to replace ext3. As it is unfinished, I'll not go into any detail.
    In future, you'll probably use the format type ext4 to mount.
  • XFS is one of the oldest journaling file systems available for UNIX and almost all Linux distributions, one of the few exceptions being Red Hat Enterprise Linux.
    Use the format type xfs to mount.
  • JFS is a 64-bit journaling filesystem created by IBM. There are two generations of JFS filesystem that are called JFS (JFS1) and JFS2, however under most operating systems, such as OS/2 and Linux, only the second generation exists and is called simply JFS.
    Use the format type jfs to mount.
  • ReiserFS (occasionally referred to as Reiser3) is a general-purpose, journaled computer file system and the first journaling file system to be included in the standard linux kernel.
    Use the format type reiserfs to mount.
  • FAT12 / FAT16 / FAT32 are relatively straightforward file systems and are supported by virtually all existing operating systems for personal computers. They are no journaling filesystems and therefore affected by fragmentation at a much higher rate than those.
    Use the format type vfat to mount.
  • NTFS is the standard file system of many newer Windows versions. It has several improvements over FAT, like the use of advanced data structures or it being a journaling filesystem. The support under Linux UNIX is good but not perfect, as the file system specification is a trade secret and the non-microsoft implementations are reverse engineered.
    Use the format type ntfs to mount. You have to have NTFS-3G installed!
All of these descriptions are based on the wikipedia articles.
Jul 10 '08 #1
Share this Article
Share on Google+
1 Comment


Expert Mod 100+
P: 2,324
Hah, perfect timing.
Just about to setup a new backup system on a nix box.
Jul 10 '08 #2