NFS – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Fri, 05 Mar 2021 03:07:09 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 How To Mount And Use NFS Shares On Linux Sharing Files Easily Using NSF https://linuxhint.com/set-up-nfs-shares-linux/ Tue, 02 Mar 2021 21:13:17 +0000 https://linuxhint.com/?p=92763

Developed by Sun Microsystems in 1984, NFS or Network File Shares is a file system protocol used for accessing files over a network similar to a local storage device.

NFS Shares are powerful and popular as they allow users to share files and directories over a local network and the internet. However, it is better to limit NFS shares to local and trusted networks as files don’t get encrypted on the machines. However, the problem was addressed and fixed on a recent version of the NFS protocol. You may need to set up complex authentication methods such as Kerberos.

This tutorial will walk you through how to set up NFS shares on a Linux system. Let us get started.

Setting up NFS Server

Let us start by setting up the NFS server. This process is fairly simple, with only a few commands:

sudo apt-get update

sudo apt-get install nfs-kernel-server

Next, create a directory in the local system which will be used as the NFS’ share root directory:

sudo mkdir /var/nfs

Set the appropriate permissions to the directory:

sudo chown nobody:nogroup /var/nfs

Next, edit the exports file in /etc/exports and add the following entry

/var/nfs <hostname>(rw,sync,root_squash,no_subtree_check)

Setting Up An NFS Client

For you to mount NFS Shares on Linux, you will need to install nfs client tools using the command:

sudo apt-get update
sudo apt-get install nfs-common

Mounting an NFS Filesystem

The process of mounting NFS file shares is very similar to mounting a regular file system in Linux. You can use the command mount. The general syntax is as:

mount <option> <nfs-server-address>:<export-directory> <mount-point>

To accomplish this, start by creating a directory to use as the NFS Share’s mount point.

sudo mkdir /mnt/shares

Next, mount the NFS share using the mount command as shown below:

sudo mount –t nfs 127.0.0.1/var/nfs /mnt/shares

Once completed, you should have access to the remote shares on the server.

Unmounting File shares

Since an NFS share is similar to a file system, you can unmount it with umount command as:

sudo umount <ip-address>/var/nfs
sudo umount /mnt/shares

You can use other options with umount command, such as a force to force-unmount the NFS shares.

Conclusion

The above is a simple guide on how to use and mount NFS shares on a Linux system. There is more to NFS than what we have discussed here; feel free to utilize external resources to learn more.

]]>
Install and Configure NFS https://linuxhint.com/install_configure_nfs/ Wed, 04 Nov 2020 16:48:47 +0000 https://linuxhint.com/?p=75335

Network File System (NFS) is an application that allows users to access and modify files on a remote computer as if they are accessing the local storage of their own computer. It is what is called a distributed file system, and it serves as a centralized filing system for a large network of computers.

NFS works with networks shared by systems with different operating systems. The administrator can also select which sections of the mounting information are made available to the client systems.

This article shows you how to install and configure NFS on your ArchLinux system.

Step 1: Set Up NFS packages

First, we will install the packages for NFS. To do so, issue the following command:

$ sudo pacman -S nfs-utils

Step 2: Set NFS to Launch at Startup

Use the commands below to set NFS to launch at startup:

$ chkconfignfs on

$ service rpcbind start

$ service nfs start


Now, NFS should launch at the startup.

Step 3: Share Directory with Client

Next, you will select a directory to share with the client and move it to /etc/exports. Use the command below to do so:

$ vi /etc/exports


Then, append this line to the files:

# /share 192.168.87.158(rw,sync,no_root_squash,no_subtree_check)

Finally, export these files with the command below:

$ exportfs -a

Step 4: Prepare the Client

The synchronization will require certain packages that you can download with the commands below:

$ sudo pacman -S nfs-utils

Step 5: Mount the Shared Directory

Once the packages are installed on the remote computer, it is now time to mount the shared directory:

$ mkdir -p /mnt/share


Then, mount the shared directory:

$ mount 192.168.87.156:/share /mnt/share/


Run a confirmation test to see if the share is mounted:

$ df -h


Type in #mount to list the mounted file systems:

$ mount

Running A Test

Bring over the contents to be distributed to the client through the server share directory. Type in the following command to run a test:

$ touch test1

$ mkdir test


Go to the /mnt/share folders in the remote computer that is serving as the client:

$ ls /mnt/share/ -lh


Then, add the entries in the /etc/fstab file to automatically mount the shared folder permanently.

Append the /etc/fstab file with the following lines:

$ vi /etc/fstab


This should mount the share folder files and all its contents.

Using NFS

We will now go over some of the options specific to NFS that might come in handy:

Fire up the terminal on the server and type the command below to see the contents up for sharing on the client machine:

$ showmount -e


To see the contents up for sharing on the server, you can use a variation of this command. Type the command below:

$ showmount -e 192.168.87.156


The following command allows you to list all the share files on the server:

$ exportfs -v

To clear the /etc/exports location and send contents back to the source, enter the following:

$ exportfs -u

Conclusion

NFS is a very simple, yet exceptionally convenient, network file sharing application. The extensive central filing system of NFS saves tons of HDD space, as many folders present on the host no longer need to be stored on each computer. The straightforward interface of NFS allows users to access the server for contents as they would access the local storage.

Keep in mind that NFS is susceptible to many exploitative attacks from the internet. As such, you should also consider setting up a firewall to protect your host from these attacks.

That is all we have for today. Stick around at linuxhint.com for more articles like this. We will post follow-ups to this particular post.

]]>
How to Mount NFS share on Debian 10 https://linuxhint.com/mount_nfs_share_debian/ Fri, 24 Jul 2020 04:39:34 +0000 https://linuxhint.com/?p=63701 The network file system NFS is an efficient way of sharing files and directories to other machines in a network. It is based on client-server architecture, where the server directory is mounted onto client systems. NFS allows clients to access shared directories as if they are a part of their own system, and remains a popular way of sharing files among Linux systems.

In this article, we will mount a NFS share on a Debian client machine manually, as well as automatically, upon system boot.

We will use Debian 10 Buster system to run the commands and procedures discussed in this article.

Pre-Requisites

Before proceeding, be sure that:

  • NFS server is installed and running on the remote machine
  • NFS shared directory on the remote server is exported
  • Firewall is allowing access to NFS clients

For the purpose of this article, we have set up two Debian10 machines with the following hostnames and IP addresses:

NFS Server

  • Hostname: nfs-serevr
  • IP address: 192.168.72.144
  • Shared directory: mnt/sharedfolder

NFS Client

  • Hostname: nfs-client
  • IP address: 192.168.72.145
  • Mount point: mnt/client_sharedfolder

Install NFS Client Package on Debian 10 Client Machine

To mount share directories on the client machine, you must first install the NFS client package on it. On the client machine, run the following commands in the Terminal to install the NFS client package:

$ sudo apt update
$ sudo apt install nfs-common

Step 1: Create a Mount Point for the NFS Server’s Shared Directory

To make the NFS server’s shared directory available to the client, you will need to mount the NFS server’s directory on an empty directory on the client machine.

First, create an empty mount point directory on the client machine. This empty directory will serve as the mount point for the remote shared directories.

We have created the new mount directory “client_sharedfolder” under the /mnt directory using the following command:

$ sudo mkdir -p /mnt/client_sharedfolder

Step 2: Mount the NFS Server Shared Directory on the Client

Mount the NFS shared directory to the mount point directory on the client using the following syntax:

$ sudo mount [NFS _IP]:/[NFS_export] [Local_mountpoint]

Where:

  • NFS_IP is the NFS server’s IP address
  • NFS_export is the shared directory on the NFS server
  • Local_mountpoint is the mount point directory on the client’s system

Based on our setup, we ran the following command:

$ sudo mount 192.168.72.144:/mnt/sharedfolder /mnt/client_sharedfolder

After mounting the NFS shared directory, you can confirm it using the following command in the Terminal:

$ df -h

Step 3: Test NFS share

Next, test the NFS share on the client machine. To perform a test create some files or directories under the NFS shared directory on the server machine and then access them from the client machine. Follow the procedure below to do this yourself:

1. On the NFS server machine, open the Terminal and use the cd command to navigate to the NFS shared directory:

$ cd /mnt/sharedfolder/

Next, create some test files:

$ sudo touch test1 test2 test3

2. On the NFS client machine, verify whether the same files exist in the local mount point directory.

$ ls /mnt/client_sharedfolder

Note that the mount command does not permanently mount the NFS file system on the mount point. You will have to mount it manually every time you reboot the system. In the next step, we will look at how to automatically mount the NFS file system at boot time.

Mounting an NFS File System Automatically

Automatically mounting an NFS file system saves you the trouble of having to manually mount the file system each time you boot your system. Below is the procedure to do so:

Edit the /etc/fstab file:

$ sudo nano /etc/fstab

Next, add an entry in the /etc/fstab file, as follows:

NFS server:directory mountpoint nfs defaults 0 0

Where:

  • NFS server is the IP address of the NFS server
  • directory is the shared directory on the NFS server
  • mountpoint is the mount point on the NFS client’s machine
  • nfs defines the file system type

Based on our setup, we have added the following entry:

192.168.72.144:/mnt/sharedfolder /mnt/client_sharedfolder nfs defaults 0 0

After adding the above entry in the /etc/fstab file, save and exit the file by Ctrl + o and Ctrl + x, respectively.

The NFS share will be mounted automatically at the specified mount point the next time you boot your system.

Unmounting an NFS File System

You can easily unmount the NFS shared directory from your system if you no longer need it. To do so, type umount, followed by the name of the mount point, as follows:

$ sudo umount [mount_point]

Based on our setup, it would be:

$ umount /mnt/client_sharedfolder

Note: The command is “umount,” not unmount (there is no “n” present in the command).

Keep in mind that if you have added the entry in the /etc/fstab file for mounting the NFS, it will be automatically mounted again the next time you boot your system. If you want to prevent it from automatically mounting on the next boot, remove the entry from the /etc/fstab file.

Remember that if the NFS file system is busy, you cannot unmount it, such as if any files are opened on it or you are working on some directory.

There you have it! A simple procedure to mount and unmount the NFS share on the Debian 10 Buster system, both manually and automatically. I hope you liked the article!

]]>
How to Mount NFS File System in Ubuntu 20.04 https://linuxhint.com/ubuntu_20-04_-mounting_nfs/ Sun, 21 Jun 2020 19:31:41 +0000 https://linuxhint.com/?p=61907 The network file system NFS enables you to share files and directories among systems in a network. NFS is based on client-server architecture; the NFS server shares the specific directories which client can connect and access by mounting them locally. With NFS, the mounted directory appears as if it resides on your local system. NFS is still the most used way of sharing files between Linux systems.

In Linux OS, you can easily mount an NFS shared directory on your local system using the mount command. The mount command mounts the file system temporarily. Once the system has been restarted, you will have to mount it again to access it. However, if you want to mount the file system permanently so that you do not have to mount it every time you boot the system, you will need to add an entry in the /etc/fstab file.

In this article, we will explain how to manually and automatically mount the NFS file system on the local system.

Pre-requisites

Before you move ahead, make sure the following pre-requisites are completed on the remote server.

  • NFS server is installed on the remote machine
  • NFS Service is running
  • NFS shared directory is exported
  • A firewall is not blocking access to client IP

We have performed the procedure mentioned in this article on the Ubuntu 20.04 system. Moreover, we have used the command line Terminal application for running the commands in Ubuntu. To open the Terminal, you can use the Ctrl+Alt+T keyboard shortcut.

Installing NFS Client Packages

To mount the NFS shared directory on your local client system, you will require the NFS client package. First, update the system repository index using the following command in Terminal:

$ sudo apt update

Then install the NFS client package in your client machine using the following command in Terminal:

$ sudo apt install nfs-common

Mounting an NFS File System Manually

In the following method, we will mount the NFS directory manually using the mount command.

Step 1: Create a mount point for the NFS server’s shared directory

Our first step will be to create a mount point directory in the client’s system. This will be the directory where all the shared files from the NFS server can be accessed.

We have created a mount point directory with the name “client_sharedfolder” under the /mnt directory.

$ sudo mkdir -p /mnt/client_sharedfolder

Step 2: Mount the NFS server shared directory on the client

The next step is to mount the shared directory on the NFS server to the client’s mount point directory. Use the following syntax to mount the NFS server shared directory to the mount point directory in the client:

$ sudo mount [NFS _IP]:/[NFS_export] [Local_mountpoint]

Where

  • NFS_IP is the NFS server’s IP address
  • NFS_export is the shared directory on the NFS server
  • Local_mountpoint is the mount point directory on the client’s system

In our example, the command would be:

$ sudo mount 192.168.72.136:/mnt/sharedfolder /mnt/client_sharedfolder

Where 192.168.72.136 is our NFS server IP, /mnt/sharedfolder is the shared directory on the NFS server, and /mnt/sharedfolder is the mount point on the client system.

Once you have mounted the NFS share, you can confirm it using the following command:

$ df –h

Step 3: Test NFS share

After you have mounted the NFS shared directory on the client machine, test it by accessing some files from the NFS server. On the NFS server machine, create any test file or directory and try accessing it from the client machine.

Use the cd command to navigate to the NFS server’s shared directory:

$ cd /mnt/sharedfolder/

Then using the touch or mkdir command, create a test file or directory. We have created some sample files named “testfile1” and “testfile2”.

$ sudo touch testfile1 testfile2

Now on the client’s machine, verify if the same files exist.

$ ls /mnt/client_sharedfolder/

The mount command mounts the NFS file system temporarily on the client system. Every time you reboot the system, you will have to manually mount it. In the next step, we will see how to make the NFS file system mount automatically at boot time.

Mounting an NFS File System automatically

In the following method, we will set up the NFS file system to automatically mount at boot time. Using this way, you will not have to mount the file system manually every time you boot your system.

Edit the /etc/fstab file using the following command:

$ sudo nano /etc/fstab

Then add an entry in /etc/fstab file using the following format.

NFS server:directory mountpoint nfs defaults 0 0

Where the NFS server: directory is the NFS server IP and its shared directory, the mount point is the mount point on the client’s machine where the NFS directory is mounted, and the nfs defines the file system type.

In our example, the entry would be:

192.168.72.136:/mnt/sharedfolder /mnt/client_sharedfolder nfs defaults 0 0

Where 192.168.72.136 is our NFS server IP, /mnt/sharedfolder is the shared directory on the NFS server, and /mnt/client_sharedfolder is the mount point on the client system.

Once you have added the above entry in the /etc/fstab file, save, and close the file. Use the Ctrl+O and then Ctrl+X to do so.

Next time you start your machine the NFS share will be automatically mounted at the specified mount point.

Unmounting the NFS File Systems

You can unmount an NFS file system from your local system at any time. Type the umount command followed by the mount point name where it is mounted.

Note: The command is “umount” not unmount.

$ sudo umount [mount_point]

In our example, it would be:

$ umount /mnt/client_sharedfolder

However, remember that, if the NFS file system has been mounted using the /etc/fstab, it will be again mounted next time you boot your system. Also note that the file system will not be unmounted if it is busy like if there are some files opened on it, or you are working on some directory.

That is all there is to it! In this article, you have explained how to mount the NFS shared directory on the Ubuntu 20.04 system both manually and automatically. In the end, we have also explained how to unmount the NFS shared directory when you no longer need it.

]]>
How to Configure NFS Server on CentOS 8 https://linuxhint.com/configure_nfs_server_centos8/ Sat, 29 Feb 2020 12:49:21 +0000 https://linuxhint.com/?p=55805 The full form of NFS is Network File System. It is a distributed file system protocol. NFS allows you to share a directory from your NFS server over the network which can be mounted from one or multiple NFS clients and be accessed simultaneously.

In this article, I am going to show you how to configure NFS server and clients on CentOS 8. So, let’s get started.

Network Topology:

Figure 1: Network topology used in this article

In this article, 3 CentOS 8 machines are used. They are connected as in figure 1.

nfs-server will be configured as an NFS file server.

nfs-client1 and nfs-client2 will be configured as NFS client. They will mount the shared filesystem path from the NFS server nfs-server.

nfs-server network configuration:

IP address: 192.168.20.178/24

nfs-client1 network configuration:

IP address: 192.168.20.176/24

nfs-client2 network configuration:

IP address: 192.168.20.177/24

Configuring the Server:

First, you have to set up a static IP address on the nfs-server CentOS 8 machine. If you need any help on that, check the article Configuring Static IP on CentOS 8.

Now, SSH into your nfs-server machine.

$ ssh shovon@192.168.20.178

Update the DNF package repository cache with the following command:

$ sudo dnf makecache

Install the nfs-utils package with the following command:

$ sudo dnf install nfs-utils

To confirm the installation, press Y and then press <Enter>.

nfs-utils package should be installed.

Now, add the nfs-server and rpcbind services to the system startup with the following command:

$ sudo systemctl enable nfs-server rpcbind

Now, start the nfs-server and rpcbind services with the following command:

$ sudo systemctl start nfs-server rpcbind

The nfs-server and rpcbind services should be active (running).

$ sudo systemctl status nfs-server rpcbind

Now, you can share any directory path on your server using NFS.

In this article, I am going to show you how to make partitions, format the partition, mount them to specific directory path and share it using NFS. If the directory path you want to share is ready, you can skip ahead.

First, find the storage device name using the following command:

$ lsblk

In my case, the name of the SSD I will use is nvme0n2. It will be different for you. So, make sure to replace it with yours from now on.

Now, run cfdisk as follows:

$ sudo cfdisk /dev/nvme0n2

If you don’t have a partition table already, cfdisk will show you this window. Select gpt and press <Enter>.

Now, select the Free space, navigate to [ New ] and press <Enter>.

Type in the partition size and press <Enter>.

NOTE: Use M for MiB, G for GiB and T for TiB disk size unit.

A new partition /dev/nvme0n2p1 should be created. Now, select [ Write ] and press <Enter>.

Now, type in yes and press <Enter>.

The changes should be written to the partition table.

Now, select [ Quit ] and press <Enter>.

As you can see, a new partition nvme0n2p1 is created.

Now, create a filesystem on the nvme0n2p1 partition with the following command:

$ sudo mkfs.ext4 -L nfs-share /dev/nvme0n2p1

Now, make a directory (in my case /nfs-share) where you want to mount the newly created partition with the following command:

$ sudo mkdir /nfs-share

Now, to automatically mount the partition when your nfs-server boots, you have to add an entry to the /etc/fstab file.

To edit the /etc/fstab file, run one of the following commands:

$ sudo nano /etc/fstab

OR

$ sudo vi /etc/fstab

Now, add the following line to the file and save the file.

/dev/nvme0n2p1    /nfs-share    ext4    defaults    0    0

Now, you can easily mount the newly created partition to the /nfs-share directory as follows:

$ sudo mount /nfs-share

As you can see, the partition is mounted to the /nfs-share directory.

Now, to share the /nfs-share directory with NFS, edit the /etc/exports configuration file with one of the following commands:

$ sudo nano /etc/exports

OR

$ sudo vi /etc/exports

Now, you have to add the following line to the /etc/exports file.

/nfs-share    192.168.20.0/24(rw,no_root_squash)

The format of the line is:

share_directory_path  host1(host1_options) host2(host2_options_)

In this article, the share_directory_path is /nfs-share

After specifying the share directory, you can add one or more hosts and access options for each host.

Here, the host is 192.168.20.0/24. So, everyone on the subnet, 192.168.20.1 to 192.168.20.254 will be able to access this share.

The options are rw and no_root_squash.

Here,

rw – allows read and write to the share

no_root_squash – does not allow NFS server to map any user or group id to anonymous user or group id.

There are many more options which you can use. To learn more about it, check the manpage of exports.

$ man exports

Now, to enable the share without restarting the server, run the following command:

$ sudo exportfs -var

If you have SELinux enabled, run the following command:

$ sudo setsebool -P nfs_export_all_rw 1

Now, to allow access to the NFS ports from the NFS clients, configure the firewall with the following command:

$ sudo firewall-cmd --add-service={nfs,nfs3,mountd,rpc-bind} --permanent

Now, for the firewall changes to take effect, run the following command:

$ sudo firewall-cmd --reload

Configuring the Client:

Now, to mount the NFS share /nfs-share from the nfs-server to nfs-client1 machine, you need to install the nfs-utils package on nfs-client1 machine as well.

First, update the DNF package repository cache as follows:

$ sudo dnf makecache

Now, install the nfs-utils package as follows:

$ sudo dnf install nfs-utils

Now, press Y and then press <Enter>.

 

nfs-utils should be installed.

Now, to confirm whether the NFS share is accessible from the client machine, run the following command:

$ sudo showmount --exports 192.168.20.178

Here, 192.168.20.178 is the IP address of nfs-server machine.

As you can see, /nfs-share is accessible from the nfs-client1 machine.

Now, make a mount point for the NFS share as follows:

$ sudo mkdir /mnt/nfs-share

Now, you can mount the NFS share /nfs-share from the nfs-server machine to the /mnt/nfs-share directory of the nfs-client1 machine with the following command:

$ sudo mount -t nfs 192.168.20.178:/nfs-share /mnt/nfs-share

The NFS share should be mounted.

If you want to mount the NFS share when your nfs-client1 machine boots, you have to add an entry to the /etc/fstab file.

Edit the /etc/fstab file with one of the following commands:

$ sudo nano /etc/fstab

OR

$ sudo vi /etc/fstab

Now, add the following line to the file.

192.168.20.178:/nfs-share /mnt/nfs-share  nfs    defaults 0 0

nfs has a lot of mount options. I’ve used the defaults mount option here. But, if you have specific requirements, you may check the manpage of nfs.

$ man nfs

Now, let’s create a new file hello.txt to the NFS share from the nfs-client1 machine.

$ echo "Hello NFS share" | sudo tee /mnt/nfs-share/hello.txt

As you can see, the file hello.txt is also created in the nfs-server.

The contents of the hello.txt file read from the nfs-server machine.

The same way you can configure nfs-client2 and access the NFS share from there.

Install nfs-utils package on nfs-client2.

$ sudo dnf install nfs-utils

Edit /etc/fstab file.

$ sudo nano /etc/fstab

OR

$ sudo vi /etc/fstab

Add the following line to it.

192.168.20.178:/nfs-share  /mnt/nfs-share    nfs    defaults 0 0

Create a mount point.

$ sudo mkdir /mnt/nfs-share

Mount the share.

$ sudo mount /mnt/nfs-share

Access the files from the share. Very simple.

$ sudo cat /mnt/nfs-share/hello.txt

This is how you configure NFS server and client on CentOS 8. Thanks for reading this article.

]]>