NFS and Samba: Linux File Sharing Setup Guide

Sharing files between Linux systems and between Linux and Windows is a common infrastructure task. NFS (Network File System) is the standard for Linux-to-Linux file sharing. Samba brings Windows-compatible file sharing to Linux, allowing Linux servers to act as file servers for Windows clients. This guide sets up both from scratch.

NFS — Linux to Linux File Sharing

Installing NFS Server (Ubuntu/Debian)

sudo apt update
sudo apt install nfs-kernel-server -y
sudo systemctl enable --now nfs-kernel-server

Creating and Exporting a Share

sudo mkdir -p /srv/nfs/shared
sudo chown nobody:nogroup /srv/nfs/shared
sudo chmod 755 /srv/nfs/shared

Edit /etc/exports to define which directories to share:

sudo nano /etc/exports
# Syntax: /path  client(options)
/srv/nfs/shared  192.168.1.0/24(rw,sync,no_subtree_check)
/srv/nfs/shared  10.0.0.50(ro,sync,no_subtree_check)     # Read-only for specific host

Apply the exports:

sudo exportfs -arv              # Apply and reload
sudo exportfs -v                # List current exports

NFS Export Options

  • rw / ro — read-write or read-only
  • sync — write to disk before responding (safer)
  • async — faster but risk of data loss on crash
  • no_subtree_check — improves reliability for most setups
  • no_root_squash — root on client maps to root on server (use carefully)
  • root_squash — (default) root on client maps to nobody on server

Mounting NFS on the Client

sudo apt install nfs-common -y

sudo mkdir /mnt/nfs-share
sudo mount 192.168.1.10:/srv/nfs/shared /mnt/nfs-share

ls /mnt/nfs-share                   # Verify contents
df -h /mnt/nfs-share                # Check mount

Persistent NFS Mount via fstab

192.168.1.10:/srv/nfs/shared  /mnt/nfs-share  nfs  defaults,_netdev  0  0

The _netdev option tells the system to wait for the network before mounting.

Samba — Linux to Windows File Sharing

Installing Samba (Ubuntu/Debian)

sudo apt install samba -y
sudo systemctl enable --now smbd nmbd

Creating a Share

sudo mkdir -p /srv/samba/shared
sudo chown -R nobody:nogroup /srv/samba/shared
sudo chmod 0777 /srv/samba/shared

Back up and edit /etc/samba/smb.conf:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
sudo nano /etc/samba/smb.conf

Add at the bottom:

[Shared]
   comment = Public Shared Folder
   path = /srv/samba/shared
   read only = no
   browsable = yes
   guest ok = yes

[PrivateShare]
   comment = Authenticated Share
   path = /srv/samba/private
   read only = no
   browsable = yes
   guest ok = no
   valid users = alok

Adding a Samba User

sudo adduser alok                      # Linux account must exist first
sudo smbpasswd -a alok                 # Set Samba password
sudo smbpasswd -e alok                 # Enable Samba account

Testing and Reloading

sudo testparm                          # Validate smb.conf
sudo systemctl restart smbd nmbd

Connecting from Linux Client

sudo apt install smbclient cifs-utils -y

smbclient //192.168.1.10/Shared -U alok       # Interactive shell
sudo mount -t cifs //192.168.1.10/Shared /mnt/smb -o user=alok,password=pass

Connecting from Windows

In File Explorer, type \192.168.1.10Shared in the address bar. Enter credentials if prompted.

Firewall Rules for NFS and Samba

# Allow NFS
sudo ufw allow from 192.168.1.0/24 to any port 2049

# Allow Samba
sudo ufw allow from 192.168.1.0/24 to any port 445
sudo ufw allow from 192.168.1.0/24 to any port 139

Summary

Use NFS for Linux-to-Linux sharing — it is simpler, faster, and better integrated with Linux permissions. Use Samba when you need Windows clients to access shared files or when you need SMB protocol compatibility. Both benefit from network-level access control — restrict shares to specific subnets and use Samba authentication rather than guest-only shares in production.