Samba / NFS File Sharing
Samba is used to share files with Windows systems (SMB/CIFS protocol), while NFS is used for file sharing between Linux systems.
Samba File Sharing
Install Samba
sudo apt update
sudo apt install samba samba-common-bin -y
# Check version
smbd --version
# Check service status
sudo systemctl status smbd nmbdBasic Configuration
The Samba configuration file is /etc/samba/smb.conf:
# Back up the original configuration
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
# Edit the configuration
sudo nano /etc/samba/smb.confPublic Share (No Authentication)
# /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
server string = Ubuntu File Server
security = user
map to guest = Bad User
dns proxy = no
# Performance tuning
socket options = TCP_NODELAY IPTOS_LOWDELAY
read raw = yes
write raw = yes
max xmit = 65535
[public]
path = /srv/samba/public
browsable = yes
read only = no
guest ok = yes
create mask = 0664
directory mask = 0775
force user = nobody# Create the shared directory
sudo mkdir -p /srv/samba/public
sudo chown nobody:nogroup /srv/samba/public
sudo chmod 0775 /srv/samba/publicAuthenticated Share
[shared]
path = /srv/samba/shared
browsable = yes
read only = no
guest ok = no
valid users = @smbgroup
create mask = 0660
directory mask = 0770
force group = smbgroup
[homes]
comment = Home Directories
browsable = no
read only = no
valid users = %S
create mask = 0600
directory mask = 0700# Create a Samba user group
sudo groupadd smbgroup
# Create a system user and add to the group
sudo useradd -M -s /usr/sbin/nologin smbuser
sudo usermod -aG smbgroup smbuser
# Set a Samba password (independent of the system password)
sudo smbpasswd -a smbuser
sudo smbpasswd -e smbuser # Enable the user
# Create the shared directory
sudo mkdir -p /srv/samba/shared
sudo chown root:smbgroup /srv/samba/shared
sudo chmod 0770 /srv/samba/sharedValidate and Start
# Test configuration file syntax
testparm
# Restart services
sudo systemctl restart smbd nmbd
# Enable at boot
sudo systemctl enable smbd nmbd
# Open Samba ports in the firewall
sudo ufw allow sambaClient Connections
Linux Client
# Install client tools
sudo apt install cifs-utils smbclient -y
# Browse shares
smbclient -L //server-ip -U smbuser
# Mount a share
sudo mkdir -p /mnt/samba
sudo mount -t cifs //server-ip/shared /mnt/samba -o username=smbuser,password=yourpass,uid=$(id -u),gid=$(id -g)
# Auto-mount at boot (/etc/fstab)
# First create a credentials file
sudo tee /etc/samba/.credentials << 'EOF'
username=smbuser
password=yourpass
EOF
sudo chmod 600 /etc/samba/.credentials
# Add an fstab entry
echo "//server-ip/shared /mnt/samba cifs credentials=/etc/samba/.credentials,uid=1000,gid=1000,iocharset=utf8 0 0" | sudo tee -a /etc/fstab
sudo mount -aWindows Client
In File Explorer, type the following in the address bar:
\\server-ip\sharedTo map a network drive:
- Right-click “This PC” -> “Map network drive”
- Enter
\\server-ip\shared - Check “Connect using different credentials”
NFS File Sharing
Install the NFS Server
sudo apt update
sudo apt install nfs-kernel-server -y
# Check status
sudo systemctl status nfs-serverConfigure Exported Directories
Edit /etc/exports:
# /etc/exports
# Format: directory client(options)
# Allow a single IP
/srv/nfs/data 192.168.1.100(rw,sync,no_subtree_check)
# Allow an entire subnet
/srv/nfs/data 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
# Allow multiple clients
/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check) 10.0.0.0/8(ro,sync,no_subtree_check)
# Read-only share
/srv/nfs/public *(ro,sync,no_subtree_check)Common Options
| Option | Description |
|---|---|
rw | Read-write access |
ro | Read-only access |
sync | Synchronous writes (safer) |
async | Asynchronous writes (faster but riskier) |
no_subtree_check | Disable subtree checking (improves performance) |
no_root_squash | Preserve client root privileges |
root_squash | Map root to nobody (default) |
all_squash | Map all users to nobody |
anonuid/anongid | Specify the UID/GID for anonymous users |
Create and Start Shares
# Create shared directories
sudo mkdir -p /srv/nfs/data /srv/nfs/shared /srv/nfs/public
sudo chown nobody:nogroup /srv/nfs/data /srv/nfs/shared /srv/nfs/public
sudo chmod 0777 /srv/nfs/data
# Apply the export configuration
sudo exportfs -ra
# View current exports
sudo exportfs -v
# Open NFS ports in the firewall
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 # rpcbindNFS Client
# Install the client
sudo apt install nfs-common -y
# View the server's export list
showmount -e server-ip
# Mount an NFS share
sudo mkdir -p /mnt/nfs
sudo mount -t nfs server-ip:/srv/nfs/data /mnt/nfs
# Mount with NFS v4
sudo mount -t nfs4 server-ip:/srv/nfs/data /mnt/nfs
# Auto-mount at boot (/etc/fstab)
echo "server-ip:/srv/nfs/data /mnt/nfs nfs defaults,_netdev 0 0" | sudo tee -a /etc/fstab
sudo mount -aAuto-Mount with autofs
# Install autofs
sudo apt install autofs -y
# Configure the master map file
echo "/mnt/nfs /etc/auto.nfs" | sudo tee -a /etc/auto.master
# Configure the NFS map
echo "data -rw,soft,intr server-ip:/srv/nfs/data" | sudo tee /etc/auto.nfs
# Restart autofs
sudo systemctl restart autofs
# Access triggers automatic mounting
ls /mnt/nfs/dataNFS Performance Tuning
# Server: increase the number of NFS threads
# Edit /etc/nfs.conf
sudo tee -a /etc/nfs.conf << 'EOF'
[nfsd]
threads = 16
EOF
# Client: optimized mount options
sudo mount -t nfs -o rw,hard,intr,rsize=1048576,wsize=1048576,timeo=600 server-ip:/srv/nfs/data /mnt/nfsSamba vs NFS Comparison
| Feature | Samba (SMB/CIFS) | NFS |
|---|---|---|
| Protocol | SMB/CIFS | NFS v3/v4 |
| Use case | Mixed Windows + Linux | Between Linux/Unix systems |
| Authentication | Separate password database | UID/GID-based or Kerberos |
| Performance | Good | Better (between Linux systems) |
| Configuration complexity | Moderate | Simple |
| Windows support | Native | Requires additional software |
| ACL support | Full | Basic |
| Encrypted transport | SMB3 supported | NFS v4 + Kerberos |
Troubleshooting
# Samba logs
sudo tail -f /var/log/samba/log.smbd
# NFS checks
showmount -e server-ip
rpcinfo -p server-ip
nfsstat
# Check mounts
mount | grep -E "(cifs|nfs)"
df -h | grep -E "(cifs|nfs)"
# Check ports
ss -tlnp | grep -E "(445|139|2049|111)"Last updated on