Unix - Network File System (NFS) in UNIX

Introduction

Network File System (NFS) is a distributed file system protocol that allows a UNIX or Linux computer to access files and directories located on another computer over a network. Instead of storing every file locally, users can access a remote directory almost as if it were part of their own computer's file system. NFS is widely used in environments where multiple computers need to share common files, applications, configuration data, or storage resources.

For example, suppose a company has a central UNIX server containing a directory called /shared. Instead of copying the contents of this directory to every workstation, the administrator can configure the server to export /shared through NFS. Authorized client computers can then mount this directory and access its contents through a local path such as /mnt/shared.

NFS Architecture

NFS follows a client-server architecture. The computer that provides files and directories is called the NFS server, while computers that access those resources are called NFS clients.

The NFS server makes selected directories available for network access. These directories are known as exports. The client connects to the server and mounts an exported directory at a location within its own file-system hierarchy.

For example:

NFS Server
   |
   |  /data
   |
 Network
   |
   +------------------+
   |                  |
Client 1           Client 2
/mnt/data          /shared/data

Both clients can access the server's /data directory through their respective mount points, provided they have the required permissions.

How NFS Works

When a user accesses a file stored on an NFS-mounted directory, the client operating system communicates with the NFS server across the network.

Consider the following command:

ls /mnt/shared

If /mnt/shared is an NFS-mounted directory, the client sends the necessary request to the NFS server. The server accesses the actual directory and returns the requested information to the client.

Similarly, when a user executes:

cat /mnt/shared/report.txt

the NFS client requests the contents of report.txt from the remote server. The server processes the request and sends the file data back over the network.

This process is generally transparent to applications. Programs can often work with NFS-mounted files using ordinary file operations without knowing that the files are physically stored on another computer.

NFS Server and Client

An NFS server is responsible for providing shared directories. The administrator decides which directories can be exported and which clients are allowed to access them.

A client needs an NFS-capable operating system and appropriate network connectivity to access the exported directory.

For example, a server might export:

/data/projects

to a particular client or network.

The client can then mount it using a command similar to:

mount server.example.com:/data/projects /mnt/projects

After successful mounting, users can access the remote files through:

cd /mnt/projects

The actual syntax and available options can vary between UNIX implementations and NFS versions.

Exporting Directories

The NFS server uses an export configuration to specify which directories can be shared and what access rules apply to them.

On many UNIX-like systems, the configuration is maintained in:

/etc/exports

A simplified example might look like:

/data/projects 192.168.1.0/24(rw,sync)

This indicates that the /data/projects directory is being exported to clients within the specified network, with particular access options.

After modifying the export configuration, the administrator generally needs to reload or re-export the configuration using the appropriate system-specific administration command.

Mounting an NFS Directory

A client mounts an exported directory into its local file-system hierarchy.

For example:

mkdir /mnt/projects

The administrator can then mount the remote directory:

mount server.example.com:/data/projects /mnt/projects

Once mounted, the directory can be accessed like a local directory:

cd /mnt/projects
ls

Files can then be opened, created, modified, or deleted according to the permissions and NFS export rules.

Automatic NFS Mounting

If an NFS directory must be available whenever the system starts, it can be configured for automatic mounting.

On many UNIX and Linux systems, the /etc/fstab file can contain an entry such as:

server.example.com:/data/projects /mnt/projects nfs defaults 0 0

This allows the system to mount the remote directory according to the specified configuration during startup.

Administrators should carefully configure automatic network mounts because the client may start before the network or NFS server becomes available.

NFS Versions

NFS has evolved through several versions.

NFSv2 was an early version with limitations such as smaller file-size support and primarily UDP-based operation.

NFSv3 introduced improvements such as support for larger files, better performance, and enhanced error reporting.

NFSv4 introduced significant architectural improvements. It provides stronger state management, improved security integration, and uses a more unified protocol design. NFSv4 also supports features such as stronger authentication mechanisms and better handling of namespaces.

Modern UNIX and Linux systems commonly use NFSv4 when available, although support for older versions may exist for compatibility.

NFS and File Permissions

NFS does not replace the normal UNIX permission system. File ownership and permissions continue to play an important role.

For example:

-rw-r--r--  user1  developers  report.txt

The server's file permissions determine what users can normally do with the file.

NFS access control and UNIX permissions therefore work together. A client may be permitted to mount a directory but still be unable to modify a particular file because of its UNIX ownership or permission settings.

Root Squashing

One important NFS security feature is root squashing.

Normally, the root user has extensive privileges on a UNIX system. If unrestricted root access were transferred from an NFS client to the server, a root user on the client could potentially manipulate files on the server with excessive privileges.

With root squashing enabled, requests from the client-side root user are mapped to an unprivileged identity on the server. This reduces the possibility of unauthorized modification of important files.

A commonly used export option is:

root_squash

Administrators should understand this option before configuring NFS exports, particularly in multi-user environments.

Read-Only and Read-Write Access

NFS exports can be configured as read-only or read-write.

A read-only export can be represented using:

ro

A read-write export can use:

rw

Read-only access is useful when clients only need to retrieve information and should not be allowed to modify the shared data.

For example, a software repository or reference-data directory might be shared as read-only.

Read-write access is appropriate when authorized users need to create or modify files on the shared storage.

Advantages of NFS

NFS provides several important advantages.

First, it allows centralized storage. Important files can be stored on a powerful server rather than duplicated across many computers.

Second, NFS simplifies file sharing. Multiple UNIX systems can access the same directory without manually copying files between machines.

Third, centralized administration becomes easier because administrators can manage shared data from a single server.

Fourth, storage resources can be used more efficiently because client systems do not necessarily need large amounts of local storage.

Finally, NFS integrates naturally with the UNIX file-system structure, making remote resources accessible through ordinary directories.

Limitations of NFS

NFS also has limitations.

Because files are accessed over a network, performance depends on network speed, latency, server capacity, and network reliability.

If the NFS server becomes unavailable, clients may have difficulty accessing files stored on that server.

Poorly configured NFS exports can also create security risks. Administrators must carefully control which clients can access shared directories and what operations they are allowed to perform.

Another consideration is that applications performing many small file operations can experience noticeable performance differences when working over NFS compared with a local filesystem.

Common Uses of NFS

NFS is commonly used for centralized home directories in UNIX environments. A user's home directory can be stored on a server and mounted automatically when the user logs into different workstations.

It can also be used for sharing project files among development teams, distributing software or application data, providing centralized storage for servers, and supporting environments where multiple UNIX systems need access to the same datasets.

For example, an organization might maintain:

/server/data
/server/projects
/server/backups

and selectively export these directories to authorized systems.

NFS vs Local File System

A local file system stores data directly on storage devices attached to the computer, such as an SSD or hard drive.

NFS stores the data on a remote server while making it accessible through a directory on the client.

For example:

Local:
 /home/user/report.txt

The file is stored on the local computer.

With NFS:

/mnt/shared/report.txt

the path appears local to the application, but the actual file may be stored on an NFS server elsewhere on the network.

This distinction is important because network availability and server performance can affect access to NFS files.

Basic NFS Workflow

A typical NFS implementation follows these steps:

  1. The administrator creates a directory on the NFS server.

  2. The directory is configured as an NFS export.

  3. Access permissions and security options are defined.

  4. The NFS server makes the export available.

  5. The client identifies the NFS server and exported directory.

  6. The client creates a local mount point.

  7. The remote directory is mounted on the client.

  8. Applications access the files through the mounted directory.

  9. When the directory is no longer required, it can be unmounted.

For example:

mkdir /mnt/shared
mount server.example.com:/data /mnt/shared

The user can then work with:

/mnt/shared

as the access point to the remote data.

Conclusion

Network File System is an important technology in UNIX environments because it allows computers to share files and directories over a network while presenting those resources through the familiar UNIX file-system structure. Its client-server architecture, centralized storage capabilities, permission integration, and support for controlled remote access make it useful in multi-user and enterprise environments. Understanding NFS requires knowledge of exports, mounts, permissions, NFS versions, access controls, and security features such as root squashing. Properly configured NFS can provide convenient and centralized file access, while careful security and performance management are essential for reliable operation.