[Linux] Configuring NFS (+ Usage Examples)
This post covers how to configure NFS and provides usage examples.
I use Ubuntu Desktop, but Red Hat-based OSes are not significantly different.
Only the specific commands might vary slightly; the overall workflow is the same across Linux OSes.
Configuring NFS
What is NFS?
NFS stands for Network File System.
Naturally, it requires an established network to function.
As the name suggests, it allows you to access a file system over a network address.
As expected, because it is based on network communication, there is a server and a client, and their setup methods differ.
Configuring NFS: Server
Install the NFS package for the server:
$ sudo apt install nfs-kernel-server
Once the package installation is complete, the /etc/exports file is generated.
The server needs to know which file paths to allow access to and the addresses of the clients.
Modify the /etc/exports file.
Modifying the exports file (example):
$ sudo vi /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/home/how2flow 192.168.0.*(rw,sync,no_root_squash,no_subtree_check)
Looking at the contents:
/home/how2flow means that all files in the server’s /home/how2flow directory will be exported.
192.168.0.*(rw,sync,no_root_squash,no_subtree_check) specifies
the client IP address range and the file system access options (in parentheses).
Among the options,
no_root_squash is an option that allows the client to have root privileges.
As you can see from the configuration, wildcards are permitted.
Therefore, you can write the client section like this:
*(rw,sync,no_root_squash,no_subtree_check)
Of course, allowing only a specific client is also possible:
192.168.11.75(rw,sync,no_root_squash,no_subtree_check)
Once you have finished modifying the file, apply the settings:
$ sudo exportfs -ra
The server configuration is complete.
What remains is how to access the server’s file system from the client.
Accessing NFS: Client
Install the NFS package for the client:
$ sudo apt install nfs-common
Once the server configuration for the client is complete, you can mount it using the following command:
$ sudo mount -t nfs <server_ip>:/<file_path>
For example, if you want to access /home/how2flow/Pictures on the server,
and the server’s IP address is 192.168.0.2:
$ sudo mount -t nfs 192.168.0.2://home/how2flow/Pictures
Check the files from the location where you executed the command:
$ ls
Pictures
Now you can access the server’s Pictures directory.
Additional Explanation of mount
Linux supports a feature called mount.
It is a function that mounts other file systems or hardware devices onto the existing file system.
When you connect a storage device like a USB or SSD, you can mount it by partition to a specific directory.
The command usage is as follows:
$ sudo mount [OPTION] <source> <destination>
For more detailed information, please refer to:
$ mount --help
Usage Examples
We’ve looked at how to configure nfs and how to use it on a client.
However, if you Google it, this is usually as far as the information goes.
So, I will add some practical usage examples.
I will introduce the scenarios where I primarily use it.
Installing an Embedded Linux Kernel
As an embedded developer working with Linux kernels, I think I’ve used it most often when testing kernels.
There are two main ways to install a kernel on an embedded device:
1. Cross-compile on a PC, then install the kernel on the embedded storage device.
2. Perform a native install directly on the embedded device.
I believe both methods have their pros and cons.
The advantage of method 1 is that the kernel build is much faster than method 2.
However, every time you build/install the kernel, you have to power off the embedded device, remove the SD card,
connect the SD card to the PC, and execute the install command.
$ cd linux
$ make "target's defconfig"
$ sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- INSTALL_MOD_PATH=/dev/disk4/rootfs -j$(nproc) modules_install
Method 2, conversely, allows for immediate installation, but the build time is painfully slow.
However, by using nfs, you can combine the best of both worlds.
First, build the kernel on the server (PC, 192.168.0.2).
$ cd linux
$ sudo make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc)
Mount the NFS on the client (Embedded) device and proceed with a native install.
$ mkdir linux
$ sudo mount -t nfs 192.168.0.2://home/how2flow/linux ./linux
$ cd linux
$ sudo make modules_install && sudo make install
By doing this, you can build the kernel quickly,
and perform the kernel installation while the embedded device system is powered on.
Other Examples
This is my primary use case,
but I will share other good use cases if I discover them in the future.
Leave a comment