It is actually very easy to mount more or less whatever you want as a
normal user without root privileges, provided the right entry has
been created in /etc/fstab
.
Of course, modifications to /etc/fstab
require root privileges. But
a single entry can
be used with much flexibility to (u)mount many different files on
different mount points, without any further editing of /etc/fstab
.
Here are two very short (5 lines + comments) Bash scripts that will do the job:
for mounting
#!/bin/sh
# usage: usmount device dir
# author: babou 2013/05/17 on https://unix.stackexchange.com/questions/32008/mount-an-loop-file-without-root-permission/76002#76002
# Allows normal user to mount device $1 on mount point $2
# Use /etc/fstab entry :
# /tmp/UFS/drive /tmp/UFS/mountpoint auto users,noauto 0 0
# and directory /tmp/UFS/
# Both have to be created (as superuser for the /etc/fstab entry)
rm -f /tmp/UFS/drive /tmp/UFS/mountpoint
ln -s `realpath -s $1` /tmp/UFS/drive
ln -s `realpath -s $2` /tmp/UFS/mountpoint
mount /tmp/UFS/drive || mount /tmp/UFS/mountpoint
# The last statement should be a bit more subtle
# Trying both is generally not useful.
and for dismounting
#!/bin/sh
# usage: usumount device dir
# author: babou 2013/05/17 on https://unix.stackexchange.com/questions/32008/mount-an-loop-file-without-root-permission/76002#76002
# Allows normal user to umount device $1 from mount point $2
# Use /etc/fstab entry :
# /tmp/UFS/drive /tmp/UFS/mountpoint auto users,noauto 0 0
# and directory /tmp/UFS/
# Both have to be created (as superuser for the /etc/fstab entry)
rm -f /tmp/UFS/drive /tmp/UFS/mountpoint
ln -s `realpath -s $1` /tmp/UFS/drive
ln -s `realpath -s $2` /tmp/UFS/mountpoint
umount /tmp/UFS/drive || umount /tmp/UFS/mountpoint
# One of the two umounts may fail because it is ambiguous
# Actually both could fail, with careless mounting organization :-)
The directory /tmp/UFS/
is created to isolate the links and avoid clashes. But the symlinks can be anywhere in user space, as long as they stay in the same place
(same path). The /etc/fstab
entry never changes either.
VITAL WARNING:
Mounting is restricted for good security reasons. Making it
more flexible may open doors for malicious software. I am not a
security expert and I would recommend that you open doors no more than
absolutely required ... using options to restrict what can be done
with the file systems that can thus be mounted.
If a knowledgeable contributor could comment further on security issues, it
might be useful.
Various options are available to restrict the use of file systems that are mounted, such as noexec
which prevents execution of
binaries, or nosuid
, and thus contribute to security. Actually, these options are added as default options when the options user
or users
are used, which is necessarily the case in what we do below. Think twice before you override these defaults. http://en.wikipedia.org/wiki/Fstab
Other options can be added for further protection. For example, the option owner
in the /etc/fstab
entry will let users deal only with files or devices they own. See man mount
for a list of options: http://linux.die.net/man/8/mount.
The use of this /etc/fstab
entry can also be restricted through the user.group ownership of the directory (or directories) containing the symlinks.
Explanation
This explanation was written before I realised I could simplify things to the two scripts above. I did not think of them right away partly because I have at hand a slightly more complex problem that they do not solve without some extra machinery. Thus my explanation may be a bit more intricate than it should, but I do not have the courage to rewrite it all from scratch.
The basic idea is to create entries in /etc/fstab
that include the
option user
or users
so that a user can ask mount
to do the
mounting specified in that entry by giving as argument the file to be
mounted or the mount point to use (but not both in my expérience).
You also need a proper entry to umount
(which is a slightly different problem - see below). The option user
is usually better than users
since it restricts permission to umount
to the user who mounted the file system, while users
will allow that to all. Unfortunately the option user
does not always work, and may entail some other steps to be made to work. This is discussed in Option "user" work for mount, not for umount.
First you add to /etc/fstab
an entry such as:
/tmp/UFS/drive /tmp/UFS/mountpoint auto users,noauto, 0 0
and use /tmp/UFS/drive
as a symbolic link (or symlink) to whatever device or file
you wish to mount,
say a file containing the image of an ISO file system /home/johndoe/john-image-file.iso
.
You also define /tmp/UFS/mountpoint
as a symlink to the mount point you wish to use, say /mnt/iso
.
You can then mount john-image-file.iso
with the command :
$ mount /tmp/UFS/drive
This is sufficient on my Mageia Linux, since the use of loop devices
has now been made implicit, and no longer requires using -o loop
explicitly. I do not know how general that is today. See
When mounting, when should I use a loop device?
This mounting appears in tables and commands :
$ df | tail -1
/dev/loop0 5,1G 5,1G 0 100% /mnt/iso
$ tail -1 /etc/mtab
/dev/loop0 /mnt/iso udf ro,nosuid,nodev,noexec,relatime,utf8 0 0
$ mount | tail -1
/home/johndoe/john-image-file.iso on /mnt/iso type udf (ro,nosuid,nodev,noexec,relatime,utf8)
$ tail -1 /proc/mounts
/dev/loop0 /mnt/iso udf ro,nosuid,nodev,noexec,relatime,utf8 0 0
$ tail -1 /proc/self/mountinfo
46 22 7:0 / /mnt/iso rw,nosuid,nodev,noexec,relatime - udf /dev/loop0 ro,utf8
$ tail -1 /proc/self/mountstats
device /dev/loop0 mounted on /mnt/iso with fstype udf
The mounting operation could work for any file or drive and requires only to
make a symbolic link from /tmp/UFS/drive
to that file or to the device for the drive. Of course, another name and location could be chosen for the symbolic link, as long as it never changes.
Dismounting the file relies in the same way on appropriate use of symbolic links. In the case of a normal device corresponding to some harware drive,
you just use the same links.
However, files containing the image of a file system are mounted via a special kind of device called a loop device, automatically allocated when you mount the file.
To dismount the file, you need to refer to the loop device, not the file.
Hence you need in /etc/fstab
an entry that matches both the loop device
used in /etc/mtab
, here /dev/loop0
, and the mount point, here
/mnt/iso
.
You cannot create such an entry in advance since the loop
device may vary, as they are allocated dynamically. Note that it is also
possible to use a fixed loop device, but it is inconvenient in other
ways. See
http://igurublog.wordpress.com/2011/01/22/how-to-allow-mounting-of-iso-files-by-a-regular-user/ (this blog actually inspired the reply here).
However, you can find the name of the loop device, here /dev/loop0
, by asking the system, like we did above in several different ways. Then our standard /etc/fstab
entry can be made to point to the right loop device via the symlink /tmp/UFS/drive
, and to the mount point as done previously with /tmp/UFS/mountpoint
. This done, the file may be dismounted with any of the following commands (provided there is no ambiguity with /etc/mtab
, which is a different problem):
$ umount /tmp/UFS/drive
$ umount /dev/loop0
$ umount /mnt/iso
$ umount /tmp/UFS/mountpoint
Since the two symlinks are needed only when the commands are issued,
they can be changed dynamically. So our single /etc/fstab
entry
allows mounting any number of files, and umounting them in any
order, without root privileges.
Other references:
mount
binary doesn't require SUID permissions then you should be able to use fakeroot without problems.fakeroot
isn't going to help here: it pretends file ownership is different, but it can't give you permissions that you don't have, such as to callmount(2)
when you aren't root.