top of page

Setting Up an NFS Server on Linux

  • Writer: CloudCastHub
    CloudCastHub
  • Aug 12, 2024
  • 4 min read

NFS lets you share files over a network. It’s different from FTP because it mounts remote directories on your machine. This makes them seem like local files. This guide will show you how to set up an NFS server on Linux, using Debian as the example.


What is an NFS Server?

NFS stands for Network File Sharing. It lets you share files and directories with other Linux machines over a network. An NFS server is a system that shares files and directories with other computers on the network.


Using an NFS server helps you store data in one place. This means you don't need lots of storage on each computer. Users can then use the shared files and directories like they're on their own computer. This is great when you're short on disk space and need to share data between computers.


  • NFS allows a system to share directories and files with others over a network.

  • Users and programs can access files on remote systems as if they were local by using NFS.

  • Local workstations use less disk space as commonly used data can be stored on a single machine and accessed over the network.

  • NFS enables setting up home directories on the NFS server for network-wide access.

  • Usage of storage devices like floppy disks, CDROM drives, and USB Thumb drives can be extended to other machines on the network via NFS, potentially reducing the need for multiple drives.


Step 1: Installing the NFS Server Software

To set up an NFS server, you need to install the NFS server software. On Debian, this is called nfs-kernel-server.


Installation on Debian

First, update your package list with `sudo apt update`.

updating packages

Then, install the NFS server software with `sudo apt install nfs-kernel-server`. This installs the NFS server and its needed parts. The service will start automatically after installation.

installing nfs server

Step 2: Configuring the NFS Server

After installing the NFS server software, you need to set it up to share directories.


Exporting Directories

In NFS, you share directories with clients by defining them in the /etc/exports file.


1. Open the exports file for editing: `sudo nano /etc/exports`.

editing exports

2. Add lines for each directory you want to share. For example, to share /kali/nfs with a client, add “/srv/nfs client_ip(rw,sync,no_subtree_check)”. Replace client_ip with the client’s IP address.


  • /srv/nfs is the directory you’re sharing.

  • client_ip should be replaced with the IP address of the client that will access the share. We are going to specify the entire subnet for this tutorial which is 192.168.1.0/24.

  • rw grants read and write permissions.

  • sync ensures changes are written to disk immediately.

  • no_subtree_check disables subtree checking for better performance.

editing exports

Save and close the file after adding the required exports.


Applying the Export Configuration

After editing the /etc/exports file, apply the changes. Run `sudo exportfs -a` to refresh the NFS server with the new configuration.

applying the export config

To ensure that NFS service starts automatically on boot, enable it with `sudo systemctl enable nfs-server`.

enabling nfs for autostart

Step 3: Setting Up NFS Client Access

Now, set up the client machines that will access the shared directories.


Mounting NFS Shares on the Client

1. Install the NFS client software on the client machine using `sudo apt install nfs-common`.

installing nfs client

2. Create a mount point on the client machine where the NFS share will be accessible using `sudo mkdir -p /mnt/nfs_share`.

creating nfs share directory

3. Mount the NFS share with the command `sudo mount server_ip:/srv/nfs /mnt/nfs_share`, where:


  • server_ip is the IP address of your NFS server.

  • /srv/nfs is the directory on the server you are sharing.

  • /mnt/nfs_share is the directory on the client where the NFS share will be mounted.

mounting nfs

If you want the NFS share to be mounted automatically at boot, add the following line to the client’s /etc/fstab file: “server_ip:/srv/nfs /mnt/nfs_share nfs defaults 0 0”.

mounting nfs

Step 4: Managing Permissions and Security

NFS uses the file system permissions. So, make sure the directory permissions are right on the server.


Setting Directory Permissions

Use `sudo chown nfsuser:nfsuser /srv/nfs` and `sudo chmod 755 /srv/nfs` to change the ownership and permissions. This lets a specific user control the NFS directory fully.

changing ownership and permissions

Securing NFS

NFS is not secure by itself. To make it safer, consider these steps:


  • Limit access to certain IP addresses in the /etc/exports file.

  • Use a firewall to block access to the NFS server.

  • Choose NFS over TCP instead of UDP for better security. Add this to the “/etc/exports” file: “/srv/nfs client_ip(rw,sync,no_subtree_check,proto=tcp)”.


Step 5: Testing the NFS Setup

After setting up the server and client, test the setup to make sure it works.


Testing File Access

On the client machine, go to the NFS mount point with `cd /mnt/nfs_share. Try making a file with `touch testfile. If the file is made, your NFS setup is good.

testing nfs setup

Conclusion

Setting up an NFS server on Linux lets you share files easily across machines. Follow this guide to install, set up, and secure an NFS server. It’s a great way to share files in a network. But, remember to keep it secure, especially with sensitive data.


FAQ

What is an NFS Server?

NFS stands for Network File System. It lets you share storage on your network. Users can see a remote drive as their own, like a USB drive. It's a simple way to share files in an organization.


How do I install the NFS Server?

Install the NFS Kernel to set up a machine to share directories. Use these commands to do it.


How do I create a shared directory for the NFS Server?

Create a shared directory on your filesystem after installing the NFS server. Use LVM for flexible storage. Make the directory with this command.


How do I define access permissions for the NFS Server?

Set permissions for your shared location to control access. If it's on the root partition, check the owner and group permissions. Make sure users can access it if they're not in the root group.


Comentarios


Cloud Cast Hub

Explore the ever-evolving world of cloud computing with Cloud Cast Hub, your premier resource for in-depth articles and expert insights on AWS and GCP

SUBSCRIBE 

Thanks for submitting!

© 2024 CloudCast Hub.

bottom of page