Mount remote directory using SSHFS

in #ssh2 years ago

SSHFS (Secure SHell FileSystem) is a file system for GNU/Linux, BSD and other UNIX like OS that allows you to mount a remote directory in userspace (filesystem userspace) making use of FUSE and ssh. Once you mounted the remote directory you can do any operation over it as if it were a local DIR.

Install

Debian/Ubuntu

$ sudo apt-get install sshfs

CentOS/RHEL/Rocky/Alma

You must enable EPEL repo

 # dnf install fuse-sshfs

Synopsis

To mount a filesystem:

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

If host is a numeric IPv6 address, it needs to be enclosed in square brackets.

To unmount it:

fusermount3 -u mountpoint # Linux

umount mountpoint # OS X, FreeBSD

Example

Mount remote dir /var/www/flossblog into local dir flossblog with default options.

$ sshfs sedlav@flossblog.com:/var/www/flossblog flossblog

To mount the remote file system after reboot add this line to the fstab

user@host:dir mountpoint sshfs _netdev,noexec,nosuid,nodev,idmap=user,identityfile=path-to-private-key,port=23,allow_other,default_permissions,ServerAliveInterval=15 0 0

Options

  • _netdev: The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).
  • noexec: No binary execution is allowed.
  • nosuid: Ignore set-user-ID and set-group-ID bits.
  • nodev: Do not interpret character or block special devices.
  • idmap=user: Map the UID/GID of the remote user to UID/GID of the mounting user..
  • identityfile: Specifies the path of the private key to make the connection, the user, in this case root, must be able to connect to the remote server using public/private key.
  • port=23: Remote port.
  • allow_other: Other users can access the file system.
  • default_permissions: Local permission checking.
  • ServerAliveInterval: Force the SSH connection to stay alive even if you have no activity.

References