linux下,nfs server的配置

news/2024/5/17 19:25:20 标签: server, linux, permissions, tcp, system, file

原文链接:http://how-to.linuxcareer.com/how-to-configure-nfs-on-linux

作者: Lubos Rendek


How to configure NFS on Linux

Contents
  • 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

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 which allows the client to automatically mount remote file systems and therefore transparently provide an access to it as if the file system is local.

If you still have some questions after reading this article please try our new LinuxCareer Forum.

2. Scenario

In this scenario we are going to export the file system from the an IP address 10.1.1.50 ( NFS server ) host and mount it on an a host with an IP address 10.1.1.55 ( NFS Client ). Both NFS server and NFS client will be running Ubuntu Linux.

3. Prerequisites

At this point, we assume that the NFS service daemon is already installed on your system, including portmap daemon on which NFS setup depends.

If you have not done so yet simply install nfs-common package on both NFS client and NFS server using using apt-get tool.

# apt-get install nfs-common

The command above will fetch and install all support files common to NFS client and NFS server including portmap.

Additionally we need to install extra package on our NFS server side.

apt-get install nfs-kernel-server

This package is the actual NFS daemon listenning on both UDP and TCP 2049 ports.

Execute rpcinfo -p to check correctness of your NFS installation and to actually confirm that NFS server is indeed running and accepting calls on a port 2049:

# rpcinfo -p | grep nfs
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs

Furthermore, before we start exporting and mounting NFS directories, your system needs to actually support network file system. To check whether your system supports NFS grep /proc/filesystems and search for nfs.

# cat /proc/filesystems | grep nfs
nodev   nfs
nodev   nfs4

If you do not see any output it means that NFS is not supported or the NFS module have not been loaded into your kernel. To load NFS module execute:

# modprobe nfs

When installed correctly, the NFS daemon should be now listening on both UDP and TCP 2049 port and portmap should be waiting for instructions on a port 111.

At this point you should have portmap listening on both NFS server and NFS client:

rpcinfo -p | grep portmap
    100000    2   tcp    111  portmapper
    100000    2   udp    111  portmapper

server-export-file" style="margin-top:0px; margin-right:-5px; margin-bottom:10px; margin-left:-5px; padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; font-size:1.4em; line-height:1.19em; font-weight:normal; color:rgb(159,159,159); border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(221,221,221); font-family:'Titillium Maps',Arial; text-align:left"> 4. Server export file

All directories we want to share over the network using NFS need to be defined on the server side of this communication and more specifically they need to be defind with /etc/exports file. In the next section you will see most common NFS exports:

4.1. Most common exports options

Here are the most common NFS export techniques and options:

/home/nfs/ 10.1.1.55(rw,sync) export /home/nfs directory for host with an IP address 10.1.1.55 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 with netmask 255.255.255.0 with read only permissions and synchronized mode
/home/nfs/ 10.1.1.55(rw,sync) 10.1.1.10(ro,sync) export /home/nfs directory for host with IP 10.1.1.55with read, write permissions, synchronized mode, and also export /home/nfs directory for another host with an IP address 10.1.1.10 with read only permissions and synchronized mode
/home/nfs/ 10.1.1.55(rw,sync,no_root_squash) export /home/nfs directory for host with an IP address 10.1.1.55with 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 read only permissions and synchronized mode
/home/nfs/ *.linuxcareer.com(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

file" style="margin-top:0px; margin-right:0px; margin-bottom:10px; margin-left:0px; padding-top:3px; padding-right:0px; padding-bottom:3px; padding-left:0px; font-size:1.4em; font-weight:normal; color:rgb(85,85,85); font-family:'Titillium Maps',Arial; text-align:left"> 4.2. Edit exports file

Now that we have familiarized our selfs with some NFS's export options we can define our first NFS export. Open up your favorite text editor, for example, vim and edit /etc/exports file by adding a line /home/nfs/ *(ro,sync) which will export /home/nfs directory for any host with read only permissions. Instead of text editor you can simply insert your NFS export line into /etc/exports file using echo command:

# echo '/home/nfs/ *(ro,sync)' > /etc/exports 
# tail -1 /etc/exports 
/home/nfs/ *(ro,sync)

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

# touch /home/nfs/nfs-test-file

NOTE: The default behavior of NFS kernel daemon is to include additional option to your export line which is "no_subtree_check". Be aware of this fact when you attempt to configure your NFS exports further.

4.3. Restart NFS daemon

Once you have edited /etc/exports file you need to restart your NFS daemon to apply any changes. Depending on your Linux distribution the restarting procedure of NFS may differ. Ubuntu and Debian users:

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

Redhat and Fedora 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 

file-system-on-client" style="margin-top:0px; margin-right:-5px; margin-bottom:10px; margin-left:-5px; padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; font-size:1.4em; line-height:1.19em; font-weight:normal; color:rgb(159,159,159); border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(221,221,221); font-family:'Titillium Maps',Arial; text-align:left"> 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.50:/home/nfs /home/nfs_local 

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

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

You may also get and an error message:

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

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

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

In any case now you should be able to access a remote /home/nfs directory locally on your NFS client.

# ls /home/nfs_local/
nfs-test-file
# cd /home/nfs_local/
# ls
nfs-test-file
# touch test
touch: cannot touch `test': Read-only file system

The above output proves that a remote NFS export is mounted and that we can access it by navigating to a local /home/nfs_local/ directory. Please notice that the touch command reports that the filesystem is mounted as read-only which was exactly our intention.

6. Configure automount

To make this completely transparent to end users, you can automount the NFS file system every time a user boots a Linux system, 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 within /etc/fstab:

10.1.1.50:/home/nfs /home/nfs_local/ nfs defaults 0 0 

as before you also use echo command to do that:

# echo "10.1.1.50:/home/nfs /home/nfs_local/ nfs defaults 0 0" >> /etc/fstab 
# tail -1 /etc/fstab 
10.1.1.50:/home/nfs /home/nfs_local/ nfs defaults 0 0

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 and Fedora 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.

systems" style="margin-top:0px; margin-right:0px; margin-bottom:10px; margin-left:0px; padding-top:3px; padding-right:0px; padding-bottom:3px; padding-left:0px; font-size:1.4em; font-weight:normal; color:rgb(85,85,85); font-family:'Titillium Maps',Arial; text-align:left"> 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 above 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 an 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


http://www.niftyadmin.cn/n/1870184.html

相关文章

RuntimeException和Exception区别

1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception。 2.Error是Throwable 的子类,用于指示合理的应用程序不应该试图捕获的严重问题。大多数这样的错误都是异常条件。虽然 ThreadDeath 错误是…

Java 异常之 RuntimeException和Exception的区别

在java的异常类体系中,Error和RuntimeException是非检查型异常,其他的都是检查型异常。 所有方法都可以在不声明throws的情况下抛出RuntimeException及其子类 不可以在不声明的情况下抛出非RuntimeException简单的说 非RuntimeException必要自己写catch块处理掉。如…

进程的real , effective , saved user ID

real User ID:实际运行此进程的用户的 uid。例如:test 用户执行ls命令,那么ls进程的real User ID就是test用户的 uid effective user ID:是可执行文件的 owner id,例如:/bin/ls 的owner id 是root&#xf…

linux文件的atime,mtime,ctime

1、atime:文件最近一次被访问的时间。(通过 read(),execel())等。 例如:可执行文件a.out执行前,执行后,通过stat观察的atime rootVM-Ubuntu203001:~/test# stat a.out Access: 2012-05-11 15:59:41.000…

Memory-mapped I/O And port I/O

1.1 Memory-mapped I/O I/O设备的寄存器和内存被映射到CPU的内存地址空间 CPU访问I/O设备的方式和操作普通内存的方式是一样的。这样简化了I/O操作。 I/O设备和普通内存共享相同的总线信号(地址、数据、控制) 因为I/O操作要比内存操作慢,所以这…

简洁又快速地处理集合——Java8 Stream(上)

Java 8 发布至今也已经好几年过去,如今 Java 也已经向 11 迈去,但是 Java 8 作出的改变可以说是革命性的,影响足够深远,学习 Java 8 应该是 Java 开发者的必修课。 今天给大家带来 Java 8 Stream 讲解,为什么直接讲这个…

tcp/ip中的术语

在tcp/ip协议模型中,不同协议层,数据包的称呼: application:应用层 presentation:表示层 session:会话层 transport:传输层 network:网络层 datalink:数据链路 physical&…

Linux系统设备(device)的major和minor number

Linux系的/dev目录下面的的设备文件是用来表示外设的,如/dev/sda1表示第一块硬盘的第一个分区。但是这个/dev/sda1仅仅是方便用户观察,linux内核中表示不同的设备是通过major 和minor number实现的,通过major和minor Number来加载相应的驱动程…