nfs
Installing the Network File System
Network File System (NFS) is a distributed filesystem protocol that allows clients to access remote files and directories as if they are available on the local system. This allows client systems to leverage large centrally shared storage. Users can access the same data from any system across the network. A typical setup for NFS includes a server that runs the NFS daemon, nfsd, and lists (export) files and directories to be shared. A client system can mount these exported directories as their local file system.
In this recipe, we will learn how to install the NFS server and client systems.
You will need two Ubuntu systems: one as a central NFS server and another as a client. For this recipe, we will refer to the NFS server with the name
Host
and the NFS client with the name Client
. The following is an example IP address configuration for the Host
and Client
systems:Host - 10.0.2.60 Client - 10.0.2.61
You will need access to a root account on both servers, or at least an account with
sudo
privileges.
Follow these steps to install NFS:
- First, we need to install the NFS server:
$ sudo apt-get update $ sudo apt-get install nfs-kernel-server
- Create the directories to be shared:
$ sudo mkdir /var/nfs
- Add this directory to NFS exports under
/etc/exports
:$ sudo nano /etc/exports
- Add the following line to
/etc/exports
:/var/nfs *(rw,sync,no_subtree_check)
- Save and close the exports file.
- Now, restart the NFS service:
$ sudo service nfs-kernel-server restart
- Next, we need to configure the client system to access NFS shares.
- Create a mount point for NFS shares.
- Install the
nfs-common
package on the client side:$ sudo apt-get install nfs-common $ sudo mkdir -p /var/nfsshare
- Mount the NFS shared directory on the newly-created mount point:
$ sudo mount 10.0.2.60:/var/nfs /var/nfsshare
- Confirm the mounted share with the following command:
$ mount -t nfs
- Now, change the directory to
/var/nfsshare
, and you are ready to use NFS.
In the preceding example, we have installed the NFS server and then created a directory that will share with clients over the network. The configuration file
/etc/exports
contains all NFS shared directories. The syntax to add new exports is as follows:directory_to_share client_IP_or_name(option1, option2, option..n)
The options used in exports are as follows:
rw
: This enables read/write access. You can enable read-only access with thero
option.sync
: This forces the NFS server to write changes to disk before replying to requests. sync is the default option; you can enable async operations by explicitly stating async. Async operations may get a little performance boost but at the cost of data integrity.no_subtree_check
: This disables subtree checking, which provides more stable and reliable NFS shares.
You can check the
exports
documentation for more export options. Use the man
command to open the exports
manual pages, as follows:$ man exports
In the preceding example, we have used the
mount
command to mount the NFS share. Once the client system has restarted, this mount will be removed. To remount the NFS share on each reboot, you can add the following line to /etc/fstab
file:10.0.2.60:/var/nfs /var/nfsshare nfs4 _netdev,auto 0 0
To mount all shares exported by the NFS server, you can use the following command:
$ sudo mount 10.0.2.60:/ /var/nfsshare
NFS is built on top of RPC authentication. With NFS version 3, the most common authentication mechanism is AUTH_UNIX. The user id and group id of the client system are sent in each RPC call, and the permissions these IDs have on the file being accessed are checked on the server. For this to work, the UID and GIDs must be the same on the server and the clients. However, you can force all access to occur as a single user and group by combining the all_squash, anonuid, and anongid export options. all_squash will map all UIDs and GIDs to the anonymous user, and anonuid and anongid set the UID and GID of the anonymous user. For example, if your UID and GID on your dev server are both 1001, you could export your home directory with a line like
/home/darren 192.168.1.1/24(rw,all_squash,anonuid=1001,anongid=1001)
I'm less familiar with NFS version 4, but I think you can set up rpc.idmapd on the clients to alter the uid and gid they send to the server.
See also
- NFS exports options at http://manpages.ubuntu.com/manpages/trusty/man5/exports.5.html
- Parallel NFS at http://www.pnfs.com/
- NFS documentation in manual pages, by using the following command:
$ man nfs
Comments
Post a Comment