NFS vs. Samba: Linux File Sharing Explained

Updated

learn about linux file sharing using NFS and SAMBA

Choose on authentication, not throughput. Samba joined to a domain authenticates the user — Kerberos or NTLMv2, per connection. NFS with sec=sys, the default, authenticates the host, and only by source port. If your clients are machines you fully control on a segment you fully control, NFS is simpler and defensible; for anything else, use Samba with domain authentication or NFSv4 with Kerberos.

Do not copy any of these. sudo exportfs -o rw,sync,no_root_squash *(directory_path), /home *(rw,async,no_subtree_check) (which exports /home to every host on the network) and a Samba share containing guest ok = yes alongside writable = yes are each a decision to remove a protection.

The substantive answer to “NFS or Samba” is upstream of all of it: NFSv3 with the default security flavour has no authentication. It trusts whatever user ID the client writes into the packet. That is not a configuration mistake you can avoid — it is the protocol.

What the exports(5) manual actually says about the uid

From exports(5), the nfs-utils manual page, under “User ID Mapping”:

“nfsd bases its access control to files on the server machine on the uid and gid provided in each NFS RPC request.”

Provided by the client. The same page lists the available security flavours as “sys (the default–no cryptographic security), krb5 (authentication only), krb5i (integrity protection), and krb5p (privacy protection)”. With sec=sys — the default — the credential is a plain XDR structure the caller fills in. Here it is, built from the layout in RFC 5531, once as an ordinary user and once as root:

$ python3 authsys.py
AUTH_SYS credential, uid=1000 gid=1000: 0000000100000024000000000000000b776f726b73746174696f6e00000003e8000003e800000001000003e8
AUTH_SYS credential, uid=0    gid=0   : 0000000100000024000000000000000b776f726b73746174696f6e0000000000000000000000000100000000
bytes that differ                     : 6 of 44

Becoming root over NFSv3 is an edit to six bytes of a packet you are already sending. Nothing signs them. This is why root_squash exists, and why turning it off matters so much: exports(5) describes the default as “This mode of operation (called `root squashing’) is the default, and can be turned off with no_root_squash”, and describes the option itself as “Turn off root squashing. This option is mainly useful for diskless clients.”

The one protection left, and how thin it is

With sec=sys the server’s remaining check is the source port. exports(5): “secure This option requires that requests not using gss originate on an Internet port less than IPPORT_RESERVED (1024). This option is on by default.” The whole protection is that binding a low port requires privilege. Run as an unprivileged user:

$ python3 -c 'import socket; ...'   # bind 127.0.0.1 on four ports
port   999: PermissionError: [Errno 13] Permission denied
port  1023: PermissionError: [Errno 13] Permission denied
port  1024: bind OK
port  2049: bind OK

That is real, and it is also the entire fence. It says “a process on that host had root”, not “this user is who they claim”. Anyone with root on any client machine inside your export’s host list clears it. Adding insecure removes even that. If the data matters, the answer is sec=krb5p, or NFSv4 with RPC-with-TLS, which exports(5) now documents via the xprtsec= option — not a longer list of tuning flags.

Three Samba lines to delete

guest ok = yes. smb.conf(5): “If this parameter is yes for a service, then no password is required to connect to the service.” Combined with writable = yes, that is an anonymous read-write share. The same manual notes it “nullifies the benefits of setting restrict anonymous = 2”. map to guest = Bad User has the same effect from the other direction — “user logins with an invalid password are rejected, unless the username does not exist, in which case it is treated as a guest login and mapped into the guest account”.

encrypt passwords = yes. Not wrong, just pointless and misleading: “This parameter has been deprecated since Samba 4.11 and support for plaintext … will be removed in a future Samba release.” It is already the default. Presenting it as a security measure you add implies the alternative is available.

socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192. The manual page carries a Warning box on this exact parameter: “Modern server operating systems are tuned for high network performance in the majority of situations; when you set socket options you are overriding those settings. Linux in particular has an auto-tuning mechanism for buffer sizes that will be disabled if you specify a socket buffer size. This can potentially cripple your TCP/IP stack.”

So which one

Choose on authentication, not throughput. Samba joined to a domain authenticates the user — Kerberos or NTLMv2, per connection. NFS with sec=sys authenticates the host, and only by source port. If your clients are machines you fully control on a segment you fully control, NFS is simpler and that trade is defensible. If any client is a laptop, a container someone else can start, or anything you would not hand a root shell on, you want Samba with domain authentication or NFSv4 with Kerberos. Everything else — rsize, async, socket buffers — is second-order, and async in particular is documented as a correctness trade, not a free win: it “allows the NFS server to violate the NFS protocol and reply to requests before any changes made by that request have been committed to stable storage”.

What was not tested

No NFS export was created or mounted and no Samba share was created or connected. The two code blocks above ran on macOS 26.6 arm64 and prove exactly what they show — the shape of an AUTH_SYS credential, and that ports below 1024 need privilege — not the behaviour of any particular server. Every statement about no_root_squash, secure, async, guest ok, map to guest, encrypt passwords and socket options is a quotation from the manual page named beside it, and nfs-vs-samba-file-sharing.sh re-fetches both pages and fails if the wording has changed; it also rebuilds both credentials byte for byte and repeats the port test. Performance numbers are absent because none were measured.

For the mode bits an export hands out, see the chmod permissions calculator; for auditing what a squashed uid can actually reach, see finding files owned by a user.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.