Seting up NFS on Linux

2024-07-28

NFS, Network File System, is a distributed file system protocol used for sharing files and directories over a network.

The files and directories can be accessed as if they were located on the remote computer.

Setting up the server

NFS is a client-server protocol.

1. Installing packages

RedHat-based distros

yum update
yum install nfs-utils rpcbind -y

Debian-based distros

apt-get update
apt-get install nfs-kernel-server rpcbind -y

2. Starting the service

Once the packages are installed, you can start the NFS service.

systemctl start nfs-server
systemctl start rpcbind

When the system is rebooted, the service will not start automatically.

If you intend to have NFS auto start on system reboot, you need to enable both the nfs server and rpc-bind services.

systemctl --enable nfs-server
systemctl --enable rpc-bind

Configuring directories

Files or folders to be accessed by NFS clients are configured in the file /etc/exports.

This file is maintained by the system administrator.

1. Adding entries

A basic entry in this file is in the following format:

[file_or_folder_path] [allowed_ip_or_network](export_options)

Note: there is no space between allowed_ip_or_network and export_options

  • file_or_folder_path

    Absolute path in the server where the folder or file is located.

  • allowed_ip_or_network

    An Ip or Network allowed to access the files or folders.

    Domain names are also accepted values.

    Wildcards characters * and ? and character classes [] can be used.

    • server1.mydomain.local

    • *.mydomain.local

  • export_options [ Optional ]

    A list of options that specify access levels granted to nfs clients.

Multiple IPs or networks can also be specified for the one file or directory entry as follows:

[file_or_folder_path] [allowed_ip_1](export_options) [allowed_ip_2](export_options)

Lines beginning with a # are treated as comments.

Empty lines in the export file are ignored.

Here's an example exports file:

# allow clients by ip
/folders/myfolder1	192.168.22.10(rw,anoguid=34,anouid=34) 
/folders/myfolder1	192.168.22.11(rw,anoguid=34,anouid=34) 

# allow all ips in a network
/folders/myfolder2	192.168.22.0/24(rw,anoguid=12,anouid=12)

# allow multiple networks
/folders/myfolder3	192.168.22.0/24(rw) 192.168.23.0/24(rw,anoguid=12,anouid=12)

# if kerberos is Setup
/folder/myotherfolder	gss/krb5 (rw,anoguid=12,anouid=12)

2. Reloading the exports file

Any time the file, etc/exports has been edited, it needs to be reloaded.

Run the following command to reload the ex

exportfs -r

All the exported fire or directory entries can be listed from the command line as follows.

exportfs -v

Mounting NFS shares

NFS shares can be mounted from any client that has access defined in the exports file, /etc/exports.

To mount an nfs share, the nfs client package must be installed.

RedHat-based distros

yum install nfs-utils

Debian-based distros

apt-get install nfs-common

Once the packages are installed, the NFS share can be mounted either manually or automatically using fstab.

1. Manually mounting nfs shares

  • First create a mount point to mount the nfs share

    mkdir /mount_point

  • Then mount the nfs share

    mount -t nfs [nfs_server_ip]:[nfs_location] [mount_point]

    mount -t nfs 192.168.10.100:/folders/myfolder1 /mount_point

2. Mounting nfs shares from fstab

NFS shares can also be automatically mounted on system reboot by adding them to /etc/fstab .

# <nfs file system>                <dir>         <type>   <options>   <dump>	<pass>
192.168.10.100:/folders/myfolder1  /mount_point  nfs      defaults    0         0