Fedora 6 and Red Hat Enterprise Linux Bible

Instead of representing storage devices as drive letters (A, B, C, and so on), as they are in Microsoft operating systems, Fedora and Red Hat Enterprise Linux systems connect file systems from multiple hard disks, floppy disks, CD-ROMs, and other local devices invisibly to form a single Linux file system. The Network File System (NFS) facility lets you extend your Linux file system in the same way, to connect file systems on other computers to your local directory structure as well.

Cross-Reference 

See Chapter 10 for a description of how to mount local devices on your Linux file system. The same command ( mount ) is used to mount both local devices and NFS file systems.

Creating an NFS file server is an easy way to share large amounts of data among the users and computers in an organization. An administrator of a Linux system that is configured to share its file systems using NFS has to perform the following tasks to set up NFS:

  1. Set up the network - If a LAN or other network connection is already connecting the computers on which you want to use NFS (using TCP/IP as the network transport), you already have the network you need.

  2. On the server, choose what to share - Decide which file systems on your Linux NFS server to make available to other computers. You can choose any point in the file system and make all files and directories below that point accessible to other computers. However, it is most secure to share a directories from the root of a file system partition.

  3. On the server, set up security - You can use several different security features to suit the level of security with which you are comfortable. Mount-level security lets you restrict the computers that can mount a resource and, for those allowed to mount it, lets you specify whether it can be mounted read/write or read-only. With user -level security, you map users from the client systems to users on the NFS server. In this way, users can rely on standard Linux read/write/execute permissions, file ownership, and group permissions to access and protect files.

  4. On the client, mount the file system - Each client computer that is allowed access to the server's NFS shared file system can mount it anywhere the client chooses. For example, you may mount a file system from a computer called oak on the /media/oak directory in your local file system. After it is mounted, you can view the contents of that directory by typing ls /media/oak . Then you can use the cd command below the /media/oak mount point to see the files and directories it contains.

Figure 18-1 illustrates a Linux file server using NFS to share (export) a file system and a client computer mounting the file system to make it available to its local users.

Figure 18-1: NFS can make selected file systems available to other computers.

In this example, a computer named oak makes its /apps/bin directory available to clients on the network (pine, maple , and spruce) by adding an entry to the /etc/exports file. The client computer (pine) sees that the resource is available, then mounts the resource on its local file system at the mount point /oak/apps . At this point, any files, directories, or subdirectories from /apps/bin on oak are available to users on pine (given proper permissions).

Although it is often used as a file server (or other type of server), Linux is a general-purpose operating system. So, any Fedora or Red Hat Enterprise Linux system can share file systems (export) as a server or use another computer's file systems (mount) as a client. Contrast this with dedicated file servers, such as NetWare, which was created to share files with client computers (such as Windows workstations) and was not intended to act as a client.

Many people use the term file system rather loosely. A file system is usually a structure of files and directories that exists on a single device (such as a hard disk partition or CD-ROM). When I talk about the Linux file system, however, I am referring to the entire directory structure (which may include file systems from several disks or NFS resources), beginning from root (/) on a single computer. A shared directory in NFS may represent all or part of a computer's file system, which can be attached (from the shared directory down the directory tree) to another computer's file system.

Sharing NFS File Systems

To share an NFS file system from your Linux system, you need to export it from the server system. Exporting is done in Fedora and Red Hat Enterprise Linux by adding entries into the /etc/exports file. Each entry identifies the directory in your local file system that you want to share with other computers. The entry identifies the other computers that can share the resource (or opens it to all computers) and includes other options that reflect permissions associated with the directory.

Remember that when you share a directory, you are sharing all files and subdirectories below that directory as well (by default). So, you need to be sure that you want to share everything in that directory structure. There are still ways to restrict access within that directory structure (those methods are described later).

Fedora and RHEL provide a graphical tool for configuring NFS called the NFS Server Configuration window ( system-config-nfs command). The following sections explain how to use the NFS Server Configuration window to share directories with other computers, and then describe the underlying configuration files that are changed to make that happen.

Using the NFS Server Configuration Window

The NFS Server Configuration window ( system-config-nfs command from the package of the same name ) allows you to share your NFS directories using a graphical interface. Start this window from the Desktop menu by clicking System Administration Server Settings NFS. (If it's not there, type yum install system-config-nfs to install it.)

To share a directory with the NFS Server Configuration window, do the following:

  1. From the NFS Server Configuration window, click File Add Share. The Add NFS Share window appears, as shown in Figure 18-2.

  2. In the Add NFS Share window Basic tab, enter the following information:

    • Directory - Type the name of the directory you want to share. (The directory must exist before you can add it.)

    • Host(s) - Enter one or more hostnames to indicate which hosts can access the shared directory. Hostnames, domain names , and IP addresses are allowed here. Separate each name with a space. (See the "Hostnames in /etc/exports" section later in this chapter for valid hostnames.) Add an asterisk (*) to place no restrictions on which hosts can access this directory.

    • Basic permissions - Click Read-only or Read/Write to let remote computers mount the shared directory with read access only or read/write access, respectively.

  3. Click the General Options tab. This tab lets you add options that define how the shared directory behaves when a remote host connects to it (mounts it):

    • Allow connections from ports 1024 and higher - Normally, an NFS client will request the NFS service from a port number under 1024. Select this option if you need to allow a client to connect to you from a higher port number. (This sets the insecure option.) To allow a Mac OS X computer to mount a shared NFS directory, you must have the insecure option set for the shared directory.

    • Allow insecure file locking - If checked, NFS will not authenticate any locking requests from remote users of this shared directory. Older NFS clients may not deliver their credentials when they ask for a file lock. (This sets the insecure_locks option.)

    • Disable subtree checking - By selecting this option, NFS won't verify that the requested file is actually in the shared directory (only that it's in the correct file system). You can disable subtree checking if an entire file system is being shared. (This sets the no_subtree_check option.)

    • Sync write operations on request - This is on by default, which forces a write operation from a remote client to be synced on your local disk when the client requests it. (This sets the sync option.)

    • Force sync of write operations immediately - If keeping the shared data immediately up-to-date is critical, select this option to force the immediate synchronization of writes to your hard disk. (This sets the no_wdelay option.)

  4. Click the User Access tab, then select any of the following options:

    • Treat the remote root user as local root - If this option is on, it enables the remote root user host accessing your shared directory to save and modify files as though he or she were the local root user. Having this on is a security risk, since the remote user can potentially modify critical files. (This sets the no_root_squash option.)

    • Treat all client users as anonymous users - When this option is on, you can indicate that particular user and group IDs be assigned to every user accessing the shared directory from a remote computer. Enter the user ID and group ID you want assigned to all remote users. (This sets the anonuid and anongid options to the numbers you choose.)

  5. Click OK. The new shared directory appears in the NFS Server Configuration window.

Figure 18-2: Identify a directory to share and access permissions with the NFS Server Configuration window.

At this point, the configuration file ( /etc/exports ) should have the shared directory entry created in it. To turn on the NFS service and make the shared directory available, type the following from a Terminal window as root user:

  1. To immediately turn on NFS, type:

    # service nfs start

  2. To permanently turn on the NFS service, type:

    # chkconfig nfs on

  3. If you have a firewall configured, you must ensure that UDP ports 111 and 2049 are accepting requests. (See Chapter 14 for information on configuring your firewall.)

The next few sections describe the /etc/exports file you just created. At this point, a client can only use your shared directory if he mounts it on his local file system. Refer to the "Using NFS file systems" section later in this chapter.

Configuring the /etc/exports File

The shared directory information you entered into the NFS Server Configuration window is added to the /etc/exports file. As root user, you can use any text editor to configure the /etc/exports file to modify shared directory entries or add new ones. Here's an example of an /etc/exports file, including some entries that it could include:

/cal *.linuxtoys.net(rw,sync) # Company events /pub (ro,insecure,all_squash) # Public dir /home maple(rw,squash uids=0-99) spruce(rw,squash uids=0-99)

The following text describes those entries:

Of course, you can share any directories that you choose (these were just examples), including the entire file system ( / ). There are security implications of sharing the whole file system or sensitive parts of it (such as /etc ). Security options that you can add to your /etc/exports file are described throughout the sections that follow.

The format of the /etc/exports file is:

Directory Host(Options) # Comments

Directory is the name of the directory that you want to share. Host indicates the host computer that the sharing of this directory is restricted to. Options can include a variety of options to define the security measures attached to the shared directory for the host. (You can repeat Host/Option pairs.) Comments are any optional comments you want to add (following the # sign).

Hostnames in /etc/exports

You can indicate in the /etc/exports file which host computers can have access to your shared directory. Be sure to have a space between each hostname. Here are ways to identify hosts:

Access Options in /etc/exports

You don't have to just give away your files and directories when you export a directory with NFS. In the options part of each entry in /etc/exports , you can add options that allow or limit access by setting read/write permission. These options, which are passed to NFS, are as follows :

User Mapping Options in /etc/exports

Besides options that define how permissions are handled generally , you can also use options to set the permissions that specific users have to NFS shared file systems.

One method that simplifies this process is to have each user with multiple user accounts have the same user name and UID on each machine. This makes it easier to map users so that they have the same permission on a mounted file system as they do on files stored on their local hard disk. If that method is not convenient , user IDs can be mapped in many other ways. Here are some methods of setting user permissions and the /etc/exports option that you use for each method:

Tip 

The exports man page describes the map_static option, which should let you create a file that contains new ID mappings. These mappings should let you remap client IDs into different IDs on the server.

Exporting the Shared File Systems

After you have added entries to your /etc/exports file, you can actually export the directories listed using the exportfs command. If you reboot your computer or restart the NFS service, the exportfs command is run automatically to export your directories. However, if you want to export them immediately, you can do so by running exportfs from the command line (as root).

Tip 

It's a good idea to run the exportfs command after you change the exports file. If any errors are in the file, exportfs will identify those errors for you. If you are not able to immediately access changed NFS shares from clients, you might need to restart the NFS and portmap services.

Here's an example of the exportfs command:

# /usr/sbin/exportfs -a -v exporting maple:/pub exporting spruce:/pub exporting maple:/home exporting spruce:/home exporting *:/media/win

The -a option indicates that all directories listed in /etc/exports should be exported. The -v option says to print verbose output. In this example, the /pub and /home directories from the local server are immediately available for mounting by those client computers that are named (maple and spruce). The /media/win directory is available to all client computers.

Running the exportfs command makes your exported NFS directories immediately available. Any changes to your /etc/export file will be used again the next time you reboot or otherwise restart the NFS service.

Starting the nfsd Daemons

For security purposes, the NFS service is turned off by default on your Fedora or RHEL system. You can use the chkconfig command to turn on the NFS service so that your files are exported and the nfsd daemons are running when your system boots.

There are two start-up scripts you want to turn on for the NFS service to work properly. The nfs service exports file systems (from /etc/exports ) and starts the nfsd daemon that listens for service requests. The nfslock service starts the lockd daemon, which helps allow file locking to prevent multiple simultaneous use of critical files over the network.

Note 

If you see the message "Cannot register service: RPC: Unable to receive; errno= connection refused ," you probably need to start the portmap service on the client machine.

You can use the chkconfig command to turn on the nfs service by typing the following commands (as root user):

# chkconfig nfs on

The next time you start your computer, the NFS service will start automatically and your exported directories will be available. If you want to start the service immediately, without waiting for a reboot, you can type the following:

# /etc/init.d/nfs start

The NFS service should now be running and ready to share directories with other computers on your network.

Using NFS File Systems

After a server exports a directory over the network using NFS, a client computer connects that directory to its own file system using the mount command. The mount command is the same one used to mount file systems from local hard disks, CDs, and floppies. Only the options to give to mount are slightly different.

Mount can automatically mount NFS directories that are added to the /etc/fstab file, just as it does with local disks. NFS directories can also be added to the /etc/fstab file in such a way that they are not automatically mounted. With a noauto option, an NFS directory listed in /etc/fstab is inactive until the mount command is used, after the system is up and running, to mount the file system.

Manually Mounting an NFS File System

If you know that the directory from a computer on your network has been exported (that is, made available for mounting), you can mount that directory manually using the mount command. This is a good way to make sure that it is available and working before you set it up to mount permanently. Here's an example of mounting the /tmp directory from a computer named maple on your local computer:

# mkdir /media/maple # mount maple:/tmp /media/maple

The first command ( mkdir ) creates the mount point directory ( /media is a common place to put temporarily mounted disks and NFS file systems). The mount command then identifies the remote computer and shared file system separated by a colon ( maple:/tmp ). Then, the local mount point directory follows ( /media/maple ).

Note 

If the mount failed, make sure the NFS service is running on the server and that the server's firewall rules don't deny access to the service. From the server, type ps ax grep nfsd. You should see a list of nfsd server processes. If you don't, try to start your NFS daemons as described in the previous section. To view your firewall rules, type iptables -L (see Chapter 14 for a description of firewalls). By default, the nfsd daemon listens for NFS requests on port number 2049. Your firewall must accept udp requests on ports 2049 (nfs) and 111 (rpc).

To ensure that the mount occurred, type mount . This command lists all mounted disks and NFS file systems. Here is an example of the mount command and its output:

# mount /dev/hda3 on / type ext3 (rw) none on /proc type proc (rw) none on /sys type sysfs (rw) none on /dev/pts type devpts (rw,gid=5,mode=620)usbdevfs on /proc/bus/usb type usbdevfs (rw) /dev/hda1 on /boot type ext3 (rw) none on /dev/shm type tmpfs (rw) maple:/tmp on /media/maple type nfs (rw,addr=10.0.0.11)

The output from the mount command shows your mounted disk partitions, special file systems, and NFS file systems. The first output line shows your hard disk ( /dev/hda3 ), mounted on the root file system ( / ), with read/write permission ( rw ), with a file system type of ext3 (the standard Linux file system type). The small /boot file system is also of type ext3 . The /proc, /sys, /dev/shm, /dev/pts , and usbdevfs mount points represent special file system types. The just-mounted NFS file system is the /tmp directory from maple ( maple:/tmp ). It is mounted on /media/maple and its mount type is nfs . The file system was mounted read/write ( rw ) and the IP address of maple is 10.0.0.11 ( addr=10.0.0.11 ).

What I just showed is a simple case of using mount with NFS. The mount is temporary and is not remounted when you reboot your computer. You can also add options to the mount command line for NFS mounts:

The next section describes how to make the mount more permanent (using the /etc/fstab file) and how to select various options for NFS mounts.

Automatically Mounting an NFS File System

To set up an NFS file system to mount automatically each time you start your Fedora or RHEL system, you need to add an entry for that NFS file system to the /etc/fstab file. The /etc/fstab file contains information about all different kinds of mounted (and available to be mounted) file systems for your Fedora or RHEL system.

The format for adding an NFS file system to your local system is the following:

host:directory mountpoint nfs options 0 0

The first item ( host:directory ) identifies the NFS server computer and shared directory. mountpoint is the local mount point on which the NFS directory is mounted, followed by the file system type ( nfs ). Any options related to the mount appear next in a comma-separated list. (The last two zeros just tell Fedora or RHEL not to dump the contents of the file system and not to run fsck on the file system.)

The following are two examples of NFS entries in /etc/fstab :

maple:/tmp /media/maple nfs rsize=8192,wsize=8192 0 0 oak:/apps /oak/apps nfs noauto,ro 0 0

In the first example, the remote directory /tmp from the computer named maple ( maple:/tmp ) is mounted on the local directory /media/maple (the local directory must already exist). The file system type is nfs , and read ( rsize ) and write ( wsize ) buffer sizes are set at 8192 to speed data transfer associated with this connection. In the second example, the remote directory is /apps on the computer named oak. It is set up as an NFS file system ( nfs ) that can be mounted on the /oak/apps directory locally. This file system is not mounted automatically ( noauto ), however, and can be mounted only as read-only ( ro ) using the mount command after the system is already running.

Tip 

The default is to mount an NFS file system as read/write. However, the default for exporting a file system is read-only. If you are unable to write to an NFS file system, check that it was exported as read/write from the server.

Mounting Noauto File Systems

In your /etc/fstab file are devices for other file systems that are not mounted automatically (probably /dev/ cdrom and /dev/fd0 , for your CD-ROM and floppy disk devices, respectively). A noauto file system can be mounted manually. The advantage is that when you type the mount command, you can type less information and have the rest filled in by the contents of the /etc/fstab file. So, for example, you could type:

# mount /oak/apps

With this command, mount knows to check the /etc/fstab file to get the file system to mount ( oak:/apps ), the file system type ( nfs ), and the options to use with the mount (in this case ro for read-only). Instead of typing the local mount point ( /oak/apps ), you could have typed the remote file system name ( oak:/apps ) instead, and had other information filled in.

Tip 

When naming mount points, including the name of the remote NFS server in that name can help you remember where the files are actually being stored. This may not be possible if you are sharing home directories ( /home ) or mail directories ( /var/spool/mail ).

Using Mount Options

You can add several mount options to the /etc/fstab file (or to a mount command line itself) to impact how the file system is mounted. When you add options to /etc/fstab , they must be separated by commas. The following are some options that are valuable for mounting NFS file systems:

Any of the options that don't require a value can have no prepended to have the opposite effect. For example, nobg indicates that the mount should not be done in the background.

Using autofs to Mount NFS File Systems on Demand

With the autofs facility configured and turned on, you can cause any NFS shared directories to mount on demand. If you know the hostname and directory being shared by another host computer, you can simply change ( cd ) to the autofs mount directory ( /net by default) and have the shared resource automatically mount and be accessible to you.

The following steps explain how to turn on the autofs facility:

  1. As root user from a Terminal window, open the /etc/auto.master file and uncomment the last line, so it appears as follows:

    /net /etc/auto.net

    This causes the /net directory to act as the mount point for the NFS shared directories you want to access on the network.

  2. Start the autofs service by typing the following as root user:

    # service autofs start

  3. Set up the autofs service to restart every time you boot your system:

    # chkconfig autofs on

Believe it or not, that's all you have to do. Provided that you have a network connection to the NFS servers from which you want to share directories, you can try to access a shared NFS directory. For example, if you know that the /usr/local/share directory is being shared from the computer on your network named shuttle, do the following:

  1. Type the following:

    $ cd /net/shuttle

    If the computer named shuttle has any shared directories that are available to you, you will be able to successfully change to that directory.

  2. Type the following:

    $ ls usr

    You should be able to see that the usr directory is part of the path to a shared directory. If there are shared directories from other top-level directories, you should see those as well (such as /var or /tmp ). Of course, seeing any of those directories is dependent on how security is set up on the server.

  3. Next, you could try going straight to the shared directory. For example:

    $ cd /net/shuttle/usr/local/share $ ls info man music television

    At this point, the ls should reveal the contents of the /usr/local/share directory on the computer named shuttle. What you can do with that content depends on how that content was configured for sharing by the server.

Unmounting NFS File Systems

After an NFS file system is mounted, unmounting it is simple. You use the umount command with either the local mount point or the remote file system name. For example, here are two ways you could unmount maple:/tmp from the local directory /media/maple :

# umount maple:/tmp # umount /media/maple

Either form will work. If maple:/tmp is mounted automatically (from a listing in /etc/fstab ), the directory will be remounted the next time you boot Fedora or RHEL. If it was a temporary mount (or listed as noauto in /etc/fstab ), it will not be remounted at boot time.

Tip 

The command is not unmount , it is umount . This is easy to get wrong.

If you get the message device is busy when you try to unmount a file system, it means the unmount fails because the file system is being accessed. Most likely, one of the directories in the NFS file system is the current directory for your shell (or the shell of someone else on your system). The other possibility is that a command is holding a file open in the NFS file system (such as a text editor). Check your Terminal windows and other shells , and cd out of the directory if you are in it, or just close the Terminal windows.

If an NFS file system won't unmount, you can force unmount it ( umount -f /media/maple ) or unmount and clean up later ( umount -l /media/maple ). The -l option is usually the better choice, because a force unmount can disrupt a file modification that is in progress.

Other Cool Things to do with NFS

You can share some directories to make it consistent for a user to work from any of several different Linux computers on your network. Some examples of useful directories to share are:

If you are working exclusively with Fedora, Red Hat Enterprise Linux, and other UNIX systems, NFS is probably your best choice for sharing file systems. If your network consists primarily of Microsoft Windows computers or a combination of systems, you may want to look into using Samba for file sharing.

Категории