Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 108Network · Storage

How to share a folder with NFS

Several Linux servers need the same files: uploaded images for all web servers, a shared backup target, home folders. NFS shares a folder over the network. This how-to sets up a safe NFSv4 export for one network, mounts it on a client (now and at boot), and fixes the usual "permission denied" — which is almost always about user IDs or export options.

  • 5 Lessen
  • 14 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

108.1

The quick answer

On the server:

dnf -y install nfs-utils
mkdir -p /srv/share && chown 2001:2001 /srv/share
echo '/srv/share 10.0.1.0/24(rw,sync,root_squash)' > /etc/exports.d/share.exports
systemctl enable --now nfs-server
exportfs -rav
firewall-cmd --permanent --add-service=nfs && firewall-cmd --reload

On the client:

dnf -y install nfs-utils
mount -t nfs4 nfs1.example.com:/srv/share /mnt/share

On Ubuntu: nfs-kernel-server (server) and nfs-common (client).

108.2

Step by step

  1. Export only to the clients that need it — never *:
OptionMeans
10.0.1.0/24Only this network may mount.
rw / roRead-write / read-only.
syncWrite to disk before answering (safe; async is faster but can lose data).
root_squashRoot on the client becomes nobody on the server (default — keep it).
all_squash,anonuid=2001,anongid=2001Every client user maps to one owner — simple for shared app data.
  1. Apply and check the exports:
exportfs -rav
exporting 10.0.1.0/24:/srv/share
exportfs -v
  1. User IDs decide access — NFS checks the numbers (UID/GID), not the names. User app with UID 2001 on the server and UID 1005 on the client are different users for NFS. Use the same UIDs everywhere (or all_squash + anonuid, or central users via IdM/LDAP).
  2. Mount at boot on the client — _netdev waits for the network, nofail keeps the boot going if the server is down:
/etc/fstab (line)
nfs1.example.com:/srv/share  /mnt/share  nfs4  defaults,_netdev,nofail  0 0
mkdir -p /mnt/share
systemctl daemon-reload
mount -a

For shares that are not always needed, autofs mounts them only on access.

  1. SELinux on the client — web servers reading from NFS need:
setsebool -P httpd_use_nfs on

108.3

Check that it worked

findmnt /mnt/share
TARGET     SOURCE                        FSTYPE OPTIONS
/mnt/share nfs1.example.com:/srv/share   nfs4   rw,relatime,vers=4.2,...
sudo -u app touch /mnt/share/test && ls -ln /mnt/share/test

The share is mounted with NFS 4.x, the app user can write, and the file has the expected UID on the server.

108.4

Common problems

ProblemCauseFix
mount.nfs4: access denied by serverClient IP not in the export, or no exportfs -r.Fix /etc/exports.d/…; exportfs -rav.
Permission denied when writingUID/GID differ, or root_squash for root.Same UIDs; all_squash + anonuid; do not write as root.
Files owned by nobodyUID mapping / idmapd domain mismatch.Same Domain in /etc/idmapd.conf on both sides; same UIDs.
mount.nfs4: Connection timed outFirewall (2049/tcp) or server down.firewall-cmd --add-service=nfs; nc -zv nfs1 2049.
Boot hangs on the NFS mountNo _netdev/nofail.Add them, or use autofs.
showmount -e does not workNFSv4-only server (no rpcbind/mountd).Normal — mount the path directly.

108.5

Go further

1Why can user app write on one client but not on another?

NFS checks UID/GID numbers; app probably has a different UID on the other client.

2What does root_squash do?

Root on the client is mapped to nobody on the server, so client root can not act as server root.

Alle commando's in dit hoofdstuk