Home » Computing » Linux » Mounting Drives in Linux

Mounting Drives in Linux

Byte City

Mounting drives in Linux is a task that sometimes needs to be performed when the auto-mounting mechanism doesn’t apply, and for neophytes can be challenging. The forums are replete with problems about mounting drives, the system not mounting drives upon plugging in the USB or inserting a CD, and permissions confusions.

The following post aims to explain as many parts of the manual process as reasonable, covering the /dev folder, mount and umount commands, fstab, umask and some particularities on filesystems and newly created disks.

The topic is fairly heavy, with many offshoot topics, and I want to keep this post as straight-to-the-task as possible, so a lot of the explanations will urge you to look up info elsewhere if you want more in-depth discussion. Generally, doing a web search on the name in underlined italics will be sufficient. I also use bold text for example snippets that you’ll need to replace, and pink text for text you would type at the command line, with green monospace text reserved for output.

Questions answered

  • How do I mount my USB key in Linux?
  • Why does my USB always mount as root?
  • How do I automatically mount a drive in Linux?
  • Why can’t I write to my USB in Linux?
  • How do I use the mount command?

/dev files

In UNIX (and Linux) everything is a file. A file is a file. A folder is a file. An entire disk is represented by a file too. So are a printer, or a running process, but that’s for another day.

The point is, somewhere on your Linux box, the disk you want to mount is represented by a file. These are the device files, which are stored in /dev: run `ls /dev` to see all files there.

Generally, the main hard disk from which the system is running is /dev/sda ; you might have a /dev/cdrom or /dev/sr0 which represent the CD/DVD-ROM drive; /dev/sdb and /dev/sdc should be two of your USB ports, and so forth.

Some disks can have partitions – separate sections on the same device which act as independent storage spaces. If a device has multiple partitions, these will be represented as individual files, such as /dev/sdb1, /dev/sdb2, etc. Read up on partitioning for more in-depth info.

In this post we are interested in mounting a disk – USB, CDROM, a new SATA drive, etc. Once the device is connected (and, I the case of SATA drives, the machine is booted; in virtual machines, this must be re-booted), you are ready to go.

Use the `lsblk` command to to list all block devices. A block device is basically a device which exhibits storage blocks – with blocks being an abstraction of groups of bytes. Read up on block devices for more in-depth info. The following is the output when I run lsblk on my Manjaro box:

[tai@manjaro-vm01 isobuild]$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0     8G  0 disk 
├─sda1   8:1    0   5.1G  0 part /
├─sda2   8:2    0 190.8M  0 part /boot
└─sda3   8:3    0   2.7G  0 part /home
sdb      8:16   0  16.8G  0 disk 
└─sdb1   8:17   0  16.8G  0 part /home/tai/isobuild
sr0     11:0    1  1024M  0 rom  

You are presented with a list of devices from /dev/* which are the files which represent your connected disks. Note in my example that sda braches off to sda1, sda2 and sda3, and the sr0 entry representing the CD slot which currently is not mounted (it may or may not have a CD in it).

You are also shown where their partitions are mounted, such as “/”, “/boot” and “/home”; their “mount points.” A mount point is a location in the file tree where the files of a disk can be accessed from.

Creating a mount point is as simple as creating an empty directory using the `mkdir` command. Normal practice is to mount devices either to /media/* (for hot-plugging devices like flash drives and CDs) or /mnt/* (more often for semi-permanent drives like network drives or external drives that are always attached), but any location will do – even somewhere in the depths of your home directory…!

If the disk is not loaded to the mount point directory, said mount point is shown as an empty directory, owned by whoever created it.

Once the disk is mounted, the mount point becomes owned by the owner of the disk. Upon unmounting, the permissions are reverted. Read up on mount points for more in-depth info.

Using the mount command

You need 3 things then to be able to mount your drive:

  • the name of the device file you want to mount (for example /dev/sdb1)
  • an empty directory into which to mount the disk (for example ~/mydisk)
  • sudo or root access

The tilde (“~”) is a shorthand notation for “my home directory.” You literally can run `cd ~/` to change to your home directory.

Use the mount command to mount the device:

sudo mount -t ext4 /dev/sdb1 ~/mydisk

This should mount your device to your chosen directory. “ext4” is one of the filesystems used by Linux systems. Others include ext2, ext3 and btrfs. Windows systems use NTFS and FAT32, Mac OS X use hfs. CDs and DVDs use iso9660 and are read-only. Substitute as necessary, for example.

sudo mount -t ntfs /dev/sdb1 ~/mydisk

The unmount command

To unmount a disk, simply run umount (no ‘n’) with the name of the directory you mounted on: `sudo umount ~/mydisk`

Unmounting ensures no processes are currently editing any fo the files on the drive, and it can be safely removed. If you get an error on trying to unmount a drive saying it is still in use, ensure of course none of your programs are still using files on the drive. Some programs have a bad habit of not letting go of files once they’re done with them, so you can try quitting the applications that previously were used to edit files directly on the disk. You may also simply still be cd’d into one of the disk’s folders – cd out of it.

For CD’s and DVD’s, you are better advised to use `eject /dev/sr0` to unmount the disk and eject it from the bay.

Ownership

You may find that a newly partitioned/formatted disk to ext4 or NTFS might be read-only – this may be because it mounted with ‘root’ as owner. To check who owns the device at the mountpoint, run `ls -ld ~/mydisk`. The -l option lists information; the -d option lists the info for a directory, rather than its contents.

If the disk was mounted with an owner of root, you have 2 options:

  • If the filesystem is NTFS or FAT32, you could use this command instead of the one above:

sudo mount -t ntfs -o gid=users,rw /dev/sdb1 ~/mydisk

    • this will treat the entire NTFS disk as being part of the “users” group
    • the ‘rw’ option ensures read/write permissions are applied
  • If the filesystem is ext/2/3/4, then you may need to re-own the disk’s file path, once the disk is mounted:

sudo chown username:users ~/mydisk

Username is your user name (if in doubt, run the command `whoami`); you can find all groups you belong to by running `groups username`, you should already be in the “users” group.

After re-owning the disk’s filesystem, you should see that the file system loads as yours from hereon out.

Note that the above command looks up your username (for example ‘alice’) and substitutes its local user ID (for example “100” for the first/only user of a locally installed system) to apply permissions – so the files are actually owned by user ID 100. We also assigned the group “users” in this instance to ensure it’s accessible to the users group. If you mount the disk on another machine, you may find it read-only again, even if your username is the same (“alice”). Maybe that username is associated with a different ID – in which case, you would either need to perform the re-owning dance again, or set the files to be fully accessible to both user and group:

sudo chmod -R g+rw

chmod changes permissions on files, “-R” causes it to recurse over directories. The “g” argument signals that we want to change permissions for the group, “+rw” indicates that we are adding read and write permissions. “o-w” instead of “g+rw” would remove write access from “others” – any user who is not the user who owns the file, or in the group “users” who have access to the file. Run `man chmod` for more in-depth information.

fstab

The /etc/fstab file defines what paritions are loaded to what mount points, typically at boot time. You can also use the fstab file to allow users mount privileges without giving them sudo access.

You can add entries to your /etc/fstab file by running `sudo nano /etc/fstab`

Add a new line with the following and save it:

/dev/sdb1 /home/username/mydisk ext4 defaults,noauto,user 0 0

Note the elements on the line:

  • the first item is the disk file
  • the second is the mount point (cannot use the “~” shorthand here)
  • the third option specifies the filesystem – specify “ext4” or “ntfs” or another, as applicable
  • the fourth is the options to load with – ‘user’ specifies allowing users to mount this device to the given location
  • set the fifth and sixth options to 0 in general practice.

Web search fstab or run `man fstab` for more in-depth info.

The above entry will allow you to simply type the following on the command line:

mount ~/mydisk

Note the differences in the command:

  • no need to use the sudo command anymore.
  • no need to specify the disk file – the directory is always associated with /dev/sdb1 in the above example
  • you can also run umount without sudo

If the device you are mounting is a new SATA drive or the likes, you might want this to load whenever the system boots. In this case, you would be best advised to get the UUID of the drive, and use that instead of the device file name.

Run `sudo blkid` to see the IDs of attached block devices. Copy the UUID, for example a1b23c-d4e5f6 and write to the /etc/fstab file:

UUID=a1b23c-d4e5f6 /home/username/mydisk ext4 defaults,rw,auto 0 2

Note the differences on the line compared to previously:

  • UUID instead of device file will load a specific device – devices can move from one file to another between reboots.
  • added the “rw” option to ensure that the drive is read/write
  • added the “auto” option to ensure automatic mounting
  • sixth option is set to “2” to trigger disk checking after the main hard drive, as applicable.

If you have not already done so, unmount the device.

Now run the command `sudo mount -a` to run a new automatic mount; check that your device mounted correctly.

Wrapping up

So the general gist again:

  1. Use `lsblk` to find out which device file your device is at
  2. Create a mount point – generally this is done in /media or /mnt
  3. Use `sudo mount` to mount a drive to a mount point
  4. If the disk is loading ‘read-only’ and is ext4 check disk ownership; if it’s NTFS, try using different mount options
  5. Use `umount` to unmount a drive before removing it, use `eject` for media that needs ejecting
  6. Edit the fstab file to allow users to mount drives without sudo
  7. Edit the fstab file with UUID and ‘auto’ option to automatically mount drive on startup (only recommended for drives hard-plugged to the machine, like SATA drives).

I hope that this post has been informative, and gives a good first starting point to troubleshooting drive loading!

BONUS

If you have an ISO file handy, try the following:

sudo mount -t iso9660 path/to/iso /some/mount/point

😉

 

Posted in Linux, Tai's FWA and tagged as , ,

Leave a Reply

Your email address will not be published. Required fields are marked *