Mount permanently NFS Share over SSH tunnel

Pre-requirements: passwordless (key based) ssh connection and NFS v4

Assuming that you can already establish the tunnel on-demand using nfs-share user and we have NFS installed. NFS server has X.X.X.X IP

Configuring a new System service for our SSH tunnel
nano /etc/systemd/system/shtunnel.service

[Unit]
Description=Create Tunnel and expose port 2049 on remote system to port 3049 on local
After=network.target

[Service]
Restart=on-failure
RestartSec=5
ExecStart=/usr/bin/ssh -i /key/id_rsa -NTC -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L 3049:localhost:2049 nfs-share@X.X.X.X

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable shtunnel
systemctl start shtunnel

Mounting the NFS share
nano nano /etc/systemd/system/opt-shared.mount

NB: mount type Systemd unit file must be named in accordance to the file system mount target.

NB: On the NFS server make sure you include “insecure”
/opt/shared/ localhost(rw,sync,no_subtree_check,insecure,nohide,fsid=1)

[Unit]
After=shtunnel.service

[Mount]
What=localhost:/opt/shared
Where=/opt/shared
Type=nfs
Options=port=3049,proto=tcp

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable opt-shared.mount
systemctl start opt-shared.mount

Leave a Reply

Your email address will not be published. Required fields are marked *

*