NFS for LINUX

来源:互联网 发布:淘宝比较知名的包包 编辑:程序博客网 时间:2024/04/28 03:21

一、NFS的原理介绍

NFS为Network  File System的缩写,可以用于不用的机器,不同操作系统之间的文件共享。与samba相比,NFS更快捷方便。简单的讲,可以把NFS服务器当做是一个文件服务器,NFS服务器可以让PC机将远程的NFS主机共享出来的目录挂载到本地机器中,挂载之后,这个目录就像是本地的一个分区一样,在有足够权限的情况下可以随意使用。

1、NFS需要启动的进程

rpc.nfsd  这个进程主要功能是管理client端是否能够登入主机,其中包含复杂的登录权限检测。

rpc.moutd 负责NFS的档案系统,当client通过rpc.nfsd登陆server后,对client存取server 的文件进行一系列的管理。NFS  server 在linux平台下需要两个套件 nfs-utils 和PORTMAP。

nfs-utils 提供rpc.nfsd 和rcp.mountd两个NFS daemon的套件

portmap 远程过程调用RPC的守护进程


2、NFS工作的大概原理:

NFS服务器共享目录、目录权限的检查

NFS服务使用RPC协议进行数据的传输

NFS服务器会开启一系列的RPC服务进程和端口

portmap启动一个111端口等待客户端连接

portmap会把访问111端口的数据发送给RPC服务对应的端口


二、NFS服务端安装、配置

1、安装nfs相关软件

[root@localhost ~]# yum install nfs-utils   -y   ##nfs-utils 为nfs的安装包

……………………

Installed:
  nfs-utils.x86_64 1:1.2.3-36.el6                                 

Dependency Installed:
  keyutils.x86_64 0:1.4-4.el6                                     
  libevent.x86_64 0:1.4.13-4.el6                                  
  libgssglue.x86_64 0:0.1-11.el6                                  
  libtirpc.x86_64 0:0.2.1-6.el6_4                                 
  nfs-utils-lib.x86_64 0:1.1.5-6.el6                              
  rpcbind.x86_64 0:0.2.0-11.el6                                   

Complete!


2、NFS配置文件的介绍

/etc/exports NFS的主配置文件,一般是空文件或者不存在,需要自己建立(相关的参数在本文最下面介绍

/usr/bin/exportfs  维护nfs共享资源的命令,可以使用这个命令来重新共享/etc/exports更改的目录资源、将NFS服务器共享的目录卸载或者重新共享,是nfs服务器中非常重要的一个命令。

/usr/sbin/showmout 该命令主要用在服务端,而show mount 主要用在客户端,可以使用它来查看NFS的服务器共享出来的目录资源。

/var/lib/nfs/*tab nfs服务器的日志文件都放在该目录下,包含两个日志文件,一个是etab,主要记录NFS共享出来的目录的完整地权限设置值;另一个是xtab,记录曾经连接到此NFS主机的相关客户端数据。

/etc/init.d/nfs nfs的服务进程,启动nfs服务

/etc/init.d/rpcbind rpcbind的守护进程,基本上第一次启动之后以后每次都会开机自动运行,不会关闭。

3、修改配置文件:

[root@localhost ~]# id nobody    ###查看nobody的id 和uid
uid=99(nobody) gid=99(nobody) groups=99(nobody)

[root@localhost ~]# cat /etc/exports     ###查看nfs的主配置文件
/nfs 192.168.254.46(rw,all_squash,anonuid=99,anongid=99)    *(ro,all_squash)
###允许192.168.254.46这个主机读写我本机的/nfs目录,其他用户只读

[root@localhost ~]# mkdir  /nfs       ##我将要共享出去的文件夹

[root@localhost ~]# chown nobody.nobody /nfs  -R               ####共享出去的文件夹属主要为普通用户,我选nobody

4、启动rpcbind服务

[root@localhost ~]# /etc/init.d/rpcbind restart
Stopping rpcbind:                                          [  OK  ]
Starting rpcbind:                                          [  OK  ]

5、重启nfs服务
[root@localhost ~]# service nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS quotas:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]

6、查看一下服务进程

[root@localhost ~]# netstat -tunlp | grep 111           ####rpcbind开启的端口为111
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2814/rpcbind        
tcp        0      0 :::111                      :::*                        LISTEN      2814/rpcbind        
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2814/rpcbind        
udp        0      0 :::111                      :::*                                    2814/rpcbind   


7、查看一下NFS共享的目录

[root@localhost ~]# exportfs 
/nfs            192.168.254.46

[root@localhost ~]# exportfs  -au    ###下载掉所有共享目录
[root@localhost ~]# exportfs           ###卸载后就无法看到共享目录了
[root@localhost ~]# exportfs  -av    ####加载所有的共享目录
exporting 192.168.254.46:/nfs


三、客户端(192.168.254.46)

1、安装nfs相关软件

[root@localhost ~]# yum install nfs-utils  -y

2、 查看nfs server的共享目录

[root@localhost ~]# showmount -e 192.168.254.153  ####showmount查看nfs  server上的共享文件夹
Export list for 192.168.254.153:
/test (everyone)

3、客户端挂载

[root@localhost ~]# mkdir /tmp/test

[root@localhost ~]# mount.nfs 192.168.254.153:/test /tmp/test/ ###nfs共享目录的挂载

[root@localhost ~]# df -Th
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
              ext4     48G  3.7G   41G   9% /
tmpfs        tmpfs    499M     0  499M   0% /dev/shm
/dev/vda1     ext4    485M   29M  431M   7% /boot
/dev/mapper/VolGroup-lv_home  ext4     49G  180M   47G   1% /home
192.168.254.153:/nfs       nfs     70G  4.7G   62G   8% /tmp/test

4、测试读写

[root@localhost ~]# cd /tmp/test/
[root@localhost test]# ls
[root@localhost test]# touch a
[root@localhost test]# mkdir test
[root@localhost test]# ls   ###查看新建的文件和文件夹
a  test


四、客户端 (192.168.254.38)

[root@localhost ~]# ifconfig eth0 | grep Bcast | awk '{print $2}'      ###查看一下客户端IP
addr:192.168.254.38
[root@localhost ~]# showmount -e 192.168.254.153    ###查看153的共享文件夹
Export list for 192.168.254.153:
/nfs 192.168.254.46

[root@localhost ~]# mkdir /test
[root@localhost ~]# mount -t nfs 192.168.254.153:/nfs /test
mount: 192.168.254.153:/nfs failed, reason given by server: Permission denied
[root@localhost ~]# mount -t nfs 192.168.254.153:/nfs /test
[root@localhost ~]# cd /test/
[root@localhost test]# touch a
touch: 无法触碰 “a”: 权限不够               ####38客户端允许挂载,权限是只读


五、/etc/exports的参数

[root@localhost ~]# cat /etc/exports 
/nfs 192.168.254.46(rw,all_squash,anonuid=99,anongid=99) *(ro,all_squash)

nfs server共享的目录; 允许挂载的IP或主机 ; 代表权限;其他IP或主机;其他主机的权限

主要介绍一下权限参数

rw:读写权限,不解释;

ro:只读权限,不解释;

no_root_squash: 如果是root用户挂载nfs共享目录,那么就是root身份,对于这个共享目录来说用户具有root权限,这个参数非常不安全,给用户的权限太大,慎用或者不建议使用!

root_squash:如果root用户使用nfs共享目录,那么用户权限就会被压缩成匿名用户,默认为nobody;

all_squash:任何用户的权限都被压缩成匿名用户,默认为nobody;

anonuid:anon是anonymous的缩写,通常是nobody的uid‘

anongid:nobody的gid;

sync:数据同步写入到内存与磁盘中

async:数据暂存在内存中而不是直接写入到磁盘。


原创粉丝点击