Linux Storage Configuration Using NFS and iSCSI
# Linux Storage Configuration Using NFS and iSCSI ## Introduction Network storage enables Linux systems to access shared storage resources over the network. NFS (Network File System) provides file-
# Linux Storage Configuration Using NFS and iSCSI
Introduction
Network storage enables Linux systems to access shared storage resources over the network. NFS (Network File System) provides file-level sharing, while iSCSI provides block-level storage over IP networks. Both are essential for enterprise storage architectures, shared home directories, and storage consolidation.
This tutorial covers NFS server and client configuration, iSCSI initiator and target setup, multipathing, storage mounting, performance tuning, and storage security.
By the end of this guide, you will be able to configure and manage NFS and iSCSI storage on Linux systems.
Technical Background
NFS (Network File System)
NFS is a distributed file system protocol that allows remote directories to be mounted as if they were local. NFSv4 is the current recommended version with improved security and performance.
NFS Architecture:
- **NFS Server**: Exports directories to clients
- **NFS Client**: Mounts exported directories
- **RPC (Remote Procedure Call)**: Underlying communication mechanism
- **Portmap/rpcbind**: Maps RPC services to ports
iSCSI (Internet Small Computer System Interface)
iSCSI encapsulates SCSI commands in TCP/IP packets, providing block-level storage over standard Ethernet networks.
iSCSI Architecture:
- **iSCSI Target**: Storage server providing LUNs (Logical Unit Numbers)
- **iSCSI Initiator**: Client that connects to targets and uses LUNs
- **LUN**: Logical Unit Number representing a storage volume
- **IQN (iSCSI Qualified Name)**: Unique identifier for targets and initiators
NFS vs iSCSI Comparison
| Feature | NFS | iSCSI | |---------|-----|-------| | Level | File-level | Block-level | | Protocol | RPC/NFS | TCP/IP (SCSI over IP) | | Mount | Mount point on client | Block device (/dev/sdX) | | Filesystem | Server manages | Client manages | | Locking | Server-side | Client-side | | Best for | Shared files, home directories | Databases, VM storage, clustering |
Prerequisites
- Linux systems with root or sudo access
- Network connectivity between server and clients
- Storage disks available on server (for NFS exports or iSCSI targets)
- nfs-utils and open-iscsi packages installed
Architecture
NFS Architecture
NFS Server NFS Client
┌─────────────────┐ ┌─────────────────┐
│ /export/data │──── NFSv4 ─────▶│ /mnt/nfs-data │
│ /export/home │──── NFSv4 ─────▶│ /mnt/nfs-home │
└─────────────────┘ └─────────────────┘iSCSI Architecture
iSCSI Target iSCSI Initiator
┌─────────────────┐ ┌─────────────────┐
│ LUN 1 (/dev/sdb)│──── iSCSI ────▶│ /dev/sdb │
│ LUN 2 (/dev/sdc)│──── iSCSI ────▶│ /dev/sdc │
└─────────────────┘ └─────────────────┘
│
Filesystem (ext4/XFS)
│
Mount point (/mnt/iscsi)Step-by-Step Implementation
Step 1: Configure NFS Server
**Purpose**: Set up an NFS server to export directories.
**Commands** (Ubuntu/Debian):
# Install NFS server
sudo apt update
sudo apt install nfs-kernel-server
# Install NFS server (RHEL/CentOS/Fedora)
sudo dnf install nfs-utils**Commands**:
# Create export directories
sudo mkdir -p /export/data
sudo mkdir -p /export/home
# Set ownership and permissions
sudo chown nobody:nogroup /export/data
sudo chown nobody:nogroup /export/home
sudo chmod 755 /export/data
sudo chmod 755 /export/home
# Configure exports
sudo nano /etc/exports**Example /etc/exports**:
/export/data 192.168.1.0/24(rw,sync,no_subtree_check)
/export/home 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
/export/backup 192.168.1.0/24(ro,sync,no_subtree_check)**Export options**:
- `rw`: Read-write access
- `ro`: Read-only access
- `sync`: Write to disk before replying (safer)
- `async`: Write to disk after reply (faster, less safe)
- `no_subtree_check`: Disable subtree checking (improves reliability)
- `no_root_squash`: Allow root on client to access as root (security risk)
- `root_squash`: Map root to anonymous (default, safer)
**Apply exports**:
# Export all directories
sudo exportfs -a
# Check exported filesystems
sudo exportfs -v
# Restart NFS server
sudo systemctl restart nfs-server
sudo systemctl enable nfs-server**Expected output**: NFS server running; directories exported.
**Verification**:
showmount -e localhost
# Shows exported directories and allowed clientsStep 2: Configure NFS Client
**Purpose**: Mount NFS exports on client systems.
**Commands** (Ubuntu/Debian):
# Install NFS client
sudo apt update
sudo apt install nfs-common**Commands**:
# Create mount points
sudo mkdir -p /mnt/nfs-data
sudo mkdir -p /mnt/nfs-home
# Mount NFS exports
sudo mount -t nfs -o vers=4 192.168.1.10:/export/data /mnt/nfs-data
sudo mount -t nfs -o vers=4 192.168.1.10:/export/home /mnt/nfs-home
# Verify mounts
df -h | grep nfs
mount | grep nfs**Configure persistent mounting** (`/etc/fstab`):
192.168.1.10:/export/data /mnt/nfs-data nfs defaults,vers=4,_netdev 0 0
192.168.1.10:/export/home /mnt/nfs-home nfs defaults,vers=4,_netdev 0 0**Test fstab**:
sudo mount -a**Expected output**: NFS exports mounted and accessible.
**Verification**:
df -h /mnt/nfs-data
# Shows NFS mount with available space
ls /mnt/nfs-data
# Shows files from NFS serverStep 3: Configure iSCSI Target (Server)
**Purpose**: Set up an iSCSI target to provide block storage.
**Commands** (Ubuntu/Debian):
# Install targetcli (iSCSI target management)
sudo apt update
sudo apt install targetcli-fb**Commands**:
# Start targetcli
sudo targetcli**targetcli interactive session**:
/> cd /backstores/block
/backstores/block> create name=lun1 dev=/dev/sdb
/backstores/block> create name=lun2 dev=/dev/sdc
# Create iSCSI target
/iscsi> create iqn.2024-01.com.ityantra:storage.target1
# Create LUN mapping
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/luns> create /backstores/block/lun1
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/luns> create /backstores/block/lun2
# Create ACL for initiator
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/acls> create iqn.2024-01.com.ityantra:client.initiator1
# Save configuration
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1> saveconfig
/> exit**Enable and start target service**:
sudo systemctl enable targetclid
sudo systemctl start targetclid**Expected output**: iSCSI target created with LUNs available.
**Verification**:
sudo targetcli ls
# Shows targets, LUNs, and ACLsStep 4: Configure iSCSI Initiator (Client)
**Purpose**: Connect client to iSCSI target and use LUNs.
**Commands** (Ubuntu/Debian):
# Install open-iscsi
sudo apt update
sudo apt install open-iscsi**Commands**:
# Discover iSCSI targets
sudo iscsiadm -m discovery -t sendtargets -p 192.168.1.10
# Login to target
sudo iscsiadm -m node -T iqn.2024-01.com.ityantra:storage.target1 -p 192.168.1.10 --login
# Verify session
sudo iscsiadm -m session
# Check new block device
lsblk | grep sd**Expected output**: iSCSI session established; new block device available.
**Verification**:
sudo iscsiadm -m session
# Shows: iqn.2024-01.com.ityantra:storage.target1 logged in
lsblk
# Shows new disk (e.g., /dev/sdb)Step 5: Format and Mount iSCSI LUN
**Purpose**: Create filesystem on iSCSI LUN and mount it.
**Commands**:
# Create filesystem on iSCSI disk
sudo mkfs.ext4 /dev/sdb
# Create mount point
sudo mkdir -p /mnt/iscsi-storage
# Mount iSCSI disk
sudo mount /dev/sdb /mnt/iscsi-storage
# Add to /etc/fstab for automatic mounting
echo "/dev/sdb /mnt/iscsi-storage ext4 defaults,_netdev 0 2" | sudo tee -a /etc/fstab**Expected output**: iSCSI LUN formatted, mounted, and configured for automatic mounting.
**Verification**:
df -h /mnt/iscsi-storage
# Shows iSCSI storage mounted
ls /mnt/iscsi-storage
# Shows empty filesystemStep 6: Configure iSCSI Multipathing (Optional)
**Purpose**: Provide redundant paths to iSCSI storage.
**Commands**:
# Install multipath-tools
sudo apt install multipath-tools
# Enable multipath
sudo mpathconf --enable
# View multipath devices
sudo multipath -ll
# Check multipath status
multipathd show maps**Expected output**: Multipath devices created with redundant paths.
**Verification**:
sudo multipath -ll
# Shows multipath devices with path statusConfiguration Examples
Complete /etc/exports (NFS Server)
# Home directories - read-write for client subnet
/home 192.168.1.0/24(rw,sync,no_subtree_check)
# Data share - read-write
/data 192.168.1.0/24(rw,sync,no_subtree_check)
# Backup share - read-only
/backup 192.168.1.0/24(ro,sync,no_subtree_check)
# Public share - no root squash (use with caution)
/public 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)NFS Client Mount Options
# /etc/fstab entries
server:/export/data /mnt/data nfs defaults,vers=4,rsize=1048576,wsize=1048576,_netdev 0 0
server:/export/home /mnt/home nfs defaults,vers=4,hard,intr,_netdev 0 0**Mount options**:
- `hard`: Retry indefinitely on server failure (default, safer)
- `soft`: Return error after timeout (faster failover, risk of data corruption)
- `intr`: Allow interrupts during hard mount
- `rsize/wsize`: Read/write block size (optimize for network)
- `_netdev`: Wait for network before mounting (important for boot)
iSCSI Initiator Configuration (`/etc/iscsi/iscsid.conf`)
# Enable CHAP authentication
node.session.auth.authmethod = CHAP
node.session.auth.username = initiator_user
node.session.auth.password = secret_password
# Startup mode
node.startup = automatic
# Timeouts
node.session.timeo.replacement_timeout = 120
node.conn[0].timeo.noop_out_interval = 5
node.conn[0].timeo.noop_out_timeout = 5iSCSI Target ACL Example
# In targetcli
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/acls> create iqn.2024-01.com.ityantra:client.initiator1
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/acls/iqn.2024-01.com.ityantra:client.initiator1> set attribute authentication=1
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/acls/iqn.2024-01.com.ityantra:client.initiator1> set attribute username=initiator_user
/iscsi/iqn.2024-01.com.ityantra:storage.target1/tpg1/acls/iqn.2024-01.com.ityantra:client.initiator1> set password=secret_passwordVerification
NFS Verification Commands
showmount -e <server> -- List exported filesystems
showmount -a <server> -- List active mounts
mount | grep nfs -- Mounted NFS filesystems
df -h -- Disk usage including NFS
cat /proc/fs/nfsd/versions -- NFS versions supportediSCSI Verification Commands
iscsiadm -m session -- Active iSCSI sessions
iscsiadm -m node -- Configured iSCSI nodes
lsblk -- Block devices including iSCSI LUNs
multipath -ll -- Multipath devices (if configured)
targetcli ls -- Target configuration (on server)Expected Results
- NFS exports accessible from clients
- iSCSI sessions established and LUNs visible
- Filesystems mounted and writable
- Storage survives reboots (fstab configured)
Troubleshooting
| Symptom | Possible Cause | Diagnostic Command | Corrective Action | Verification | |---------|---------------|-------------------|-------------------|--------------| | NFS mount fails | Server unreachable or export missing | `showmount -e <server>` | Fix server; verify export path | Mount succeeds | | NFS read-only | Export configured as ro | `cat /etc/exports` | Change to rw; re-export | Write succeeds | | Stale file handle | Server rebooted | `dmesg \| grep nfs` | Unmount and remount | Stale handle cleared | | iSCSI discovery fails | Target unreachable or wrong IP | `iscsiadm -m discovery` | Fix network; verify target IP | Targets discovered | | iSCSI login fails | Authentication or ACL missing | `iscsiadm -m session` | Configure CHAP; add ACL | Login succeeds | | iSCSI device not found | Session not logged in | `iscsiadm -m session` | Login to target | Device appears | | Multipath not working | Only one path configured | `multipath -ll` | Configure second network path | Multiple paths active | | Slow NFS performance | Network or rsize/wsize | `nfsstat -c` | Adjust mount options; check network | Performance improves | | NFS server not running | Service not started | `sudo systemctl status nfs-server` | Start service | Service active |
Best Practices
1. **Use NFSv4**: Better security and performance than NFSv3. 2. **Use sync for critical data**: Ensures data written to disk before acknowledging. 3. **Use _netdev in fstab**: Prevents boot delays when network storage unavailable. 4. **Use hard mounts for NFS**: Prevents data corruption on server failure. 5. **Configure CHAP for iSCSI**: Authenticates iSCSI connections. 6. **Use multipathing for iSCSI**: Provides path redundancy. 7. **Export specific subnets**: Limit NFS access to trusted networks. 8. **Use root_squash**: Prevents root on client from having root access on server. 9. **Monitor storage capacity**: Alert on high utilization. 10. **Test failover**: Verify storage access after network or server failures.
Conclusion
Linux network storage using NFS and iSCSI provides flexible options for shared storage. NFS is ideal for file-level sharing (home directories, shared files), while iSCSI is suitable for block-level storage (databases, VM storage, clustering). Proper configuration of exports, mounts, authentication, and multipathing ensures reliable, secure network storage. Regular monitoring and testing maintain storage availability and performance.
References
- NFS Documentation (https://nfs.sourceforge.net/)
- NFSv4 RFC 7530 (https://tools.ietf.org/html/rfc7530)
- iSCSI RFC 3720 (https://tools.ietf.org/html/rfc3720)
- targetcli Documentation (https://github.com/agrover/targetcli-fb)
- open-iscsi Documentation (https://github.com/open-iscsi/open-iscsi)
- Linux NFS HOWTO (https://tldp.org/HOWTO/NFS-HOWTO/)