Setting up an NFS (Network File System) server allows you to share directories and files across a network. Here’s a guide on how to set up an NFS server and connect to it:
Setting up NFS Server:
- Install NFS Server:
- On the server machine, install the NFS server package. The package name might vary based on your Linux distribution. For example, on Ubuntu:
sudo apt-get install nfs-kernel-server
- On the server machine, install the NFS server package. The package name might vary based on your Linux distribution. For example, on Ubuntu:
- Create a Directory to Share:
- Choose a directory that you want to share and create it:
sudo mkdir /shared_directory
- Choose a directory that you want to share and create it:
- Configure NFS Exports:
- Open the
/etc/exports
file to define the directories to be shared. Add the following line to share the/shared_directory
:/shared_directory *(rw,sync,no_subtree_check)
The options
rw
(read-write),sync
(synchronous), andno_subtree_check
(disable subtree checking) are common settings.
- Open the
- Restart NFS Service:
- Restart the NFS service to apply the changes:
sudo systemctl restart nfs-kernel-server
- Restart the NFS service to apply the changes:
- Allow NFS Through Firewall:
- If you are using a firewall, allow NFS traffic:
sudo ufw allow from <client_IP_address> to any port nfs
Replace
<client_IP_address>
with the actual IP address of the client machine.
- If you are using a firewall, allow NFS traffic:
Connecting to NFS Server:
- Install NFS Client:
- On the client machine, install the NFS client package. The package name may vary based on your Linux distribution. For example, on Ubuntu:
sudo apt-get install nfs-common
- On the client machine, install the NFS client package. The package name may vary based on your Linux distribution. For example, on Ubuntu:
- Create a Mount Point:
- Create a local directory on the client machine where you want to mount the NFS share:
sudo mkdir /mnt/nfs_share
- Create a local directory on the client machine where you want to mount the NFS share:
- Mount NFS Share:
- Mount the NFS share on the client:
sudo mount <server_IP_address>:/shared_directory /mnt/nfs_share
Replace
<server_IP_address>
with the actual IP address of the NFS server.
- Mount the NFS share on the client:
- Verify Mount:
- Verify that the NFS share is mounted:
df -h
You should see the NFS share listed in the output.
- Verify that the NFS share is mounted:
Automount NFS Share (Optional):
- Install Autofs:
- Install the autofs package on the client machine:
sudo apt-get install autofs
- Install the autofs package on the client machine:
- Configure Autofs:
- Edit the
/etc/auto.master
file and add a line for the NFS share:/mnt/nfs_share /etc/auto.nfs_share
- Create the
/etc/auto.nfs_share
file and add the following line:nfs_share -fstype=nfs <server_IP_address>:/shared_directory
- Restart the autofs service:
sudo systemctl restart autofs
- Edit the
Now, the NFS share will be automatically mounted when accessed, and it will be unmounted when not in use.
Adjust the settings and paths based on your specific setup. Always ensure that the necessary ports are open, and firewalls are configured appropriately for NFS traffic.