29.8 C
Texas
angeloma
Senior Writer and partner

Configure NFS server on Ubuntu 20.04

Hello, friends. File sharing on the Internet or a local network is one of the most important services there is. So, today I will show you how to install an NFS server on Ubuntu 20.04.

Install NFS Server on Ubuntu 20.04

Before we start, we have to clarify that I will use two nodes for this post. The Server has Ubuntu 20.04 whose IP address is 65.21.183.2 and a client computer with the following IP address 116.203.116.248.

So, let’s get started.

On the computer that we will use as a server, you have to install the following package nfs-kernel-server.

- Advertisement -
sudo apt update
sudo apt install nfs-kernel-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  keyutils libevent-2.1-7 libnfsidmap2 libtirpc-common libtirpc3 nfs-common rpcbind
Suggested packages:
  open-iscsi watchdog
The following NEW packages will be installed:
  keyutils libevent-2.1-7 libnfsidmap2 libtirpc-common libtirpc3 nfs-common nfs-kernel-server rpcbind
0 upgraded, 8 newly installed, 0 to remove and 18 not upgraded.
Need to get 641 kB of archives.
After this operation, 2,342 kB of additional disk space will be used.
Do you want to continue? [Y/n]

By default, Ubuntu 20.04 uses the latest version of the protocol and disables version 2. You can check this by running the following command.

sudo cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2

Configuring the NFS server

The main NFS configuration file is /etc/default/nfs-kernel-server where you can make the relevant settings. Now, the default configuration is sufficient for most cases so we will not edit it.

Now we need to create the folders we are going to share. The first thing is that in this case, the NFS root directory will be /srv/nfs/ but it can be whatever you want.

Within this directory, I will create two more. One dedicated to data called data and one dedicated to documents called documents. Remember that this is an example and you have to adapt the fields to your own.

sudo mkdir -p /srv/nfs/data
sudo mkdir -p /srv/nfs/documents

Normally you would associate a system directory with these paths. This is done for security reasons because it makes it impossible for a client user to have permission and access to system folders. In this case, data will be associated with /opt/data and documents with /opt/documents. Both folders must be created.

sudo mount --bind /opt/data /srv/nfs/data
sudo mount --bind /opt/documents /srv/nfs/documents

Also, it is recommended to have this automatically mounted on system startup.

To do this, edit the file /etc/fstab.

sudo nano /etc/fstab

And add the following:

/opt/data /srv/nfs/data none bind 0 0 0
/opt/documents /srv/nfs/documents none bind 0 0 0

Save the changes and close the editor.

Export the file system

It is now necessary to define the file systems to be exported and the clients that will be allowed to access those resources. To do this, you need to edit the file /etc/exports.

sudo nano /etc/exports

And add the following:

/srv/nfs 116.203.116.248(rw,sync,no_subtree_check,crossmnt,fsid=0)
/srv/nfs/data 116.203.116.248(rw,sync,no_subtree_check)
/srv/nfs/documents 116.203.116.248(rw,sync,no_subtree_check)

Remember to replace the IP address with your client’s IP address. Each of the options is to define the behavior in the folder.

The fsid=0 option, which defines the NFS root directory. Only the IP address 116.203.116.248 is allowed access, although you can also give permissions to members of a subnet. crossmntes needed to share directories that are subdirectories of an exported directory.

The second and third lines grant read and write permissions to the client. The sync option tells NFS to write changes to disk first.

So, since there are a lot of options, you can look them all up with the man exports command.

Apply the changes, by running

sudo exportfs -ar

So, on the server we are ready. Now let’s go to the client.

Working with the NFS Client

Now it is time to work on the client. In this case, I’m using Ubuntu 20.04 so to install the NFS client just run

sudo apt update
sudo apt install nfs-common

Create the folders to mount the volume, for example:

sudo mkdir -p /data
sudo mkdir -p /documents

And proceed to mount the remote volumes in the following way

sudo mount -t nfs -o vers=4 65.21.183.2:/data /data /data
sudo mount -t nfs -o vers=4 65.21.183.2:/documents /documents

Replace the IP address with that of your server. You can also use the domain for this but it will have to be resolved.

If you run the df -h command you can see how they have been mounted correctly

df -h
1.- NFS server running on the client with Ubuntu 20.04
1.- NFS server running on the client with Ubuntu 20.04

So, enjoy it.

Conclusion

In this post, you learned how to set up an NFS server on Ubuntu 20.04 quickly and easily. I hope you enjoyed it and got through it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article