Installation and configuration of Linux NFS server

1. Introduction to NFS service

  NFS is the abbreviation of Network File System, that is, network file system. A protocol for use in distributed file systems, developed by Sun and published in 1984. The function is to allow different machines and different operating systems to share individual data with each other through the network, so that the client can access the data located in the server disk through the network, which is a method to realize disk file sharing between Unix-like systems. .

  The basic principle of NFS is "allowing different clients and servers to share the same file system through a set of RPCs". It is independent of the operating system and allows systems with different hardware and operating systems to share files together.

  NFS relies on the RPC protocol during file transfer or information transfer. RPC, Remote Procedure Call (Remote Procedure Call) is a mechanism that enables clients to execute programs in other systems. NFS itself does not provide protocols and functions for information transmission, but NFS allows us to share data through the network, because NFS uses some other transmission protocols. And these transport protocols use this RPC function. It can be said that NFS itself is a program that uses RPC. Or NFS is also an RPC SERVER. So as long as NFS is used, the RPC service must be started, whether it is NFS SERVER or NFS CLIENT. In this way, SERVER and CLIENT can realize the correspondence of PROGRAM PORT through RPC. The relationship between RPC and NFS can be understood in this way: NFS is a file system, and RPC is responsible for the transmission of information.

2. System environment

System platform: CentOS release 5.6 (Final)

NFS Server IP:192.168.1.108

Firewall is off /iptables: Firewall is not running.

SELINUX=disabled

3. Install NFS service

The installation of NFS is very simple, only two packages are required, and in normal cases, it is installed as the default package of the system.

  • nfs-utils-* : includes basic NFS commands and monitoring programs 
  • portmap-* : support connections to secure NFS RPC services

1. Check whether the system has NFS installed

The system has installed two packages of nfs-utils portmap by default.

2. If the software package required for NFS is not installed in the current system, it needs to be installed manually. The installation files of the nfs-utils and portmap packages will be in the system CD.

# mount /dev/cdrom /mnt/cdrom/
# cd /mnt/cdrom/CentOS/
# rpm -ivh portmap-4.0-65.2.2.1.i386.rpm
# rpm -ivh nfs-utils-1.0.9-50.el5.i386.rpm
# rpm -q nfs-utils portmap

Fourth, the NFS system daemon

  • nfsd : It is the basic NFS daemon, the main function is to manage whether the client can log in to the server;
  • mountd : It is an RPC mount daemon whose main function is to manage the NFS file system. After the client successfully logs in to the NFS server through nfsd, before using the files provided by the NFS service, it must also pass the verification of the file usage permission. It reads the NFS configuration file /etc/exports to compare client permissions.
  • portmap : The main function is to perform port mapping work. When a client tries to connect and use a service provided by an RPC server (such as an NFS service), the portmap will provide the managed port corresponding to the service to the client, so that the client can request the service from the server through this port.

Five, NFS server configuration

The configuration of the NFS server is relatively simple, you only need to set it in the corresponding configuration file, and then start the NFS server.

Common directories of NFS

/etc/exports Main configuration file for NFS service
/usr/sbin/exportfs Administration Commands for NFS Services
/usr/sbin/showmount client view command
/var/lib/nfs/etab                      记录NFS分享出来的目录的完整权限设定值
/var/lib/nfs/xtab                      记录曾经登录过的客户端信息

NFS服务的配置文件为 /etc/exports,这个文件是NFS的主要配置文件,不过系统并没有默认值,所以这个文件不一定会存在,可能要使用vim手动建立,然后在文件里面写入配置内容。

/etc/exports文件内容格式:

<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]

a. 输出目录:

输出目录是指NFS系统中需要共享给客户机使用的目录;

b. 客户端:

客户端是指网络中可以访问这个NFS输出目录的计算机

客户端常用的指定方式

  • 指定ip地址的主机:192.168.0.200
  • 指定子网中的所有主机:192.168.0.0/24 192.168.0.0/255.255.255.0
  • 指定域名的主机:david.bsmart.cn
  • 指定域中的所有主机:*.bsmart.cn
  • 所有主机:*

c. 选项:

选项用来设置输出目录的访问权限、用户映射等。

NFS主要有3类选项:

访问权限选项

  • 设置输出目录只读:ro
  • 设置输出目录读写:rw

用户映射选项

  • all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);
  • no_all_squash:与all_squash取反(默认设置);
  • root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);
  • no_root_squash:与rootsquash取反;
  • anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
  • anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

其它选项

  • secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
  • insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
  • sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
  • async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
  • wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
  • no_wdelay:若有写操作则立即执行,应与sync配合使用;
  • subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
  • no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

六、NFS服务器的启动与停止

在对exports文件进行了正确的配置后,就可以启动NFS服务器了。

1、启动NFS服务器

为了使NFS服务器能正常工作,需要启动portmap和nfs两个服务,并且portmap一定要先于nfs启动。

# service portmap start
# service nfs start

2、查询NFS服务器状态

# service portmap status
# service nfs status  

3、停止NFS服务器

要停止NFS运行时,需要先停止nfs服务再停止portmap服务,对于系统中有其他服务(如NIS)需要使用时,不需要停止portmap服务

# service nfs stop
# service portmap stop

4、设置NFS服务器的自动启动状态

对于实际的应用系统,每次启动LINUX系统后都手工启动nfs服务器是不现实的,需要设置系统在指定的运行级别自动启动portmap和nfs服务。

# chkconfig --list portmap
# chkconfig --list nfs

设置portmap和nfs服务在系统运行级别3和5自动启动。

# chkconfig --level 35 portmap on
# chkconfig --level 35 nfs on

七、实例

1、将NFS Server 的/home/david/ 共享给192.168.1.0/24网段,权限读写。

服务器端文件详细如下:

# vi /etc/exports

/home/david 192.168.1.0/24(rw)

2、重启portmap 和nfs 服务

# service portmap restart
# service nfs restart
# exportfs

3、服务器端使用showmount命令查询NFS的共享状态

# showmount -e    //默认查看自己共享的服务,前提是要DNS能解析自己,不然容易报错

# showmount -a    //显示已经与客户端连接上的目录信息

4、客户端使用showmount命令查询NFS的共享状态

# showmount -e NFS服务器IP

5、客户端挂载NFS服务器中的共享目录

命令格式

# mount NFS服务器IP:共享目录 本地挂载点目录

# mount 192.168.1.108:/home/david/ /tmp/david/

# mount |grep nfs

挂载成功。

查看文件是否和服务器端一致。

6、NFS的共享权限和访问控制

现在我们在/tmp/david/ 里面建立一个文件,看看权限是什么

# touch 20130103

这里出现Permission denied,是因为NFS 服务器端共享的目录本身的写权限没有开放给其他用户,在服务器端打开该权限。

# chmod 777 -R /home/david/

再次在客户端/tmp/david/ 里面建立一个文件

我用root 用户建立的文件,变成了nfsnobody 用户。

NFS有很多默认的参数,打开/var/lib/nfs/etab 查看分享出来的/home/david/ 完整权限设定值。

# cat /var/lib/nfs/etab

默认就有sync,wdelay,hide 等等,no_root_squash 是让root保持权限,root_squash 是把root映射成nobody,no_all_squash 不让所有用户保持在挂载目录中的权限。所以,root建立的文件所有者是nfsnobody。

下面我们使用普通用户挂载、写入文件测试。

# su - david

$ cd /tmp/david/

$ touch 2013david

普通用户写入文件时就是自己的名字,这也就保证了服务器的安全性。

  关于权限的分析

  1. 客户端连接时候,对普通用户的检查

    a. 如果明确设定了普通用户被压缩的身份,那么此时客户端用户的身份转换为指定用户;

    b. 如果NFS server上面有同名用户,那么此时客户端登录账户的身份转换为NFS server上面的同名用户;

    c. 如果没有明确指定,也没有同名用户,那么此时 用户身份被压缩成nfsnobody;

  2. 客户端连接的时候,对root的检查

    a. 如果设置no_root_squash,那么此时root用户的身份被压缩为NFS server上面的root;

    b. 如果设置了all_squash、anonuid、anongid,此时root 身份被压缩为指定用户;

    c. 如果没有明确指定,此时root用户被压缩为nfsnobody;

    d. 如果同时指定no_root_squash与all_squash 用户将被压缩为 nfsnobody,如果设置了anonuid、anongid将被压缩到所指定的用户与组;

7、卸载已挂载的NFS共享目录

# umount /tmp/david/

八、启动自动挂载nfs文件系统

格式:

<server>:</remote/export> </local/directory> nfs < options> 0 0

# vi /etc/fstab

保存退出,重启系统。

查看/home/david 有没有自动挂载。

自动挂载成功。

九、相关命令

1、exportfs

如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:

  # exportfs [-aruv]

  -a 全部挂载或卸载 /etc/exports中的内容 
  -r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab
  -u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
  -v 在export的时候,将详细的信息输出到屏幕上。

具体例子: 
  # exportfs -au 卸载所有共享目录
  # exportfs -rv 重新共享所有目录并输出详细信息

2、nfsstat

查看NFS的运行状态,对于调整NFS的运行有很大帮助。

3、rpcinfo

查看rpc执行信息,可以用于检测rpc运行情况的工具,利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些。

4、showmount

  -a 显示已经于客户端连接上的目录信息
  -e IP或者hostname 显示此IP地址分享出来的目录

5、netstat

可以查看出nfs服务开启的端口,其中nfs 开启的是2049,portmap 开启的是111,其余则是rpc开启的。

最后注意两点,虽然通过权限设置可以让普通用户访问,但是挂载的时候默认情况下只有root可以去挂载,普通用户可以执行sudo。

NFS server 关机的时候一点要确保NFS服务关闭,没有客户端处于连接状态!通过showmount -a 可以查看,如果有的话用kill killall pkill 来结束,(-9 强制结束)

 

CentOS 6.3下Samba服务器的安装与配置

http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html

 

http://www.cnblogs.com/mchina/archive/2013/01/03/2840040.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326355889&siteId=291194637