Skip to content
Tutorials Center
My learning

Topics

Lesson 90 of 95 ยท System Administration

File Sharing: NFS, Samba and rsync Servers

Share files between Linux hosts and with Windows: export directories over NFS, publish Samba shares, and run rsync over SSH or as a daemon for backups and mirrors.

A single machine's disk is rarely enough. Home directories should follow you from one workstation to another, a build server wants the same files as the web servers, a Windows laptop needs to open a folder on the Linux box, and backups have to travel to a second machine. Linux offers a tool for each of these jobs, and picking the right one saves a lot of trouble.

This lesson compares the three classics: NFS (mount a remote directory as if it were local, Linux and Unix clients), Samba (speak the SMB protocol that Windows and macOS use) and rsync (copy only what changed, locally or over SSH). The sandbox has no network, no root and no rsync binary, so server steps are read-only; the runnable examples parse the real configuration formats and rebuild the "compare two trees and copy the differences" idea in plain shell. It builds on Users, Groups and sudo, Permissions, SSH, scp and Working Remotely and Archives.

Choosing a tool

Need Tool Notes
Linux clients share live files NFS Transparent mounts, POSIX permissions, fast on a trusted LAN
Windows/macOS clients open a share Samba (SMB) Users and passwords, works with Windows ACLs
Copy or back up files, one way rsync Sends only changes; resumes; over SSH
Occasional single files scp / sftp See the SSH lesson
flowchart LR
  subgraph server["File server"]
    D["/srv/shared"]
  end
  C1["Linux client<br/>mount -t nfs"] -->|NFS| D
  C2["Windows laptop<br/>\\\\server\\shared"] -->|SMB / Samba| D
  B["Backup host<br/>rsync -a"] -->|SSH| D

One directory on the server can be reached live over NFS and SMB, and copied over SSH with rsync.

NFS: mounting a remote directory

With NFS the server exports directories and clients mount them. Version 4 (NFSv4) needs only TCP port 2049 and is the version to use today.

Server (Debian/Ubuntu package nfs-kernel-server, RHEL package nfs-utils):

CONSOLE
$ sudo apt install nfs-kernel-server
$ sudo mkdir -p /srv/shared
$ sudo chown nobody:nogroup /srv/shared

Exports are listed in /etc/exports, one directory per line, followed by clients and their options:

TEXT
# directory        client(options) ...
/srv/shared        192.168.1.0/24(rw,sync,no_subtree_check)
/srv/readonly      192.168.1.10(ro,sync,no_subtree_check) 192.168.1.11(ro)
Option Meaning
rw / ro Read-write or read-only
sync Reply only after data is safely written (the safe default)
async Reply earlier; faster but data can be lost in a crash
root_squash Map the client's root to nobody (default; keep it)
no_root_squash Client root is root on the server: dangerous
no_subtree_check Avoids a slow, error-prone check; recommended

Apply and check the exports:

CONSOLE
$ sudo exportfs -ra            # re-read /etc/exports
$ sudo exportfs -v             # list active exports with their effective options
$ sudo ufw allow from 192.168.1.0/24 to any port nfs

Client (package nfs-common or nfs-utils):

CONSOLE
$ showmount -e fileserver                     # what does the server export?
$ sudo mkdir -p /mnt/shared
$ sudo mount -t nfs4 fileserver:/srv/shared /mnt/shared
$ df -h /mnt/shared                           # it looks like any other filesystem

To mount at boot, add a line to /etc/fstab. The _netdev and nofail options stop a missing server from hanging the boot:

TEXT
fileserver:/srv/shared  /mnt/shared  nfs4  defaults,_netdev,nofail  0  0

A classic NFS surprise: permissions use numeric user IDs. If ada is UID 1000 on the client and UID 1001 on the server, files appear to belong to someone else. Keep UIDs identical across machines (or use central login such as LDAP). You can check an exports file with ordinary tools before reloading it:

Bash Checking an /etc/exports file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

That last check catches a real and nasty mistake: 192.168.1.0/24 (rw) with a space means "that network gets default options, and everyone gets read-write".

Samba: shares for Windows and macOS

Samba implements the SMB/CIFS protocol. One service (smbd) serves files, and the configuration is the INI-style /etc/samba/smb.conf.

CONSOLE
$ sudo apt install samba
$ sudo mkdir -p /srv/samba/team
$ sudo groupadd team && sudo chgrp team /srv/samba/team
$ sudo chmod 2770 /srv/samba/team          # setgid: new files keep the group
INI
[global]
   workgroup = WORKGROUP
   server role = standalone server
   map to guest = never
   server min protocol = SMB2

[team]
   comment = Team files
   path = /srv/samba/team
   read only = no
   valid users = @team
   create mask = 0660
   directory mask = 2770

Samba keeps its own password database, separate from the Linux one, so each user must be added to it:

CONSOLE
$ sudo smbpasswd -a ada            # ada must already exist as a Linux user
$ testparm                         # validate smb.conf and print the effective settings
$ sudo systemctl restart smbd
$ smbclient -L //fileserver -U ada # list shares from a client
$ sudo mount -t cifs //fileserver/team /mnt/team -o username=ada,uid=1000

Windows users reach it with \\fileserver\team in Explorer. Open ports 445 (SMB) only to your LAN. A share has two layers of permission, and the stricter one wins: the Samba settings (valid users, read only) and the Linux permissions on the directory. When a user "can see the share but not write", check the Linux side first with ls -ld /srv/samba/team and id ada.

The smb.conf format is easy to inspect with awk. This script lists every share and its path, and warns about shares that allow guests:

Bash Listing the shares in an smb.conf

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The special [homes] share automatically exposes each user's home directory under their own name.

rsync: copy only what changed

rsync compares source and destination and transfers just the differences, so the second run of a backup is fast. It works locally, over SSH, or against an rsync daemon. The options you will use most:

Option Meaning
-a Archive mode: recurse and keep permissions, times, symlinks, owner
-v / -h / --progress Verbose, human sizes, progress
-z Compress during transfer (useful on slow links)
-n or --dry-run Show what would happen; change nothing
--delete Remove files at the destination that no longer exist at the source
--exclude 'PATTERN' Skip matching files
-e ssh Use SSH as the transport (the default for remote paths)
--partial Keep half-transferred files so a retry resumes
CONSOLE
$ rsync -avh --dry-run  ~/project/ backup:/srv/backups/project/   # preview first
$ rsync -avh --delete   ~/project/ backup:/srv/backups/project/   # mirror
$ rsync -avz -e 'ssh -p 2222' ~/project/ ada@host:/srv/project/
$ rsync -a --exclude 'node_modules' --exclude '*.log' src/ dest/

A trailing slash on the source changes the meaning. rsync -a src/ dest/ copies the contents of src into dest; rsync -a src dest/ creates dest/src. Combined with --delete a wrong slash can remove files, so run with --dry-run first.

For a "pull" server that others can fetch from without a shell account, run the rsync daemon (port 873) with /etc/rsyncd.conf:

INI
[mirror]
    path = /srv/mirror
    comment = Public mirror
    read only = yes
    uid = nobody
    gid = nogroup

Clients use the double-colon or URL form: rsync -av rsync://host/mirror/ ./local-mirror/. For anything private, prefer rsync over SSH with keys, which gives authentication and encryption for free; combine it with a scheduled job (see Packages, Services and systemd for timers and cron).

The sandbox has no rsync, but the idea is simple: list the files on both sides, copy the new and changed ones, and with --delete remove the extras. Here is a "dry run" in shell:

Bash What would rsync do? A dry run in shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Real rsync decides "changed" by size and modification time (fast) or checksums (-c, slow but exact), and transfers only the changed blocks of large files. The shape of the logic is the same.

Which protocol, when?

  • NFS for Linux-to-Linux shared storage inside a trusted network. It has no user authentication of its own beyond host and UID trust, so never expose it to the internet.
  • Samba whenever Windows or macOS users are involved. Use named users, group-based valid users, and restrict ports to the LAN.
  • rsync over SSH for backups and deployments. It is encrypted, scriptable and resumable.
  • For anything over the internet, tunnel through SSH or a VPN rather than opening NFS or SMB ports.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which line in /etc/exports shares /srv/data read-only with the 10.0.0.0/24 network?

    The client and its options must be written together with no space: client(options). A space before the parenthesis applies those options to everyone.
  2. 2. Why do files on an NFS mount sometimes appear to belong to the wrong user?

    Only the numbers travel over the network. Keep UIDs and GIDs consistent across hosts or use central authentication.
  3. 3. What is the difference between rsync -a src/ dest/ and rsync -a src dest/?

    The trailing slash on the source means 'the contents of'. Without it the directory itself is copied.
  4. 4. A Windows user can open a Samba share but cannot save files. What should you check first?

    Both layers must allow writing: the share settings in smb.conf and the filesystem permissions of the Linux user who is connected.
  5. 5. Which rsync option lets you preview a sync without changing anything?

    --dry-run lists what would be transferred or deleted. Always use it before --delete.

Summary

  • NFS exports directories (/etc/exports, exportfs -ra) to Linux clients that mount -t nfs4 server:/path /mnt; permissions follow numeric UIDs, and root_squash should stay on.
  • Samba (smb.conf, testparm, smbpasswd -a) shares folders with Windows and macOS; the stricter of share settings and Linux permissions wins.
  • rsync transfers only differences: use -a, test with --dry-run, watch the trailing slash and be careful with --delete.
  • rsync over SSH is the default for backups; the rsync daemon (port 873) suits public mirrors.
  • Keep NFS and SMB inside trusted networks; reach remote sites through SSH or a VPN.

Next: Compiling Software from Source and Building Packages