Linux Iptables Allow NFS Clients to Access the NFS Server

last updated in Categories , , , , , , , , , ,

The portmapper assigns each NFS service to a port dynamically at service startup time. How do I allow legitimate NFS clients to access the NFS server using RHEL / Fedora / CentOS Linux 5.x iptables firewall?

You need to open the following ports:
a] TCP/UDP 111 – RPC 4.0 portmapper

b] TCP/UDP 2049 – NFSD (nfs server)

c] Portmap static ports – Various TCP/UDP ports defined in /etc/sysconfig/nfs file.

Configure NFS Services to Use Fixed Ports

However, NFS and portmap are pretty complex protocols. Firewalling should be done at each host and at the border firewalls to protect the NFS daemons from remote
access, since NFS servers should never be accessible from outside the organization. However, by default, the portmapper assigns each NFS service to a port dynamically at service startup time.

Dynamic ports cannot be protected by port filtering firewalls such as iptables. First, you need to configure NFS services to use fixed ports. Open /etc/sysconfig/nfs, enter:
# vi /etc/sysconfig/nfs
Modify config directive as follows to set TCP/UDP unused ports:

# TCP port rpc.lockd should listen on.
# UDP port rpc.lockd should listen on.
# Port rpc.mountd should listen on.
# Port rquotad should listen on.
# Port rpc.statd should listen on.
# Outgoing port statd should used. The default is port is random

Here is sample listing from one of my production NFS server:


Save and close the files. Restart NFS and portmap services:
# service portmap restart
# service nfs restart
# service rpcsvcgssd restart

Update /etc/sysconfig/iptables files

Open /etc/sysconfig/iptables, enter:
# vi /etc/sysconfig/iptables
Add the following lines, ensuring that they appear before the final LOG and DROP lines for the RH-Firewall-1-INPUT chain:

-A RH-Firewall-1-INPUT -s -m state --state NEW -p udp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -s -m state --state NEW -p tcp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -s -m state --state NEW -p tcp --dport 2049 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p tcp --dport 32803 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p udp --dport 32769 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p tcp --dport 892 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p udp --dport 892 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p tcp --dport 875 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p udp --dport 875 -j ACCEPT
-A RH-Firewall-1-INPUT -s  -m state --state NEW -p tcp --dport 662 -j ACCEPT
-A RH-Firewall-1-INPUT -s -m state --state NEW -p udp --dport 662 -j ACCEP

Save and close the file. Replace with your actual LAN subnet /mask combo. You need to use static port values defined by /etc/sysconfig/nfs config file. Restart iptables service:
# service iptables restart

This entry is 6 of 15 in the Linux / UNIX NFS File Server Tutorial series. Keep reading the rest of the series:
  1. CentOS / Redhat: Setup NFS v4.0 File Server
  2. Debian / Ubuntu Linux: Setup NFSv4 File Server
  3. Mac Os X: Mount NFS Share / Set an NFS Client
  4. RHEL: How Do I Start and Stop NFS Service?
  5. How To Restart Linux NFS Server Properly When Network Become Unavailable
  6. Linux Iptables Allow NFS Clients to Access the NFS Server
  7. Debian / Ubuntu Linux Disable / Remove All NFS Services
  8. Linux: Tune NFS Performance
  9. Mount NFS file system over a slow and busy network
  10. Linux Track NFS Directory / Disk I/O Stats
  11. Linux Disable / Remove All NFS Services
  12. Linux: NFS4 mount Error reason given by server: No such file or directory
  13. Linux NFS Mount: wrong fs type, bad option, bad superblock on fs2:/data3 Error And Solution
  14. CentOS / RHEL CacheFS: Speed Up Network File System (NFS) File Access
  15. Increase NFS Client Mount Point Security


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

21 comment

  1. I know the title specifies using IPtables to allow NFS connections, but I prefer a simpler method. I prefer to use the hosts.allow and hosts.deny files, as the configuration is *much* simpler.

    Within the hosts.allow simply set:

    That’s all you need. Because portmap is the lead process, spawning and defining ports for the other services, if you simple limit access to portmap you’ve effectively set the same access restrictions.

  2. Christer.

    host.allows simply protects only the portmap service itself. It is still possible for attackers to guess the port numbers of NFS services and attack those services directly, even if they are denied access to the portmapper. Combining both iptables and host.allows should provide the better security.

    Appreciate your post!

  3. How do you restart nlockmgr before reloading the iptables firewall? Preferably without breaking the NFS clients that are active….


  4. btw (sorry to post twice in quick succession), I noticed that LOCKD_TCPPORT (32803) isn’t included in your IPTABLES rules.. it is necessary to uncomment this line in /etc/sysconfig/iptables?

  5. BTW, Vivek:

    Admin’s will also have to restart the nfslock service in order for rpc.statd and lockd to re-read their configurations.

    # service portmap restart
    # service nfs restart
    # service nfslock restart

    [root@vm2 ~]# for x in MOUNTD_ STATD_ LOCKD_ RQUOTAD_ ; do echo "Scripts that containt \"$x...\"" ; grep $x /etc/init.d/* | cut -d':' -f 1 | uniq ; echo ; done
    Scripts that containt "MOUNTD_..."

    Scripts that containt "STATD_..."

    Scripts that containt "LOCKD_..."

    Scripts that containt "RQUOTAD_..."

  6. TCP Wrapper (a.k.a. , libwrap, hosts_access(), and/or hosts.allow & hosts.deny) can be used to restrict access to portmap and to nfs: From the Red Hat Enterprise Linux 5 mountd man page…

    rpc.mountd(8) rpc.mountd(8)

    rpc.mountd – NFS mount daemon

    This rpc.mountd version is protected by the tcp_wrapper library. You have to give the clients access to rpc.mountd if they should be allowed to use it. To allow connects from clients of the domain you could use the following line in /etc/hosts.allow:


    You have to use the daemon name mountd for the daemon name (even if the binary has a different name).

    …and according to The Linux NFS-HOWTO all five services can be protected using TCP Wrapper…

    The first step in doing this is to add the followng entry to /etc/hosts.deny:


    Starting with nfs-utils 0.2.0, you can be a bit more careful by controlling access to individual daemons. It’s a good precaution since an intruder will often be able to weasel around the portmapper. If you have a newer version of nfs-utils, add entries for each of the NFS daemons (see the next section to find out what these daemons are; for now just put entries for them in hosts.deny):


  7. It seemed the udp port 2049 should also be enabled for NFSD, right?

    “-A RH-Firewall-1-INPUT -s -m state –state NEW -p udp –dport 2049 -j ACCEPT”

    1. Roc,

      In the case of having full control over your clients, who *should* connect over TCP your added firewall rule is not needed.
      But, if you perform a remote install – of CentOS / RedHat 5.5 in my case – over NFS, it is absolutely needed.
      Message on the client after a noticeable delay “That directory could not be mounted from the server.”

      I personally believe it’s a bug caused by migration of scripts/config files from RH3 via RH4 to RH5 or maybe earlier already.

      For me your post came too late, but nevertheless thanks and a confirm of its need in certain circumstances.

      Han Coumans

    2. Confirmed!

      I wanted to boot knoppix via PXE. Knoppix is mounted via nfs and needed udp (this line in /etc/sysconfig/iptables) to work


  8. Vivek,

    I just used this fix on Centos 5.5 and it works fine. (I tried Christer Edwards simple solution first and that didn’t). So many thanks.

    But – isn’t it horrible to have to do all this just to get NFS working? There is a graphical tool for NFS in Server Settings. I see now this allows fixed NFS ports to be set, if you knew this to be necessary. But I don’t think it fixes iptables. I did use system-config-securitylevel to add the ports – which worked. At the least, there should be a step by step guide on the Centos Wiki. If you don’t mind, I might have a go at that. Thanks again.

  9. Do you also need:

    -A RH-Firewall-1-INPUT -s -m state –state NEW -p udp –dport 2049 -j ACCEPT


  10. Indeed very useful.
    The only thing is really to *write* the /etc/sysconfig/nfs file with the lines /xxPORT=/ uncommented! even if the commented values matches the wanted ones …

  11. On CentOS (and probably RedHat and Fedora), a “newbie” method is to use the graphical tool : “system-config-firewall”

  12. Hi,

    I’m using CentOS 6.2 now, and everything worked, I did add the extra udp 2049 rule which was missed by the author.


    the term “RH-Firewall-1-INPUT” did not work, I got this error (line 13 was the first line from this FAQ)

    iptables: Applying firewall rules: iptables-restore: line 13 failed

    I changed it to “INPUT” and that fixed it.

    now my /etc/sysconfig/iptables looks like this:

    I guess this changed between centOS 5 and CentOS 6?

    Still, have a question? Get help on our forum!