NFS安装笔记

来源:互联网 发布:点歌软件电脑 编辑:程序博客网 时间:2024/06/08 15:40
一、NFS服务简介

  NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。
  NFS 的基本原则是“容许不同的客户端及服务端通过一组RPC分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。
  NFS在文件传送或信息传送过程中依赖于RPC协议。RPC,远程过程调用 (Remote Procedure Call) 是能使客户端执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能的。可以说NFS本身就是使用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责负责信息的传输。
二、系统环境


系统平台:

[root@localhost ~]# cat /etc/issueCentOS release 6.6 (Final)Kernel \r on an \m

NFS Server IP:192.168.1.231
防火墙关闭

[root@localhost ~]# service iptables statusiptables: Firewall is not running.

SELINUX 也关闭

[root@localhost ~]# sestatus -vSELinux status:                 enabled   

还在运行。现在执行关闭

setenforce 0 可以临时关闭,但重启之后还是会变成原来的状态。

修改/etc/sysconfig/selinux文件可以永久地禁用它

[root@localhost ~]# vi /etc/sysconfig/selinux # This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:#     enforcing - SELinux security policy is enforced.#     permissive - SELinux prints warnings instead of enforcing.#     disabled - No SELinux policy is loaded.#SELINUX=enforcingSELINUX=disabled# SELINUXTYPE= can take one of these two values:#     targeted - Targeted processes are protected,#     mls - Multi Level Security protection.SELINUXTYPE=targeted

三、安装NFS服务
先检查是否安装
[root@localhost ~]# rpm -qa |grep nfs[root@localhost ~]# rpm -qa |grep portmap[root@localhost ~]# 

因为我的系统是最小化安装是没有安装任何东西的。
下面我们开始安装这两个软件
    nfs-utils-* :包括基本的NFS命令与监控程序 
    portmap-* :支持安全NFS RPC服务的连接
我们这次安装模式为YUM安装
[root@localhost ~]# yum -y install nfs-utils rpcbind

备注一下

centos 5 :

yum -y install nfs-utils portmap

centos 6(在CentOS 6.3当中,portmap服务由rpcbind负责) :

yum -y install nfs-utils rpcbind
测试 安装是否成功

[root@localhost ~]# service rpcbind startStarting rpcbind:                                          [  OK  ][root@localhost ~]# service nfs startStarting NFS services:                                     [  OK  ]Starting NFS mountd:                                       [  OK  ]Starting NFS daemon:                                       [  OK  ]Starting RPC idmapd:                                       [  OK  ][root@localhost ~]# 

四 服务器端的配置
1:下面开始创建共享目录
[root@localhost ~]# mkdir nfs[root@localhost ~]# cd nfs[root@localhost nfs]# mkdir test[root@localhost test]# pwd/root/nfs/test

2:NFS文件配置

[root@localhost ~]# vi /etc/exports 

加一行
/root/nfs/test/ 192.168.1.232(rw,no_root_squash,no_all_squash,sync)
让配置立即生效

[root@localhost ~]# exportfs -r

下面是参数配置详细说明

注:配置文件说明:

/usr/local/test/ 为共享的目录,使用绝对路径。
192.168.1.15(rw,no_root_squash,no_all_squash,sync) 为客户端的地址及权限,地址可以是一个网段,一个IP地址或者是一个域名,域名支持通配符,如:*.youxia.com,地址与权限中间没有空格,权限说明:
rw:read-write,可读写;
ro:read-only,只读;
sync:文件同时写入硬盘和内存;
async:文件暂存于内存,而不是直接写入内存;
no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。
root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;
all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;
anonuid:匿名用户的UID值,通常是nobody或nfsnobody,可以在此处自行设定;
anongid:匿名用户的GID值。


重起服务

[root@localhost ~]# service nfs restartShutting down NFS daemon:                                  [  OK  ]Shutting down NFS mountd:                                  [  OK  ]Shutting down NFS services:                                [  OK  ]Shutting down RPC idmapd:                                  [  OK  ]Starting NFS services:                                     [  OK  ]Starting NFS mountd:                                       [  OK  ]Starting NFS daemon:                                       [  OK  ]Starting RPC idmapd:                                       [  OK  ][root@localhost ~]# service rpcbind restartStopping rpcbind:                                          [  OK  ]Starting rpcbind:                                          [  OK  ][root@localhost ~]# 

五:客户端测试挂载 192.168.1.232

[root@localhost nfs]# mount -t nfs 192.168.1.231:/root/nfs/test/ /root/nfs/test/mount: wrong fs type, bad option, bad superblock on 192.168.1.231:/root/nfs/test/,       missing codepage or helper program, or other error       (for several filesystems (e.g. nfs, cifs) you might       need a /sbin/mount.<type> helper program)       In some cases useful info is found in syslog - try       dmesg | tail  or so

出现上面的错误
我们现在在YUM一下就OK了
[root@localhost nfs]# yum install nfs-utils


结束就OK了
安装之后,/sbin/下面多了两个mount文件,分别是mount.nfs和mount.nfs4:


[root@localhost nfs]# ls /sbin/mount*/sbin/mount.nfs  /sbin/mount.nfs4  /sbin/mount.tmpfs

下面在一次挂截


[root@localhost nfs]# mount -t nfs 192.168.1.231:/root/nfs/test/ /root/nfs/test/[root@localhost nfs]# mount/dev/sda3 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")/dev/sda1 on /boot type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)192.168.1.231:/root/nfs/test/ on /root/nfs/test type nfs (rw,vers=4,addr=192.168.1.231,clientaddr=192.168.1.232)

六:测试一下挂截是否成功
服务端:192.168.1.231
[root@localhost ~]# cd nfs/test/[root@localhost test]# vi aaaa[root@localhost test]# lltotal 4-rw-r--r--. 1 root root 26 Apr 15 17:54 aaaa[root@localhost test]# 

客户端:192.168.1.232

[root@localhost test]# lltotal 4-rw-r--r--. 1 root root 26 Apr 16  2016 aaaa

测试OK

七 :解除挂载

[root@localhost ~]# umount /root/nfs/test[root@localhost ~]# mount/dev/sda3 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")/dev/sda1 on /boot type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

如果

[root@localhost ~]# umount /root/nfs/testumount.nfs: /root/nfs/test: device is busyumount.nfs: /root/nfs/test: device is busy

接触挂载出现上面的提示 我们现在这样操作

[root@localhost ~]# fuser -m -v /root/nfs/test                     USER        PID ACCESS COMMAND/root/nfs/test:      root       1371 ..c.. bash                     root       1384 F.c.. vi[root@localhost ~]# kill -9 1371[root@localhost ~]# kill -9 1384[root@localhost ~]# umount /root/nfs/test[root@localhost ~]# mount/dev/sda3 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")/dev/sda1 on /boot type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)



0 0