ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


FreeBSD Basics

Configuring and Using NFS

07/26/2000

In order to manipulate data using Unix, a file system that contains the data must first be mounted. If the data is located on a hard drive, floppy, or CD-ROM drive physically cabled to that computer, you can mount that device directly using the mount command to specify the device, the filesystem, and a local mountpoint. To automate this process, you can add the correct entry to your /etc/fstab file.

If you wish to access data that is physically located on another Unix computer, you will need to use NFS, the Network File System. The computer where the data is physically located must be running nfsd, the NFS daemon. It must also have a properly configured configuration file which is read by nfsd; this file is called /etc/exports.

The computer that wishes to access the remote data must be running the NFS client and must be mentioned in the /etc/exports of the computer running nfsd.

The easiest way to install nfsd and the nfs client is through /stand/sysinstall. Let's pretend I want a computer named alpha that has an IP address of 10.0.0.1 to access the /usr directory on a computer named gamma that has an IP address of 10.0.0.3. Gamma needs the nfsd, and alpha needs the nfs client. Let's start by logging on as root at gamma. Then type:

/stand/sysinstall

and select "Configure" and then "Networking." Press the space bar on the option "NFS Server" to select it. You will receive a message that will invoke the vi editor to edit /etc/exports.

The examples given in the default /etc/exports are straightforward enough to get NFS up and running. If you want to fine-tune NFS and gain a better understanding of it, open up another virtual terminal and type:

man exports

For now, let's just see if we can get NFS to work. Arrow down to the # in the line:

#/usr		huey louie dewie

press Esc-x to remove the #, arrow over to the "h" in huey and continue to press "x" to remove huey louie dewie. Press Esc-i and type alpha -- the line should now look like this:

/usr		alpha

Then press Esc and :wq to save your changes and quit the editor. Exit out of /stand/sysinstall.

We now need to start the nfsd. Press Alt-F1 to return to the main terminal, then type:

shutdown now
Press Enter when you get this message:
Enter full pathname of shell or RETURN for /bin/sh:

Then type:

exit

This causes FreeBSD to restart all of your daemons and reread their configuration files. Your startup messages should include the following:

Mounting NFS file systems.
Doing additional network setup: portmap
Starting final network daemons: mountd nfsd rpc.statd.

If you receive an error message instead, you may have disabled the portmap daemon in rc.conf.

If so, login as root and edit the rc.conf file:
pico /etc/defaults/rc.conf

Press Ctrl-W and type portmap to search for the portmap_enable line, which should read:

portmap_enable="YES"  #Run the portmapper service (or NO).

If it is set to NO instead, change the NO to YES, and check your change very carefully before saving it.

Issue another shutdown now, as above, and watch your boot messages. To ensure that nfsd did start, you can enter this command:

ps -aux

and look for the lines:

USER   PID %CPU %MEM   VSZ RSS TT STAT STARTED TIME    COMMAND
daemon 117 0.0  0.5    884 132 ?? Is   8:34AM  0:00.01 /usr/sbin/portma
root   122 0.0  0.7    488 184 ?? Is   8:34AM  0:00.02 mountd -r
root   124 0.0  0.0    328   0 ?? Is   8:34AM  0:00.01 nfsd: master (nf
root   126 0.0  0.0    320   0 ?? I    8:34AM  0:00.00 nfsd: server (nf
root   127 0.0  0.0    320   0 ?? I    8:34AM  0:00.00 nfsd: server (nf
root   128 0.0  0.0    320   0 ?? I    8:34AM  0:00.00 nfsd: server (nf
root   129 0.0  0.0    320   0 ?? I    8:34AM  0:00.00 nfsd: server (nf
root   130 0.0  0.5 263024 140 ?? Is   8:34AM  0:00.00 rpc.statd

The last thing we need to do on this computer is ensure there is an entry for alpha in /etc/hosts.

pico /etc/hosts
10.0.0.1	alpha

Let's also test connectivity:

ping 10.0.0.1

and

ping alpha

should both be successful.

Now, login as root at alpha.

/stand/sysinstall

And select "Configure," then "Networking"; use your spacebar to select the NFS client and press OK, then exit out of /stand/sysinstall.

Before we try mounting gamma's /usr directory at alpha, let's create a mountpoint on alpha:

mkdir /share

Now issue the mount command:

mount gamma:/usr /share

If you issued this as root, you should receive your command prompt back without any error messages. To see if it mounted:

df

which should return a line that reads something like this:

gamma:/usr	5996471	  1069259   4447495	  19%	   /share

and if you issue these commands:

cd /share
ls

you should be able to see the contents of gamma's /usr directory.

When you are finished, you can unmount /share like any other mounted file system:

umount /share

Congratulations, you've just created your first NFS server; now let's fine-tune it a bit.

Normally, you won't want to export your entire /usr directory structure. Even if permissions are set correctly, you don't want remote users poking about your home and bin directories. Instead, create a subdirectory of /usr to contain the information you want remote users to access. As a practical example, I copy the packages collection into a directory I've created called /usr/packages. I then export /usr/packages and /usr/ports, so my /etc/exports looks like this:

/usr/ports	10.0.0.1
/usr/packages   10.0.0.1

The new mount command at alpha is:

mount gamma:/usr/packages /share
cd /share
ls

The above command will only show the packages collection stored on gamma. An added side benefit to the client is that they don't have to copy a package onto their hard drive in order to install it. The pkg_add command works quite nicely from the mountpoint.

NFS's behaviour is a little weird if you want to share your CD-ROM over the network. If I edit /etc/exports on gamma to add the following line:

/cdrom	-alldirs	10.0.0.1

I would expect alpha to be able to access the contents of a data CD-ROM located on gamma. If I put a data CD-ROM in gamma's CD-ROM drive, and at alpha type:

mount gamma:/cdrom /share

I get:

nfs: can't access /cdrom: Permission denied

Hmmmm... Let's try a shutdown now at gamma; perhaps nfsd needs to re-read /etc/exports. No, that made things worse, as the boot messages now include:

Starting final network daemons: mountdJun 3 15:25:48 gamma 
mountd[1537]: could not remount /cdrom: Invalid argument
Jun 3 15:25:48 gamma mountd[1537]: bad exports list line /cdrom -alldirs
10.0.0.1

If I wait long enough, I'll also start to get periodic error messages at Alt-F1. So what happened? Let's take a look at /etc/fstab:

pico /etc/fstab
# Device		Mountpoint	FStype	Options		Dump	Pass#
/dev/acd0c		/cdrom		cd9660	ro,noauto	0	0

And there's one problem: I tried to export a filesystem that is not set to mount automatically when I reboot. I can't export a filesystem that isn't mounted locally. To complicate matters further, I can't export a directory I've created, unless I create it as a subdirectory of /usr. This is where NFS gets a bit weird. Try this at gamma:

mkdir /usr/cdrom
pico /etc/exports

and modify the line to read:

/usr/cdrom	-alldirs	10.0.0.1

Save the change and issue a shutdown now. This time there shouldn't be any error messages. Now try

df

and you'll see something like this:

Filesystem  1K-blocks     Used    Avail Capacity  Mounted on
/dev/ad0s1a     49583    33637    11980    74%    /
/dev/ad0s1f   5996471  1069263  4447491    19%    /usr
/dev/acd0c     470754   470754        0   100%    /usr/cdrom
/dev/ad0s1e     19815     5671    12559    31%    /var
procfs              4        4        0   100%    /proc
mfs:1919        68647        1    63155     0%    /tmp

Note that /usr/cdrom was mounted for you; if you try

mount /cdrom
you'll see something like
cd9660: Device busy 

However,

cd /usr/cdrom
ls

should show the contents of the CD-ROM. Now, at alpha,

mount gamma:/usr/cdrom /share
cd /share
ls

should also show the contents of gamma's CD-ROM.

cd /
umount /share

You will have to be extra careful if you decide to export CD-ROMs; you don't want to inadvertently eject a mounted CD-ROM or reboot without a CD-ROM in your CD-ROM drive. I usually leave the cdrom reference commented out in /etc/exports like so:

#/usr/cdrom	-alldirs	10.0.0.1

and uncomment it out when I actually want to share my CD-ROM. I then need to issue a shutdown now so the nfsd can mount the CD-ROM. If I try to mount it myself:

mount /usr/cdrom

I get:

mount: /usr/cdrom: unknown special file or file system

However, once nfsd has mounted it, I can unmount it normally:

umount /usr/cdrom

This should get you started with NFS; read the man page for nfsd to learn how to give read-only access, group access, and user access.

Whenever you edit /etc/exports, always test your changes at the client. Mount the newly exported data to ensure that you haven't given the client access to more data then you intended.

One final note on NFS: portmap can be a security risk on computers attached to insecure networks such as the Internet. Usually, in a networked environment, there is only one computer with the Internet connection as it controls Internet access for the rest of the network. Don't run the nfsd on that computer.

Next week, we'll start familiarizing ourselves with the shells that come with your FreeBSD system.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.


Read more FreeBSD Basics columns.

Discuss this article in the Operating Systems Forum.

Return to the BSD DevCenter.

 

Copyright © 2009 O'Reilly Media, Inc.