Tuesday 2010-12-28

To set up autofs with sshfs so that it works with your ssh-agent, you need to set your ssh-agent to use a pre-defined path, and then tell autofs about it.

  1. Fix your shell. For bash, do something like:
    file.exists() { test -e "$1" ; }
    export SSH_AUTH_SOCK="${HOME}/.ssh/agent"
    ssh_agent_check() {
    	ssh_agent=$(ssh-add -l 2>&1)
    	no_identities="The agent has no identities."
    	no_agent="Could not open a connection to your authentication agent."
    	[[ "${ssh_agent}" = "$no_agent" ]] && { 
    		file.exists ${SSH_AUTH_SOCK} && { 
    			killall -9 ssh-agent 2>/dev/null
    			command rm ${SSH_AUTH_SOCK} 2>/dev/null
    		}
    		ssh-agent -a ${SSH_AUTH_SOCK} >/dev/null
    	}
    	ssh_agent=$(ssh-add -l 2>&1)
    	[[ "${ssh_agent}" = "${no_identities}" ]] && { 
    		ssh-add
    	}
    }
    ssh() { ssh_agent_check; command ssh "$@"; }
    scp() { ssh_agent_check; command scp "$@"; }
    
  2. Add the environment to /etc/conf.d/autofs, where $HOME is your home, not root's.
    export SSH_AUTH_SOCK="${HOME}/.ssh/agent"
    
  3. Set up /etc/autofs/auto.master like everyone tells you on the internet, replacing $MNT with where you want all the sshfs mounts to live (I use /n), $UID = your UID, $GID ....
    /$MNT /etc/autofs/auto.sshfs uid=$UID,gid=$GID,--timeout=30
    
  4. Set up /etc/autofs/auto.sshfs also like everyone says, changing $USER and $HOST
    haller.ws    -fstype=fuse,rw,allow_other,IdentityFile=/home/$USER/.ssh/id_dsa,Port=22 :sshfs\#$USER@$HOST\:/
    

That's it. $( /etc/rc.d/autofs restart ) and you should should be good to go. Otherwise, add verbose to your /etc/conf.d/autofs daemonoptions. ;)

You might be able to get away with running autofs from one's xinitrc, so that would inherit the SSH_AUTH_SOCK and you might not need to use a predefined SSH_AUTH_SOCK.