viernes, 1 de agosto de 2014

How to mount a remote directory over ssh on Linux

Suppose you would like to mount a remote directory locally. However, you only have ssh access to the host where the remote directory resides, and there is no network file system (e.g., NFS, Samba) available to export the directory with. In this case, you can actually mount a remote directory over ssh, and access the directory via file system interfaces.

Mounting a remote folder over ssh is handled by FUSE kernel module, which allows one to create a virtual file system in user space. sshfs and gvfs are two such virtual file systems built on FUSE that allow one to mount a remote file system over ssh. In this post, I will show how to mount a remote directory over ssh with sshfs and gvfs.

Mount a remote directory over ssh with sshfs

To install sshfs on Ubuntu or Debian:

$ sudo apt-get install sshfs

To install sshfs on CentOS, RHEL or Fedora, first enable EPEL repository on your system, and then run the following.

$ sudo yum install sshfs

Next, if you want to use sshfs as a non-root user, you need to add the user to a group called fuse. That is:

$ sudo usermod -a -G fuse <user_name>

Run the following to make group membership change activated.

$ exec su -l $USER

Finally, you can mount a remote directory using sshfs as follows.

$ sshfs my_user@remote_host:/path/to/directory <local_mount_point>

The above command will ask you for ssh password for the remote host. Once you enter the password, a remote directory will become available at the local mount point. If you want to set up passwordless mounting, all you have to do is to set up passwordless ssh login to my_user@remote_host.

To unmount a ssh-mounted directory:

$ fusermount -u <local_mount_point>

If you would like to automatically mount over ssh upon boot, set up passwordless ssh login, and append the following in /etc/fstab.