How to Install and Configure an NFS Linux Server and Client

How to Install and Configure an NFS Linux Server and Client

Introduction to NFS and AutoFS

Overview of the Video

  • The video introduces the concept of NFS (Network File System) and outlines the goal of setting up both an NFS server and client.
  • The presenter mentions that they will also cover AutoFS, a utility for on-demand mounting of NFS shares, enhancing control over file system management.

Setting Up the Environment

Server Requirements

  • Viewers are advised to have two servers: one designated as the NFS server and another as the NFS client.
  • The presenter suggests using a vanilla Ubuntu Server instance for this setup, noting that other Linux distributions can work but Ubuntu is recommended due to its extensive support.

Creating Directories for Sharing

Directory Structure

  • A base directory named /exports is created to serve as a parent directory for all NFS shares.
  • Subdirectories such as backup and documents are established within /exports, emphasizing that users can create any directories they prefer while maintaining consistency in naming.

Installing NFS Server Package

Installation Process

  • The installation of the NFS server package is initiated with the command sudo apt install nfs-kernel-server.
  • After installation, it’s confirmed that an NFS server service is running, although it has not yet been configured with any shared directories.

Checking Service Status

Verifying Configuration

  • The status of the NFS service is checked using systemctl status nfs-kernel-server, revealing that it is active but exited due to lack of configured shares.
  • It’s explained that without entries in the /etc/exports file specifying shared directories, there’s nothing for the service to manage.

Configuring Exports File

Preparing for Configuration

  • Before making changes, viewers are shown how to view the contents of /etc/exports, which currently contains commented lines indicating no active configurations.
  • The presenter decides to rename this file for backup purposes before proceeding with new configurations.

Setting Up an NFS Server

Creating and Configuring the Exports File

  • The process begins with creating a new exports file using sudo nano /etc/exports, where Nano is suggested for its user-friendliness among Linux text editors.
  • The configuration requires specifying the full path to directories intended for sharing, followed by options that dictate access permissions.
  • A network identifier (e.g., 10.10.10.0/24) must be set, which determines which hosts can connect to the NFS server; it's crucial to adjust this according to your specific network settings.
  • While it’s possible to leave the network identifier blank for open access, it’s advised against due to security risks; instead, modify it based on your local IP subnet.
  • Options like rw (read-write) or ro (read-only), along with no_subtree_check, enhance security by preventing potential exploitation of parent directory information.

Saving Changes and Restarting the NFS Server

  • After editing, save changes in Nano using Ctrl + O and exit with Ctrl + X; these modifications won't take effect until the NFS server is restarted.
  • Restart the NFS server with sudo systemctl restart nfs-kernel-server and check its status to ensure it's running correctly without errors related to the exports file.

Creating Test Files

  • To verify functionality, create test files within shared directories located at /exports; use Nano again as root (sudo nano) since local users lack write permissions initially.
  • Create a test file named test1.txt in the backup directory containing "hello world" and another named test2.txt in documents stating "learn Linux TV".

Connecting an NFS Client

Installing NFS Client Package

  • Transitioning to client setup involves installing only the necessary NFS client package (nfs-common) via sudo apt install nfs-common, avoiding unnecessary server installations.

Testing Connectivity

  • Use the command showmount -e <server_ip> to check shared directories from the client side; knowing your server's IP address is essential for this step.

NFS Client Setup and Auto-Mounting

Understanding NFS Client Connection

  • The speaker confirms the IP address of the server (10.10.10.22) and explains how to find one's own IP address.
  • Running the showmount -e command with the server's IP should list shared directories; if it fails, there may be configuration or firewall issues.
  • Successful connection indicates that the NFS client can access the NFS server without problems.

Creating Directories for Mounting

  • A parent directory is created on the client under /mnt/NFS to organize subdirectories for mounting.
  • Subdirectories are named after shared directories from the server for consistency, starting with "backup" and "documents."
  • These directories are initially empty as no mounts have been established yet.

Mounting NFS Exports

  • The command sudo mount <server_ip>:<share_path> <local_mount_point> is used to mount a backup directory from the server.
  • After mounting, running df -h shows that the backup directory is successfully mounted locally, confirming functionality with a test file present.
  • The documents directory is also mounted using a similar command structure, verifying both exports work correctly.

Unmounting Directories

  • To unmount, use sudo umount <local_mount_point>, which removes both backup and documents directories from being mounted.
  • Confirmation of successful unmounting shows neither directory remains active.

Introduction to Auto FS for Automatic Mounting

  • The speaker introduces Auto FS as a utility that allows on-demand mounting of NFS shares, addressing potential locking issues in NFS.
  • Auto FS mounts quickly enough that applications won't notice any downtime during access attempts.

Setting Up Auto FS

  • Before setting up Auto FS, existing subdirectories are removed since Auto FS will manage their creation automatically.
  • A check ensures no mounts exist before deletion to prevent data loss; this step emphasizes caution when managing file systems.

Auto FS Configuration and Setup

Initial Setup of Auto FS

  • The speaker introduces Auto FS, noting that it is already installed and running but requires configuration to mount directories.
  • The primary configuration file for Auto FS is identified as auto.master, located in /etc/. This file is crucial as it is the first one read during startup.

Editing the Configuration Files

  • The speaker emphasizes adding a line at the end of the auto.master file to configure NFS mounting, specifically pointing to /mnt/nfs.
  • A reference to another configuration file, auto.nfs, is made. This file was created upon installation and will be used for defining specific mounts.

Options for Mounting

  • Additional options are introduced: --ghost creates placeholder directories in the parent directory, ensuring they exist when accessed.
  • The --timeout option is set to 60 seconds, which instructs Auto FS to unmount shares that haven't been accessed within this timeframe, enhancing system efficiency.

Configuring Specific Mount Points

  • The speaker prepares to add specific mount configurations into the next file. Two mounts are identified for tracking by Auto FS.
  • Clarification on naming conventions: "backup" refers not to server directory names but how they will appear on the client side when mounted.

Understanding File System Types and Permissions

  • The filesystem type is specified as NFS4 with read-write permissions (RW). Changes can be restricted at both server and client levels.
  • An IP address of the server hosting shared directories must be included along with the full path where exports are located.

Reviewing Configuration Files

  • To clarify relationships between files, commands are shown to display contents from both auto.master and auto.nfs, reinforcing their interdependence in configuring Auto FS.

Understanding Auto FS and NFS Mounting

Configuration of Auto FS

  • The initial setup involves configuring the base directory for NFS mounts, specifically set to /mnt/nfs, where all subdirectories will be mounted.
  • The auto.nfs file specifies shares like "backup" and "documents," which will be mounted under the base directory as /mnt/nfs/backup and /mnt/nfs/documents.

Restarting Auto FS

  • After configuration, a sanity check shows no NFS mounts are present, confirming that everything has been unmounted as expected.
  • Upon restarting Auto FS, it appears to run without errors; however, no mounts are visible yet because Auto FS only mounts directories upon access.

Accessing Mounted Directories

  • Listing the storage for /mnt/nfs triggers Auto FS to mount both "backup" and "documents" directories automatically.
  • The command df -h reveals that both directories are now mounted due to accessing the parent directory.

Understanding Timeout Behavior

  • A 60-second timeout is in place for these mounts; running df -h resets this timer each time it's executed.
  • This behavior occurs because df -h retrieves information from the server, causing the directories to remain mounted until they are accessed again after 60 seconds.

Monitoring Unmounting Process

  • Using a watch command allows observation of when the mounts disappear after 60 seconds of inactivity.
  • Once the timeout elapses, both directories become unmounted as intended by Auto FS's configuration.

Final Verification of Mount Status

  • Running df -h confirms that no NFS exports are listed since they have been successfully unmounted.
  • Accessing specific subdirectories (like documents or backup individually) can trigger their mounting based on user requests.

Conclusion and Summary of Setup Process

  • By listing storage with options like ls -l, users can ensure all necessary directories are mounted correctly under /mnt/nfs.
  • The video concludes with a successful demonstration of setting up an NFS server on Linode and utilizing Auto FS for automatic mounting.
Video description

In this video, @LearnLinuxTV teaches how to set up your own NFS (Network File System) server and client and also how to configure Auto FS which enables you to mount things like NFS on demand for more control. Chapters: 0:00 Introduction 0:40 Create NFS Shares 3:10 Install NFS Server 4:10 Configure the NFS Server 4:56 Examine the /etc/exports file 6:03 Create a fresh /etc/exports file 8:38 Restart the NFS Server 9:25 Test the NFS Server 10:40 Configure the NFS Client 10:50 Install NFS Client App 11:30 Test Server Connectivity 12:56 Create Parent Directory 14:20 Mounting NFS Exports 16:00 Unmounting NFS Exports 16:30 How to auto-mount NFS Exports 18:50 Configure autofs 24:18 Config file demonstration 25:50 Restart and check our configuration 28:44 Verify mounts are actually unmounted 31:10 Conclusion New to Cloud Computing? Get started here with a $100 credit → https://www.linode.com/lp/youtube-viewers/?ifso=linodetube&utm_source=youtube&utm_medium=TopDocs Read the doc for more information on using an NFS server → https://www.linode.com/docs/guides/using-an-nfs-server-on-ubuntu2004/ Learn more about mounting NFS shares → https://www.linode.com/docs/guides/how-to-mount-nfs-shares-on-debian-9/ Subscribe to get notified of new episodes as they come out → https://www.youtube.com/linode?sub_confirmation=1 #Developers #NFS #Linux Network File System, NFS, Linux; TopDocs @LearnLinuxTV