Gentoo Wiki


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc



You want to have directories auto mounted for you when you access them. This is useful for nfs, samba, and boot mounts/partitions. Think of having /usr/portage exist on a central nfs capable server and all of your client machines are then able to automatically mount /usr/portage whenever you perform an emerge (This example would save you ~300MB of disk space).

Kernel Support

First you need to make sure your server and client machines have support for network filesystems (NFS or SMB for example). In your kernel configuration ensure you have the following options set:

Linux Kernel Configuration: automounter support
File Systems --->
  <*> Kernel automounter version 4 support (also supports v3)

Installing the support as a module works as well, build and install the kernel as normal.

Install package(s)

First, install autofs on workstation(s). This will provide automounting for local mounts only (extra partitions, floppy, cd/dvd roms, etc):

emerge autofs

Next, depending on whether the workstation will need network shares automounted, you'll need nfs-utils (for nfs shares) and samba (for samba shares). Both are completely optional:

emerge -av nfs-utils samba

System Setup and Configuration

Verify Manual Mounting

Once autofs is installed, you need to verify that you are able to mount the filesystems you intend to setup as automounts.

This example only targets local mounts with comments for network filesystem mounts.

mount /dev/hda1 /mnt/boot
mount /dev/cdrom /mnt/cdrom
mount /dev/fd0 /mnt/floppy

Config files

Modify the autofs master configuration file. This file lists one or more directories to take control over (it will automatically create and remove directories as you access them). This directory (the "mount point") will be controlled by the autofs daemon and it's therefore strongly recommended that you do *not* use the "mount" command to explicitly mount filesystems to this directory.

Each line in the master configuration file gives autofs a mount point, and the name of another configuration file that contains the file systems to auto-mount there. You can add any number of mount points, but we will only need one.

All config files must have a new line at the end

File: /etc/autofs/auto.master
 # As usual, lines in the file that start with '#' are comments

 # mount point   config file              options
 #/misc          /etc/autofs/auto.misc
 #/home          /etc/autofs/auto.home
 /mnt/auto       /etc/autofs/    --timeout=5 --ghost

The --timeout option in auto.master tells the automounter to unmount the file systems after (in this case) 5 seconds of inactivity. There are many discussions on the internet about whether this should be very short (several seconds) or long (a minute or more). That discussion is outside the scope of this document.

The --ghost option tells the automounter to create "ghost" versions (i.e. empty directories) of all the mount points listed in the configuration file (in this case /etc/autofs/, regardless of whether any of the file systems is actually mounted or not. This is very convenient and highly recommended, because it will show you the available auto-mountable file systems as existing directories, even when their file systems aren't currently mounted. Without the --ghost option, you'll have to remember the names of the directories. As soon as you try to access one of them, the directory will be created and the file system will be mounted. When the file system gets unmounted again, the directory is destroyed too, unless the --ghost option was given.

File: /etc/autofs/
 #directory name                        options for mount                              device to mount
 boot                                   -fstype=ext2                                   :/dev/hda1
 cdrom                                  -fstype=iso9660,ro                             :/dev/cdrom
 floppy                                 -fstype=auto                                   :/dev/fd0
 storage                                -rsize=8192,wsize=8192,soft,timeo=30,rw
 #portage                               -rsize=8192,wsize=8192,soft,timeo=30,rw        hydra.localnet:/usr/portage
 #smb                                   -fstype=smbfs,credentials=/etc/smb.auth        ://solfish/Share
 # SMB example
 #some-win-smb                          -fstype=smbfs,credentials=/etc/smb.auth        ://some-win-host/path/to/smb
 # NFS example
 #some-nfs                              -rsize=8192,wsize=8192,soft,timeo=14,rw        some-nfs-host:/path/to/nfs

 # An example with wildcards, see autofs(5):
 #*                                     -fstype=iso9660,ro,loop                        :/images/&.iso
File: /etc/smb.auth
# chmod 600 /etc/smb.auth

Russian charset and codepage (example):

  XPcomputer              -fstype=smbfs,iocharset=utf8,codepage=cp866,dmask=0700,fmask=sk=0600,credentials=/etc/smb.auth                     ://

Directory and File Permissions

Now we have configured autofs basic mounting we must restrict permissions.

The automounted filesystems usually have undesired permissions or you might want to restrict permissions for certain users. This can be globally controlled by using the mounting instructions fmask and dmask.

See below for an example limiting the resource to the mounted user only:

File: /etc/autofs/
 # -- snip --
 insecure-smb         -fstype=smbfs,credentials=/etc/smb.auth                           ://solfish/Share
 more-secure-smb      -fstype=smbfs,dmask=0700,fmask=0700,credentials=/etc/smb.auth     ://solfish/Share
 # -- snip --

NTFS permissions

When mounting an NTFS partition, you have to take a few extra steps to get the permissions right. The Linux NTFS drivers are read-only, so we never want to set the 'write' bit. Additionally, we want to clear the 'execute' for files, but set it for directories (to allow us to 'cd' into them). By default, each partition has a group of 'disk', so we will allow 'read' access to only users who are members of group 'disk'. Finally, since Windows is case-insensitive (c:\Windows, c:\WINDOWS, c:\windows are all the same), we might as well disable case-sensitivity.

As an example:

File: /etc/autofs/
 # -- snip --
 c    -fstype=ntfs,fmask=0337,dmask=0227,gid=disk,case_sensitive=false    :/dev/hdaX
 # -- snip --

Directory Creation and Linking

Now that we have configured autofs to manage /mnt/auto, we need to create this directory.

mkdir -p /mnt/auto

We can also create some interesting links so that things in common places can be easily accessed.

Verify nothing is mounted at these locations:

umount /boot
umount /mnt/cdrom
umount /mnt/floppy

Remove the original directories

### WARNING DO NOT REMOVE THE /boot directory. It is a vital directory. 
rmdir /mnt/cdrom
rmdir /mnt/floppy

Create interesting links:

ln -s /mnt/auto/boot /boot
ln -s /mnt/auto/cdrom /mnt/cdrom
ln -s /mnt/auto/floppy /mnt/floppy

Startup and System Boot

After this is complete, start the AUTOFS daemon by issuing:

/etc/init.d/autofs start

If you wish to have this behavior on default startup, issue:

rc-update add autofs default


I have encountered a problem using autofs to mount remote nfs shares. The problem was that the server's log reports successful authentication whereas the client told "permission denied by server". The workaround is to put "/etc/init.d/portmap restart" into "/etc/conf.d/local.start". That problem seems to be related to how portmap and nfs interact...

Also, by default, gnome (without gnome-volume-mounter) won't display the icons for floppy and CD etc. Re-installing gnome-volume-mounter as a workaround requires pmount, which fails to install once autofs is running ( [Errno 13] Permission denied: '/media/.keep#new' ), of course because autofs is managing /media. As a workaround to this problem, I stopped autofs, renamed media to mediaauto, so autofs can manage its mediaauto directory with the appropriate floppy and cdrom symlinks from /media. This way you can have autofs (for automounting floppies and cdrom), hal, dbus and gnome-volume-manager happily installed together.

If you experience sluggish transfer speeds between a linux client and a samba server, change -fstype from smbfs to cifs. Be sure to have cifs support compiled into [or as modules] your kernel.

-fstype=smbfs -> -fstype=cifs


Added by Turbomaniac_7 2007-04-10 This for the ones to wish to auto-mount by UUID instead:

Note: I will be using my USB harddrive as an example here.

First we have to figure out the UUID for the device we wish autofs to work with. 'tune2fs -l <device>' can be used but only if the partition is a EXT2/EXT3, therefore the 'blkid' is more universal.

Lets see what we got with the 'blkid' command:

# blkid
/dev/hda1: UUID="1b50a494-c9e4-4d92-91a6-31b6662b7231" TYPE="ext2"
/dev/hda2: UUID="3eb9c4b5-fb87-4728-a5ae-5f06d40493bd" TYPE="swap"
/dev/hda3: UUID="0f19ba2a-8506-49a1-b23f-f8676727e54a" SEC_TYPE="ext2" TYPE="ext3"
/dev/sde1: LABEL="USB-120GB" UUID="3212-4c62" TYPE="vfat"

Inside '/etc/autofs/' instead of: ':/dev/hdaX' we have to write 'UUID="3212-4c62"'. Like this:

File: /etc/autofs/
 # -- snip --
 usb-120gb  -fstype=vfat,fmask=0137,dmask=0027,uid=1000,gid=100   UUID="3212-4c62"
 # -- snip --

That's it.


added by Pillarsdotnet 08:24, 4 December 2007 (UTC)

Here is a udev-only solution, that doesn't depend on autofs, hald, uuid or device name:

Find an existing udev rule that is launched after the usb partition node is created

For me, the rule was in /etc/udev/rules.d/60-persistent-storage.rules, and it looked like this:

File: /etc/udev/rules.d/60-persistent-storage.rules
 # -- snip --
 ENV{DEVTYPE}=="partition", IMPORT{program}="vol_id --export $tempnode"
 # -- snip --

Modify the rule to run mount/unmount scripts of your choosing

File: /etc/udev/rules.d/60-persistent-storage.rules
 # -- snip --
 ENV{DEVTYPE}=="partition", IMPORT{program}="vol_id --export $tempnode", RUN+="/lib/udev/usb_mount", ENV{REMOVE_CMD}="/lib/udev/usb_unmount"
 # -- snip --

Write the mount/unmount scripts

File: /lib/udev/usb_mount
 [ "$ACTION" = "add" -a "$DEVTYPE" = "partition" -a "$ID_BUS" = "usb" ] || exit 0
 chown root:usb "$DEVNAME"
 chmod 0660 "$DEVNAME"
 mkdir $MNTDIR
 /bin/mount -t $ID_FS_TYPE  $DEVNAME $MNTDIR -o noatime,noexec,nosuid,nodev,nodiratime,nosuid,rw,umask=0,dmask=0,fmask=111,uid=65534,gid=65534
File: /lib/udev/usb_unmount
 [ "$ACTION" = "remove" ] && (grep -q "^$DEVNAME" /proc/mounts || grep -q "^$DEVNAME" /etc/mtab) || exit 0
 /bin/umount -l "$DEVNAME"
 rmdir /mnt/auto/${DEVNAME##/dev/}

UDEV Examples

This is a simple example on how to automount a particular usb hard disk using udev.

1.- Identify the disk:

udevinfo -a -p $(udevinfo -q path -n /dev/sdb1)

2.- Write the rule in /etc/udev/rules.d/10-local.rules creating a symlink to the device and running mount:

KERNEL=="sd?1", SUBSYSTEMS=="scsi", ATTRS{model}=="HD400LD", GROUP="users", SYMLINK+="PeekBox", RUN+="/bin/mount /dev/PeekBox"

3.- Define the drive in /etc/fstab

/dev/PeekBox    /mnt/PeekBox    vfat    noauto,users,gid=100,umask=007  0 0

When you plug the usb hard disk it gets automounted:

$ mount
/dev/sdb1 on /mnt/PeekBox type vfat (rw,noexec,nosuid,nodev)

UDEV with autofs

added by docbill, 29 Feb 2008 - modified 11 Sep 2008 by Anaphyis

If you want to want to combine the advantages of UDEV (that is mounting a medium when added and removed) and autofs (unmounting when not in use) you can use this approach.

Add an entry to your /etc/auto.master

File: /etc/auto.master
 # -- snip --
 /media   /etc/auto.usb --timeout=5 --ghost
 # -- snip --

Note: The auto.usb name is completely arbitrary. You can also use this file to mount removable media via eSATA or mobile racks.

Find an existing udev rule that is launched after your filesystem is recognized

Note: In newer udev Versions this rule file is found in /lib/udev/rudles.d instead. As they are overwritten at an update you should use the persistent storage rule file as a template for a own ruleset.

File: /etc/udev/rules.d/60-persistent-storage.rules
 # -- snip --
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{ID_FS_UUID_ENC}=="?*", SYMLINK+="disk/by-uuid/$env{ID_FS_UUID_ENC}"
 # -- snip --

Modify the rule to run automount/unautomount scripts of your choosing

File: /etc/udev/rules.d/60-persistent-storage.rules
 # -- snip --
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{ID_FS_UUID_ENC}=="?*", SYMLINK+="disk/by-uuid/$env{ID_FS_UUID_ENC}", RUN+="/lib/udev/usb_automount", ENV{REMOVE_CMD}="/lib/udev/usb_unautomount"
 # -- snip --

Write the mount/unmount scripts

File: /lib/udev/usb_automount
[ "x$ACTION" = "xadd" -a "x$DEVTYPE" = "xpartition" ] || exit 0
grep -q ":$DEVICE\$" /etc/auto.usb && exit 0

grep -q "^$DEVNAME" /etc/fstab && exit 0
grep -q "^$DEVICE" /etc/fstab && exit 0
grep -q "^label:$ID_FS_LABEL_ENC" /etc/fstab && exit 0
LABEL=$(echo "$ID_FS_LABEL_ENC" |sed 's, ,_,g')
if [ -z "$LABEL" ]; then
while ( grep -q "^$MNTDIR " /etc/auto.usb )
   seq=$(expr $seq + 1)
if  blkid $DEVICE|grep -q TYPE\=\"ext3\"; then
elif blkid $DEVICE|grep -q ext2; then
elif blkid $DEVICE|grep -q ntfs; then
elif blkid $DEVICE|grep -q fat; then
echo "$MNTDIR $MOUNTOPTS :$DEVICE" >> /etc/auto.usb

Note: You might want to check the mount options, especially the writeback mode for ext3 and the gid/uid combinations for the M$ filesystems. Check also the tune2fs manpage on how to set volume names. If you want to use automount for USB devices only, change the second line to:

File: /lib/udev/usb_automount
-- snip --
[ "x$ACTION" = "xadd" -a "x$DEVTYPE" = "xpartition" -a "x$ID_BUS" = "xusb" ] || exit 0

-- snip --

File: /lib/udev/usb_automount_clean
for i in $(sed -e 's,.*:,,g' /etc/auto.usb)
    if [ -n "$i" -a ! -e "$i" ] 
        k=$(echo "$i"|sed 's,/,\\/,g')
        sed -ic -e "/:$k\$/d" /etc/auto.usb
service autofs reload

Start everything off

chmod ugo+rX /lib/udev/usb_*automount*
touch /etc/auto.usb
service autofs restart
udevadm control --reload_rules
udevadm trigger

You should now see your removable devices not mentioned in fstab automatically added to /media - unless of course you used the line to restrict autofs to USB devices only.

See also

Automount Howto - From the TLDP

Retrieved from ""

Last modified: Sat, 11 Oct 2008 09:41:00 +0000 Hits: 126,406