Troubleshooting CIFS Mount Failures: Hostname Resolution in fstab vs Direct IP Access


3 views

When mounting CIFS shares via /etc/fstab, many administrators encounter a peculiar scenario where hostnames defined in /etc/hosts fail to resolve during mount operations, while direct IP addressing works perfectly. Here's a deep dive into why this occurs and how to fix it.

The classic error pattern looks like this:

mount error: could not resolve address for NAS-5h2-20
Unable to determine destination address.

Yet basic network utilities work fine:

ping NAS-5h2-20
# Returns proper response
nslookup NAS-5h2-20 192.168.1.1
# Returns NXDOMAIN (as expected)

CIFS mounts during boot occur before network interfaces are fully initialized. The resolution sequence differs from normal commands:

  1. Early-boot mounts use minimal resolver configuration
  2. mount.cifs may bypass standard glibc resolution
  3. NetworkManager/DHCP may update /etc/resolv.conf too late

Option 1: Force hosts file precedence

# In /etc/nsswitch.conf
hosts: files mdns4_minimal [NOTFOUND=return] dns

Option 2: Network dependency in systemd

# Create /etc/systemd/system/mnt-backuppc.mount.d/network.conf
[Unit]
After=network-online.target
Wants=network-online.target

Option 3: CIFS-specific workaround

//NAS-5h2-20/backuppc /mnt/backuppc cifs _netdev,auto,user=THEUSER,password=THEPASSWORD 0 0

Check resolution timing with:

strace -e trace=open mount -t cifs //NAS-5h2-20/backuppc /mnt/backuppc

Verify DNS resolution paths:

getent hosts NAS-5h2-20
nmcli dev show | grep DNS

For critical systems where hostname resolution absolutely must work, consider a hybrid approach:

#!/bin/bash
if ! mount | grep -q /mnt/backuppc; then
    if ping -c 1 NAS-5h2-20 &>/dev/null; then
        mount /mnt/backuppc
    else
        mount -t cifs //192.168.1.29/backuppc /mnt/backuppc
    fi
fi

When mounting CIFS shares via /etc/fstab, I encountered an interesting anomaly: while IP-based mounts work perfectly, hostname-based entries consistently fail despite proper /etc/hosts configuration. Here's the technical deep dive into this behavior and solutions.

The problematic fstab entry:

//NAS-5h2-20/backuppc  /mnt/backuppc  cifs  auto,user=THEUSER,password=THEPASSWORD,cifsacl,uid=109  0  0

Error output:

mount: wrong fs type, bad option, bad superblock on //NAS-5h1-15/backuppc,
   missing codepage or helper program, or other error
   (for several filesystems (e.g. nfs, cifs) you might
   need a /sbin/mount. helper program)
   In some cases useful info is found in syslog - try
   dmesg | tail or so

# syslog shows:
Unable to determine destination address.

What's particularly puzzling is that these commands work flawlessly:

ping NAS-5h2-20              # Successful resolution
smbclient -L //NAS-5h2-20    # Connects properly
mount -t cifs //192.168.1.29/backuppc /mnt/backuppc -o user=THEUSER,password=THEPASSWORD

The issue stems from how mount.cifs handles name resolution differently than standard system utilities. Key findings:

  • mount.cifs bypasses glibc's name resolution in certain cases
  • It may attempt NetBIOS/WINS resolution before checking /etc/hosts
  • The mount process runs in a different namespace than user-space tools

Option 1: Using _netdev Parameter

Add the _netdev option to ensure networking is available before mount attempts:

//NAS-5h2-20/backuppc  /mnt/backuppc  cifs  _netdev,auto,user=THEUSER,password=THEPASSWORD,cifsacl,uid=109  0  0

Option 2: Implementing a systemd Mount Unit

Create /etc/systemd/system/mnt-backuppc.mount:

[Unit]
Description=CIFS Mount for BackupPC
Requires=network-online.target
After=network-online.service

[Mount]
What=//NAS-5h2-20/backuppc
Where=/mnt/backuppc
Type=cifs
Options=user=THEUSER,password=THEPASSWORD,cifsacl,uid=109

[Install]
WantedBy=multi-user.target

Option 3: Forcing Hosts File Resolution

Modify nsswitch.conf to prioritize files over DNS:

hosts: files mdns4_minimal [NOTFOUND=return] dns

Option 4: Using mDNS (.local domain)

If your NAS supports it, try using the .local domain:

//NAS-5h2-20.local/backuppc  /mnt/backuppc  cifs  auto,user=THEUSER,password=THEPASSWORD,cifsacl,uid=109  0  0

To debug resolution issues:

# Check name resolution order
getent hosts NAS-5h2-20

# Test CIFS connectivity
smbclient -L //NAS-5h2-20 -U THEUSER%THEPASSWORD

# Verbose mount attempt
mount -v -t cifs //NAS-5h2-20/backuppc /mnt/backuppc -o user=THEUSER,password=THEPASSWORD

# Check kernel messages
dmesg | grep CIFS
journalctl -xe

For production environments, consider these best practices:

  • Use static IPs or DHCP reservations for critical mounts
  • Implement proper DNS records rather than /etc/hosts modifications
  • Consider automount solutions for more resilient mounts
  • For temporary solutions, the IP-based approach remains most reliable