1. Install the NFS server
Install the NFS server on the server that needs to share files:
sudo apt update sudo apt install -y nfs-kernel-server
2. Create a shared directory
Create a directory as an NFS shared directory (for example /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_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_subtree_check: Reduce subtree checking and improve performance.
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!