Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mount NFS Folder via SSH Tunnel

1. Install NFS on Server

Install the required packages (Ubuntu 12.04):

apt-get install nfs-kernel-server portmap

2. Share NFS Folder

Open the exports file:

vim /etc/exports

Add the following line:

/home/proudlygeek  localhost(insecure,rw,sync,no_subtree_check)

Restart NFS Service:

service nfs-kernel-server restart

or just export the FS:

exportfs -a

3. Install NFS Client

Install the client:

apt-get install nfs-common portmap

Make a mount folder:

mkdir /mnt/nfs-share

Setup an SSH tunnel (local-to-remote port):

ssh -fNv -L 3049:localhost:2049 user@hostname

Mount the folder:

mount -t nfs -o port=3049 localhost:/home/proudlygeek /mnt/nfs-share
@lethalman

This comment has been minimized.

Copy link

commented Jan 2, 2016

Tried it, the performance is quite similar to sshfs in that saving a file may take few seconds, unfortunately :(

@RicterZ

This comment has been minimized.

Copy link

commented Sep 13, 2016

Hello, which version of NFS was supported in this post?
By capturing the traffic of showmount and mount, I found both of them had two ports (111 and 2049) traffic and there was some UDP traffic.

@zewt

This comment has been minimized.

Copy link

commented Dec 30, 2016

This works for me with a couple changes:

  • Add "-o proto=tcp", so it doesn't try other protocols.
  • Add "insecure" to the /etc/exports entries. SSH won't be using a low port, so it'll reject it for this. (Port numbers as a security mechanism are really silly these days--this shouldn't be the default.)

It's still unusably slow, though.

@sarnobat

This comment has been minimized.

Copy link

commented Nov 14, 2018

Doesn't work for me :(

mount_nfs: can't mount / from localhost onto /private/tmp/nfs: Connection refused
@mc256

This comment has been minimized.

Copy link

commented May 19, 2019

Alternatively, you could use autossh to keep the port forwarding alive.
autossh -M <monitor_SSH_port> -N -L <your_local_port>:localhost:2049 <remote_server_user>@<remote_server> -f
mount -t nfs -o port=<your_local_port> localhost:/home/proudlygeek /mnt/nfs-share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.