Beginners guide to mount NFS share in Linux with examples | GoLinuxCloud (2024)

In this tutorial I will share the steps to mount NFS share on the client nodes

Topics we will cover hide

Overview on NFS

Pre-requisites

Install NFS Client

Mount NFS File System manually

Mount NFS File System Persistently

Removing NFS Mounts from the Client

How to fix "umount: device is busy"

Conclusion

Overview on NFS

  • The Network File System (NFS) is a distributed file system that provides transparent access to remote disks
  • Instead of duplicating common directories such as /usr/local on every system, NFS provides a single copy of the directory that is shared by all systems on the network.
  • For the user, NFS means that he or she doesn’t have to log into other systems to access files.
  • An NFS server is a host that owns one or more filesystems and makes them available on the network;
  • NFS clients mount filesystems from one or more servers.

Pre-requisites

Setup NFS exports Server

Configuring NFS Server is not covered as part of this article so I will assume you already a NFS server up and running. In this article we will only cover the NFS client part i.e. to mount NFS share on the client from the server.

In this example I have setup nfs exports on server1 (10.43.138.1) with below configuration

[root@server1 ~]# exportfs -v/ISS <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

Install NFS Client

The NFS client package will vary based on the Linux distribution.

On Ubuntu install nfs-common

$ sudo apt install -y nfs-common

On RHEL/CentOS environment install nfs-utils

[root@server2 ~]# yum -y install nfs-utils
ALSO READWhat does echo -n do in Linux? [SOLVED]

Mount NFS File System manually

You can use mount command to mount the NFS file system form remote server to your localhost.The syntax to mount NFS File System on the client node would be:

mount [OPTIONS] NFS_SERVER:/PATH/TO/EXPORTED/DIR /MOUNT_POINT_ON_CLIENT

As per our configuration

  • NFS_SERVER is server1 (10.43.138.1)
  • /PATH/TO/EXPORTED/DIR is /ISS
  • /MOUNT_POINT_ON_CLIENT is /tmp/logs

So to mount NFS manually we will execute below command on the client i.e. server2 (10.43.138.2)

We need the mount point, so I will create the mount point

[root@server2 ~]# mkdir /tmp/logs

Next mount the NFS file system from server1 on server2

[root@server2 ~]# mount -t nfs 10.43.138.1:/ISS /tmp/logs

Verify if the NFS FS is mounted properly

[root@server2 ~]# df -h /tmp/logsFilesystem Size Used Avail Use% Mounted on10.43.138.1:/ISS 685G 169G 482G 26% /tmp/logs

Now based on the permission of your NFS share you can access the data of /ISS from server1 on /tmp/logs on server2

Mount NFS File System Persistently

Now with mount command the changes are not persistent and will not survive a reboot. So if you wish to mount your NFS File System after every reboot then you must add this in /etc/fstab or create a systemd unit file to update fstab during reboot.

In this article we will use our traditional fstab to auto-mount the file system.First of all make sure your mount point exists

[root@server2 ~]# mkdir /tmp/logs

Next update /etc/fstab with below content

# NFS_SERVER:/PATH/TO/EXPORTED/DIR /MOUNT_POINT_ON_CLIENT TYPE_OF_FS OPTIONS DUMPPASS 10.10.0.10:/backups /var/backups nfs defaults 0 0

Save and exit the file.

Next to verify if this is working, first un-mount the NFS File System (if in mounted state)

[root@server2 ~]# umount /tmp/logs

Now we will use fstab to mount all the FS available in /etc/fstab. You can safely execute this command and it will not break anything in your environment

[root@server2 ~]# mount -a

Now verify if your NFS File System is properly mounted:

[root@server2 ~]# df -h /tmp/logsFilesystem Size Used Avail Use% Mounted on10.43.138.1:/ISS 685G 169G 482G 26% /tmp/logs

That's it so next time your client reboots, the NFS File System will be automatically mounted as long as your NFS server is up and running.

ALSO READ6 Tools making Automation Testing easy in Linux

Additional Options to mount NFS File System

This section will be applicable based on your NFS server configuration.

For example in the above case if I check for the options using which NFS was mounted

[root@server2 ~]# mount | grep logs10.43.138.1:/ISS on /tmp/logs type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.43.138.2,local_lock=none,addr=10.43.138.1)

Here this command tells us that the NFS was mounted using NFSv4 with different rsize, wsize etc. So while performing the mount we have an option to modify these values.

Provide NFS version while mounting the NFS File System

If your NFS server allows you to choose a NFS version for the client mount then you can use -o nfsvers=<ver>, for example to mount using NFSv3

[root@server2 ~]# mount -o nfsvers=3 10.43.138.1:/ISS /tmp/logs

Similarly if your client and server supports you can provide different NFS version.

Setting Block Size to Optimize Transfer Speeds

The mount command options rsize and wsize specify the size of the chunks of data that the client and server pass back and forth to each other. If no rsize and wsize options are specified, the default varies by which version of NFS we are using.

[root@server2 ~]# mount -o rsize=<val>,wsize=<val> 10.43.138.1:/ISS /tmp/logs

Removing NFS Mounts from the Client

To un-mount the NFS mount point you can just use umount command followed by the mount point path

# umount /MOUNT_POINT

OR if you are not aware of the mount point you can also provide the REMOTE_SERVER and REMOTE_DIR PATH i.e.

# umount NFS_SERVER:/PATH/TO/EXPORTED/DIR

So in our example to un-mount our NFS File System we will use

[root@server2 ~]# umount /tmp/logs

OR

[root@server2 ~]# umount 10.43.138.1:/ISS

Next use df or mount command to make sure the NFS FS is not mounted any more.

How to fix "umount: device is busy"

It is possible sometimes you may get this error

umount.nfs4: <mount_point>: device is busy

This is most likely because the mount_point on which your NFS file system is mounted is in use by some process. So either you can find and kill that process or let the process complete.

Kill the process

To kill the process first you should know the process which is occupying the mount_point. You can use

# fuser -m /mount_point

This will give you the PID of the process using the mount_point in below format, in this example my mount_point is /mnt:

[root@server2 ~]# fuser -m /mnt/mnt: 12594

So now you can search for process with PID 12594

[root@server2 ~]# ps -p 12594 PID TTY TIME CMD12594 pts/0 00:00:00 tail

Now since you know the process which is occupying your NFS mount point, you can either choose to kill it or let the process complete

You can also use lsof to detect the list of process using a file system:

# lsof +f -- <mountpoint or device>

Lazy un-mount

If you choose to ignore the PID and want to let it complete,how would you know when the process is complete?

You will have to constantly monitor the PID to make sure it is complete after which only you can un-mount your NFS File System. SO to overcome this we have something called lazy un-mount where we can trigger umount with -l or --lazy, for example:

[root@server2 ~]# umount /mntumount.nfs4: /mnt: device is busy

The default umount is not working so let's try lazy un-mount

[root@server2 ~]# umount --lazy /mnt

The exit status is success so our command was executed successfully:

[root@server2 ~]# echo $?0

So this will detach the file system from the file system hierarchy now, and cleanup all references to the file system as soon as it is not busy anymore. The recommended use-case for umount -l is to prevent hangs on shutdown due to an unreachable network share where a normal umount will hang due to a downed server or a network partition.

[root@server2 ~]# umount --lazy /mnt
ALSO READ[Solved] Found a swap file by the name .XXX.swp

Conclusion

In this tutorial we learned about methods to mount NFS shares on client nodes. You can use these steps across different Linux distributions such as Red Hat, Fedora, CentOS, SuSE, Ubuntu, Debian etc. The only difference would be the NFS client package to be installed.

Lastly I hope the steps from the article to mount NFS on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Views: 4,551

Beginners guide to mount NFS share in Linux with examples | GoLinuxCloud (2024)

References

Top Articles
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6075

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.