How to set up NFS (Network File System) on RHEL / CentOS / Fedora and Debian / Ubuntu

NFS (Network file system) is mainly designed to be shared files and folders between Linux/Unix systems from by Sun Microsystems in 1980 year… It allows you to mount local file systems over the network and remote hosts to interact with them as if they were installed locally on the same system. Through NFS, we can set up file sharing between Unix in Linux system and Linux for the system Unix

Benefits of NFS

  1. NFS creates local access to remote files.
  2. It uses a standard architecture client/server to exchange files between all machines based on * NIX
  3. Through NFS it is not necessary that both machines run on the same OS
  4. Through NFS we can customize the solution centralized storage
  5. Users get their data regardless of their physical location.
  6. Automatic renewal for new files.
  7. Newer version NFS supports mounting acl, pseudo as root.
  8. Can be protected firewalls and Kerberos

NFS services

Service System V-launched… Server package NFS includes three tools included in packages portmap and nfs-Utils

  1. portmap: displays calls made from other machines to the correct service RPC (not required with NFSv4 ).
  2. nfs: converts remote requests file sharing into queries on the local file system.
  3. rpc.mountd: this service is responsible for mounting and unmounting file systems.

Important configuration files for NFS

  1. / etc / exports: its main config file NFS, all exported files and catalogswhich are defined in this file and on destination NFS server
  2. / etc / fstab: To mount NFS directory on your system without reboots, we need to record in / etc / fstab
  3. / etc / sysconfig / nfs: Configuration file NFS to control which port RPC and other services listening

Configuring and Mounting NFS on a Linux Server

To customize mount NFSwe will need at least two cars Linux/Unix… In this tutorial, we’ll be using two servers.

  1. NFS server: nfsserver.example.ru with IP – 192.168.0.55
  2. NFS client: nfsclient.example.ru with IP – 192.168.0.60

Installing NFS Server and NFS Client

We need to install packages NFS on our NFS serveras well as by car NFS client… We can install it with “yum”(Red Hat Linux) and installation package “apt-get”(Debian and Ubuntu).

[[email protected] ~]# yum install nfs-utils nfs-utils-lib
[[email protected] ~]# yum install portmap (not required with NFSv4)
[[email protected] ~]# apt-get install nfs-utils nfs-utils-lib

Now run service on both machines.

[[email protected] ~]# /etc/init.d/portmap start
[[email protected] ~]# /etc/init.d/nfs start
[[email protected] ~]# chkconfig --level 35 portmap on
[[email protected] ~]# chkconfig --level 35 nfs on

After installing the packages and starting the services on both machines, we need to configure both machines to share files.

Setting up an NFS server

First, let’s set up the server NFS

Setting up the export directory

To exchange a directory with NFS, we need to write to the configuration file “/ etc / exports”. Here we will create a new directory named “nfsshare” In chapter “/”For exchange with client server, you can also share an existing directory with NFS.

[[email protected] ~]# mkdir /nfsshare

Now we need to write to “/ etc / exports”And restart services to make our directory shareable on the web.

[[email protected] ~]# vi /etc/exports

/nfsshare 192.168.0.60(rw,sync,no_root_squash)

In the example above, there is a directory under / entitled “nfsshare“, Currently shared with an IP client”192.168.0.60”With privileges reading and records (RW), you can also use hostname client instead of IP in the example above.

NFS options

Some other options we can use in files “/ etc / exports”For file sharing looks like this.

  1. ro: With this option we can provide read-only access to shared files, i.e. client will only be able read
  2. rw: This option allows client to server access for both for reading and records within the shared directory.
  3. sync: Synchronization confirms requests to the shared directory only after changes were committed.
  4. no_subtree_check: This option prevents checking subtree… When a shared directory is a subdirectory of a larger filesystem, NFS scans every directory above it to check its permissions and details. Disable check subtree can improve reliability NFSbut reduce safety
  5. no_root_squash: This phrase allows root, connect to a specific folder.

For more options with “/ etc / exports“, It is recommended to read pages guidelines for export

NFS client setup

After setting NFS-server, we need mount this shared directory or partition on client server.

Mounting shared directories on an NFS client

Now on NFS client, we need mount this directory to be accessed locally. To do this, first, we need to find out what resources are available on the remote or NFS server.

[[email protected] ~]# showmount -e 192.168.0.55

Export list for 192.168.0.55:
/nfsshare 192.168.0.60

The command shows that the directory named “nfsshare“Available on”192.168.0.55”To share with your server.

Mounting an accessible directory in NFS

In order to mount general NFS directory, we can use the following mount command.

[[email protected] ~]# mount -t nfs 192.168.0.55:/nfsshare /mnt/nfsshare

The above command will set the shared directory to “/ mnt / nfsshare”On the client’s server. You can check it with the following command.

[[email protected] ~]# mount | grep nfs

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.0.55:/nfsshare on /mnt type nfs (rw,addr=192.168.0.55)

The above mount command mounts on NFS shared directory on NFS client temporarily to mount the NFS directory constantly on your system regardless of reboots, we need to make an entry in “/ etc / fstab“.

[[email protected] ~]# vi /etc/fstab

Add the following new line as shown below.

192.168.0.55:/nfsshare /mnt  nfs defauls 0 0

Testing the NFS Installation Behavior

We can test our installing an NFS server by creating test file on the server side and check its presence on NFS client side or vice versa.

Server side nfsserver

We have created a new text file named “nfstest.txt”In this general directory.

[[email protected] ~]# cat > /nfsshare/nfstest.txt

This is a test file to test the working of NFS server setup.

Client side nfsclient

Go to the shared directory on client server and you will find the shared file without any manual update or reboot service.

[[email protected]]# ll /mnt/nfsshare
total 4
-rw-r--r-- 1 root root 61 Sep 21 21:44 nfstest.txt
[email protected] ~]# cat /mnt/nfsshare/nfstest.txt
This is a test file to test the working of NFS server setup.

Removing an NFS mount

If you want to unmount this shared directory from the server after you are done with file sharing you can simply unmount this particular directory using the command “umount“. See this example below.

[email protected] ~]# umount /mnt/nfsshare

You can see that the mount has been removed on the filesystem.

[[email protected] ~]# df -h -F nfs

You will see that these shared directories are no longer available.

Important Commands for NFS

Some more important commands for NFS

  1. showmount -e : Shows available shared objects on the local computer
  2. showmount -e : List of available shared objects on remote server
  3. showmount -d : List of all subdirectory
  4. exportfs -v : Displays a list of shared files and options on server
  5. exportfs -a : Export all available objects listed in / etc / exports, or name
  6. exportfs -u : Re-export of all available objects listed in / etc / exports, or name
  7. exportfs -r : Refresh server list after change / etc / exports

It’s all about mount NFS for now, if you’re interested, you can read the guide on how to set up an NFS server on Ubuntu 16. Leave your comments and suggestions below in the comment box.

Sidebar