How to set up sshfs with autofs

Introduction

This is my personal log of how to set up a server and a client machine to automount a remote directory using sshfs.

Server side

On a server machine called foobar

Edit /etc/ssh/sshd_config and change the line from

Subsystem sftp /usr/lib/openssh/sftp-server
to
Subsystem sftp internal-sftp

At the end of /etc/ssh/sshd_config, adding the sshfs user to an existing AllowUsers config entry:

AllowUsers sshfs
Match Group sftpchroot
    ChrootDirectory /home/%u
    ForceCommand internal-sftp
    X11Forwarding no
    AllowTcpForwarding no

Restart sshd

/etc/init.d/sshd restart

Add to groups and create the sshfs user:

# groupadd sftpchroot
# useradd -g sftpchroot -d /home/sshfs -m -s /sbin/nologin sshfs
# chown root:root /home/sshfs
# mkdir -p /home/sshfs/home/sshfs
# chown sshfs:sftpchroot /home/sshfs/home/sshfs

Prepare the directories and mount points. We'll use /x/vision for this example

# mkdir /home/sshfs/home/sshfs/vision

Edit /etc/fstab and add:

/x/vision /home/sshfs/home/sshfs/vision   none    bind 0 0

Them mount it

# mount /home/sshfs/home/sshfs/vision

The client side

You will need a kernel that supports automounting and has FUSE support. Look in the filesystems section.

As I run Gentoo Linux, the following installs the latest autofs

# echo net-fs/autofs >> /etc/portage/package.keywords
# emerge net-fs/autofs sys-fs/sshfs-fuse

Edit /etc/autofs/auto.master and add something like (use appropriate uid and groupid):

/mnt/sshfs /etc/autofs/auto.sshfs uid=1000,gid=100,-v,--timeout=300

Create /etc/autofs/auto.sshfs and add en entry pointing to the server called foobar:

vision   -fstype=fuse,rw,sync,intr,nodev,nonempty,noatime,allow_other,max_read=65536,compression=no,Ciphers=arcfour :sshfs\#sshfs@foobar\:vision

Create symlink to the directory:

ln -s /mnt/sshfs/vision /vision

After sending root's public key to foobar and adding it to /home/sshfs/.ssh/authorized_keys, we bootstrap the ssh connection:

# su -
# ssh sshfs@foobar

Add autofs to the rc start process and start it

# rc-update add autofs default
# /etc/init.d/autofs start

A useful bash alias to have so that the automounted directories are transparent in your login session are

alias ll='/bin/ls -l --dereference-command-line-symlink-to-dir'
alias ls='/bin/ls -F --dereference-command-line-symlink-to-dir'

Congratulations. You now have sshfs working with autofs

Fudge
11 June 2014