centos6安装NFS文件共享系统

centos6安装NFS文件共享系统

NFS-Network File System定义:
用于UNIX/Linux系统间通过网络进行文件共享,用户可以把网络中NFS服务器提供的共享目录挂载到本地文件目录中,可以象对本地文件系统一样操作NFS文件系统中内容。

centos6安装NFS文件共享系统

一、环境介绍:
服务器M1:centos 192.168.1.248
客户端M2:centos 192.168.1.249

二、服务器端M1上操作:
centos 5 :
#yum -y install nfs-utils portmap    【在CentOS 6.3当中,portmap服务由rpcbind负责】
centos 6 :
#yum -y install nfs-utils rpcbind

1、创建共享目录:
[root@M1 /]# mkdir /data/wwwroot/web/nfs_test

2、NFS文件配置:
[root@M1 /]# vi /etc/exports
#增加一行:
/data/wwwroot/web/nfs_test/ 192.168.1.249(rw,no_root_squash,no_all_squash,sync)

或者:
/data/wwwroot/web/nfs_test/ 192.168.1.249(rw,sync,no_root_squash)
/data/wwwroot/web/nfs_test/ *(ro)
【表示只有192.168.1.249客户端能够以读写权限挂载共享目录,其他客户端只能以只读权限挂载。】

或者:
/data/wwwroot/web/nfs_test/ 192.168.1.249(ro)   【允许192.168.16.128只读权限访问/website】

注:配置文件说明:
/data/wwwroot/web/nfs_test/ 为共享的目录,使用绝对路径。
192.168.1.249(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@M1 /]# exportfs -r        【使配置生效】

3、为NFS指定固定端口,运行以下命令
[root@M1 /]#vi /etc/sysconfig/nfs

搜索和设置如下所示的端口配置:
RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004

4、开放防火墙中的上述端口,运行以下命令:
[root@M1 /]# iptables  -I INPUT -p tcp –dport 111 -j ACCEPT
[root@M1 /]# iptables  -I INPUT -p udp –dport 111 -j ACCEPT
[root@M1 /]# iptables  -I INPUT -p tcp –dport 2049 -j ACCEPT
[root@M1 /]# iptables  -I INPUT -p udp –dport 2049 -j ACCEPT
[root@M1 /]# iptables  -I INPUT -p tcp –dport 30001:30004 -j ACCEPT
[root@M1 /]# iptables  -I INPUT -p udp –dport 30001:30004 -j ACCEPT
[root@M1 /]# service iptables save
[root@M1 /]# service iptables restart

5、关闭SELinux 【略】

6、启动NFS相关服务,运行以下命令
[root@M1 /]# chkconfig nfs on
[root@M1 /]# chkconfig rpcbind on
[root@M1 /]# service nfs start
[root@M1 /]# service rpcbind start

7、检查NFS的相关端口是否已经启用,运行以下命令
[root@M1 /]# service iptables status
[root@M1 /]# rpcinfo -p localhost

二、客户端端M2上操作:

1、NFS客户端不需要启动NFS服务,但需要安装nfs-utils,运行以下命令
[root@M2 ~]# yum install -y nfs-utils
[root@M2 ~]# mkdir -p /data/wwwroot/web/nfs_test
[root@M1 /]# chkconfig nfs on
[root@M2 ~]# service nfs restart
在本地测试遇到问题:
rpc.mountd: svc_tli_create: could not open connection for tcp6 [FAILED]
Starting NFS daemon:
一直卡住的问题,然后重启服务器,才能启动nfs服务(不知线上的服务器上会不会有这样的问题)

[root@M2 ~]# showmount -e 192.168.1.248    【-e选项显示NFS服务端的导出列表。】

        如果客户端访问服务器报错,提示:clnt_create: RPC: Program not registered
        [root@M2 ~]# showmount -e 192.168.1.248
        clnt_create: RPC: Program not registered
        解决办法:在服务器上先停止rpcbind,
        /etc/init.d/rpcbind stop
        然后在停止nfs
        /etc/init.d/nfs stop
        最后在重启rpcbind和nfs,一定要按顺序启动和停止
        /etc/init.d/rpcbind start
        /etc/init.d/nfs start
        再执行:        
        [root@M2 ~]# showmount -e 192.168.1.248
        Export list for 192.168.1.248:
        /home/data/wwwroot/web/nfs_test 192.168.1.249
        成功!

2、挂载服务器的nfs
[root@M2 ~]# mount -t nfs 192.168.1.248:/data/wwwroot/web/nfs_test /data/wwwroot/web/nfs_test    
或者是:
[root@M2 ~]# mount -t nfs 192.168.1.248:/data/wwwroot/web/nfs_test /data/wwwroot/web/nfs_test -o proto=tcp -o nolock  【如果是网络不太稳定,NFS默认是用UDP协议,换成TCP协议即可】
[root@M2 ~]# df -h    【查看挂载】
192.168.1.248:/data/wwwroot/web/nfs_test
                       18G  1.5G   15G   9% /home/data/wwwroot/web/nfs_test
                       
3、系统启动加载
[root@M2 ~]# vi /etc/fstab
新增一条记录:
192.168.1.248:/data/wwwroot/web/nfs_test /data/wwwroot/web/nfs_test nfs defaults 0 0

[root@M2 ~]# mount -a
其中,第5个字段设置为0表示共享目录的文件系统不需要使用dump命令进行转储,第6个字段设置为0表示共享目录的文件系统不需要使用fsck命令进行检查。

4、测试:
客户端生成一个文件:

[root@M2 /]# cd /data/wwwroot/web/nfs_test
[root@M2 test]# echo “hello nfs test”>test
[root@M2 test]# ll
total 4
-rw-r–r– 1 root root 15 Apr  9 13:24 test
同样的方式在服务端检查,如果文件相同则挂载成功!

五、解除挂载:
[root@M2 ~]# umount /data/wwwroot/web/nfs_test
[root@M2 ~]#
如果遇到:umount.nfs: /data/wwwroot/web/nfs_test: device is busy
可能用命令:

[root@M2 /]# fuser -m -v /usr/local/test
                     用户     进程号 权限   命令
/usr/local/test/:              root       2798 ..c.. bash
                     root       2996 ..c.. su
[root@M2 /]# kill -9 2798
[root@M2 /]# kill -9 2996

[root@M2 /]# umount /usr/local/test

优点:比较简单的解决方案就是使用NFS服务,NFS做完linux系统广泛通用的文件共享系统,从NFSv1,NFSv2到NFSv3和NFSv4,NFS也是非常稳定和久经考验的解决方案。
缺点:NFS的缺点也是比较明显的,单节点风险,比如NFS服务挂掉,整个文件就不能访问,网站Down掉,因此NFS只能作为一个暂存的解决方案,使用NFS一定要有一个备份系统。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇