Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question
    
Looks like this is a known bug that is probably not fixed yet (no new version of sshfs since 2008). –  Vinh Nguyen May 12 '11 at 18:42
    
My solution to all this can be found here. –  Vinh Nguyen May 12 '11 at 19:34
add comment

3 Answers 3

up vote 3 down vote accepted

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
share|improve this answer
    
Thanks for this. How would you incorporate this in /etc/fstab? –  Vinh Nguyen Jan 13 '12 at 22:34
    
Use \040 for spaces: sshfs#USERID@HOST:/TARGETPATH /MOUNT_POINT fuse user,noauto,ssh_command=/path/to/sshcmd\040-Y\040-X 0 0 –  Manwe Jan 16 '12 at 10:16
    
I have something like the following, and it does not mount at startup: sshfs#username@server: /mnt/server fuse fsname=sshfs#username@server:,comment=sshfs,noauto,users,exec,uid=1000,gid=1000,‌​allow_other,reconnect,transform_symlinks,BatchMode=yes,ssh_command=/path/to/sshcm‌​d\040-Y\040-X\040-C 0 0 Do you have any suggestions? –  Vinh Nguyen Jan 16 '12 at 19:36
    
In my example and yours there is noauto options which means it is not automatically mounted. Depending on your system adding _netdev might do the trick or leaving the noauto in place and adding a line to /etc/rc.local like mount /mnt/server. This all requires that your network is working! –  Manwe Jan 16 '12 at 20:26
    
sorry please ignore my previous inquiry. I accidentally uncommented one the comment lines with comments. Even though I don't have a mount issue, I don't think the -Y -X -C is being honored because when I ssh into the same server, I can not open X11 windows. I was able to when I manually mounted outside of /etc/fstab as you originally proposed. –  Vinh Nguyen Jan 17 '12 at 17:02
show 9 more comments

Yesterday I had exactly the same problem. I've modified sshcmd so that nows it also tries to contact the user's ssh-agent. Thanks Manwë!

#!/bin/bash
# Open a ssh connection as a given user, thus using his/hers authentication
# agent and/or config files.
: ${ADDOPTS:="-2Ax"}
: ${LOCAL:="kreator"}
export SSH_AUTH_SOCK=$(find /tmp/ssh-* -type s -user ${LOCAL} -name agent* | tail -1)
declare -a options=( $* )

# Remove unwanted options
for (( i=0,fin=${#options[*]} ; i < fin ; i++ ))
do
    case ${options[$i]} in
            (-a|-oClearAllForwardings=*)    unset options[$i]
                                            ;;
    esac
done

exec /bin/su ${LOCAL} -c "$(which ssh) ${ADDOPTS} ${options[*]}"
share|improve this answer
add comment

Try this:

sshfs -ossh_command="ssh -A me@host1_wan_ip ssh $@" me@host2_lan_ip:/ /media/me/mount1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.