Mount NFS over SSH
Why
The reason to mount NFS over SSH is simple for my case: my NAS is in my home, but I need to access it from my laptop when I'm outside my home. I chose NFS as I stay at home most of the time, in which case my laptop is in the same LAN as the NAS. There is obviously no need to set up another set of mechanism.
To secure the NAS, it is better that it stays in the LAN. It would be simpler to set only one port mapping rule from the configuration interface of the router, that is the port for SSH (I still need SSH anyway), from LAN port 22 to WAN port 20022.
In this way, I don't need to set up the authentication and encryption for NFS separately.
Installing NFS on the both machines is easy. Mounting can be easily done when connecting directly. However, when trying to mount over SSH, I found the steps found by Google does not work.
How
In my case, NFSv3 is used.
In addition to nfsd
, mountd
also need to be accessed from the client. By default, mountd uses a random port. However, to access mountd easily, I fixed the port used by mountd by adding those lines to /etc/services
of the server:
mount 32759/udp
mount 32759/tcp
After restarting the server to make the changes into effect, running ssh
on the client machine to forward the ports:
ssh home.kmxz.net -fNv -p 20022 -L 3049:localhost:2049
ssh home.kmxz.net -fNv -p 20022 -L 33759:localhost:32759
The next step is to mount the NFS on the client machine:
sudo mount -v -t nfs -o soft,intr,nolock,port=3049,mountport=33759,tcp localhost:/mnt/aufs /mnt/nas
nolock
option is necessary, otherwise the client machine will try to lock files on the client itself. tcp
is also necessary, as mountproto
will be UDP by default, but SSH only support TCP forwarding.