Configure encrypted network storage with LUKS and NFS for secure file sharing

Advanced 45 min May 12, 2026 61 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up enterprise-grade encrypted network storage by combining LUKS disk encryption with NFS file sharing. This advanced configuration provides secure, centralized file access across multiple systems while maintaining data protection at rest.

Prerequisites

  • Root access to server
  • Additional storage device for encryption
  • Network connectivity between server and clients
  • Basic understanding of Linux file systems and networking

What this solves

This tutorial configures encrypted network storage using LUKS (Linux Unified Key Setup) and NFS (Network File System) to create secure, centralized file sharing. You'll combine disk-level encryption with network file sharing to protect sensitive data both at rest and in transit across multiple systems.

Step-by-step configuration

Update system packages and install required tools

Start by updating your package manager and installing the cryptsetup utilities for LUKS and NFS server components.

sudo apt update && sudo apt upgrade -y
sudo apt install -y cryptsetup nfs-kernel-server nfs-common
sudo dnf update -y
sudo dnf install -y cryptsetup nfs-utils

Prepare the storage device for encryption

Identify your target storage device and initialize it with LUKS encryption. Replace /dev/sdb with your actual device path.

Warning: This will destroy all existing data on the device. Ensure you have backups before proceeding.
sudo lsblk
sudo cryptsetup luksFormat /dev/sdb

Enter a strong passphrase when prompted. This passphrase encrypts your entire storage device.

Open the encrypted device and create filesystem

Unlock the LUKS container and create an ext4 filesystem on the encrypted volume.

sudo cryptsetup luksOpen /dev/sdb encrypted_storage
sudo mkfs.ext4 /dev/mapper/encrypted_storage

Create mount point and configure persistent mounting

Create the directory where your encrypted storage will be mounted and configure automatic mounting on boot.

sudo mkdir -p /mnt/encrypted_nfs
sudo mount /dev/mapper/encrypted_storage /mnt/encrypted_nfs

Create key file for automatic unlocking

Generate a key file to automatically unlock the encrypted volume on boot without manual password entry.

sudo dd if=/dev/urandom of=/etc/luks-keys/storage.key bs=1024 count=4
sudo chmod 400 /etc/luks-keys/storage.key
sudo cryptsetup luksAddKey /dev/sdb /etc/luks-keys/storage.key

Configure crypttab for automatic decryption

Add the encrypted device to crypttab so it automatically unlocks during boot using the key file.

encrypted_storage /dev/sdb /etc/luks-keys/storage.key luks

Configure fstab for automatic mounting

Add the decrypted volume to fstab for automatic mounting after decryption.

echo '/dev/mapper/encrypted_storage /mnt/encrypted_nfs ext4 defaults 0 2' | sudo tee -a /etc/fstab

Create NFS export directories and set permissions

Create subdirectories within the encrypted storage for different NFS exports and configure appropriate permissions.

sudo mkdir -p /mnt/encrypted_nfs/shared
sudo mkdir -p /mnt/encrypted_nfs/private
sudo chown nobody:nogroup /mnt/encrypted_nfs/shared
sudo chown nobody:nogroup /mnt/encrypted_nfs/private
sudo chmod 755 /mnt/encrypted_nfs/shared
sudo chmod 750 /mnt/encrypted_nfs/private

Configure NFS exports

Define which directories are exported via NFS and specify client access controls in the exports configuration.

/mnt/encrypted_nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
/mnt/encrypted_nfs/private 192.168.1.100(rw,sync,no_subtree_check,no_root_squash)

Replace the IP addresses and subnets with your actual network configuration. The shared directory allows access from the entire subnet, while private restricts access to a single host.

Enable and start NFS services

Enable the NFS server daemon and export the configured directories to make them available to network clients.

sudo systemctl enable --now nfs-kernel-server
sudo exportfs -ra
sudo systemctl status nfs-kernel-server
sudo systemctl enable --now nfs-server
sudo exportfs -ra
sudo systemctl status nfs-server

Configure firewall rules for NFS

Open the necessary ports for NFS communication. NFS requires multiple ports for different services.

sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw allow from 192.168.1.0/24 to any port 111
sudo ufw allow from 192.168.1.0/24 to any port 2049
sudo ufw reload
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --reload

Configure NFS client systems

On client systems that need to access the encrypted NFS storage, install NFS client packages and create mount points.

sudo apt install -y nfs-common
sudo mkdir -p /mnt/nfs_shared
sudo mkdir -p /mnt/nfs_private
sudo dnf install -y nfs-utils
sudo mkdir -p /mnt/nfs_shared
sudo mkdir -p /mnt/nfs_private

Mount NFS shares on client systems

Mount the encrypted NFS shares on client systems and configure persistent mounting.

sudo mount -t nfs 192.168.1.10:/mnt/encrypted_nfs/shared /mnt/nfs_shared
sudo mount -t nfs 192.168.1.10:/mnt/encrypted_nfs/private /mnt/nfs_private

Add these mounts to /etc/fstab on client systems for persistence:

192.168.1.10:/mnt/encrypted_nfs/shared /mnt/nfs_shared nfs defaults 0 0
192.168.1.10:/mnt/encrypted_nfs/private /mnt/nfs_private nfs defaults 0 0

Configure performance optimization

Optimize NFS performance by tuning mount options and server parameters for better throughput and reduced latency.

sudo mount -o remount,rsize=1048576,wsize=1048576,hard,intr /mnt/nfs_shared

Update the fstab entries with optimized mount options:

192.168.1.10:/mnt/encrypted_nfs/shared /mnt/nfs_shared nfs rsize=1048576,wsize=1048576,hard,intr 0 0

Set up automated backup procedures

Create backup scripts to regularly backup the encrypted storage and NFS configuration files.

#!/bin/bash

Backup encrypted NFS data and configuration

BACKUP_DIR="/backup/nfs-$(date +%Y%m%d)" mkdir -p "$BACKUP_DIR"

Backup NFS data

rsync -av /mnt/encrypted_nfs/ "$BACKUP_DIR/data/"

Backup configuration files

cp /etc/exports "$BACKUP_DIR/exports" cp /etc/fstab "$BACKUP_DIR/fstab" cp /etc/crypttab "$BACKUP_DIR/crypttab"

Create LUKS header backup

cryptsetup luksHeaderBackup /dev/sdb --header-backup-file "$BACKUP_DIR/luks-header-backup"

Compress backup

tar -czf "/backup/encrypted-nfs-$(date +%Y%m%d).tar.gz" -C /backup "nfs-$(date +%Y%m%d)" rm -rf "$BACKUP_DIR"
sudo chmod +x /etc/cron.daily/backup-encrypted-nfs

Verify your setup

Test that your encrypted NFS storage is working correctly by checking the encryption status, NFS exports, and client connectivity.

# Check LUKS encryption status
sudo cryptsetup status encrypted_storage

Verify NFS exports

sudo exportfs -v

Test NFS connectivity from client

showmount -e 192.168.1.10

Check mounted filesystems

df -h | grep encrypted df -h | grep nfs

Test file operations

echo "test file" | sudo tee /mnt/encrypted_nfs/shared/test.txt cat /mnt/nfs_shared/test.txt

Security hardening

Enable NFS over TLS (NFSv4.2)

Configure transport layer security for NFS connections to encrypt data in transit.

[nfsd]
vers4.2=y
xprtsec=tls

[gssd]
use-gss-proxy=1

Configure Kerberos authentication

Set up Kerberos authentication for stronger access controls on NFS shares.

/mnt/encrypted_nfs/shared 192.168.1.0/24(rw,sync,sec=krb5p,no_subtree_check)
/mnt/encrypted_nfs/private 192.168.1.100(rw,sync,sec=krb5p,no_subtree_check)

Implement access logging

Enable comprehensive logging for NFS access monitoring and security auditing.

# NFS logging configuration
kern.* /var/log/nfs.log
daemon.* /var/log/nfs.log
sudo systemctl restart rsyslog

Performance optimization

Tune NFS server threads

Optimize the number of NFS server threads based on expected concurrent client connections.

RPCNFSDCOUNT=16
RPCMOUNTDOPTS="--manage-gids -p 2000"

Configure kernel parameters for NFS performance

Tune kernel parameters to optimize NFS performance for your workload.

# Increase NFS performance
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
sudo sysctl --system

Disaster recovery procedures

Create disaster recovery documentation

Document the recovery process for your encrypted NFS setup including key locations and restore procedures.

# Encrypted NFS Disaster Recovery

Recovery Steps

  1. Restore LUKS header: cryptsetup luksHeaderRestore /dev/sdb --header-backup-file luks-header-backup
  2. Unlock device: cryptsetup luksOpen /dev/sdb encrypted_storage
  3. Mount filesystem: mount /dev/mapper/encrypted_storage /mnt/encrypted_nfs
  4. Restore configuration files: /etc/exports, /etc/fstab, /etc/crypttab
  5. Restart NFS services

Key Files

  • LUKS key: /etc/luks-keys/storage.key
  • NFS exports: /etc/exports
  • Mount config: /etc/fstab
  • Crypt config: /etc/crypttab

Test recovery procedures

Regularly test your disaster recovery procedures to ensure they work when needed.

# Test LUKS header backup
sudo cryptsetup luksHeaderBackup /dev/sdb --header-backup-file /tmp/test-header

Verify backup integrity

sudo cryptsetup luksHeaderRestore /dev/sdb --header-backup-file /tmp/test-header --test-passphrase

Clean up test files

rm /tmp/test-header

Common issues

Symptom Cause Fix
NFS mount fails with "access denied" Incorrect export configuration or firewall blocking Check /etc/exports syntax and verify firewall rules with sudo exportfs -ra
LUKS device won't unlock on boot Missing or incorrect key file permissions Verify key file exists at /etc/luks-keys/storage.key with chmod 400
Poor NFS performance Suboptimal mount options or network configuration Use rsize=1048576,wsize=1048576 mount options and check network MTU
"Device or resource busy" during umount Active processes using the mounted filesystem Use sudo lsof /mnt/encrypted_nfs to find processes, then sudo fuser -km /mnt/encrypted_nfs
NFS exports not visible to clients NFS services not running or ports blocked Check sudo systemctl status nfs-kernel-server and verify ports 111, 2049 are open

Next steps

Running this in production?

Need this managed? Running encrypted NFS at scale adds complexity around key management, performance tuning, failover procedures, and 24/7 monitoring. Our managed platform covers monitoring, backups and incident response by default.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.