In this guide, we will walk you through the process of installing and configuring an NFS (Network File System) server on Debian 12. NFS is a network protocol that allows you to mount a remote file system over a network connection. By setting up an NFS server, you can share files and directories between multiple machines in your network.
Before we begin, let’s make sure you have all the prerequisites in place:
- Two Debian 12 servers: one will act as the NFS server, and the other as the NFS client.
- A non-root user with sudo/administrator privileges on both servers.
Now, let’s dive into the installation and configuration steps.
Installing NFS Server
To start, we need to install the NFS server package on the machine that will act as the server. The NFS package is available by default in the Debian repository.
First, update the package repository by running the following command:
sudo apt update
Once the repository is updated, install the NFS server package by running the following command:
sudo apt install nfs-kernel-server nfs-common
During the installation, you will be prompted to confirm. Type ‘y’ and press ENTER to proceed.
After the installation is complete, the NFS server service will be created, running, and enabled by default on your Debian machine. You can verify the status of the service by running the following command:
sudo systemctl status nfs-server
You should see the status as ‘active (running)’ if the service is running correctly.
Configuring NFSv4
The latest version of the NFS protocol is NFSv4, which offers enhanced security and performance features. In this section, we will configure NFSv4 on the server.
Open the NFS configuration file /etc/default/nfs-common using the nano editor:
sudo nano /etc/default/nfs-common
In this file, find the parameter NEED_STATD and set it to ‘no’. Then, find the parameter NEED_IDMAPD and set it to ‘yes’. These changes are required for NFSv4 to work properly.
NEED_STATD="no" NEED_IDMAPD="yes"
Save the file and exit the editor.
Next, open the NFS server configuration file /etc/default/nfs-kernel-server:
sudo nano /etc/default/nfs-kernel-server
In this file, add the following configuration to disable NFSv2 and NFSv3 and allow only NFSv4:
RPCNFSDOPTS="-N 2 -N 3" RPCMOUNTDOPTS="--manage-gids -N 2 -N 3"
Save the file and exit the editor.
To apply the changes, restart the NFS server service:
sudo systemctl restart nfs-server
Configuring Firewall via UFW
To secure your NFS server, it is essential to configure the firewall. In this section, we will use UFW (Uncomplicated Firewall) to allow access to the NFS server only from trusted networks.
If UFW is not installed, you can install it by running the following command:
sudo apt install ufw -y
Once UFW is installed, enable it and allow SSH access by running the following commands:
sudo ufw enable sudo ufw allow ssh
Next, allow access to the NFS server from your local network subnet. Replace 192.168.10.0/24 with your actual subnet:
sudo ufw allow from 192.168.10.0/24 to any port nfs
After making these changes, reload UFW and check the status to ensure the changes have been applied:
sudo ufw reload sudo ufw status
You should see that the NFS port 2049 is added to UFW, allowing access from your local network subnet.
Setting Up Pseudo Filesystem and Exports
In this section, we will set up a pseudo filesystem on the NFS server and configure the exports.
A pseudo filesystem allows you to set up a single shared filesystem for all your NFS directories instead of using multiple shared directories.
First, create the necessary directories using the following commands:
mkdir -p /shared/{data,documents} sudo chown -R nobody:nogroup /shared
Next, create the export directories:
mkdir -p /exports/{data,home,documents} sudo chown -R nobody:nogroup /exports
Now, mount the directories as pseudo filesystems:
sudo mount --bind /home /exports/home sudo mount --bind /shared/data /exports/data sudo mount --bind /shared/documents /exports/documents
Verify the mounted filesystems using the df command:
sudo df -ah
You should see the three pseudo filesystems listed.
To make the pseudo filesystems mount permanently, you can add them to the /etc/fstab file. Open the file using the nano editor:
sudo nano /etc/fstab
Add the following lines to the file:
/home /exports/home none bind /shared/data /exports/data none bind /shared/documents /exports/documents none bind
Save the file and exit the editor.
Next, open the exports file:
sudo nano /etc/exports
Add the following line to the file:
/exports 192.168.10.0/255.255.255.0(rw,no_root_squash,no_subtree_check,crossmnt,fsid=0)
Save the file and exit the editor.
Restart the NFS server service to apply the changes:
sudo systemctl restart nfs-server
To check the exported directories, run the following command:
sudo showmount -e 192.168.10.15
You should see the /exports directory listed as an exported directory.
Setting Up NFS Client
Now that the NFS server is configured, we can set up the NFS client machine and mount the shared filesystem.
Log in to the client machine and update the Debian repository:
sudo apt update
Install the NFS client package:
sudo apt install nfs-common
Next, create the target mount directories:
mkdir -p /users/data/documents
Mount the exported pseudo filesystems to the target directories:
sudo mount.nfs4 192.168.10.15:/home /users sudo mount.nfs4 192.168.10.15:/data /data sudo mount.nfs4 192.168.10.15:/documents /documents
Verify the mounted filesystems using the df command:
sudo df -h
You should see the pseudo filesystems from the NFS server mounted to their respective target directories.
To mount the NFS server permanently, we can edit the /etc/fstab file. First, unmount the current mounts:
sudo umount /users /data /documents
Open the fstab file:
sudo nano /etc/fstab
Add the following lines to the file:
192.168.10.15:/home /users nfs4 soft,intr,rsize=8192,wsize=8192 192.168.10.15:/data /data nfs4 soft,intr,rsize=8192,wsize=8192 192.168.10.15:/documents /documents nfs4 soft,intr,rsize=8192,wsize=8192
Save the file and exit the editor.
Reload the systemd manager and mount all filesystems in /etc/fstab:
sudo systemctl daemon-reload sudo mount -a
Verify the mounted filesystems using the df command:
sudo df -h
You should see that the NFS server is mounted to each target directory.
Conclusion
Congratulations! You have successfully installed and configured an NFS server on Debian 12, as well as set up an NFS client to access the shared filesystem. By following these steps, you can easily share files and directories across multiple machines in your network.
Remember, if you’re looking for reliable and scalable cloud hosting solutions, Shape.host offers SSD Linux VPS services that can enhance your NFS server performance. You can count on Shape.host’s expertise to provide efficient and secure hosting solutions for your business needs.