Introduction
Network File System (NFS) is a robust protocol that empowers efficient file sharing and collaboration between computers over a network. Whether you’re setting up a shared resource within a local network or enabling remote access to files, NFS serves as a versatile solution. In this comprehensive guide, we will take you through the step-by-step process of how to install and configure both the NFS server and client on the Ubuntu 22.04 operating system, facilitating seamless data sharing and access.
Table of Contents
Here’s how the NFS Server-Client architecture works:
- NFS Server: The NFS server is a computer or device that exports directories or specific files to be shared with remote clients. It runs the NFS server software, which manages the file system and handles requests from NFS clients. The NFS server makes its shared resources available to authorized NFS clients over the network.
- NFS Client: The NFS client is a computer or device that connects to the NFS server to access shared files and directories. It runs the NFS client software, which handles the communication with the NFS server and manages the mounting of remote directories onto the local file system.
When a client needs to access files from an NFS server, it uses the mount command to mount the remote directory onto a local directory on the client’s file system. Once the mount operation succeeds, the client gains the ability to access files and directories at the remote location as if they were integral to its local file system. Let’s now begin learning how to install and configure an NFS server and client.
Prerequisites
Before we delve into process how to install and configure an NFS server and client, ensure you have the following prerequisites:
- Ubuntu Server: For install NFS server. Make sure you have a clean installation of Ubuntu Server. To set this up, follow our guide Initial Setup Ubuntu Server 22.04.
- The Debian 10 machine will use this example as a Linux Client machine.
- A non-root user with root/administrator privileges.
- Firewall to allow traffic on port 2049
Supported NFS Versions
Ubuntu 22.04 supports the following versions of NFS.
NFS version 3 (NFSv3)
- Has support for safe asynchronous writes and is more robust at error handling than the previous NFSv2
- Supports 64-bit file sizes and offsets, allowing clients to access more than 2 GB of file data.
NFS version 4 (NFSv4)
- Works through firewalls and on the Internet
- No longer requires rpcbind service
- Supports Access Control Lists (ACLs)
- Utilizes stateful operations.
Install and Configure NFS Server on Ubuntu 22.04
The first step is to install NFS server. We’ll install the necessary packages, create and export the NFS directories, and configure the firewall.
Step 1: System Update
Before installing any packages, run the apt command below to update and refresh your Ubuntu repository.
samm@nfs-server:~$ sudo apt update
samm@nfs-server:~$ sudo apt upgrade -y
Step 2: Install NFS Server
The NFS server package provides user-space support needed to run the NFS kernel server. To install the package, run:
samm@nfs-server:~$ sudo apt install nfs-kernel-server
Input Y to confirm the installation and press ENTER, and the installation will begin.
The output will be similar to this:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
keyutils libnfsidmap1 nfs-common rpcbind
Suggested packages:
watchdog
The following NEW packages will be installed:
keyutils libnfsidmap1 nfs-common nfs-kernel-server rpcbind
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 521 kB of archives.
After this operation, 1,973 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Once the NFS server installation is completed, verify the NFS server service by executing the following command. In the Ubuntu installation, the NFS server will be enabled automatically and will run at system boot.
samm@nfs-server:~$ sudo systemctl is-enabled nfs-server
enabled
samm@nfs-server:~$ sudo systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2023-08-03 15:55:03 WIB; 6min ago
Main PID: 40972 (code=exited, status=0/SUCCESS)
CPU: 7ms
Aug 03 15:55:02 nfs-server systemd[1]: Starting NFS server and services...
Aug 03 15:55:02 nfs-server exportfs[40971]: exportfs: can't open /etc/exports for reading
Aug 03 15:55:03 nfs-server systemd[1]: Finished NFS server and services.
On the Ubuntu 22.04 server, the NFS configuration has changed. The main configuration for NFS is located at the file “/etc/nfs.conf“, which allows you to set up and configure the NFS server. You can find another configuration for the NFS service at “/etc/default/nfs-*”. This configuration allows you to set up how the NFS service will run.
Also, the defaults NFS versions enabled on the default installation are the NFSv3 and NFSv4. You can check the default enabled NFS versions using the below command.
samm@nfs-server:~$ sudo cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2
Step 3: Setting up Shared Directory
After installing the NFS server package, now you will be setting up shared directories. NFS shared directories can be defined via the file “/etc/exports” file. Also, you can specify the client IP address for the shared directory or set up the read/write to the shared directory.
Run the below command to create new shared directories in “/data”.
samm@nfs-server:~$ sudo mkdir /data
Now change the ownership and the permission of shared directories using the below command. The user and group should be “nobody:nogroup” and the permission will be “777” to ensure shared directories is writable.
samm@nfs-server:~$ sudo chown -R nobody:nogroup /data
samm@nfs-server:~$ sudo chown -R 777 /data
Step 4: Configuring NFS exports on Ubuntu 22.04
The next step involves adding the file systems that will be exported and specifying the clients permitted to access those shares in the /etc/exports file.
Now we need to modify /etc/exports to configure NFS share. The structure is:
/path/to/directory IP Address Client (options)
samm@nfs-server:~$ sudo vi /etc/exports
Add the following configuration to the file.
/data 172.32.1.0/24(rw,sync,no_root_squash,no_subtree_check)
When you have finished, save and close the file.
- The directory “/data” will be available for all clients in the entire network “172.32.1.0/24”.
- rw – enable read and write for the shared directory.
- sync – allow NFS to write changes before responding to client machines and ensure the NFS server is always presented to clients.
- no_subtree_check – disable subtree checking and ensure there will be no conflict when users change the filename.
Once you’re done with the settings, use the exportfs utility to selectively export directories without restarting the NFS service.
samm@nfs-server:~$ sudo exportfs -a
Now run the following command to restart and verify the NFS service. And you should see the NFS service is running with a new exported directory.
samm@nfs-server:~$ sudo systemctl restart nfs-server
samm@nfs-server:~$ sudo systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2023-08-03 16:40:58 WIB; 5s ago
Process: 41365 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 41366 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
Main PID: 41366 (code=exited, status=0/SUCCESS)
CPU: 8ms
Aug 03 16:40:58 nfs-server systemd[1]: Starting NFS server and services...
Aug 03 16:40:58 nfs-server systemd[1]: Finished NFS server and services.
Run the following command to check available shared directories on the NFS server. You should see directories “/data” available as shared directories for clients.
samm@nfs-server:~$ sudo exportfs -v
The output will be similar to this:
/data 172.32.1.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
Step 5: Securing NFS Server with Firewall
After setting up NFS shared directory, it’s time to secure the NFS server using the Firewall, in this case, the default firewall for Ubuntu is UFW.
As you can see on top, each shared directory is accessible through a specific client IP address and network. So you will need to specify the client IP address or network in the UFW firewall rule that will be allowed to access the NFS service port.
Run the ufw command below to allow the client IP address “172.32.0.0/24” to access the NFS service port.
samm@nfs-server:~$ sudo ufw allow from 172.32.0.0/24 to any port nfs
Rule added
At this point, reload the UFW firewall rules and confirm the list of firewall regulations using the following command. You should observe that the default NFS service port “2049” is accessible exclusively through designated client IP addresses and networks.
samm@nfs-server:~$ sudo ufw reload
Firewall reloaded
samm@nfs-server:~$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp ALLOW IN Anywhere
80/tcp ALLOW IN Anywhere
443 ALLOW IN Anywhere
2049 ALLOW IN 172.32.0.0/24
22/tcp (v6) ALLOW IN Anywhere (v6)
80/tcp (v6) ALLOW IN Anywhere (v6)
443 (v6) ALLOW IN Anywhere (v6)
Install and Configure NFS Clients
Now that the NFS server is set up and shares are exported, the next step is to configure the clients and mount the remote file systems.
This time we using Debian 10 for NFS Client
Step 1: System Update
Run the apt command below to update and refresh your Debian repositories.
samm@nfs-client:~$ sudo apt update
samm@nfs-client:~$ sudo apt upgrade
Step 2: Install NFS Client
Now install the “nfs-common” package using the following command.
samm@nfs-client:~$ sudo apt install nfs-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
nfs-common is already the newest version (1:1.3.4-2.5+deb10u1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Step 3: Setting up Mount Directory
Before start mounting the NFS shared directory, you will be creating a new specific directory for the mount destination. In this example. the destination directory for mounting the NFS shares is the “/opt/data” directory.
samm@nfs-client:~$ sudo mkdir -p /opt/data
Now we’ll be able to mount the NFS shares. So, to try it out, pick a directory to mount to, and run the mount command as root privileges to mount the networked share.
samm@nfs-client:~$ sudo mount -t nfs4 172.32.1.221:/data /opt/data
Where 172.32.1.221 is the IP of the NFS server. You can also use the hostname instead of the IP address, but it needs to be resolvable by the client machine. Typically, you achieve this by mapping the hostname to the IP address in the /etc/hosts file.
Step 4: Automatically Mount NFS
For a more permanent solution, you can add the share to your client’s /etc/fstab file. The overall syntax looks a lot like the command that you just used to mount your share. Start with the location of the share on your network. Afterwards, specify where to mount the share. The filesystem type here is nfs4.
samm@nfs-client:~$ sudo vi /etc/fstab
Add the following configuration to the file.
172.32.1.221:/data /opt/data nfs4 defaults,_netdev 0 0
Next, run the mount command below to check and verify the “/etc/fstab” file. If your configuration is accurate, all filesystems listed in the “/etc/fstab” file will be mounted onto your machine.
samm@nfs-client:~$ sudo mount -a
Verify the list of mounted disks on your client machine using the below command. And you should see the NFS shared directory “/data” is mounted to the target directory “/opt/data”.
samm@nfs-client:~$ df -hT
The command will print all mounted file systems. The last line is the mounted share:
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 983M 0 983M 0% /dev
tmpfs tmpfs 200M 13M 187M 7% /run
/dev/sda1 ext4 31G 1.9G 27G 7% /
tmpfs tmpfs 998M 0 998M 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup
tmpfs tmpfs 200M 0 200M 0% /run/user/1101
172.32.1.221:/data nfs4 15G 7.4G 6.6G 54% /opt/data
Provided the mount succeeded, you’ll be able to access your shared files in the directory where you mounted them.
Conclusion
You’ve successfully install and configure an NFS server-client on Ubuntu 22.04, enabling efficient file sharing and access across your network. NFS streamlines collaboration and data sharing, making it an indispensable tool for organizations of all sizes. By following this comprehensive guide, you’ve gained the skills needed to set up and manage NFS for enhanced productivity and seamless file sharing.
Also Read Our Other Guides :
- How To Use Rsync to Sync Local and Remote Directories in Linux
- How To Create and Use Swap File on Linux System
- How To Install and Configure Elasticsearch on Ubuntu Server 22.04
- How To Install Jenkins on Ubuntu 22.04
- How To Get Total Inodes and Increase Disk Inode Number in Linux
Finally, now you have learned how to nstall and Configure NFS Server Client on Ubuntu 22.04