NSF (Network-base File System) can allow computers (clients) to access files on another computer(server) over the network even the clients have different operating systems from the server. And this article will guide you exstablishing your own NFS service on CentOS 7, which is generally used as server os.


Table of Contents

  1. Prerequisites
  2. NFS and RPC Services
  3. Configure NSF Server
    1. 1. Install Required NFS and RPC Packages
    2. 2. Export Shared Directories
    3. 3. Create Shared Directories
    4. 4. Enable and Start Services
    5. 5. Export the Share
  4. Configure NSF Clients
    1. 1. Install required packages
    2. 2. Mount the Shared NFS Directory
  5. Configure Firewalld for NFS and RPC Services

Prerequisites

To establish your NFS service, you need at least two instances (one as server, and the remains as clients) with CentOS 7 fresh installed. CentOS 7 supports NFSv3 and NFSv4.

NFS and RPC Services

NFS service needs RPC service, which includes:

  • rpcbind.service
  • rpcbind.socket
  • rpcidmapd.service
  • rpc-statd.service

Configure NSF Server

Now we will configure our nfs server supposed that ip is 192.168.0.2.

1. Install Required NFS and RPC Packages

1
sudo yum install -y nfs-utils rpcbind

2. Export Shared Directories

The fortmat of nfs config file /etc/exports is like:

1
/path/to/shared client1_ip (options) [client2_ip(options)]

Suppose you want to expose the dirs to client1 with ip 192.168.0.3, the file is like:

1
/path/to/shared 192.168.0.3(rw,sync,no_wdelay)

One example:

1
/opt/shared 192.168.0.3(rw,sync)

If you wish all clients have rights to access to the server, you can make it by the following configure:

1
/path/to/shared *(ro,sync)

Caution: do not grant writing right to everyone.

3. Create Shared Directories

1
2
3
mkdir -p /path/to/shared
sudo chown -R nobody:nobody /path/to/shared
sudo chmod -R 755 /path/to/shared

4. Enable and Start Services

1
2
3
4
5
6
7
8
sudo systemctl enable --now nfs-server
sudo systemctl enable --now rpcbind
## the following two may not work since CentOS 7.1
sudo systemctl enable --now nfs-lock
sudo systemctl enable --now nfs-idmap
## check nfs status
sudo systemctl status nfs
sudo systemctl status rpcbind

5. Export the Share

1
2
3
4
5
## exportfs -r will re-exports entries in /etc/exports
## and sync /var/lib/nfs/etab with /etc/exports.
sudo exportfs -r
## restart nfs service
sudo systemctl restart nfs-server

Configure NSF Clients

1. Install required packages

1
2
3
4
## install nfs-utils
sudo yum install -y nfs-utils rpcbind
## enable rpcbind needed by nfs
sudo systemctl enable --now rpcbind

2. Mount the Shared NFS Directory

1
2
sudo mkdir -p /local/path
sudo mount -t nfs -orw,nosuid remote_host:/path/to/shared /local/path

The following is an example

1
sudo mount -t nfs -o rw,nosuid 192.168.0.2/opt/shared /opt/shared

This mounts /opt/shared on 192.168.0.2 to /opt/shared on local machine
If you want to mount at boot time, you can add the following line to /etc/fstab, and backup /etc/fstab before changing it:

1
remote_host:/path/to/shared /local/path nfs rw,nosuid 0 0

Configure Firewalld for NFS and RPC Services

If you enable the firewalld service, you need to allow the nfs, mountd, rpc-bind services through the firewall.

1
2
3
4
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --reload

Now you can access files on 192.168.0.2 from clients.