- Linux NFS Server Setup and Configuration With Examples
- Install NFS
- Installing NFS can be done for Ubuntu, Debian, Kali, Mi
- Ubuntu, Debian, Kali, Mint:
- Fedora, CentOS, RedHat:
- Configuration Files
- Ubuntu, Debian, Kali, Mint:
- Fedora, CentOS, RedHat:
- Services
- Export File
- Create Share
- Reload NFS Server
- Ubuntu, Debian, Kali, Mint:
- Fedora,CentOS, RedHat:
- List Shares
- Mount NFS Share
- Unmount NFS
- How to Setup NFS (Network File System) on RHEL/CentOS/Fedora and Debian/Ubuntu
- Setup and Configure NFS Mounts on Linux Server
- Setting Up the NFS Server
- Setting Up the NFS Client
- Test the Working of NFS Setup
- Removing the NFS Mount
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- What is linux nfs server
- Contents
- Installation
- Configuration
- Server
- Starting the server
- Restricting NFS to interfaces/IPs
- Firewall configuration
- Enabling NFSv4 idmapping
- Client
- Manual mounting
- Mount using /etc/fstab
- Mount using /etc/fstab with systemd
- As systemd unit
- Mount using autofs
- Tips and tricks
- Performance tuning
- Automatic mount handling
- systemd/Timers
- Using a NetworkManager dispatcher
- Troubleshooting
Linux NFS Server Setup and Configuration With Examples
Network file system is a simple way to share files and folders over the network. NFS is developed by Sun Microsystems in 1980. NFS is very popular in the Linux and Unix world. NFS is an alternative for SMB which is Windows ecosystem file-sharing protocol. We will look at how to install the NFS server, share files and mount shares in this tutorial with some extra configurations.
Install NFS
Installing NFS can be done for Ubuntu, Debian, Kali, Mi
Ubuntu, Debian, Kali, Mint:
Fedora, CentOS, RedHat:
Manpage for NFS daemon or simple server can get like below.
Man
Configuration Files
NFS server works as a daemon as expected. Daemon configuration file is by default in /etc/default/nfs-kernel-server for Ubuntu-based systems
Ubuntu, Debian, Kali, Mint:
Fedora, CentOS, RedHat:
Services
NFS server has more than one service to share files and folders.
Export File
Shares are generally defined in different configuration files than daemon configuration files. NFS shares are called exports and stored into /etc/exports . Below we can see the default export file.
Export files
As we see in this file some examples of shares are defined in comments which have no effect.
Create Share
As stated previously to create share an entry should be created in the export file. We will create a simple entry that does not have advanced attributes. Say our directory we want to share is located at /share .
Put the following line to the /etc/exports
Reload NFS Server
We have to change the configuration of the NFS server and created a new share to enable the new configuration we should reload the server.
Ubuntu, Debian, Kali, Mint:
Fedora,CentOS, RedHat:
List Shares
NFS has the ability to list shares. This feature is similar to SMB. We will run list command from our client named ubu2 . While trying to list share there can be an error like clnt_create: RPC: Port mapper failure — Unable to receive: errno 113 (No route to host) which simply means a network access error. Generally, firewalls will prevent access to the NFS service to get a share list. Create appropriate rules to access these services.
List NFS Shares
Mount NFS Share
NFS shares can be mounted to the local system with regular mount command and NFS related parameters. To mount NFS share NFS file system libraries must be installed which is provided by NFS packages we have previously installed.
Mount NFS Share
- While mounting shares NFS type provided with -t nfs
- And remote NFS share specified 192.168.122.179:/share
- Local mount path is working directory mnt
Unmount NFS
Unmounting is the same as regular disk-based file systems. This will unmount the NFS file system. We will unmount the path /mnt in this example.
Unmount NFS
Источник
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
- NFS allows local access to remote files.
- It uses standard client/server architecture for file sharing between all *nix based machines.
- With NFS it is not necessary that both machines run on the same OS.
- With the help of NFS we can configure centralized storage solutions.
- Users get their data irrespective of physical location.
- No manual refresh needed for new files.
- Newer version of NFS also supports acl, pseudo root mounts.
- 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.
- portmap : It maps calls made from other machines to the correct RPC service (not required with NFSv4).
- nfs: It translates remote file sharing requests into requests on the local file system.
- rpc.mountd: This service is responsible for mounting and unmounting of file systems.
Important Files for NFS Configuration
- /etc/exports : Its a main configuration file of NFS, all exported files and directories are defined in this file at the NFS Server end.
- /etc/fstab : To mount a NFS directory on your system across the reboots, we need to make an entry in /etc/fstab.
- /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.
- NFS Server: nfsserver.example.com with IP-192.168.0.100
- 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.
NFS Options
Some other options we can use in “/etc/exports” file for file sharing is as follows.
- 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.
- rw: This option allows the client server to both read and write access within the shared directory.
- sync: Sync confirms requests to the shared directory only once the changes have been committed.
- 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.
- 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.
- showmount -e : Shows the available shares on your local machine
- showmount -e: Lists the available shares at the remote server
- showmount -d : Lists all the sub directories
- exportfs -v : Displays a list of shares files and options on a server
- exportfs -a : Exports all shares listed in /etc/exports, or given name
- exportfs -u : Unexports all shares listed in /etc/exports, or given name
- 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.
Источник
What is linux nfs server
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:
- The NFS root is /srv/nfs .
- 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:
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:
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.
Источник