SoFunction
Updated on 2025-05-04

Summary of the problem of installing NFS in ubuntu 22.04

1. Overview

1. Definition

NFS (Network File System) is a distributed file system protocol that was originally developed by Sun Microsystems and was released in 1984. It allows different hosts to share files and directories over the network, just as these files and directories are part of local storage.

2. Working principle

  • Client-server architecture: The core working principle of NFS is its client-server architecture. The NFS server provides file system resources, and clients request to access these resources over the network.

  • Remote Procedure Call (RPC): NFS relies on the RPC mechanism to enable communication between clients and servers. RPC allows clients to directly call services or functions on the server to define standard interfaces for file operations.

  • Port Management: NFS uses multiple ports during operation, with port 111 as the RPC Portmapper service port and port 2049 as the standard NFS service port for the file system.

3. Architecture

  • NFS Server: Responsible for managing and sharing remote file systems with clients on the network.

  • Client: By mounting the remote file system, users can access remote files like local files.

  • Transmission Protocol: NFS uses the TCP/IP protocol to communicate and transmits data and control information based on the RPC protocol.

4. Version evolution

  • NFSv4.2: The most widely used version currently, combining the advantages of modern network storage technology, is suitable for a variety of application scenarios, including virtualization and cloud storage.

5. Application scenarios

  • Enterprise file sharing: In an enterprise environment, NFS is used to share files and directories between different workstations and servers, making it easier for teamwork to collaborate.

  • Virtualization Environment: In the virtualization platform, NFS can serve as a storage backend to provide storage and sharing of virtual machine disk images.

  • High Performance Computing (HPC): In a high performance computing cluster, NFS is used as a shared file system between nodes, allowing compute nodes to access shared datasets and intermediate results.

  • Data Backup and Recovery: NFS can be used to centrally store backup data, simplifying the data backup and recovery process.

  • Multi-platform data access: NFS supports a variety of operating systems, including Unix, Linux, and Windows, making file sharing between different platforms easy.

  • Media and content production: In the field of video editing and media production, NFS can be used to share large-capacity media files, and multiple editors can access and edit the same material at the same time.

  • Database file storage: Some database systems support storing database files in NFS shares, making it easier to share data across multiple database instances and applications.

6. Advantages

  • Simple and easy to use: NFS provides transparent file access methods, and users can access remote files like local files.

  • Cross-platform support: Supports a variety of operating systems, including Unix, Linux, and Windows.

  • Efficient data sharing: You can efficiently share files between multiple clients, allowing a large number of users to access the same data at the same time.

  • Centralized management: NFS allows files to be stored centrally on the server, simplifying backup and management.

  • Flexible permission management: supports UNIX file permissions model, allowing fine-grained access control.

  • Good scalability: It can be easily scaled to suit more users and larger data sets.

7. Disadvantages

  • Security issues: Early versions of NFS had some security issues, but subsequent versions improved by introducing authentication and encryption mechanisms.

  • Performance depends on network: NFS performance depends to a large extent on the bandwidth and latency of the network.

  • Configuration Complexity: In some cases, configuring NFS can be complicated, especially when multiple clients and complex permission management are involved.

  • File locking problem: When multiple clients access the same file at the same time, file locking problems may occur, and they need to be solved through lock management services.

NFS is a powerful distributed file system protocol that is widely used in a variety of scenarios, from enterprise-level file sharing to high-performance computing and virtualized environments. Its flexibility and efficiency make it an important tool in modern computing environments.

2. Installation

1. Install the NFS server
Install the NFS server on the server that needs to share files:

Update list

sudo apt update

Install nfs

sudo apt install -y nfs-kernel-server

2. Create a shared directory
Create a directory as an NFS shared directory (for example /data /nfs_share):

sudo mkdir -p /data/nfs_share

Set permissions for shared directories (make sure other users can access):

sudo chmod 777 /data/nfs_share

3. Configure NFS Sharing
Edit the NFS configuration file /etc/exports and add the configuration of the shared directory:

sudo vi /etc/exports

Add the following content to the file (modify as needed):

/data/nfs_share *(rw,sync,no_all_squash,no_root_squash,no_subtree_check)

Parameter explanation:

/data/nfs_share: shared directory path.
*: The IP range of the client allowed to access, * represents all IPs (can be modified according to actual conditions, for example: 192.168.1.0/24).
rw: Allow read and write permissions.
sync: Synchronous writes to disk.

no_all_squash, does not map all users to anonymous users
no_root_squash, allowing the client's root user to access as root

no_subtree_check: Reduce subtree checking and improve performance.

Note: K8S mounts NFS, please make sure that no_all_squash and no_root_squash must exist! ! !

Otherwise, the file cannot be written

4. Export the shared directory
Run the following command to make the configuration take effect:

sudo exportfs -a

5. Start NFS Service
Start the NFS service and set up power-on:

sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server

6. Install the NFS client
Install the NFS client on the client that needs to mount the shared directory:

sudo apt update
sudo apt install -y nfs-common

7. Mount the shared directory (client)
Mount the shared directory of the NFS server on the client:

sudo mkdir -p /mnt/nfs_share
sudo mount -t nfs 10.0.2.15:/data/nfs_share /mnt/nfs_share

Parameter explanation:
10.0.2.15: The IP address of the NFS server.
/data/nfs_share: The shared directory of the server.
/mnt/nfs_share: The mount point of the client.

Verify that the mount is successful:

# df -hT|grep nfs
10.0.2.15:/data/nfs_share nfs4 49G 14G 34G 29% /mnt/nfs_share

There is a mount message, which means the mount has been successful

View files

ls /mnt/nfs_share

8. Configure automatic mount (optional)
If you want to automatically mount the NFS shared directory when the client is powered on, you can edit the /etc/fstab file:

sudo vi /etc/fstab

Add the following:

10.0.2.15:/data/nfs_share /mnt/nfs_share nfs defaults 0 0

Save and exit the editor.

9. Firewall configuration (optional)
If the server or client has firewall enabled, you need to allow NFS services to pass:

sudo ufw allow nfs

Or manually allow ports used by NFS (default is 2049):

sudo ufw allow 2049

10. Testing and Verification
Create a test file on the server:

echo "Hello from NFS server" | sudo tee /data/nfs_share/

Check on the client to see if the file is accessible:

cat /mnt/nfs_share/

If Hello from NFS server is displayed, the NFS configuration is successful.

Frequently Asked Questions

1. Permissions issue: Make sure that the permissions of the shared directory are set correctly (such as 777).
2. Firewall problem: Check whether the firewall blocks the NFS port (2049).

3. Mounting failed:

Check whether the /etc/exports configuration on the server is correct and run sudo exportfs -ra to reexport.

4. Network problems:

Ensure that the network connection between the client and the server is normal.

Through the above steps, you can successfully install and configure NFS on Ubuntu!

This is the end of this article about installing NFS on ubuntu 22.04. For more related content on installing NFS, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!