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
/exportsis created to serve as a parent directory for all NFS shares.
- Subdirectories such as
backupanddocumentsare 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/exportsfile 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) orro(read-only), along withno_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-serverand 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.txtin the backup directory containing "hello world" and another namedtest2.txtin 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) viasudo 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 -ecommand 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/NFSto 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 -hshows 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.masterfile 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:
--ghostcreates placeholder directories in the parent directory, ensuring they exist when accessed.
- The
--timeoutoption 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.masterandauto.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.nfsfile specifies shares like "backup" and "documents," which will be mounted under the base directory as/mnt/nfs/backupand/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/nfstriggers Auto FS to mount both "backup" and "documents" directories automatically.
- The command
df -hreveals 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 -hresets this timer each time it's executed.
- This behavior occurs because
df -hretrieves 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 -hconfirms 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.