[Debian-Sarge] Tunneling NFS over SSH
[Debian-Sarge] Tunneling NFS over SSHLast Update: 27-09-2006 @ ~21:40
Reason: Added fixed ports for nfs server to make life easy :)
WelcomeThe goal of this howto is building a NFS server that works on a SSH tunnel. This way all traffic between your hosts and the file server is encrypted and thus more secure :)
Normally you should enter a password every time you try to establish a SSH connection but since we could be mounting at bootup we will use ssh-keygen to create a keypair so we can login without entering a password. We will, however, limit that login session to executing just 1 command ;)
We will use a new clean Debian Sarge install to begin with.
In this howto I will use the fictional domain "linux.lan".
We will start with the NFS server.
apt-get -y install nfs-kernel-server
First configure it to run on fixed ports, this will make building a firewall much easier but equally important it aids in simpler client mounts.
echo "STATDOPTS=--port 2231" > /etc/default/nfs-common
Create a new user called sleeper to use for setting up the ssh tunnel from other hosts. We will generate a key for this account so you can login with a keyfile instead of typing your password everytime. The account will also be restricted to execute 'sleep' trough this way. Other commands will simply fail.
Now switch over to a client that will use our fileserver.
First we need a key:
ssh-keygen -t rsa -b 2048(use defaults and NO passphrase!)
Now copy the .pub file to the homedir of sleeper on the server:
scp -P 12345 ~/.ssh/id_rsa.pub firstname.lastname@example.org:~/
Now back to the server: As the 'sleeper' user we need to configure/install the key:
client="client.linux.lan",command="/bin/sleep 600d"substitute "client" with the correct hostname of your client, or use ip numbers.
(but make sure every entry stays on 1 line!)
Every client that needs access to the fileserver needs to store his security data (from the id_rsa.pub file) in the authorized_keys file, so you should repeat this for every host.
Mounting NFS over SSH on your clientsIssue these commands to start the tunnels for nfs and mountd:
(syntax: ssh -f -c encyption -L localport:nfsserver:nfsport -l username nfsserver remotecommand)
Also note that the portnumber for mountd is different with every restart of the NFS server... Keep that in mind.
ssh -f -i /root/.ssh/id_rsa -c blowfish -L 61001:10.0.0.241:2049 -l sleeper 10.0.0.241 sleep 600d
Now edit your fstab and mount:
echo "localhost:/export/data /mnt nfs tcp,rsize=8192,wsize=8192,intr,rw,bg,nosuid,port=61001,mountport=62001,noauto" >> /etc/fstab
Ofcourse we need some mountable folders (shares) defined on the NFS server:
/home/export/data 10.0.0.241(rw,root_squash,sync)Notice the ip address is the nfs server itself? Its because youll mount them from localhost when you have established the ssh tunnel.
Some security settings since we dont want anyone from outside our network to access the server:
echo "portmap: ALL" > /etc/hosts.deny
/etc/init.d/nfs-kernel-server restartThats it ! You can now mount the filesystem on your clients without the need to supply a password. And ofcourse all traffic will be encrypted :)