feed-image  ISSN 1836-5930



Receive Your Complimentary Guide to Linux NOW!


A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)




Poll

Do you care about your privacy when using a FACEBOOK?
 

linuxconfig.org
is hosted by:



Partner Linux Sites
TuxMachines
DebianAdmin
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
FreeSoftwareLinux
All For Linux

HowTo configure NFS
Article Index
1. Introduction
2. Scenario
3. Prerequisites
4. Server export file
4.1. Most common exports options
4.2. Edit exports file
4.3. Restart NFS daemon
5. Mount remote file system on client
6. Configure automount
7. Conclusion
8. Appendix A
8.1. Turn off firewall on Redhat like systems:
8.2. Add iptables rules to allow NFS communication

Author: Lubos Rendek


1. Introduction

The Network File System is certainly one of the most widely used network services. Network file system (NFS) is based on the Remote procedure call. It allows the client to automount and therefore, transparently access the remote file systems on the network.

2. Scenario

In this scenario we are going to export the file system from the linuxconfig.org (IP address 10.1.1.200) host and mount it on linuxconfig.local(IP address 10.1.1.100).

3. Prerequisites

At this point, we assume that the NFS service daemon is already installed on your system, including portmap daemon on which NFS setupt depends. Moreover, your system needs to support the NFS file system.

$ cat /proc/filesystems 

NFS file system supported

NFS daemon should be listening on both standard ports 2049 and portmap on port 111.

NFS daemon port 2049 portmap port 111
Another way to check if NFS is functioning, is to use the rpcinfo command.

# rpcinfo -p  

You should get a response/output similar to one below:

check if NFS is functioning

4. Server export file

All NFS server exports need to be defined in /etc/exports file.

4.1. Most common exports options

Here are the most common export techniques and options:

/home/nfs/ 10.1.1.100(rw,sync) export /home/nfs directory for host with IP 10.1.1.100 with read, write permissions, and synchronized mode
/home/nfs/ 10.1.1.0/24(ro,sync) export /home/nfs directory for network 10.1.1.0 netmask 255.255.255.0 with read only permissions and synchronized mode
/home/nfs/ 10.1.1.100(rw,sync) 10.1.1.10(ro,sync) export /home/nfs directory for host with IP 10.1.1.100 with read, write permissions, synchronized mode, and also export /home/nfs directory for hosts with IP 10.1.1.10 with read only permissions and synchronized mode
/home/nfs/ 10.1.1.100(rw,sync,no_root_squash) export /home/nfs directory for host with IP 10.1.1.100 with read, write permissions, synchronized mode and the remote root user will be treated as a root and will be able to change any file and directory.
/home/nfs/ *(ro,sync) export /home/nfs directory for any host with a read only permission and synchronized mode
/home/nfs/ *.linuxconfig.org(ro,sync) export /home/nfs directory for any host within linuxconfig.org domain with a read only permission and synchronized mode
/home/nfs/ foobar(rw,sync) export /home/nfs directory for hostname foobar with read, write permissions and synchronized mode

4.2. Edit exports file

Open up your favorite text editor, for example, vim and edit /etc/exports file and add line /home/nfs/ *(ro,sync) to export /home/nfs directory for any host with read only permissions. edit NFS exports file

Be sure that the directory you export by NFS exists. You can also create a file inside the /home/nfs directory which will help you troubleshoot once you mount this file system remotely.

# touch /home/nfs/test_file 

4.3. Restart NFS daemon

Once you edit /etc/exports file you need to restart NFS daemon to apply changes in the /etc/exports file. Depending on your Linux distribution, the restarting of NFS may differ. Debian users:

# /etc/init.d/nfs-kernel-server restart 

Redhat users

# /etc/init.d/nfs restart 

If you later decide to add more NFS exports to the /etc/exports file, you will need to either restart NFS daemon or run command exportfs:

# exportfs -ra 

5. Mount remote file system on client

First we need to create a mount point:

# mkdir /home/nfs_local 

If you are sure that the NFS client and mount point are ready, you can run the mount command to mount exported NFS remote file system:

# mount 10.1.1.200:/home/nfs /home/nfs_local 

In case that you need to specify a type of the filesystem you can do this by:

# mount -t nfs 10.1.1.200:/home/nfs /home/nfs_local 

You may get error message

mount: mount to NFS server failed: timed out (retrying). 

This may mean that your server supports higher versions of nfs and therefore you need to pass one extra argument to your nfs client. In this example we use nfs version 3:

# mount -t nfs -o nfsvers=3 10.1.1.200:/home/nfs /home/nfs_local 

Mount remote file system on client

Now you should be able to see that the file system is mounted. Notice that the mount command reports that the filesystem is mounted as "read and write", although you can see that it provides a "read only" permission.

6. Configure automount

To make this completely transparent to end users, you can automount the NFS file system every time a user boots a PC, or you can also use PAM modules to mount once a user logs in with a proper username and password. In this situation just edit /etc/fstab to mount system automatically during a system boot. You can use your favorite editor and create new line like this:

10.1.1.200:/home/nfs /home/nfs_local/ nfs defaults 0 0 

in /etc/fstab or

# echo "10.1.1.200:/home/nfs /home/nfs_local/ nfs defaults 0 0" >> /etc/fstab 

Configure NFS automount

7. Conclusion

The Network File System comes with tons of export options. What has been shown here, just barely scratches the surface of NFS. Please visit Linux NFS-HOWTO hosted by linux documentation project or NFS homepage for more details.

8. Appendix A

Following section of this NFS tutorial is going to be devoted to RedHat like Linux systems which by default block all incoming traffic to a NFS server by engaging firewall using iptables rules. For this reason when the firewall is running on your NFS server, you might get this error when mounting NFS filesytem:  mount.nfs: mount to NFS server '10.1.1.13' failed: System Error: No route to host. This error message has nothing to do with your NFS  configuration, all what needs to be done is either turn off the firewall or add iptables rules to allow traffic on portmap port 111, nfs port 2049 and random ports for other nfs services.

There are two solutions to this problem: easy solution is to turn off the firewall completely and the right solution to add appropriate iptables rules.

8.1. Turn off firewall on Redhat like systems:

The easiest solution is to just turn off the firewall. This will automatically grant access to the nfs daemon to anyone. I would suggest this solution only for testing purposes of your NFS configuration. Enter the following command to stop firewall and clean up all iptables rules:

# service iptables stop

Now when your NFS settings are correct you should be able to mount nfs filesystem from you client machine.

8.2. Add iptables rules to allow NFS communication

This is a more complex but right solution to the given problem. First we need to set static port for nfs services such as rquotad, mountd, statd, and lockd by editing /etc/sysconfig/nfs file. Add or uncomment following lines in your /etc/sysconfig/nfs file:

LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662

 

Restart you NFSD daemon with following commands:

# /etc/init.d/nfs restart
# /etc/init.d/nfslock restart

Use rpcinfo command to confirm a validity of your new ports settings:

# rpcinfo -p localhost
The output should be similar to the one below:
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100011 1 udp 999 rquotad
100011 2 udp 999 rquotad
100011 1 tcp 1002 rquotad
100011 2 tcp 1002 rquotad
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100021 1 udp 32769 nlockmgr
100021 3 udp 32769 nlockmgr
100021 4 udp 32769 nlockmgr
100021 1 tcp 32803 nlockmgr
100021 3 tcp 32803 nlockmgr
100021 4 tcp 32803 nlockmgr
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100005 1 udp 892 mountd
100005 1 tcp 892 mountd
100005 2 udp 892 mountd
100005 2 tcp 892 mountd
100005 3 udp 892 mountd
100005 3 tcp 892 mountd
100024 1 udp 662 status
100024 1 tcp 662 status

Save your current iptables rules into iptables-rules-orig.txt :

# iptables-save > iptables-rules-orig.txt

Create file called iptables-nfs-rules.txt with the following content:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2:200]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p esp -j ACCEPT
-A RH-Firewall-1-INPUT -p ah -j ACCEPT
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 111 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 2049 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 32769 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 32769 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 32803 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 32803 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 662 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 662 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 892 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 892 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT

Apply new rules with iptables-restore, where the single argument will be iptables-nfs-rules.txt file:

NOTE: this will create a new set of iptables rules. If you have already defined some iptables rules previously, you may want to edit iptables-rules-orig.txt  and use it with iptables-restore command instead.

# iptables-restore iptables-nfs-rules.txt

Save these new rules, so you do not have to apply new rules for nfs daemon next time you restart your server:

# service iptables save

Now your server is ready to accept client nfs requests. Optionally, you may restart iptables rules / firewall with the following command:

# service iptables restart

Get FREE complimentary Linux Guides

The GNU/Linux Advanced Administration
The GNU/Linux systems have reached an important level of maturity, allowing to integrate them in almost any kind of work environment, from a desktop PC to the sever facilities of a big company.

In this ebook "The GNU/Linux Operating System", the main contents are related with system administration. You will learn how to install and configure several computer services, and how to optimize and synchronize the resources using GNU/Linux.

The topics covered in this 500+ page eBook include Linux network, server and data administration, Linux kernel, security, clustering, configuration, tuning, optimization, migration and coexistence with non-Linux systems. A must read for any serious Linux system admin.

A Newbie's Getting Started Guide to Linux
Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it.

You've probably heard about Linux, the free, open-source operating system that's been pushing up against Microsoft. It's way cheaper, faster, safer, and has a far bigger active community than Windows, so why aren't you on it? Don't worry, Makeuseof.com understands. Like many things, venturing off into a completely unknown world can seem rather scary, and also be pretty difficult in the beginning. It's while adapting to the unknown, that one needs a guiding, and caring hand. This guide will tell you all you need to know in 20 illustrated pages, helping you to take your first steps. Let your curiosity take you hostage and start discovering Linux today, with this manual as your guide! Don't let Makeuseof.com keep you any longer, and download the Newbie's Initiation to Linux. With this free guide you will also receive daily updates on new cool websites and programs in your email for free courtesy of MakeUseOf.

Linux from Scratch
Linux from Scratch describes the process of creating your own Linux system from scratch from an already installed Linux distribution, using nothing but the source code of software that you need.

This 318 page eBook provides readers with the background and instruction to design and build custom Linux systems. This eBook highlights the Linux from Scratch project and the benefits of using this system. Users can dictate all aspects of their system, including directory layout, script setup, and security. The resulting system will be compiled completely from the source code, and the user will be able to specify where, why, and how programs are installed. This eBook allows readers to fully customize Linux systems to their own needs and allows users more control over their system.

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)
Getting Started with Ubuntu 10.04 (Lucid Lynx) is a comprehensive beginners guide for the Ubuntu operating system; it features comprehensive guides, How Tos and information on anything you need to know after first installing Ubuntu.

Designed to be as user-friendly and easy to follow as possible, it should provide the first point of reference to any Ubuntu newcomer with lots of information. The manual has step by step instructions and includes lots of screenshots to show you how to do tasks. It also includes a Troubleshooting section to help you solve common Ubuntu problems quickly. Download this 160+ page manual today.

Comments (27)
  • saipavan  - How to view the mounted files

    Hi
    How to viewopen the mounted files in NFS
    Thanks in advance

  • Lubos  - How to view the mounted files

    H,

    I'm no sure if I understand your question. To open files you simply navigate to a directory where did you mount your NFS share. The rest is as per usual .

  • Rahn Boulay  - NFS unmounting

    Question I NFS mount a file system. (Manually)
    it mount's it twice. After about 2min it unmounts both. WHY what can I do to stop this besides cd'ing into the mount and making it busy???

    Thanks
    Rahn

  • Abhi  - How to share a mounted nfs drive to another pc

    Scenario:

    1)PC1 user is hosting a directory which he has shared to all other users

    2)PC2 user has mounted the shared NFS directory of PC1 on a directory named /myshare in PC2

    Now PC2 user is also owner of PC1, PC2 & PC3. He wishes to share /myshare (which is actually a mounted NFS drive from PC1) to PC3, and mount that shared NFS drive to a directory in PC3

    Can you tell me how to do that??

  • Vwbond  - Thanks for writing this up!

    This is a outstanding guide for NFS!

  • Rena

    Yes.....
    this is really a very helpful doc which explained all the aspects for configuring nfs..........
    thanks

  • Bill Graham  - Error on CentOS 5

    Flushing chain `INPUT'
    Flushing chain `FORWARD'
    Flushing chain `OUTPUT'
    Flushing chain `RH-Firewall-1-INPUT'
    Deleting chain `RH-Firewall-1-INPUT'
    iptables-restore v1.3.5: no command specified
    Error occurred at line: 30

    I receive the above message when running iptables-restore in verbose mode. Is there a way to isolate the problem and work around it?

    Thank you.

  • Lubos  - iptables-restore v1.3.5: no command specified

    What do you mean by verbose mode? Is there a iptables-restore verbose mode?

    or sounds like your script doesn't have the proper path, or isn't calling the commands correctly

  • Bill Graham  - iptables-restore v1.3.5: no command specified

    The iptables command has a verbose mode (-v). See iptables -h. Regardless, I received the same error, "no command specified," with or without the switch.

    The file (iptables-restore iptables-nfs-rules.txt) was copied from the above listing with no changes. I ran it as root from the /etc/sysconfig folder "iptables-restore iptables-nfs-rules.txt" where I saved the new file. Where is it supposed to be run from? As far as I can determine, I followed the instructions as stated to that point. What is the "proper path" and how should it be calling the commands? Thank you.

  • Lubos  - iptables-restore v1.3.5: no command specified

    Hi Bill,

    No idea with this amount of information. iptable does have a verbose mode but iptable-restore does not. At least not on debian: man iptables-restore .

    Did you try some other one line iptables script to see if you get the same error?

    The script in this guide is working correctly for me and it helped some other readers too.

    lubos

  • Bill Graham  -  iptables-restore v1.3.5: no command specified

    Lubos,

    FYI, on my server, CentOS 5, iptables-restore -h results in:
    Usage: iptables-restore [-b] [-c] [-v] [-t] [-h]
    [ --binary ]
    [ --counters ]
    [ --verbose ]
    [ --test ]
    [ --help ]
    [ --noflush ]
    [ --modprobe=]

    I tried entering the commands one at a time. I read in another post to proceed each line with "iptables" but this resulted in an error for the line :INPUT ACCEPT [0:0]. I was not sure what lines from the file to start with and the impact of omitting the first five lines. Thank you for looking at this.

  • Bill Graham  - iptables-restore v1.3.5: no command specified

    Lubos,

    After opening up the CentOS Firewall configuration panel and changing a few settings, as if by magic, this worked. I can't explain it but I will accept it. Thank you, I am once again mounting an NFS volume on my workstation to this server.

  • lubos  -  iptables-restore v1.3.5: no command specified

    It is great that it works again. and Thanks for an update. In case you find what the problem was let me know and I will update this article if necessary.

    thanks

  • dev  - permission denied

    hello friends,

    i m using centos 5 ent. when i m going to mount that directory it send error "permission denied"
    i have disable firewall and iptables in the machine. and sometime it send "can't get address" is there any one have solution ?

  • lubos  - NFS "Permission denied" errors

    make sure that the NFS exports are for correct directories and and subnets. run "exportfs -a" to apply changes you made in your /etc/exports file.

  • margherita  - no_root_squash

    Sorry, just one doubt/comment. You say that with the option "no_root_squash" the remote root user will not be treated as a root but as a default nfs user. Isn't it exactly the opposite?

    In any case thank you very much for this nice howto: without it I would still be wondering why I cannot nfs mount a directory that exists in a RedHat host!!

  • Anonymous  - confirming Margherita's doubts on the 'no_root_squ

    I think you are right Margherita (but I am a beginner myself)!

    As this is a fairly coarse security thing I hope Lubos will change it or explain it to us :)

    Anyway, thanks for putting in the effort and sharing your knowledge Lubos!

  • Lubos  - no_root_squash

    Hi Gyus,

    thank you for pointing that out. I have corrected this problem. It also should be said that from security reasons no_root_squash should not be used as it can lead to a trojan's created by remote root users as they can access and modify any directory and file.

    thank you again

    Lubos

  • Sounman2020  - Absolutely the best advice on the net for NFS

    Yours is the ONLY advice on the internet that will actually get NFS working correctly through a firewall! Believe me, I tried many other places first, and none of them work. You set it out so clearly. Thank you so much!

    Stuart

  • Anonymous  - Appu

    i am still getting this error

    mount: mount to NFS server '192.168.1.10' failed: timed out (retrying).

    help me

  • Lubos  - mount: mount to NFS server '192.168.1.10' failed:

    Hi,

    for start you may check:
    - NFS server's /etc/hosts.allow and /etc/host.deny files
    - firewall on the server and client
    - are directories on the NFS server exported for your client's subnet or IP

    lubos

  • nikolai  - i think it is iptables

    Hi Lubos,
    when you mentioned iptables it hit me in the head.
    I bet it is the reason. Problem is i cannot turn ip tables off - my server is sort of "all in one" thing :-)

    i can see what ports are nfs using for udp and tcp. portmap is 111 (i think it's a standard).
    So i would greatly appreciate if you would tel me how can i insert into iptables line that will open those ports for me.
    unfortunately i do not know correct syntax of iptables commands.
    thnx in advance

  • Lubos  - mount.nfs: mount to NFS server '10.1.1.13' failed:

    Hi Nikolai,

    I guess that your are not the only one who has this problem and in the future there will be more people struggling with this firewall and nfs issue.

    Therefore, I have decided to add one more section to this article called Appendix A ( located at the end of the article) which is dedicated to Redhat like linux systems on how to configure iptables firewall rules for nfs service. I have tested all settings on Centos 5 today and all works fine. Let me know if it solves your problem.

  • nikolai  -  seems very simple but fails everytime

    it seems pretty straight forward, but for some reason it fails.
    i'm running centos 5.4 on my server. i have configured /etc/exports as you have described.
    then tried to mount it from my network pc and got following error:

    failed: System Error: No route to host

    i have 2 network cards on my server, do i have to have some special settings in exports for that?

  • Lubos  -  NFS error (No route to host)

    at the first glance this error does not seem to be related to the NFS configuration. Here are some troubleshooting hints:

    - can you ping NFS server from NFS client and vice versa?
    - can you telnet to server's NFS port 2049 from client machine?
    - do you run firewall on your NFS server or client?
    - are there any iptables rules blocking NFS requests on your server/client?
    - did you try to disable server's iptables?
    - is NFS client and NFS server on the same subnet ?

    let us know what solves your problem.:-)

  • Andy Lavarre  - Thank you

    I have struggled with this for several years. Yours is the first clear guidance I have found. man mount(8) is certainly not in this category!! :-)

    Thanks again.

    Best regards, Andy

  • Manny  - Tanks

    You are the only one in the hole Internet how have written a complete guide for the NFS connection that WORKS.

    Tanks.

Write comment
NOTE: To unsubscribe enter your email, select "do not dotify" with title: UNSUBSCRIBE and Send.
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security
Please input the anti-spam code that you can read in the image.