Nahashon

Setting up NFS on Linux

What is NFS ?

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

 

1. Installing packages

 

yum update
yum install nfs-utils rpcbind -y
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

# for the services to autostart on boot, use the following
systemctl enable --now nfs-server
systemctl enable --now rpcbind
 

 

3. 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.

 

yum install nfs-utils
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 using 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

 

 

 

Share this article