Sshfs

From ParabolaWiki
Jump to: navigation, search

You can use sshfs to mount a remote system - accessible via SSH - to a local folder, so you will be able to do any operation on the mounted files with any tool (copy, rename, edit with vim, etc.). Using sshfs instead of shfs is generally preferred as a new version of shfs hasn't been released since 2004.

1 Installation

To install the needed packages, do:

# pacman -S sshfs

This should install fuse and sshfs, and maybe other packages.

2 Usage

First a kernel module should be loaded, so as root, do:

# modprobe fuse

(You can put fuse into the module-list of /etc/rc.conf to auto-load at boot.)

2.1 Mounting

You will use the command sshfs. To mount a remote directory:

# sshfs USERNAME@HOSTNAME_OR_IP:/PATH LOCAL_MOUNT_POINT SSH_OPTIONS

For example:

# sshfs sessy@mycomputer:/home/sessy /mnt/sessy -C -p 9876

Where 9876 is the port number.

Also, make certain that before connecting, you set the file permissions for any local client folders you will attempt to mount a remote directory to. I.e., do not have everything owned by root! You could also run the mount command as a regular user, it should work as well.

SSH will ask for the password, if needed. If you do not want to type in your password 49 times a day, then read this: How to Use RSA Key Authentication with SSH or Using SSH Keys.

2.2 Unmounting

To unmount the remote system:

# fusermount -u LOCAL_MOUNT_POINT

Example:

# fusermount -u /mnt/sessy

3 Tips

To quickly mount a remote dir, do some file-management and unmount it, put this in a script:

sshfs USERNAME@HOSTNAME_OR_IP:/PATH LOCAL_MOUNT_POINT SSH_OPTIONS
mc ~ LOCAL_MOUNT_POINT
fusermount -u LOCAL_MOUNT_POINT

This will mount the remote directory, launch MC, and unmount it when you exit.

Thunar has issues with FAM and remote file access. If you experience remote folders not displaying, getting kicked back to the home directory, or other remote file access issues through Thunar, replace fam with gamin. Gamin is derived from fam.

# pacman -S gamin
# nano /etc/rc.conf  #remove fam in daemons

4 Chrooting

You may want to jail a (specific) user to a directory.To do this, edit /etc/ssh/sshd_config:

/etc/ssh/sshd_config
.....
Match User someuser 
       ChrootDirectory /chroot/%u
       ForceCommand internal-sftp #to restrict the user to sftp only
       AllowTcpForwarding no
       X11Forwarding no
.....
Note: The chroot directory must be owned by root, otherwise you will not be able to connect. For more info check the manpages for Match, ChrootDirectory and ForceCommand.

5 Helpers

If you often need to mount sshfs filesystems you may be interested in using an sshfs helper, such as sftpman.

It provides a command-line and a GTK frontend, to make mounting and unmounting a simple one click/command process.

6 Troubleshooting

6.1 Connection reset by peer

  • If you are trying to access the remote system with a hostname, try using its IP address, as it can be a domain name solving issue. Make sure you edit /etc/hosts with the server details.
  • If you are using non-default key names and are passing it as -i .ssh/my_key, this won't work. You have to use -o IdentityFile=/home/user/.ssh/my_key, with the full path to the key.
  • Adding the option 'sshfs_debug' (as in 'sshfs -o sshfs_debug user@server ...') can help in resolving the issue.
  • If you're trying to sshfs into a router running DD-WRT or the like, there is a solution here.
  • Forum thread: sshfs: Connection reset by peer
Note: When providing more than one option for sshfs, they must be comma separated. Like so: 'sshfs -o sshfs_debug,IdentityFile=</path/to/key> user@server ...')

6.2 Remote host has disconnected

  • If you recieve this message directly after attempting to use sshfs, try checking the path of your Subsystem listed in etc/ssh/sshd_config on the remote machine to see if it is valid.
  • you can check this by typing find / grep XXXX where XXXX is the path of the subsystem

7 fstab

An example on how to use sshfs to mount a remote filesystem through /etc/fstab

USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs  defaults 0 0

Take for example the fstab line

llib@192.168.1.200:/home/llib/FAH /media/FAH2 fuse.sshfs  defaults 0 0

The above will work automatically if you are using an SSH key for the user. See Using SSH Keys.

If you want to use sshfs with multiple users :

user@domain.org:/home/user  /media/user  fuse.sshfs   defaults,allow_other    0  0

If umount complains that the filesystem is not in fstab, this can be worked around by creating a script 'sbin/mount.fuse.sshfs':

#!/bin/bash
DEVICE="$1"
MOUNTPOINT="$2"
OPTIONS="$4"

OPTIONS="${OPTIONS/,noauto/}"
OPTIONS="${OPTIONS/,user/}"

# workaround to conflicting 'user' options
# in fstab, specify 'login=joe' instead of 'user=joe'
OPTIONS="${OPTIONS/,login=/,user=}"

exec /usr/bin/sshfs "$DEVICE" "$MOUNTPOINT" -o "$OPTIONS"

If you get "connection reset by peer" from using the fstab method, it's possible your PC is not yet connected to the internet at that point of the boot process. The solution for me was (as I'm using XFCE) to add a startup command to mount it using sshfs. At that point the internet connection should already be ready.

In my case, I also got "connection reset by peer" but it had nothing to do with the network; in fact I was trying to mount from rc.local script which runs close to last in runlevel 2, and by that time many other initscripts that depend on the network have already run. Instead, it turned out that the fusectl mount on /sys/fs/fuse/connections has not been completed yet. I solved this with a loop:

while ! grep -qw fusectl /proc/filesystems ||
      ! grep -qw /sys/fs/fuse/connections /proc/mounts ; do true ; done
mount /var/cache/apt/archives

I do hope this is useful to someone, as most google hits for 'sshfs "connection reset"' lead to the usual Ubuntu garbage.

8 Options

sshfs can automatically convert your local and remote user id's, if you add the idmap option:

# sshfs -o idmap=user sessy@mycomputer:/home/sessy /mnt/sessy -C -p 9876

If you have a different login on the remote system, it can still work if you provide the ssh standard option User:

# sshfs -o idmap=user,User=sessy2 sessy@mycomputer:/home/sessy /mnt/sessy -C -p 9876

(I've used first form, second is based on docs, so YMMV, but it should at least be close)

9 See also

10 Acknowledgement

This wiki article is based on ArchWiki. We may have removed non-FSDG bits from it.