Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Last active October 2, 2024 08:26
Show Gist options
  • Save proudlygeek/5721498 to your computer and use it in GitHub Desktop.
Save proudlygeek/5721498 to your computer and use it in GitHub Desktop.
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
@lucabrunox
Copy link

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

@RicterZ
Copy link

RicterZ 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
Copy link

zewt 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
Copy link

Doesn't work for me :(

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

@mc256
Copy link

mc256 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

@aure-olli
Copy link

I had to use localhost6 in /etc/exports for this to work. Weirdly, NFS recognize that localhost and localhost6 are the same (and only the first one will be used), but they are not equivalent.

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

@BilyZ98
Copy link

BilyZ98 commented Feb 12, 2022

One thing I am curious about is does the remote port has a specific meaning? is it a ssh port or is it just a random port ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment