Network File System (NFS) is a protocol in Linux that allows files to be shared between multiple computers over a network. It provides a way for users to access files on remote systems as if they were part of the local file system. This is particularly useful in environments where centralized data storage is required, and it is important in networked environments such as corporate networks, server farms, or development clusters.
How NFS Works
- Server: The system where files are physically stored and shared with other machines.
- Client: The system that accesses the shared files over the network.
- NFS enables a client to mount a remote file system and interact with it as though it were a local file system.
Key Features of NFS
- Transparency: Users can access remote files in the same way they access local files.
- Compatibility: Works across different UNIX-like systems (Linux, macOS, etc.).
- Centralized Storage: Ideal for centralizing storage across multiple systems.
- Performance: Uses RPC (Remote Procedure Call) for efficient communication.
- Security: Can be secured using methods such as Kerberos for authentication and encryption.
NFS Components
- nfs-server: The server program that provides access to file systems over the network.
- nfs-client: The client program that allows a system to mount and access the shared file system.
- /etc/exports: A file on the server that defines which directories can be shared and with which clients.
Steps to Set Up NFS
1. Install NFS Utilities
On the Server:
sudo apt-get install nfs-kernel-server
or
sudo yum install nfs-utils
On the Client:
sudo apt-get install nfs-common
or
sudo yum install nfs-utils
2. Configure the NFS Server
Step 1: Create a directory to share:
sudo mkdir -p /mnt/nfs_share
Step 2: Set permissions for the directory:
sudo chown nobody:nogroup /mnt/nfs_share sudo chmod 777 /mnt/nfs_share
Step 3: Edit the /etc/exports
file to specify which directories to share and with which clients. This file defines the access control for NFS shares. Open the file:
sudo nano /etc/exports
Add the following line to share the /mnt/nfs_share
directory with the client system (identified by IP address):
/mnt/nfs_share 192.168.1.10(rw,sync,no_subtree_check)
Explanation of options:
rw
: Allows the client read and write access.sync
: Writes changes to the disk before responding, ensuring data integrity.no_subtree_check
: Prevents subtree checking, improving performance.
Step 4: Export the file system:
sudo exportfs -a
Step 5: Start and enable the NFS service:
sudo systemctl start nfs-kernel-server sudo systemctl enable nfs-kernel-server
3. Configure the NFS Client
Step 1: Create a directory to mount the shared folder on the client machine:
sudo mkdir -p /mnt/nfs_client_share
Step 2: Mount the shared folder from the NFS server:
sudo mount 192.168.1.20:/mnt/nfs_share /mnt/nfs_client_share
Here, 192.168.1.20
is the IP address of the NFS server, and /mnt/nfs_share
is the directory shared from the server.
Step 3: Verify the mounted file system:
df -h
You should see the shared NFS directory mounted on the client system.
4. Automating Mount with fstab
To automatically mount the NFS share on the client system during boot, add the following line to the /etc/fstab
file:
192.168.1.20:/mnt/nfs_share /mnt/nfs_client_share nfs defaults 0 0
5. Manage NFS Access Permissions
NFS can be fine-tuned by specifying different access controls for different clients in /etc/exports
. For example:
Read-only access:
/mnt/nfs_share 192.168.1.10(ro,sync,no_subtree_check)
Access for multiple clients:
/mnt/nfs_share 192.168.1.10(rw,sync,no_subtree_check) 192.168.1.11(ro,sync,no_subtree_check)
6. Unmounting the NFS Share
To unmount the NFS share on the client system:
sudo umount /mnt/nfs_client_share
Troubleshooting Common NFS Issues
NFS Service Not Running: Ensure that the NFS server service is running with the command:
sudo systemctl status nfs-kernel-server
Firewall Blocking NFS: Ensure that the firewall is configured to allow NFS traffic. On most systems, NFS uses port 2049. You can open this port using the ufw
firewall on Ubuntu:
sudo ufw allow 2049/tcp sudo ufw allow 2049/udp
NFS Stale File Handle: This error can occur if the NFS server restarts or exports are updated while the client is still accessing the old file handle. To resolve this, unmount and remount the NFS share.
Lab Exercise: Working with NFS
Objective
Set up a basic NFS server and mount a shared directory on the client system.
Steps
- On the NFS Server:
- Install NFS utilities.
- Create a directory to share (
/mnt/nfs_share
). - Configure
/etc/exports
and export the share. - Start the NFS server service.
- On the NFS Client:
- Install NFS utilities.
- Create a directory to mount the shared folder (
/mnt/nfs_client_share
). - Mount the NFS share from the server.
- Verify the mounted directory.
- Optional:
- Configure automatic mounting using
fstab
. - Unmount the shared directory when done.
- Configure automatic mounting using