Edited 1 week ago by ExtremeHow Editorial Team
NetworkingNFSFile SystemsServer SetupClient AccessConfigurationPermissionsStorageCross-PlatformCollaboration
This content is available in 7 different language
Network File System (NFS) is a popular protocol that allows different systems to share files over a network. With NFS, users can access files on remote systems as if they were on their local machine. This can be incredibly useful for collaborative work environments or situations where you need centralized storage.
NFS allows a system to share directories and files with others over a network. Using NFS, users and programs can access files on a remote system almost as if they were on a local disk.
NFS can be highly beneficial for secure file sharing in a Linux environment. It simplifies file access, reduces storage costs, and enables easier collaboration.
Before setting up NFS, make sure all systems are on the same network. You must also have superuser (root) privileges on both the server and client systems.
The first step involves installing NFS on the server. This process will vary slightly depending on your Linux distribution.
# For Debian/Ubuntu
sudo apt update
sudo apt install nfs-kernel-server
# For CentOS/RHEL
sudo yum install nfs-utils
These commands will install the required NFS packages on the server.
Next, you must decide which directory to share on the network. It is common to create a dedicated directory for sharing.
# Create a directory to share
sudo mkdir -p /srv/nfs/shared
Now, set the proper permissions for the directory:
# Change ownership to nobody:nogroup
sudo chown nobody:nogroup /srv/nfs/shared
# Set the permissions
sudo chmod 777 /srv/nfs/shared
After creating and setting permissions for the directory, you must edit the /etc/exports file to define the directory and set access permissions for the clients.
# Edit the exports file
sudo nano /etc/exports
Add the following line to the file to share the directory:
/srv/nfs/shared <client_ip>(rw,sync,no_subtree_check)
Replace <client_ip>
with the IP addresses of the client systems that need access. If multiple clients need access, list them separated by spaces.
After configuring /etc/exports
file, start the NFS service:
# Start and enable the NFS server
sudo systemctl restart nfs-kernel-server
sudo systemctl enable nfs-kernel-server
On the client system, make sure you have installed the required NFS client packages.
# For Debian/Ubuntu
sudo apt install nfs-common
# For CentOS/RHEL
sudo yum install nfs-utils
Now, you can mount the shared directory on the client machine. First, create a directory where you will mount the NFS share:
# Create a mount point for the NFS share
sudo mkdir -p /mnt/nfs/shared
Then, mount the directory with the following command:
# Mount the shared directory
sudo mount <server_ip>:/srv/nfs/shared /mnt/nfs/shared
Replace <server_ip>
with the IP address of your server.
To ensure that the NFS share is mounted at boot time, you can add it to the /etc/fstab file. Open the file and add a line like this:
<server_ip>:/srv/nfs/shared /mnt/nfs/shared nfs defaults 0 0
You can verify that the NFS share is mounted correctly by using the following command:
# Verify the mount
df -h | grep <server_ip>
This command will show the NFS shares listed as being mounted under the /mnt/nfs/shared directory.
Network firewalls can sometimes block NFS traffic. Make sure the firewalls on both the client and server machines allow the NFS ports: TCP and UDP port 111 and TCP and UDP port 2049.
# To open NFS ports on the server
sudo iptables -A INPUT -p tcp -m multiport --dports 111,2049 -j ACCEPT
sudo iptables -A INPUT -p udp -m multiport --dports 111,2049 -j ACCEPT
Make sure that the permissions on the shared directory are configured correctly. Permissions problems can arise due to ownership, group, or too restrictive permissions.
Always check that the NFS server is running correctly using the systemctl status command:
# Check the status of NFS service
sudo systemctl status nfs-kernel-server
It is possible to export directories to multiple clients. You can do this by listing multiple client IPs in your /etc/exports
file:
/srv/nfs/shared client1_ip(rw,sync,no_subtree_check) client2_ip(rw,sync,no_subtree_check)
You can also make some shares read-only for specific clients if you want. You can specify this in the /etc/exports
file:
/srv/nfs/shared client1_ip(rw,sync,no_subtree_check) client2_ip(ro,sync,no_subtree_check)
NFSv4 brings many improvements and may be needed when using more modern distributions or for performance enhancements. NFSv4 configuration is largely the same, but with complex options specifically for performance and security enhancements.
Setting up an NFS server in a Linux environment is an effective way to enable file sharing within a network. By following these steps and considering both your network layout and file sharing requirements, you can create a robust sharing environment. Always remember to keep your exports secure and tailor your configuration to your users' needs for optimal efficiency and performance. Enjoy seamless collaboration with network file sharing via NFS!
If you find anything wrong with the article content, you can