NFS client and server handy commands

In case you are using NFS server, here are some handy commands you can use while working in production. These were tested in Ubuntu Linux.

Install nfs server

To install nfs server

$ sudo apt-get install nfs-common 
$ sudo apt-get install nfs-kernel-server 

Add the following line to /etc/hosts.allow

rpcbind mountd nfsd statd lockd rquotad : 127.0.0.1

export a directory

Create the directory to be exported.

$ sudo mkdir -p /myexport/dir1 

Add the following line to /etc/exports

/myexport/dir1 *(rw,sync,no_subtree_check,no_root_squash)

Note that this will allow any client to mount the exported directory on server if port 2049 is accessible from client to server. You may want to set appropriate client IPs here.

Now run the following to export the directory:

$ exportfs -ar

show what exports are available on a server

Use showmount to show what exports are available on localhost

showmount -e localhost

show what exports are available on a server from client

Use showmount on client machine to show what exports are available from nfs server

showmount -e nfsserver1.com
Export list for nfsserver1.com:
/myexport/dir1 *

Mount directory from client

Mount directory from client

$ mkdir -p /mnt1/nfsserver1.com/myexport/dir1
$ mount -t nfs nfsserver1.com:/myexport/dir1 /mnt1/nfsserver1.com/myexport/dir1

Displaying clients connected to server

Use netstat to list all connections and grep for NFS port.

MYIP=`ifconfig eth0 | grep "inet addr:" |awk -F":" '{print $2}' | awk '{print $1}'`
$ netstat -an | grep $MYIP:2049
tcp        0      0 172.30.0.30:2049        10.197.76.130:899       ESTABLISHED

Note: In case you want to automount the file during startup, add the following line in /etc/fstab

nfsserver1.com:/myexport/dir1 /mnt1/nfsserver1.com/myexport/dir1 nfs rw

After that mount and unmount can be done directly using

mount /mnt1/nfsserver1.com/myexport/dir1
or
umount /mnt1/nfsserver1.com/myexport/dir1
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments