Using NFS on AlmaLinux 9 to Share Files with Remote Systems

Posted: 2024-02-14

AlmaLinux 9 provides two mechanisms for sharing files and folders with other systems on a network. One approach is to use a technology called Samba. Samba is based on Microsoft Windows Folder Sharing and allows Linux systems to make folders accessible to Windows systems and access Windows-based folder shares from Linux. This approach can also be used to share folders between other Linux and UNIX-based systems if they have Samba support installed and configured. This is the most popular approach to sharing folders in heterogeneous network environments. Folder sharing using Samba is covered in Sharing Files between AlmaLinux 9 and Windows with Samba.

Another option, explicitly targeted at sharing folders between Linux and UNIX-based systems, uses Network File System (NFS). NFS allows the file system on one Linux computer to be accessed over a network connection by another Linux or UNIX system. NFS was originally developed by Sun Microsystems (now part of Oracle Corporation) in the 1980s and remains the standard mechanism for sharing remote Linux/UNIX file systems.

NFS is very different from the Windows SMB resource-sharing technology used by Samba. This chapter will look at the network-based sharing of folders between AlmaLinux 9 and other UNIX/Linux-based systems using NFS.

Ensuring NFS Services are running on AlmaLinux 9

The first task is to verify that the NFS services are installed and running on your AlmaLinux 9 system. This can be achieved from the command line or the Cockpit interface.

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

Begin by installing the NFS service by running the following command from a terminal window:

# dnf install rpcbind nfs-utilsCode language: plaintext (plaintext)

Next, configure these services so that they automatically start at boot time:

# systemctl enable rpcbind nfs-serverCode language: plaintext (plaintext)

Once the services have been enabled, start them as follows:

# systemctl start rpcbind nfs-serverCode language: plaintext (plaintext)

Configuring the Firewall to Allow NFS Traffic

Next, the firewall needs to be configured to allow NFS traffic. To achieve this, run the following firewall-cmd commands where <zone> is replaced by the appropriate zone for your firewall and system configuration:

# firewall-cmd --zone=<zone> --permanent --add-service=mountd
# firewall-cmd --zone=<zone> --permanent --add-service=nfs
# firewall-cmd --zone=<zone> --permanent --add-service=rpc-bind
# firewall-cmd --reload 
Code language: plaintext (plaintext)

Specifying the Folders to be Shared

Now that NFS is running and the firewall has been configured, we need to specify which parts of the AlmaLinux 9 file system may be accessed by remote Linux or UNIX systems. These settings can be declared in the /etc/exports file, which must be modified to export the directories for remote access via NFS. The syntax for an export line in this file is as follows:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

<export> <host1>(<options>) <host2>(<options>)...Code language: plaintext (plaintext)

In the above line, <export> is replaced by the directory to be exported, <host1> is the name or IP address of the system to which access is being granted, and <options> represents the restrictions that are to be imposed on that access (read-only, read-write, etc.). Multiple host and options entries may be placed on the same line if required. For example, the following line grants read-only permission to the /datafiles directory to a host with the IP address 192.168.2.38:

/datafiles 192.168.2.38(ro)Code language: plaintext (plaintext)

The use of wildcards is permitted to apply an export to multiple hosts. For example, the following line permits read-write access to /home/demo to all external hosts:

/home/demo *(rw)Code language: plaintext (plaintext)

For this chapter, we will configure the /etc/exports file as follows:

/tmp *(rw,sync)
/vol1 192.168.86.42(ro,sync)Code language: plaintext (plaintext)

Once configured, the table of exported file systems maintained by the NFS server needs to be updated with the latest /etc/exports settings using the exportfs command as follows:

# exportfs -aCode language: plaintext (plaintext)

It is also possible to view the current share settings from the command line using the exportfs tool:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

# exportfsCode language: plaintext (plaintext)

The above command will generate the following output:

/vol1 192.168.86.42
/tmp <world>Code language: plaintext (plaintext)

Accessing Shared Folders

The shared folders may be accessed from a client system by mounting them manually from the command line. However, before attempting to mount a remote NFS folder, the package must first be installed on the client system:

# dnf install nfs-utilsCode language: plaintext (plaintext)

To mount a remote folder from the command line, open a terminal window and create a directory where you would like the remote shared folder to be mounted:

$ mkdir /home/demo/tmpCode language: plaintext (plaintext)

Using NFS on AlmaLinux 9 to Share Files with Remote Systems Next, enter the command to mount the remote folder using either the IP address or hostname of the remote NFS server, for example:

$ sudo mount -t nfs 192.168.86.24:/tmp /home/demo/tmpCode language: plaintext (plaintext)

The remote /tmp folder will then be mounted on the local system. Once mounted, the /home/ demo/tmp folder will contain the remote folder and all its contents.

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

Options may also be specified when mounting a remote NFS filesystem. The following command, for example, mounts the same folder but configures it to be read-only:

$ sudo mount -t nfs -o ro 192.168.86.24:/tmp /home/demo/tmpCode language: plaintext (plaintext)

Mounting an NFS Filesystem on System Startup

It is also possible to configure an AlmaLinux 9 system to automatically mount a remote file system each time it starts up by editing the /etc/fstab file. When loaded into an editor, it will likely resemble the following:

/dev/mapper/almalinux-root /           xfs     defaults        0 0
UUID=bc3e41a5-24ff-47c7    /boot       xfs     defaults        0 0
UUID=78B1-8B87             /boot/efi    vfat    umask=0077,shortname=winnt 0 2
/dev/mapper/almalinux-home /home       xfs     defaults        0 0
/dev/mapper/almalinux-swap noneCode language: plaintext (plaintext)

To mount, for example, a folder with the path /tmp, which resides on a system with the IP address 192.168.86.24 in the local folder with the path /home/demo/tmp (note that this folder must already exist), add the following line to the /etc/fstab file:

192.168.86.24:/tmp /home/demo/tmp nfs rw 0 0Code language: plaintext (plaintext)

Next time the system reboots, the /tmp folder on the remote system will be mounted on the local /home/demo/tmp mount point. All the files in the remote folder can then be accessed as if they reside on the local hard disk drive.

Unmounting an NFS Mount Point

Once a remote file system is mounted using NFS, it can be unmounted using the umount command with the local mount point as the command-line argument. The following command, for example, will unmount our example filesystem mount point:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

$ sudo umount /home/demo/tmpCode language: plaintext (plaintext)

Accessing NFS Filesystems in Cockpit

In addition to mounting a remote NFS file system on a client using the command line, it is also possible to perform mount operations from within the Cockpit web interface. Assuming that Cockpit has been installed and configured on the client system, log into the Cockpit interface from within a web browser and select the Storage option from the left-hand navigation panel. If the Storage option is not listed, the cockpit-storaged package will need to be installed:

# dnf install cockpit-storaged
# systemctl restart cockpit.socketCode language: plaintext (plaintext)

Once the Cockpit service has restarted, log back into the Cockpit interface, at which point the Storage option should now be visible.

Once selected, the main storage page will include a section listing any currently mounted NFS file systems, as illustrated in Figure 18-1:

Figure 18-1

To mount a remote filesystem, click on the ‘+’ button highlighted above and enter information about the remote NFS server and file system share together with the local mount point and any necessary options into the resulting dialog before clicking on the Add button:

Figure 18-2

To modify, unmount, or remove an NFS filesystem share, select the corresponding mount in the NFS Mounts list (Figure 18-1 above) to display the page shown in Figure 18-3 below:

 

You are reading a sample chapter from AlmaLinux 9 Essentials. Buy the full book now in eBook format.

Full book includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook 

 

Figure 18-3

Using NFS on AlmaLinux 9 to Share Files with Remote Systems Within this screen, perform tasks such as changing the server or mount points or unmounting the file system. For example, the Remove option unmounts the file system and deletes the entry from the /etc/fstab file so that it does not re-mount the next time the system reboots.

Summary

The Network File System (NFS) is a client/server-based system, originally developed by Sun Microsystems, which provides a way for Linux and Unix systems to share filesystems over a network. NFS allows a client system to access and (subject to permissions) modify files located on a remote server as though those files are stored on a local filesystem. This chapter has provided an overview of NFS and outlined the options for configuring client and server systems using the command line or the Cockpit web interface.


Categories