Network file system nfs linux

Network file system nfs linux

Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems in 1984, allowing a user on a client computer to access files over a network in a manner similar to how local storage is accessed.

Contents

Installation

Both client and server only require the installation of the nfs-utils package.

It is highly recommended to use a time synchronization daemon to keep client/server clocks in sync. Without accurate clocks on all nodes, NFS can introduce unwanted delays.

Configuration

Server

Global configuration options are set in /etc/nfs.conf . Users of simple configurations should not need to edit this file.

The NFS server needs a list of exports (see exports(5) for details) which are defined in /etc/exports or /etc/exports.d/*.exports . These shares are relative to the so-called NFS root. A good security practice is to define a NFS root in a discrete directory tree which will keep users limited to that mount point. Bind mounts are used to link the share mount point to the actual directory elsewhere on the filesystem.

Consider this following example wherein:

  1. The NFS root is /srv/nfs .
  2. The export is /srv/nfs/music via a bind mount to the actual target /mnt/music .

To make the bind mount persistent across reboots, add it to fstab:

Add directories to be shared and limit them to a range of addresses via a CIDR or hostname(s) of client machines that will be allowed to mount them in /etc/exports , e.g.:

It should be noted that modifying /etc/exports while the server is running will require a re-export for changes to take effect:

To view the current loaded exports state in more detail, use:

For more information about all available options see exports(5) .

Starting the server

Restricting NFS to interfaces/IPs

By default, starting nfs-server.service will listen for connections on all network interfaces, regardless of /etc/exports . This can be changed by defining which IPs and/or hostnames to listen on.

Restart nfs-server.service to apply the changes immediately.

Firewall configuration

To enable access through a firewall, TCP and UDP ports 111 , 2049 , and 20048 may need to be opened when using the default configuration; use rpcinfo -p to examine the exact ports in use on the server:

When using NFSv4, make sure TCP port 2049 is open. No other port opening should be required:

When using an older NFS version, make sure other ports are open:

To have this configuration load on every system start, edit /etc/iptables/iptables.rules to include the following lines:

The previous commands can be saved by executing:

If using NFSv3 and the above listed static ports for rpc.statd and lockd the following ports may also need to be added to the configuration:

To apply changes, Restart iptables.service .

Enabling NFSv4 idmapping

This article or section needs expansion.

The NFSv4 protocol represents the local system’s UID and GID values on the wire as strings of the form user@domain . The process of translating from UID to string and string to UID is referred to as ID mapping. See nfsidmap(8) for details.

Even though idmapd may be running, it may not be fully enabled. If /sys/module/nfs/parameters/nfs4_disable_idmapping or /sys/module/nfsd/parameters/nfs4_disable_idmapping returns Y on a client/server, enable it by:

Set as module option to make this change permanent, i.e.:

To fully use idmapping, make sure the domain is configured in /etc/idmapd.conf on both the server and the client:

See [2] for details.

Client

Users intending to use NFS4 with Kerberos need to start and enable nfs-client.target .

Manual mounting

For NFSv3 use this command to show the server’s exported file systems:

For NFSv4 mount the root NFS directory and look around for available mounts:

Читайте также:  Что будет с файлами после обновления до windows 10

Then mount omitting the server’s NFS export root:

If mount fails try including the server’s export root (required for Debian/RHEL/SLES, some distributions need -t nfs4 instead of -t nfs ):

Mount using /etc/fstab

Using fstab is useful for a server which is always on, and the NFS shares are available whenever the client boots up. Edit /etc/fstab file, and add an appropriate line reflecting the setup. Again, the server’s NFS export root is omitted.

Some additional mount options to consider:

rsize and wsize The rsize value is the number of bytes used when reading from the server. The wsize value is the number of bytes used when writing to the server. By default, if these options are not specified, the client and server negotiate the largest values they can both support (see nfs(5) for details). After changing these values, it is recommended to test the performance (see #Performance tuning). soft or hard Determines the recovery behaviour of the NFS client after an NFS request times out. If neither option is specified (or if the hard option is specified), NFS requests are retried indefinitely. If the soft option is specified, then the NFS client fails a NFS request after retrans retransmissions have been sent, causing the NFS client to return an error to the calling application.

Mount using /etc/fstab with systemd

Another method is using the x-systemd.automount option which mounts the filesystem upon access:

To make systemd aware of the changes to fstab, reload systemd and restart remote-fs.target [3].

The factual accuracy of this article or section is disputed.

As systemd unit

Create a new .mount file inside /etc/systemd/system , e.g. mnt-myshare.mount . See systemd.mount(5) for details.

What= path to share

Where= path to mount the share

Options= share mounting options

To use mnt-myshare.mount , start the unit and enable it to run on system boot.

automount

To automatically mount a share, one may use the following automount unit:

Disable/stop the mnt-myshare.mount unit, and enable/start mnt-myshare.automount to automount the share when the mount path is being accessed.

Mount using autofs

Using autofs is useful when multiple machines want to connect via NFS; they could both be clients as well as servers. The reason this method is preferable over the earlier one is that if the server is switched off, the client will not throw errors about being unable to find NFS shares. See autofs#NFS network mounts for details.

Tips and tricks

Performance tuning

When using NFS on a network with a significant number of clients one may increase the default NFS threads from 8 to 16 or even a higher, depending on the server/network requirements:

It may be necessary to tune the rsize and wsize mount options to meet the requirements of the network configuration.

In recent linux kernels (>2.6.18) the size of I/O operations allowed by the NFS server (default max block size) varies depending on RAM size, with a maximum of 1M (1048576 bytes), the max block size of the server will be used even if nfs clients requires bigger rsize and wsize . See https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/5.8_Technical_Notes/Known_Issues-kernel.html It is possible to change the default max block size allowed by the server by writing to the /proc/fs/nfsd/max_block_size before starting nfsd. For example, the following command restores the previous default iosize of 32k:

To make the change permanent, create a systemd-tmpfile:

To mount with the increased rsize and wsize mount options:

Furthermore, despite the violation of NFS protocol, setting async instead of sync or sync,no_wdelay may potentially achieve a significant performance gain especially on spinning disks. Configure exports with this option and then execute exportfs -arv to apply.

Automatic mount handling

This trick is useful for NFS-shares on a wireless network and/or on a network that may be unreliable. If the NFS host becomes unreachable, the NFS share will be unmounted to hopefully prevent system hangs when using the hard mount option [5].

Make sure that the NFS mount points are correctly indicated in fstab:

Create the auto_share script that will be used by cron or systemd/Timers to use ICMP ping to check if the NFS host is reachable:

Читайте также:  What is an authenticated user in windows

in the auto_share script above.

Make sure the script is executable.

Next check configure the script to run every X, in the examples below this is every minute.

systemd/Timers

Finally, enable and start auto_share.timer .

Using a NetworkManager dispatcher

NetworkManager can also be configured to run a script on network status change.

The easiest method for mount shares on network status change is to symlink the auto_share script:

However, in that particular case unmounting will happen only after the network connection has already been disabled, which is unclean and may result in effects like freezing of KDE Plasma applets.

The following script safely unmounts the NFS shares before the relevant network connection is disabled by listening for the pre-down and vpn-pre-down events, make the script is executable:

Create a symlink inside /etc/NetworkManager/dispatcher.d/pre-down to catch the pre-down events:

Troubleshooting

There is a dedicated article NFS/Troubleshooting.

Источник

How to Setup NFS (Network File System) on RHEL/CentOS/Fedora and Debian/Ubuntu

NFS (Network File System) is basically developed for sharing of files and folders between Linux/Unix systems by Sun Microsystems in 1980. It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. With the help of NFS, we can set up file sharing between Unix to Linux system and Linux to Unix system.

Setup NFS Server and NFS Client in Linux

Benefits of NFS
  1. NFS allows local access to remote files.
  2. It uses standard client/server architecture for file sharing between all *nix based machines.
  3. With NFS it is not necessary that both machines run on the same OS.
  4. With the help of NFS we can configure centralized storage solutions.
  5. Users get their data irrespective of physical location.
  6. No manual refresh needed for new files.
  7. Newer version of NFS also supports acl, pseudo root mounts.
  8. Can be secured with Firewalls and Kerberos.
NFS Services

Its a System V-launched service. The NFS server package includes three facilities, included in the portmap and nfs-utils packages.

  1. portmap : It maps calls made from other machines to the correct RPC service (not required with NFSv4).
  2. nfs: It translates remote file sharing requests into requests on the local file system.
  3. rpc.mountd: This service is responsible for mounting and unmounting of file systems.
Important Files for NFS Configuration
  1. /etc/exports : Its a main configuration file of NFS, all exported files and directories are defined in this file at the NFS Server end.
  2. /etc/fstab : To mount a NFS directory on your system across the reboots, we need to make an entry in /etc/fstab.
  3. /etc/sysconfig/nfs : Configuration file of NFS to control on which port rpc and other services are listening.

Setup and Configure NFS Mounts on Linux Server

To setup NFS mounts, we’ll be needing at least two Linux/Unix machines. Here in this tutorial, I’ll be using two servers.

  1. NFS Server: nfsserver.example.com with IP-192.168.0.100
  2. NFS Client : nfsclient.example.com with IP-192.168.0.101
Installing NFS Server and NFS Client

We need to install NFS packages on our NFS Server as well as on NFS Client machine. We can install it via “yum” (Red Hat Linux) and “apt-get” (Debian and Ubuntu) package installers.

Now start the services on both machines.

After installing packages and starting services on both the machines, we need to configure both the machines for file sharing.

Setting Up the NFS Server

First we will be configuring the NFS server.

Configure Export directory

For sharing a directory with NFS, we need to make an entry in “/etc/exports” configuration file. Here I’ll be creating a new directory named “nfsshare” in “/” partition to share with client server, you can also share an already existing directory with NFS.

Now we need to make an entry in “/etc/exports” and restart the services to make our directory shareable in the network.

In the above example, there is a directory in / partition named “nfsshare” is being shared with client IP “192.168.0.101” with read and write (rw) privilege, you can also use hostname of the client in the place of IP in above example.

Читайте также:  Запуск net приложений под linux
NFS Options

Some other options we can use in “/etc/exports” file for file sharing is as follows.

  1. ro: With the help of this option we can provide read only access to the shared files i.e client will only be able to read.
  2. rw: This option allows the client server to both read and write access within the shared directory.
  3. sync: Sync confirms requests to the shared directory only once the changes have been committed.
  4. no_subtree_check: This option prevents the subtree checking. When a shared directory is the subdirectory of a larger file system, nfs performs scans of every directory above it, in order to verify its permissions and details. Disabling the subtree check may increase the reliability of NFS, but reduce security.
  5. no_root_squash: This phrase allows root to connect to the designated directory.

For more options with “/etc/exports“, you are recommended to read the man pages for export.

Setting Up the NFS Client

After configuring the NFS server, we need to mount that shared directory or partition in the client server.

Mount Shared Directories on NFS Client

Now at the NFS client end, we need to mount that directory in our server to access it locally. To do so, first we need to find out that shares available on the remote server or NFS Server.

Above command shows that a directory named “nfsshare” is available at “192.168.0.100” to share with your server.

Mount Shared NFS Directory

To mount that shared NFS directory we can use following mount command.

The above command will mount that shared directory in “/mnt/nfsshare” on the client server. You can verify it following command.

The above mount command mounted the nfs shared directory on to nfs client temporarily, to mount an NFS directory permanently on your system across the reboots, we need to make an entry in “/etc/fstab“.

Add the following new line as shown below.

Test the Working of NFS Setup

We can test our NFS server setup by creating a test file on the server end and check its availability at nfs client side or vice-versa.

At the nfsserver end

I have created a new text file named “nfstest.txt’ in that shared directory.

At the nfsclient end

Go to that shared directory in client server and you’ll find that shared file without any manual refresh or service restart.

Removing the NFS Mount

If you want to unmount that shared directory from your server after you are done with the file sharing, you can simply unmount that particular directory with “umount” command. See this example below.

You can see that the mounts were removed by then looking at the filesystem again.

You’ll see that those shared directories are not available any more.

Important commands for NFS

Some more important commands for NFS.

  1. showmount -e : Shows the available shares on your local machine
  2. showmount -e: Lists the available shares at the remote server
  3. showmount -d : Lists all the sub directories
  4. exportfs -v : Displays a list of shares files and options on a server
  5. exportfs -a : Exports all shares listed in /etc/exports, or given name
  6. exportfs -u : Unexports all shares listed in /etc/exports, or given name
  7. exportfs -r : Refresh the server’s list after modifying /etc/exports

This is it with NFS mounts for now, this was just a start, I’ll come up with more option and features of NFS in our future articles. Till then, Stay connected with Tecmint.com for more exciting and interesting tutorials in future. Do leave your comments and suggestions below in the comment box.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Оцените статью