Abstract
| Goal | Access network-attached storage with the NFS protocol. |
| Objectives |
|
| Sections |
|
| Lab |
Access Network-Attached Storage |
Identify NFS export information, create a directory to use as a mount point, mount an NFS export with the mount command or by configuring the /etc/fstab file, and unmount an NFS export with the umount command.
The Network File System (NFS) is an internet standard protocol that Linux, UNIX, and similar operating systems use as their native network file system. NFS is an open standard that supports native Linux permissions and file-system attributes.
By default, Red Hat Enterprise Linux 9 uses NFS version 4.2. RHEL fully supports both NFSv3 and NFSv4 protocols. NFSv3 might use either a TCP or a UDP transport protocol, but NFSv4 supports only TCP connections.
NFS servers export directories. NFS clients mount exported directories to an existing local mount point directory. NFS clients can mount exported directories in multiple ways:
Manually by using the mount command.
Persistently at boot by configuring entries in the /etc/fstab file.
On demand by configuring an automounter method.
The automounter methods, which include the autofs service and the systemd.automount facility, are discussed in the Automount Network-Attached Storage section.
You must install the nfs-utils package to obtain the client tools for manually mounting, or for automounting, to obtain exported NFS directories.
[root@host ~]# dnf install nfs-utilsRHEL also supports mounting shared directories from Microsoft Windows systems by using the same methods as for the NFS protocol, by using either the Server Message Block (SMB) or the Common Internet File System (CIFS) protocols. Mounting options are protocol-specific, and depend on your Windows Server or Samba Server configuration.
The NFS protocol changed significantly between NFSv3 and NFSv4. The method to query a server to view the available exports is different for each protocol version.
NFSv3 used the RPC protocol, which requires a file server that supports NFSv3 connections to run the rpcbind service.
An NFSv3 client connects to the rpcbind service at port 111 on the server to request NFS service.
The server responds with the current port for the NFS service.
Use the showmount command to query the available exports on an RPC-based NFSv3 server.
[root@host ~]# showmount --exports server
Export list for server
/shares/test1
/shares/test2The NFSv4 protocol eliminated the use of the legacy RPC protocol for NFS transactions.
Use of the showmount command on a server that supports only NFSv4 times out without receiving a response, because the rpcbind service is not running on the server.
However, querying an NFSv4 server is simpler than querying an NFSv3 server.
NFSv4 introduced an export tree that contains all of the paths for the server's exported directories.
To view all of the exported directories, mount the root (/) of the server's export tree.
Mounting the export tree's root provides browseable paths for all exported directories, as children of the tree's root directory, but does not mount ("bind") any of the exported directories.
[root@host ~]#mkdir[root@host ~]#/mountpointmount[root@host ~]#server://mountpointls/mountpoint
To mount an NFSv4 export when browsing the mounted export tree, change directory to an exported directory path.
Alternatively, use the mount command with an exported directory's full path name to mount a single exported directory.
Exported directories that use Kerberos security do not allow mounting or accessing a directory when browsing an export tree, even though you can view the export's path name.
Mounting Kerberos-protected shares requires additional server configuration and the use of Kerberos user credentials, which are discussed in the Red Hat Security: Identity Management and Active Directory Integration (RH362) training course.
After you identify the NFS export to mount, create a local mount point if it does not yet exist.
Although the /mnt directory is available for use as a temporary mount point, recommended practice is not to use /mnt for long-term or persistent mounting.
[root@host ~]# mkdir /mountpointAs with local volume file systems, mount the NFS export to access its contents. NFS shares can be mounted temporarily or permanently, only by a privileged user.
[root@host ~]# mount -t nfs -o rw,sync server:/export /mountpointThe -t nfs option specifies the NFS file-system type.
However, when the mount command detects the server:/export syntax, the command defaults to the NFS type.
With the -o flag, you can add a list of comma-separated options to the mount command.
In the example, the rw option specifies that the exported file system is mounted with read/write access.
The sync option specifies synchronous transactions to the exported file system.
This method is strongly recommended for all production network mounts where transactions must be completed or else return as failed.
Using a manual mount command is not persistent.
When the system reboots, that NFS export is not still mounted.
Manual mounts are useful for providing temporary access to an exported directory, or for test mounting an NFS export before persistently mounting it.
To persistently mount an NFS export, edit the /etc/fstab file, and add the mount entry with similar syntax to manual mounting.
[root@host ~]#vim /etc/fstab...server:/export/mountpointnfs rw 0 0
Then, you can mount the NFS export by using only the mount point.
The mount command obtains the NFS server and mount options from the matching entry in the /etc/fstab file.
[root@host ~]# mount /mountpointAs a privileged user, unmount an NFS export with the umount command.
Unmounting a share does not remove its entry in the /etc/fstab file, if that file exists.
Entries in the /etc/fstab file are persistent and are remounted during boot.
[root@host ~]# umount /mountpointA mounted directory can sometimes fail to unmount, and returns an error that the device is busy. The device is busy because either an application is keeping a file open within the file system, or some user's shell has a working directory in the mounted file-system's root directory or below it.
To resolve the error, check your own active shell windows, and use the cd command to leave the mounted file system.
If subsequent attempts to unmount the file system still fail, then use the lsof (list open files) command to query the mount point.
The lsof command returns a list of open file names and the process which is keeping the file open.
[root@host ~]# lsof /mountpoint
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
program 5534 user txt REG 252.4 910704 128 /home/user/programWith this information, gracefully close any processes that are using files on this file system, and retry the unmount.
In critical scenarios only, when an application cannot be closed gracefully, kill the process to close the file.
Alternatively, use the umount -f option to force the unmount, which can cause loss of unwritten data for all open files.