NFS¶
Practical 6.3 nfs-annex.pdf · PDF
Samba asked every person for a name and a password. Now imagine fifty Linux machines in a company, all needing the same project folder the moment they start, with nobody at the keyboard. Who types fifty passwords at 8:00 in the morning?
Nobody. There is another door into the building, built for machines instead of people. It is fast, it is simple, and it trusts in a way that will surprise you: on this page you will make the server write "joan" on a file that Joan never touched.
What you will learn
- Explain export and mount, and write a correct line in
/etc/exports - Mount an NFS export on a Linux client, by hand and at boot with
/etc/fstab - Explain that NFS identifies people by UID number and trusts the client machine, and what root squash does
- Find the mistake in an exports line with one wrong space
- Choose between SMB and NFS for a given situation
- Why one space in a config file can open a folder to the whole world
The staff door¶
NFS (Network File System) is a protocol that makes a directory of another machine appear inside your own directory tree, so programs do not notice the difference.
It was created by Sun Microsystems in 1984 for its Unix workstations. The protocol was published openly (version 2 as RFC 1094, in 1989), so every Unix copied it. Today's version 4 uses a single port: TCP 2049.
Remember the building with four doors from the Samba file server page. NFS is a staff door at the back. There is no reception. The guard only looks at which van arrives (the address of the client machine), and then believes the badge number each person shows. No password.
flowchart LR
subgraph SAMBA ["Front door: Samba"]
direction TB
P["<b>a person</b>"] --> R["<b>reception</b><br>user + password"] --> D1["the files"]
end
subgraph NFS ["Staff door: NFS"]
direction TB
V["<b>a machine</b><br>shows a badge number (UID)"] --> G["<b>guard</b><br>is this van on my list?"] --> D2["the files"]
end
Two verbs:
- The server exports a directory: it offers it to certain machines, with one line in
/etc/exports. - The client mounts it on a folder of its own tree: exactly the
mountfrom Unit 4, withserver:/pathin the place of/dev/sdb1.
The export line¶
| Part | Meaning |
|---|---|
/srv/nfs/projects |
The directory the server offers |
192.168.122.0/24 |
Who may mount it: here, every machine of our virtual network |
rw / ro |
Read and write, or read only |
sync |
Answer "saved" only when the data is really on the disk |
no_subtree_check |
The normal choice today: it turns off an old check that caused problems |
No space between the network and the bracket. You will see why further down.
Export and mount¶
On the server:
sudo apt install nfs-kernel-server
sudo mkdir -p /srv/nfs/projects
echo "/srv/nfs/projects 192.168.122.0/24(rw,sync,no_subtree_check)" \
| sudo tee -a /etc/exports
sudo exportfs -ra
sudo exportfs -v
/srv/nfs/projects
192.168.122.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,root_squash,no_all_squash)
exportfs -ra re-reads /etc/exports; exportfs -v shows what is really exported. There are more options than you wrote: the defaults. Keep two of them in mind for later: sec=sys and root_squash.
On the client (deb-lab):
sudo apt install nfs-common
showmount -e ubu-srv
sudo mkdir -p /mnt/projects
sudo mount ubu-srv:/srv/nfs/projects /mnt/projects
df -hT /mnt/projects
showmount -e lists what the server exports; df -hT shows the type nfs4. Now try to write:
Both fail with Permission denied, even with sudo. The export says rw. Why? Make a guess before reading on.
Where people get stuck
mount: bad optionor wrong fs type →nfs-commonis missing on the client.mount.nfs: access denied by server→ the network in/etc/exportsis wrong, or you forgotexportfs -ra.- The mount hangs →
ping ubu-srvfirst; a firewall on the server needs2049/tcpopen.
Numbers, not names¶
First, the two doors again. rw in the export is door 3: the machine may write. Door 4 is still the file system: /srv/nfs/projects belongs to root, mode 755. You are not its owner, so you cannot write. The stricter door wins, as on the Share permissions page.
But how does the server know who "you" are, with no password? The client sends a number: your UID (user ID). That is what sec=sys means.
| UID | On deb-lab it is... |
On ubu-srv it is... |
|---|---|---|
| 1000 | you (the user of the installation) | you (the user of the installation) |
| 1003 | maybe nobody, maybe somebody else | joan, if he was the third user created |
| 0 | root |
root... but see below |
Names are never sent. If number 1003 arrives, the server writes "joan" on the file, whoever sent it. Give the folder to your own number on the server (sudo chown 1000 /srv/nfs/projects) and touch works from the client. Then compare:
ls -ln /mnt/projects # numbers: the same on both machines
ls -l /mnt/projects # names: each machine shows its own name for that number
The same file can show two different owner names, one per machine.
Root squash¶
And root, UID 0? Whoever is root on the client would be root on the server's files. So the server changes 0 into the UID of a user with no rights, nobody (65534). This is root squash, and it is on by default. That is why sudo touch failed too.
The badge experiment¶
On the server, make a folder only Joan can enter (mkdir -m 700, chown joan) and read Joan's number with id -u joan. On the client, create a user with the same number:
On the server, sudo ls -l /srv/nfs/projects/joan-only shows hello.txt owned by joan. Joan was never there. The server believed the badge number.
Careful: NFS trusts machines, not people
Samba trusts a person who proves a password. NFS (in this default mode) trusts a machine and believes whatever number it sends. Anyone who is root on a client can become any user. So you only export to machines you administer yourself. That is not "NFS is insecure": inside your own network, trusting your own machines is exactly what you want.
How do real companies keep the numbers equal on every machine? One central list of users for all machines: a directory, a domain. And for networks you do not control, NFS has a mode with real proof of identity (Kerberos). Both come back later in this unit.
Mount it at boot¶
What is mounted by hand is gone after a reboot. Add one line at the end of /etc/fstab on the client:
_netdev= "this needs the network: wait for it".nofail= "if the server is off, boot anyway". Without it, a client can stop at boot because a server is down: the classic way to break twenty PCs with one line.
Test before rebooting:
If mount -a complains, the boot would too: fix it first. findmnt should show the source ubu-srv:/srv/nfs/projects and type nfs4, also after a reboot.
The space trap¶
A read-only export for documents. Look at these two lines:
/srv/nfs/docs 192.168.122.0/24(ro,sync,no_subtree_check)
/srv/nfs/docs 192.168.122.0/24 (ro,sync,no_subtree_check)
One space of difference. With the first one, writing from the client gives Read-only file system, a different message from Permission denied: this time the export said no. With the second one:
exportfs: No options for /srv/nfs/docs 192.168.122.0/24: suggest 192.168.122.0/24(sync) to avoid warning
exportfs: No host name given with /srv/nfs/docs (ro,sync,no_subtree_check), suggest *(ro,sync,no_subtree_check) to avoid warning
$ sudo exportfs -v
/srv/nfs/docs 192.168.122.0/24(sync,...,ro,root_squash,no_all_squash)
/srv/nfs/docs <world>(sync,...,ro,root_squash,no_all_squash)
With the space there are two rules: our network with the default options, and <world> (every machine that can reach the server) with the options in the bracket. Had the bracket said rw, the whole world could write. And exportfs only printed a warning.
SMB or NFS?¶
| SMB (Samba) | NFS | |
|---|---|---|
| Born | IBM, 1983; the protocol of Windows | Sun Microsystems, 1984; the protocol of Unix |
| Port | TCP 445 | TCP 2049 |
| Who is identified | A person: user and password | A machine (address) + a UID number |
| Clients | Windows, Linux, macOS: built in | Linux and Unix: built in. Not there by default on Windows |
| Configuration | /etc/samba/smb.conf |
/etc/exports |
| Use it when | People on mixed PCs open folders | Linux machines you administer share a tree, mounted at boot |
The question that decides: who are the clients, and who must be identified, people or machines? Tramuntana's answer is both: Samba for the people's department folders, NFS for the project tree of the Linux machines.
Key takeaways¶
- NFS (Sun, 1984, TCP 2049) lets a server export a directory that clients mount into their own tree.
- An export is one line in
/etc/exports: directory, allowed network, options in brackets with no space.exportfs -raapplies it,exportfs -vshows it. - By default NFS trusts the client machine and the UID number it sends; names are never sent. Root squash turns the client's root into
nobody. rwin the export only lets the machine try: the file system still decides for each UID.- At boot, mount it in
/etc/fstabwith_netdev,nofail, and test withmount -abefore rebooting. - SMB identifies people on mixed PCs; NFS shares a tree between Linux machines you administer.
Quick check¶
1. In its default set-up, how does an NFS server know which user is writing a file?
- By the UID number the client sends
- By the password the user types
- By the user name the client sends
- By the user's public SSH key
With sec=sys, the client sends a UID number and the server believes it. Names are never sent, and there is no password.
2. What does root squash do?
- It treats the client's root as the user nobody
- It deletes the files created by root
- It asks root for a password before writing
- It blocks the client machine completely
Without it, root on any client would be root on the server's files. Root squash turns UID 0 into nobody (65534), and it is on by default.
3. An export reads /srv/docs 10.0.0.0/24 (rw), with a space before the bracket. Who can write?
- Every machine that can reach the server
- Only the machines in 10.0.0.0/24
- Nobody, because the line is invalid
- Only the root user of the server
The space splits it into two rules: the network with default options (read only), and <world> with (rw). exportfs only prints a warning.
4. What is nofail for in an /etc/fstab line for a network mount?
- The client still boots if the server is off
- It makes the mount faster
- It mounts the share read only
- It hides errors from the log
Without nofail, a dead server can stop the client at boot. _netdev adds "wait for the network first".
5. The export says rw, yet a normal user on the client gets Permission denied. Why?
- The directory's file-system permissions do not let that UID write
- The client forgot to install nfs-common
- NFS never allows writing from a client
- The user needs a Samba password first
rw only lets the machine write. The file system is the second door: the folder belongs to root with mode 755, so another UID cannot write. The stricter door wins.
6. Writing to a mounted export gives Read-only file system. Which door said no?
- The export, because it says ro
- The file system permissions of the folder
- Root squash on the server
- The firewall on port 2049
Read-only file system comes from the export (ro). A refusal from the file system permissions says Permission denied instead.
7. Tramuntana has Windows laptops for the designers and Linux build machines for the developers. Select the two that are true.
- Samba fits the department folders opened by people on mixed PCs
- NFS fits a project tree mounted at boot on the Linux machines
- NFS is the better choice for the Windows laptops
- Samba cannot be used at all by Linux clients
Decide by who is identified: people with passwords on mixed PCs (SMB), or machines you administer (NFS). Windows has no NFS client by default; Linux speaks both.
Files are shared. One more thing in every office is shared, and it always causes a queue. Next: Print server.