Sshfs with x forwarding

sshsshfsx11-forwarding

I've been using sshfs to mount directories from servers on my laptop.

I also have the following in my ~/.ssh/config:
ControlMaster auto

I set this because it keeps a single ssh connection alive whenever I
ssh into a server. I do this particularly for fast browsing in emacs
using tramp.

Since I have sshfs in my /etc/fstab, the ssh connection from sshfs is
the one that stays alive. A lot of times I also need X11 forwarding
(ssh -X -Y -C), and since I'm always using the original connection
from sshfs, X11 isn't forwarded.

X11 works when I remove the sshfs mount from my fstab, and I ssh into
my servers manually with the -X -Y -C option.

Is there a way to turn on -X -Y -C (or the likes) for sshfs? I tried adding
ForwardX11 yes
ForwardX11Trusted yes

in ~/.ssh/config and /etc/ssh/ssh_config to force the option to always
be on (might have security issue). However, sshfs in /etc/fstab does
not pick this up.

Do you have any suggestions? Thanks!

Best Answer

The way around this problem is to use script in place of ssh command.

man pages of sshfs (at least since 2.0 April 2008)

-o ssh_command=CMD
  execute CMD instead of 'ssh'

Like this:

sshfs user@remote: mountpoint -o ssh_command='/path/to/sshcmd -Y -X -C '

And here's sshcmd

#!/bin/bash

# declare array for ssh options
declare -a CLEANED_SSH_OPTS
declare -a ADD_OPTIONS

# add options to be automatically added to the ssh command here.
# example
#ADD_OPTIONS=( '-C' )
# empty default
ADD_OPTIONS=(  )

for OPT in "$@"; do 
  # add list of values to be removed
  # from sshfs ssh options
  case $OPT in
    "-x")
     # this and these like this will be removed
    ;;
    "-a")
    ;;
    "-oClearAllForwardings=yes")
    ;;
    *)
      # These are ok.. add
      NUM=${#CLEANED_SSH_OPTS[@]}
      CLEANED_SSH_OPTS[$NUM]="$OPT"
    ;;
  esac
done

# Start ssh with CLEANED options
exec ssh ${ADD_OPTIONS[@]} ${CLEANED_SSH_OPTS[@]}
# replace above exec with the next one if you ssh tunneling to run as your 
# local user. Like when using automatic mounts from fstab.
# Please note that this has some extra security issues.
#exec su YourUserName -c "ssh ${ADD_OPTIONS[@]} ${CLEANED_SSH_OPTS[@]}"

Adding something like this to fstab should allow automatic mounting of sshfs and enable forwarding. Do note that when mount happens it's usually root user so you might have to make appropriate changes to sshcmd (see last line of sshcmd).

sshfs#USERID@HOST:/TARGETPATH /MOUNT_POINT fuse _netdev,user,ssh_command=/path/to/sshcmd\040-Y\040-X 0 0
Related Question