Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 24Naam in het boek: Accessing Network Storage

Share folders with NFS and mount them automatically

NFS shares folders over the network between Linux systems. This chapter shows how to set up a simple NFS server, mount a share by hand and through /etc/fstab, and use autofs to mount shares automatically when a user opens them — including the wildcard setup for home folders.

  • 11 Lessen
  • 13 Commando's
  • 1 Labs

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

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

Examendoelen in dit hoofdstuk

  • Netwerkbestandssystemen met NFS koppelen en ontkoppelen
  • autofs instellen

24.1

Understand NFS and its security

An NFS server exports folders; clients mount them like a local disk. RHEL uses NFSv4 by default (one TCP port: 2049).

  • By default (sec=sys) the server trusts the UID and GID the client sends. Users need the same UIDs on all systems (or central users from IdM/LDAP).
  • root_squash (default) maps root on the client to the user nobody, so client root is not root on the share.
  • For real security use Kerberos (sec=krb5p: authentication and encryption).

24.2

Set up an NFS server

Install nfs-utils, write the exports, start the service and open the firewall.

  1. Install and create the folder to share.
    dnf install -y nfs-utils
    mkdir -p /nfsdata /home/ldap
    echo "shared data" > /nfsdata/readme.txt
  2. Describe the exports.
    /etc/exports
    /nfsdata    192.168.122.0/24(rw,sync)
    /home/ldap  192.168.122.0/24(rw,sync,no_root_squash)
  3. Start the server and open the firewall.
    systemctl enable --now nfs-server
    firewall-cmd --add-service={nfs,mountd,rpc-bind} --permanent
    firewall-cmd --reload
  4. Check the exports.
    exportfs -v
    exportfs -ra
Export optionMeaning
rw / roRead-write / read-only
syncWrite to disk before answering (safe)
root_squashClient root → nobody (default)
no_root_squashClient root stays root (only for trusted clients)
all_squashEvery user → nobody

Notitie

mountd and rpc-bind are only needed for NFSv3 and for showmount. Pure NFSv4 needs only the nfs service.

24.3

Mount an NFS share

See what the server offers, then mount it like a disk with type nfs.

dnf install -y nfs-utils
showmount -e server1
Export list for server1:
/home/ldap 192.168.122.0/24
/nfsdata   192.168.122.0/24
mkdir /mnt/data
mount -t nfs server1:/nfsdata /mnt/data
findmnt /mnt/data
umount /mnt/data

Onthoud

With NFSv4 you can also mount the server root and browse: mount server1:/ /mnt shows all exports as folders.

24.4

Mount NFS through fstab

For a share that must always be there, add it to /etc/fstab with _netdev, so it is mounted after the network is up.

/etc/fstab (line)
server1:/nfsdata   /mnt/data   nfs   _netdev,defaults   0 0
systemctl daemon-reload
mount -a
findmnt -t nfs4

Op het werk

Add nofail for shares that are not critical: the system then boots even when the NFS server is down.

24.5

Understand autofs

autofs mounts a share only when someone opens its folder, and unmounts it after some idle minutes. No boot problems when the server is away, and no need to mount many home folders.

  • The master map (/etc/auto.master or files in /etc/auto.master.d/) names a base folder and a map file.
  • The map file lists the subfolders (keys) and where they come from.
dnf install -y autofs
systemctl enable --now autofs

24.6

Define an indirect autofs mount

An indirect map creates subfolders under a base folder: /shares/data is mounted when someone opens it.

  1. Add a line to the master map.
    /etc/auto.master.d/shares.autofs
    /shares   /etc/auto.shares
  2. Create the map: key, mount options, source.
    /etc/auto.shares
    data   -rw   server1:/nfsdata
  3. Restart autofs and open the folder.
    systemctl restart autofs
    ls /shares
    ls /shares/data
    readme.txt
    findmnt /shares/data

Notitie

The folder /shares/data appears only when you open it (ls /shares can be empty before). Do not create it yourself.

24.7

Use wildcards for home folders

With * as the key and & in the source, one line serves every user: /home/ldap/lisa mounts server1:/home/ldap/lisa.

/etc/auto.master.d/home.autofs
/home/ldap   /etc/auto.home
/etc/auto.home
*   -rw   server1:/home/ldap/&
systemctl restart autofs
su - ldapuser1
pwd
/home/ldap/ldapuser1

Examentip

The classic task: "users from the LDAP/NFS server must get their home folder automatically under /home/ldap" — that is exactly this wildcard map. Check with su - USER and findmnt.

24.8

Use a direct autofs mount

A direct map mounts at a full path anywhere in the file system. In the master map the base is /-.

/etc/auto.master.d/direct.autofs
/-   /etc/auto.direct
/etc/auto.direct
/srv/projects   -rw,soft   server1:/nfsdata
systemctl restart autofs
ls /srv/projects

24.9

Remember the main points

The most important facts of this chapter, in short.

  • NFS shares folders over the network. RHEL uses NFSv4 by default (port 2049).
  • Server: install nfs-utils, write the share in /etc/exports, run exportfs -r, systemctl enable --now nfs-server, open the services nfs (and mountd, rpc-bind for showmount).
  • Client: showmount -e SERVER lists shares; mount SERVER:/path /mnt mounts one.
  • At boot: an fstab line with type nfs and the option _netdev.
  • autofs mounts shares only when a user opens the folder and unmounts them later.
  • Master map /etc/auto.master.d/NAME.autofs; the map file lists the keys and shares.
  • An indirect map mounts under one base folder; a wildcard map (* -rw server:/home/&) works for all user folders; a direct map uses full paths (/-).

24.10

Learn the key terms

The words of this chapter in simple English.

TermMeaning
NFSNetwork File System: share folders between Linux systems.
ExportA folder that the NFS server shares.
/etc/exportsThe list of exports and who may use them.
showmountA tool that lists the exports of a server.
_netdevAn fstab option: wait for the network before mounting.
autofsA service that mounts shares when they are used.
Master mapThe autofs file that points to the map files.
Indirect mapA map with keys under one base folder.
Direct mapA map with full mount paths.
Wildcard mapA map with * and & that works for many folders.

24.11

Check what you learned

Think about the answer first, then open it.

1How do you see the shares of server2?
2Write an fstab line that mounts server2:/data on /mnt/data at boot.

server2:/data /mnt/data nfs defaults,_netdev 0 0.

3Which file shares /data with the network 192.168.122.0/24, read-write?

/etc/exports with the line /data 192.168.122.0/24(rw).

4How do you load a changed /etc/exports without a restart?
5Home folders of LDAP users must appear under /home/remote when they are used. Which autofs lines do you write?

/home/remote /etc/auto.remote in a file in /etc/auto.master.d/, and * -rw server:/home/remote/& in /etc/auto.remote.

6What does & mean in an autofs map?

It is replaced by the key (the folder name the user opened).

7Which service must run for autofs?
8Why does ls /home/remote show nothing before a user logs in?

autofs mounts a folder only when it is opened by its name.

Oefenen

Alle commando's in dit hoofdstuk