SaltyCrane Blog — Notes on JavaScript and web development

Notes on sshfs on Ubuntu

sshfs is an easy way to mount a remote filesystem using ssh and FUSE. If your remote server is already running a ssh server that supports sftp (Ubuntu's ssh server does), there is nothing to set up on the remote server and set up on the client is relatively easy.

Other options for mounting a remote filesystem are WebDAV, Samba, and NFS. I'm no expert, but from what I've gathered, sshfs is faster than WebDAV and slower than Samba and NFS. However, Samba and NFS are typically more difficult to set up than sshfs. Here are my notes for setting up sshfs. I am running on Ubuntu Hardy.

OPTION 1: Use sshfs from the command line

  • Install sshfs
    $ apt-get update
    $ apt-get install sshfs
    
  • Create a mount point
    $ mkdir -p /var/www/remote_files
    
  • Mount the remote filesystem
    $ sshfs [email protected]:/mnt/files /var/www/remote_files \
    > -o IdentityFile=/path/to/my_ssh_keyfile \
    > -o ServerAliveInterval=60 -o allow_other
    
    where:
    • root is the ssh username
    • 10.232.139.234 is the remote host
    • /mnt/files is the remote path
    • /var/www/remote_files is the local path
    • /path/to/my_ssh_keyfile is the ssh keyfile
    • The ServerAliveInterval option will keep your connection from timing out.
    • The allow_other option allows other users to access the filesystem

OPTION 2: Use sshfs with /etc/fstab

  • Install sshfs as above
  • Edit /etc/fstab:
    sshfs#[email protected]:/mnt/files /var/www/remote_files fuse allow_other,IdentityFile=/path/to/my_ssh_keyfile,ServerAliveInterval=60 0 0
    where the options are explained above.
  • Create a mount point
    $ mkdir -p /var/www/remote_files
    
  • Mount
    $ mount /var/www/remote_files
    

For more help, try sshfs --help

usage: sshfs [user@]host:[dir] mountpoint [options]

general options:
    -o opt,[opt...]        mount options
    -h   --help            print help
    -V   --version         print version

SSHFS options:
    -p PORT                equivalent to '-o port=PORT'
    -C                     equivalent to '-o compression=yes'
    -1                     equivalent to '-o ssh_protocol=1'
    -o reconnect           reconnect to server
    -o sshfs_sync          synchronous writes
    -o no_readahead        synchronous reads (no speculative readahead)
    -o sshfs_debug         print some debugging information
    -o cache=YESNO         enable caching {yes,no} (default: yes)
    -o cache_timeout=N     sets timeout for caches in seconds (default: 20)
    -o cache_X_timeout=N   sets timeout for {stat,dir,link} cache
    -o workaround=LIST     colon separated list of workarounds
             none             no workarounds enabled
             all              all workarounds enabled
             [no]rename       fix renaming to existing file (default: off)
             [no]nodelay      set nodelay tcp flag in ssh (default: on)
             [no]nodelaysrv   set nodelay tcp flag in sshd (default: off)
             [no]truncate     fix truncate for old servers (default: off)
             [no]buflimit     fix buffer fillup bug in server (default: on)
    -o idmap=TYPE          user/group ID mapping, possible types are:
             none             no translation of the ID space (default)
             user             only translate UID of connecting user
    -o ssh_command=CMD     execute CMD instead of 'ssh'
    -o ssh_protocol=N      ssh protocol to use (default: 2)
    -o sftp_server=SERV    path to sftp server or subsystem (default: sftp)
    -o directport=PORT     directly connect to PORT bypassing ssh
    -o transform_symlinks  transform absolute symlinks to relative
    -o follow_symlinks     follow symlinks on the server
    -o no_check_root       don't check for existence of 'dir' on server
    -o SSHOPT=VAL          ssh options (see man ssh_config)

FUSE options:
    -d    -o debug          enable debug output (implies -f)
    -f                      foreground operation
    -s                      disable multi-threaded operation

    -o allow_other          allow access to other users
    -o allow_root           allow access to root
    -o nonempty     allow mounts over non-empty file/dir
    -o default_permissions enable permission checking by kernel
    -o fsname=NAME          set filesystem name
    -o subtype=NAME         set filesystem type
    -o large_read           issue large read requests (2.4 only)
    -o max_read=N           set maximum size of read requests

    -o hard_remove          immediate removal (don't hide files)
    -o use_ino              let filesystem set inode numbers
    -o readdir_ino          try to fill in d_ino in readdir
    -o direct_io            use direct I/O
    -o kernel_cache         cache files in kernel
    -o [no]auto_cache       enable caching based on modification times
    -o umask=M              set file permissions (octal)
    -o uid=N                set file owner
    -o gid=N                set file group
    -o entry_timeout=T      cache timeout for names (1.0s)
    -o negative_timeout=T  cache timeout for deleted names (0.0s)
    -o attr_timeout=T       cache timeout for attributes (1.0s)
    -o ac_attr_timeout=T   auto cache timeout for attributes (attr_timeout)
    -o intr                 allow requests to be interrupted
    -o intr_signal=NUM      signal to send on interrupt (10)
    -o modules=M1[:M2...]  names of modules to push onto filesystem stack

    -o max_write=N          set maximum size of write requests
    -o max_readahead=N      set maximum readahead
    -o async_read           perform reads asynchronously (default)
    -o sync_read            perform reads synchronously

Module options:

[subdir]
    -o subdir=DIR           prepend this directory to all paths (mandatory)
    -o [no]rellinks         transform absolute symlinks to relative

[iconv]
    -o from_code=CHARSET   original encoding of file names (default: UTF-8)
    -o to_code=CHARSET      new encoding of the file names (default: ANSI_X3.4-1968)

References

Webdav vs. sshfs

Comments


#1 Brendan Miller commented on :

I find these flags are useful:

-o cache=no,idmap=user

Assuming you are talking to something over the local network, turning off caching can prevent you from having to wait for the cache to expire to get some new file.

The idmap=user is useful if the other machine is not on the same domain. It will translate ids for whatever use you log in as to your own user name.


#2 Eliot commented on :

Brendan: Thank you for the tips.


#3 damdam commented on :

After connecting remote folder with sshfs, if it lost the connection because of any reason like network issues while connection is active, it hangs the whole Ubuntu system (10.04) or Nautilus! I think this is a bug...


#4 Mike commented on :

Excellent instructions. Very concise. Worked like a charm.