Cooking with UNIX/Linux
Cooking with UNIX/Linux > Network File System (NFS)

Network File System (NFS)

NFS RECIPE by Frederik Dannemare

### Install the necessary packages on the server

# sudo apt-get install nfs-common nfs-kernel-server portmap


### Install the necessary packages on the clients

# sudo apt-get install nfs-common portmap


### Share (as read-only) /data to all client machines in
### subnet 192.168.1.0 by adding this line to /etc/exports

/data 192.168.1.0/255.255.255.0(ro,hard,intr)

### Share (as read-write) /usr/local to client machine
### wih IP 192.168.1.5 by adding this line to /etc/exports

/usr/local 192.168.1.5(rw,hard,intr)

### Using the hard option will make client processes using
### files on the nfs server hang and wait if the server goes
### down for whatever reason. The the server is back online
### the processes will continue undisturbed from where they
### were. In case you want to be able to kill the hanging
### processes for whatever reason, you will also need to
### specify the intr option. Without the intr option,
### it will be impossible to kill the hanging processes.


### For security reasons use hosts.[allow|deny] to restrict
### access to the daemons related to NFS. Start out by denying
### all hosts access (in /etc/hosts.deny) to any of the daemons,
### and then list the allowed hosts explictly in /etc/hosts.allow
### This should be done on the server and all clients

### /etc/hosts.deny

portmap: ALL
rpc.lockd: ALL
rpc.statd: ALL
rpc.mountd: ALL
rpc.nfsd: ALL

### /etc/hosts.allow

portmap: 192.168.1.0/255.255.255.0
rpc.lockd: 192.168.1.0/255.255.255.0
rpc.statd: 192.168.1.0/255.255.255.0
rpc.mountd: 192.168.1.0/255.255.255.0
rpc.nfsd: 192.168.1.0/255.255.255.0


### Whenever you change something in /etc/exports you should
### use exportfs to have the nfs daemon re-read its content

# exportfs -r


### In order to mount an NFS exported filesystem on a server
### called nfsserver, simply do

# mkdir /mnt/nfs
# mount -t nfs nfsserver:/usr/local /mnt/nfs


### Using showmount on the server you can see which clients
### have the exported filesystems attached

# showmount


### To print NFS statistics use nfsstat

# nfsstat


### Put this into /etc/fstab to have the shares mounted at boot time

# <file system>       <mount point>   <type>  <options>     <dump>  <pass>
nfsserver:/usr/local  /mnt/nfs1       nfs     rw,hard,intr  0       0
nfsserver:/data       /mnt/nfs2       nfs     ro,hard,intr  0       0