The Network File System is a fast and light way to share files over an internal Local Area Network with Linux (on PC or in embedded devices like smart TVs and media centers), Unix and MacOS/OSX clients. Depending on your needs, you may want to use Samba or the SSH Filesystem too or instead.
In this tutorial we will create the following setup: nfs shares available to devices in LAN. nfs will create a “virtual” root on the exported filesystem, this prevents users from manipulating files outside of the shared folder. (fsid=root) nfs won't be checking if accessed tree of dirs is in the NFS filesystem because above option makes sure they can't get out of it anyway (no_subtree_check, default option so we don't need to write this in the config) nfs treats all users who access this disk's contents (create, read, modify, delete) as anonymous. (all_squash) nfs replies to requests before any changes made by that requests have been committed to the storage (async) nfs allows access from clients that don't use a reserved port for NFS (insecure)
In the tutorial we assume that you have already set up the storage system with the folder you want to share with nfs, if you didn't do that yet, please do it before proceeding.
Note that for this to work you will need to change read/write permissions to the folder you are sharing.
chmod -R a+rw /mnt/share
Optimal way would be to change the group of the folder to “users” and have r/w added only to the group, or anyway use the POSIX user/group ownerships.
For most users a single free-for-all share like this is enough, so for now it will stay like this.
We will now need to set up the firewall to open ports needed by this service.
The portmap service uses port 111 on both TCP and UDP, nfsd standard are ports between 32777 and 32780 on both TCP and UDP.
These ports are opened automatically on LAN when you install the packages. You can check yourself with
Install nfs-kernel-server metapackage, it will pull down all other needed packages for you.
root@LEDE:~# opkg install nfs-kernel-server Installing nfs-kernel-server (2.1.1-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/packages/x86_64/packages/nfs-kernel-server_2.1.1-1_x86_64.ipk Installing libwrap (7.6-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/packages/x86_64/packages/libwrap_7.6-1_x86_64.ipk Installing librpc (2015-11-04-a921e3de-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/packages/x86_64/base/librpc_2015-11-04-a921e3de-1_x86_64.ipk Installing kmod-fs-nfs-common (4.4.61-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/targets/x86/64/packages/kmod-fs-nfs-common_4.4.61-1_x86_64.ipk Installing kmod-fs-exportfs (4.4.61-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/targets/x86/64/packages/kmod-fs-exportfs_4.4.61-1_x86_64.ipk Installing kmod-fs-nfsd (4.4.61-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/targets/x86/64/packages/kmod-fs-nfsd_4.4.61-1_x86_64.ipk Installing kmod-dnsresolver (4.4.61-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/targets/x86/64/packages/kmod-dnsresolver_4.4.61-1_x86_64.ipk Installing kmod-fs-nfs (4.4.61-1) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/targets/x86/64/packages/kmod-fs-nfs_4.4.61-1_x86_64.ipk Installing portmap (6.0-4) to root... Downloading http://downloads.lede-project.org/releases/17.01.1/packages/x86_64/packages/portmap_6.0-4_x86_64.ipk Configuring kmod-fs-nfs-common. Configuring kmod-fs-exportfs. Configuring kmod-fs-nfsd. Configuring libwrap. Configuring librpc. Configuring portmap. Configuring kmod-dnsresolver. Configuring kmod-fs-nfs. Configuring nfs-kernel-server. exportfs: /etc/exports : Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/mnt". Assuming default behaviour ('no_subtree_check'). NOTE: this default has changed since nfs-utils version 1.0.x
NOTE: You may need to install kmod-loop to fix “mount: can't setup loop device: No such file or directory” errors. (see https://dev.openwrt.org/ticket/11541)
WIP (should work the same as with most linux, once you install the packages for the client)
Most Linux distributions support NFS without need to install anything, or provide pre-configured NFS clients as installable packages. In case your distribution is missing support, you need to install the client software. ArchLinux Wiki is a good starting point: https://wiki.archlinux.org/index.php/NFS.
MacOS X 10.2 and later also supports NFS natively.
For Windows it's a bit more complex as you may or may not have it depending on Windows version and type, also the native one does not really perform that well. JFtp is a third party client, but there are probably others too. Quite frankly, if you want to share files with Windows it's far better to set up Samba instead.
Use the file
/etc/exports to configure your shares.
These are the default contents:
This is what you should write in it if you have a shared folder in /mnt/share
First goes the path of the shared folder, (/mnt).
Then goes the IP list of clients (in this case * as any IP is accepted)
Then there is a list of options for this share, that are nfs options you can read in the nfs manpage
Here is another example, showing how you can write IPs and their netmasks:
/mnt/sda2 192.168.1.2,192.168.1.3,192.168.1.4(ro,sync) /mnt/sda3 192.168.1.2(rw,sync) /mnt/sda4 192.168.1.0/255.255.255.0(rw,sync) /mnt/sda5 192.168.1.0/24(rw,sync)
If you set up extroot on an nfs share, use the path on /overlay/ partition, else you cannot export the mounted fs.
When NFS services are already running, use the command service nfsd reload to reload and apply changes on the fly.
NFS services are usually enabled and started on installation, verify with
ps whether the services are running.
The following entries should appear in the process list:
/usr/sbin/rpc.mountd -p 32780 /usr/sbin/rpc.statd -p 32778 -o 32779 /usr/sbin/portmap
in case they are not then you need to do this manually.
root@LEDE:~# service portmap start && service portmap enable root@LEDE:~# service nfsd start && service nfsd enable
netstat -l command to see whether portmap is listening on port 111 for both tcp and udp. The nfsd process may use varying ports.
sudo mount 192.168.1.1:/mnt/share /home/alby/nfs_share
Or mount permanently with entries in the
/etc/fstab on each client PC:
192.168.1.1:/mnt/sda2 /media/LEDE nfs ro,async,auto 0 0 192.168.1.1:/mnt/sda4 /media/remote_stuff nfs rw,async,auto 0 0
Check the Arch Wiki for more info. WARNING: If you are using a Linux distro with systemd init system (Debian/Unbuntu/Arch/OpenSUSE/Fedora/CentOS and derivatives, most major distros use it) always place “nofail” in nfs mount options. This will tell systemd that this partition isn't critical for boot so your PC will start up fine even if the NFS share is unavailable. If you don't add this option and the NFS share isn't available on boot, your PC will not start up at all and you will have to use a Linux live-cd to go and fix the fstab entry.
With portmap running on your OpenWrt-Machine you can use
rpcinfo -p 192.168.1.254 on clients side to see open ports.
This is a tutorial There are many other tutorials if you search on the net.
Java client: JFtp.
Native client is avalible only for some editions of windows, including:
The NFS Services is turned off by default. You have to enable them on Start > Control Panel > Programs > Turn Windows features on or off > Services for NFS. Then you could map the network drives on My Computer.
If the loopback device support is missing, an error like “Cannot register service: RPC: Timed out” may appear. Installing the kmod-loop package should solve this issue.
number of threads usable by nfsd can be increased by echo X > /proc/fs/nfsd/threads
Max block size can be changed similar way, it's here /proc/fs/nfsd/max_block_size
Client-side you can add these mount options async,rsize=XXX,wsize=XXX,noatime
rsize and wsize specify the size of read and writes, increasing or decreasing them can make a difference, depending on network configuration. Max size is stated in /proc/fs/nfsd/max_block_size and is 16384 bytes (16 KiB) in a default install.
Since netfilter will track every connection, if you use MASQUERADING for example, you could disable con-tracking for data connections by adding this in your /etc/firewall.user:
IPT=iptables NET_LAN=192.168.1.1/24 IF_LAN=eth0 $IPT -t raw -A PREROUTING -i $IF_LAN -s $NET_LAN -p tcp --dport 32777:32780 -j CT --notrack #---------- don't track nfs $IPT -t raw -A PREROUTING -i $IF_LAN -s $NET_LAN -p udp --dport 32777:32780 -j CT --notrack #---------- don't track nfs $IPT -t raw -A OUTPUT -o $IF_LAN -d $NET_LAN -p tcp --sport 32777:32780 -j CT --notrack #---------- don't track nfs $IPT -t raw -A OUTPUT -o $IF_LAN -d $NET_LAN -p udp --sport 32777:32780 -j CT --notrack #---------- don't track nfs