Home » Setup Linux NFS Server and Client

Setup Linux NFS Server and Client

by tuanlp

 Network File Sharing (NFS) is a protocol that allows you to share directories and files with other Linux clients over a network. Shared directories are typically created on a file server, running the NFS server component. Users add files to them, which are then shared with other users who have access to the folder. An NFS file share is mounted on a client machine, making it available just like folders the user created locally. NFS is particularly useful when disk space is limited and you need to exchange public data between client computers.


Below are the high-level steps we will follow to set up our NFS.

1. Install and activate nfs server on NFS Server
2. Create the NFS Share Folder and export it
3. Install nfs client and mount the NFS Share Folder on Client


Below are setup details and the same will be used in this demonstration.

Sr. No. Hostname IP Role
1 test-machine01 192.168.114.177 NFS Server
2 test-machine02 192.168.114.176 NFS Client


Step 1. Install and activate NFS ServerInstall NFS Server using OS yum command. Once installation is done enable nfs service and add port in firewall using below commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@test-machine01 u01]# yum -y install nfs-utils
Loaded plugins: langpacks, ulninfo
Repository ol7_UEKR5 is listed more than once in the configuration
Dependencies Resolved
 
==============================================================================================================================================================================================
 Package                                    Arch                                    Version                                                 Repository                                   Size
==============================================================================================================================================================================================
Updating:
 nfs-utils                                  x86_64                                  1:1.3.0-0.68.0.1.el7.1                                  ol7_latest                                  412 k
 
Transaction Summary
==============================================================================================================================================================================================
Upgrade  1 Package
Updated:
  nfs-utils.x86_64 1:1.3.0-0.68.0.1.el7.1
 
Complete!
[root@test-machine01 u01]#
 
[root@test-machine01 psql_backup]# systemctl enable nfs-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
 
[root@test-machine01 psql_backup]# systemctl start nfs-server.service
 
[root@test-machine01 psql_backup]#
[root@test-machine01 psql_backup]# systemctl status nfs-server.service
â nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2021-08-15 10:01:55 +03; 39s ago
  Process: 63520 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 63506 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 63504 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 63506 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service
 
Aug 15 10:01:54 test-machine01 systemd[1]: Starting NFS server and services...
Aug 15 10:01:55 test-machine01 systemd[1]: Started NFS server and services.
[root@test-machine01 psql_backup]#
 
[root@test-machine01 psql_backup]# firewall-cmd --permanent --add-service=nfs
success
[root@test-machine01 psql_backup]# firewall-cmd --reload
success
[root@test-machine01 psql_backup]#


Step 2Create the NFS Share Folder and export it on NFS Serve: We will be using /u01/psql_backup directory as our NFS share. Edit nfs configuration file/etc/exports and add the below line.

*: This mount has access to any system in the Network
rw: Read Write
sync: Any update in file from the Client-side and it will update in Server also.

1
2
3
4
5
6
7
8
9
10
11
12
[root@test-machine01 psql_backup]# mkdir /u01/psql_backup
[root@test-machine01 psql_backup]#
[root@test-machine01 psql_backup]# chown postgres:postgres /u01/psql_backup
[root@test-machine01 psql_backup]#
[root@test-machine01 psql_backup]# vi /etc/exports
/u01/psql_backup *(rw,sync)
:wq!
[root@test-machine01 psql_backup]# cat /etc/exports
/u01/psql_backup *(rw,sync)
[root@test-machine01 psql_backup]#
[root@test-machine01 psql_backup]# exportfs -a
[root@test-machine01 psql_backup]#


Step 3. Install NFS client and mount the NFS Share Folder on NFS Client: Use OS yum command to install nfs package. Once the package is installed, you can use the command showmount to search for available NFS Server. Add NFS Share details in /etc/fstab so each time Server mount NFS drive automatically during server reboot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[root@test-machine02 etc]# yum -y install nfs-utils
Loaded plugins: langpacks, ulninfo
Dependencies Resolved
 
==============================================================================================================================================================================================
 Package                                          Arch                                  Version                                               Repository                                 Size
==============================================================================================================================================================================================
Installing:
 nfs-utils                                        x86_64                                1:1.3.0-0.68.0.1.el7.1                                ol7_latest                                412 k
Installing for dependencies:
 gssproxy                                         x86_64                                0.7.0-30.el7_9                                        ol7_latest                                110 k
 keyutils                                         x86_64                                1.5.8-3.el7                                           ol7_latest      
Installed:
  nfs-utils.x86_64 1:1.3.0-0.68.0.1.el7.1
Transaction Summary
==============================================================================================================================================================================================
Install  1 Package (+10 Dependent packages)
 
Dependency Installed:
  gssproxy.x86_64 0:0.7.0-30.el7_9     keyutils.x86_64 0:1.5.8-3.el7     libbasicobjects.x86_64 0:0.1.1-32.el7  libcollection.x86_64 0:0.7.0-32.el7  libevent.x86_64 0:2.0.21-4.el7
  libini_config.x86_64 0:1.3.1-32.el7  libnfsidmap.x86_64 0:0.25-19.el7  libpath_utils.x86_64 0:0.2.1-32.el7    libref_array.x86_64 0:0.1.5-32.el7   libverto-libevent.x86_64 0:0.2.5-4.el7
 
Complete!
[root@test-machine02 etc]#
 
[root@test-machine02 etc]#
[root@test-machine02 etc]# showmount -e test-machine01
Export list for test-machine01:
/u01/psql_backup *
[root@test-machine02 etc]#
 
[root@test-machine02 u01]#
[root@test-machine02 u01]# vi /etc/fstab
test-machine01:/u01/psql_backup /u01/psql_backup nfs rw,sync,hard,intr 0 0
:wq!
[root@test-machine02 u01]#
[root@test-machine02 u01]# cat /etc/fstab
test-machine01:/u01/psql_backup /u01/psql_backup nfs rw,sync,hard,intr 0 0
[root@test-machine02 u01]#
[root@test-machine02 u01]# mkdir /u01/psql_backup
[root@test-machine02 u01]#
[root@test-machine02 u01]# chown postgres:postgres /u01/psql_backup
[root@test-machine02 u01]#
[root@test-machine02 u01]# mount /u01/psql_backup
[root@test-machine02 u01]#
[root@test-machine02 u01]# df -h /u01/psql_backup
Filesystem                       Size  Used Avail Use% Mounted on
test-machine01:/u01/psql_backup   60G   42G   19G  69% /u01/psql_backup
[root@test-machine02 u01]#

You may also like