What is NFS in Linux?

Topics Covered

NFS (Network File System) in Linux is a distributed file system protocol that is used for sharing of files and directories across a network. It enables users to access files and directories on remote systems as if they were stored locally on their machines. In this article, we will explore What an is nfs server in linux and what is nfs in linux with working and setup.

How it Works ?

NFS works on the client-server model, where one or more NFS servers host the shared resources, and NFS clients can access and mount these resources to access the data. The working of NFS is through a combination of protocols and services working together.

Protocols

  • The RPC (Remote Procedure Call) protocol is used for communication between programs running on different machines and allows the client to perform operations like reading, writing, and accessing files.
  • The NFS protocol specifies the format of the data being exchanged between the client and the server.

NFS Server: The NFS server is used for hosting the shared files and directories. There are multiple configurations to be made in the server which allows the directories to the used by clients on request from clients.

NFS Client: The NFS client is a machine that accesses the shared resources from the NFS server. When a client needs to access a file or directory on the NFS server, it sends an RPC request to the server, which then processes the request and returns the appropriate data.

The process of sharing files and directories with NFS involves the following steps of exporting and mounting:

  • On the NFS server, the configurations for the directories to be shared along with client-specific permissions are made.
  • On the NFS client, the shared directories are mounted onto local mount points.
  • After mounting, the shared directories become an integral part of the client's file system, and users can access them just like any local file.

Caching To optimize performance, NFS clients employ caching mechanisms. When a file is read from the NFS server, the client caches the data locally. Subsequent read requests for the same file can be satisfied from the cache, reducing the need for network communication.

Required Services:

Let's explore these services that are used by the NFS for sharing and accessing the files in Linux systems:

  • The NFS service is responsible for exporting directories and files to be shared with NFS clients. It manages the access control and authentication mechanisms, allowing clients to access shared resources securely over the network.
  • The nfslock service provides file locking functionality for NFS clients. File locking is crucial to ensure that multiple clients can work with shared files simultaneously without data corruption or conflicts.
  • The RPCbind service translates RPC program numbers into universal addresses, for communication between NFS clients and servers. It resolving the port number for specific RPC services for proper communication between clients and servers.
  • The rpc.nfsd process is a kernel module that handles the actual NFS requests on the server side. It listens for incoming NFS requests from clients, processes them, and manages the file operations on shared directories.

Several RPC processes are used by NFS to have smooth communication between NFS clients and servers. These processes are:

  • The rpc.mountd process is responsible for handling mount requests from NFS clients. When an NFS client requests to mount a shared directory from the NFS server, the rpc. mountd process is responsible for processing this request.
  • The lockd process manages file locking for NFS clients to maintain data integrity in a multi-client NFS environment.
  • The rpc. statd process, also known as the NFS status monitor, is responsible for monitoring the status of NFS clients.
  • The rpc.rquotad process manages disk quotas for NFS clients. Quotas limit the amount of disk space a user or group can consume on the NFS server.
  • The rpc.idmapd process is responsible for mapping user and group IDs between the NFS client and server.

RPC program numbers are unique identifiers assigned to remote services and are used for communication between different processes. Universal addresses are used by RPC clients to identify the network location of a specific RPC service or program.

Setting Up NFS Server with NFS Share

The steps for setting up a NFS server with NFS share in a Ubuntu system are:

  1. Ensure that the NFS server package(nfs-kernel-server) is installed on your Linux system. If it is not installed, use the package manager to install the package.

showing the output

  1. Select or create the directory you want to share with NFS clients. Example: Let us create a directory called shared_directory in the root (/) path:
  1. Set the appropriate permissions to control access. In this example, we grant full read, write, and execute permissions to everyone.

appropriate permissions

The numerical value 777 represents permission such as read, write, and execute for all users in Linux. The -R is used for recursive or to apply permission to all its subdirectories and files within.

/etc/exports File:

The /etc/exports file is a configuration file used by the NFS server to specify which directories should be made available for NFS clients to access and also the access permissions for those clients. Each line in the file defines an exported directory and the corresponding access options.

Syntax

  • <directory>: The absolute path of the directory to be exported.
  • <client>: Specifies the clients or networks allowed to access the exported directory. It can be an individual client IP address, a subnet in CIDR notation, or an asterisk (*) to allow all clients.
  • options: A comma-separated list of access options for the exported directory.

Here is a tabulation of different options that can be used in the /etc/exports file:

OptionsDescription
rwGrants read and write access to the shared directory. Clients can both read and modify files in the exported directory.
roGrants read-only access to the shared directory. Clients can read files but cannot modify or write to the directory.
syncRequires changes to be written to the server's disk before confirming the completion of an NFS operation. This ensures that data is committed immediately to disk, but may impact performance.
asyncDelays writing changes to disk, allowing faster NFS operations but increasing the risk of data loss in case of a server crash.
no_root_squashBy default, the NFS server will map the root user to an anonymous user on the server, preventing root privileges on the client. Using no_root_squash allows the root user on the client to have root privileges on the server.
root_squashPrevent root access to the shared directory.
all_squashMaps all remote users, including root, to an anonymous user on the server, preventing any user from having special privileges.
no_subtree_checkDisables subtree checking for improving performance. When exporting a subdirectory of a filesystem, NFS performs subtree checking to ensure that the subdirectory is indeed a part of the exported filesystem.
  1. Edit the NFS exports file (/etc/exports) to specify the directory to be shared and define client access permissions. Use a text editor like nano to open the file:

define client access

  1. Add the following line to the exports file to allow all clients to access the shared directory:

Save and exit the file (press Ctrl+X, then Y, then Enter).

  1. Apply the changes using the exportfs command:

The -r option is used to re-read the /etc/exports file and update the export table accordingly. The -a option specifies that all directories listed in the /etc/exports file should be re-exported.

  1. Start the NFS server service and enable it to launch on system boot:
  1. To verify that the NFS share is correctly set up and available for clients, use the following command:

Ouptut:

Setting Up NFS on Client Machine and NFS Share Mounting:

The steps for setting up a NFS on a client machine and mounting the NFS share are:

  1. Install the NFS client package on your Linux system by using the apt package manager. The package manager may differ based on the Linux distro you are using.
  1. Select a local directory on the client machine where you want to mount the shared resources. Example: Let us create a directory called mount_point in the user's home directory:
  1. Use the mount command to mount the NFS share from the server onto the client's designated mount point. Syntax:

Replace <server-ip> with the IP address of the NFS server, and <shared-directory> with the path of the shared directory on the server. Example:

The 192.168.1.100 is the IP address of the NFS server hosting the shared directory. The path of the shared directory on the NFS server and local mount point are also specified. If the command is successful, there will no no output.

  1. To ensure that the NFS share is successfully mounted, we can check the mount point on the client machine using the dh command.

The dh command provides a summary of disk usage on the system. It displays information about the filesystems mounted on the system, including their total size, used space, available space, and usage percentage. The -h option makes the output more human-readable.

Automount the NFS Share To ensure the NFS share is automatically mounted each time the client system starts, we need to an entry to the /etc/fstab file.

The /etc/fstab file, also known as the File Systems Table, is a configuration file that has lists of file systems and devices that are automatically mounted during the system boot process.

Syntax:

  • <file_system>: Remote location of the file system to be mounted.
  • <mount_point>: Directory in the file system where the contents of the device or remote location will be mounted.
  • <type>: Type of the file system to be mounted. Common types include ext4, nfs, vfat, and more. You can use auto if you want the system to determine the file system type automatically.
  • <options>: Comma-separated list of mount options.
  • <dump>: Indicates whether the file system should be included in backups.
  • <pass>: Order in which the file system checker should check the file system during the system boot process.

Steps:

  1. Open the /etc/fstab file in a text editor
  1. Add the following line at the end of the file, replacing <server-ip> and <shared-directory> with the appropriate values: Syntax:

Example:

The above example specifies, that NFS share should not be included in backups and file system should not check the file system during the boot process. The defaults indicates the default mount options for the nfs file share.

  1. Save and exit the file by pressing Ctrl+X, then Y and followed by Enter.

Conclusion

  • NFS in Linux is a distributed file system protocol for sharing of files and directories across a network.
  • NFS operates on the client-server model, where the NFS server hosts shared resources, and NFS clients can access them remotely.
  • To set up an NFS server with NFS share, install the NFS server package, create and export the shared directory, start the NFS server, and verify the NFS share status.
  • The /etc/exports file is a configuration file used by the NFS server for sharing directories.
  • Setting up NFS on the client machine involves installing the NFS client package, creating a mount point, and mounting the NFS share using the 'mount' command.
  • The /etc/fstab file is used to automatically mount file systems during system boot.