以下是一次nfs服务的配置记录文本,添加了注释:

NFS配置记录

配置说明:
1、仅单向nfs配置,nfs服务器192.168.1.100,客户端192.168.1.102
2、在centos6.x系统中,原来的portmap改名为rpcbind;
3、在启动nfs服务器前先启动portmap(rpcbind)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#192.168.1.100 server
login as: root
root@192.168.1.100’s password:
Last login: Tue Oct 9 04:50:30 2012 from 192.168.1.252
[root@lovn ~]# cd /home
[root@lovn home]# ll
总用量 65656
drwxr-xr-x. 25 root root 4096 9月 1 19:50 android
drwxr-x—. 5 12275 5000 4096 8月 10 04:19 android-sdk-linux
-rw-r–r–. 1 root root 67206726 8月 30 23:55 jdk-7u4-linux-i586.rpm
drwx——. 4 smb1 samba 4096 10月 7 06:58 smb1
drwx——. 4 smb2 samba 4096 10月 9 04:52 smb2
drwx——. 4 test test 4096 10月 9 04:55 test

#检查必备的相关包 portmap
[root@lovn home]# rpm -qa | grep portmap
#没有,则安装
[root@lovn home]# yum install portmap
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.neu.edu.cn
* extras: mirror.neu.edu.cn
* updates: mirrors.sin3.sg.voxel.net
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package rpcbind.i686 0:0.2.0-8.el6 will be updated
—> Package rpcbind.i686 0:0.2.0-9.el6 will be an update
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
rpcbind i686 0.2.0-9.el6 base 51 k

Transaction Summary
================================================================================
Upgrade 1 Package(s)

Total download size: 51 k
Is this ok [y/N]: y
Downloading Packages:
rpcbind-0.2.0-9.el6.i686.rpm | 51 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : rpcbind-0.2.0-9.el6.i686 1/2
Cleanup : rpcbind-0.2.0-8.el6.i686 2/2
Verifying : rpcbind-0.2.0-9.el6.i686 1/2
Verifying : rpcbind-0.2.0-8.el6.i686 2/2

Updated:
rpcbind.i686 0:0.2.0-9.el6

Complete!

#portmap 改叫rpcbind了~~~
#检查nfs的相关包
[root@lovn home]# rpm -qa |grep nfs
nfs-utils-lib-1.1.5-4.el6.i686
nfs-utils-1.2.3-15.el6.i686
nfs4-acl-tools-0.3.3-5.el6.i686

#创建一个目录
[root@lovn home]# mkdir /home/nfs
#编辑nfs的默认配置文件
[root@lovn home]# vi /etc/exports
#添加
/home/nfs 192.168.1.0/24(rw,root_squash,no_all_squash,sync)

[root@lovn home]# exportfs -r

##
#配置文件每行分为段:
#第一段为共享的目录,使用绝对路径
#第二段为客户端地址及权限:
#地址可以使用完整IP或网段,例如192.168.1.1或192.168.1.0/24,192.168.1.0/255.255.255.0也可以
#地址可以使用主机名,DNS解析的和本地/etc/hosts解析的都行,支持通配符,例如:*.domainname
#权限有:
#rw:read-write,可读写;(该目录分享的权限是可擦写 (read-write) 或只读 (read-only),但最终能不能读写,还是与文件系统的 rwx 及身份有关。)
#ro:read-only,只读;
#sync:文件同时写入硬盘和内存;
#async:文件暂存于内存,而不是直接写入内存;
#no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不#安全的。
#root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobod#y或nfsnobody身份;
#all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;
#anonuid:匿名用户的UID值,通常是nobody或nfsnobody,可以在此处自行设定;
#anongid:匿名用户的GID值。

#启动portmap(rpcbind)
[root@lovn home]# service rpcbind start
#查看portmap(rpcbind)的状态
[root@lovn home]# service rpcbind status
rpcbind (pid 2895) 正在运行…
#启动nfs
[root@lovn home]# service nfs start
启动 NFS 服务: [确定]
关掉 NFS 配额: [确定]
启动 NFS 守护进程: [确定]
启动 NFS mountd: [确定]
[root@lovn home]#
[root@lovn home]# showmount –help
Usage: showmount [-adehv]
[–all] [–directories] [–exports]
[–no-headers] [–help] [–version] [host]

#测试
[root@lovn home]# showmount -e 192.168.1.100
Export list for 192.168.1.100:
/home/nfs 192.168.1.0/24
[root@lovn home]#

[root@lovn home]# rpcinfo -p
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 44251 status
100024 1 tcp 55169 status
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 52231 nlockmgr
100021 3 udp 52231 nlockmgr
100021 4 udp 52231 nlockmgr
100021 1 tcp 43585 nlockmgr
100021 3 tcp 43585 nlockmgr
100021 4 tcp 43585 nlockmgr
100005 1 udp 42985 mountd
100005 1 tcp 45622 mountd
100005 2 udp 47913 mountd
100005 2 tcp 56059 mountd
100005 3 udp 46379 mountd
100005 3 tcp 54646 mountd

[root@lovn home]# service iptables stop
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
[root@lovn home]#

[root@lovn home]# cd nfs
[root@lovn nfs]# ll
总用量 0
[root@lovn nfs]# echo "hello,nfs on 192.168.1.100" >> nfs100.txt
[root@lovn nfs]# ll
总用量 4
-rw-r–r–. 1 root root 27 10月 11 04:48 nfs100.txt
[root@lovn nfs]#

[root@lovn home]# chmod 777 nfs
[root@lovn home]# ll
总用量 65660
drwxr-xr-x. 25 root root 4096 9月 1 19:50 android
drwxr-x—. 5 12275 5000 4096 8月 10 04:19 android-sdk-linux
-rw-r–r–. 1 root root 67206726 8月 30 23:55 jdk-7u4-linux-i586.rpm
drwxrwxrwx. 2 root root 4096 10月 11 04:48 nfs
drwx——. 4 smb1 samba 4096 10月 7 06:58 smb1
drwx——. 4 smb2 samba 4096 10月 9 04:52 smb2
drwx——. 4 test test 4096 10月 9 04:55 test

#客户端写入测试后查看
[root@lovn home]# ll nfs
总用量 8
-rw-r–r–. 1 root root 27 10月 11 04:48 nfs100.txt
-rw-r–r–. 1 nfsnobody nfsnobody 36 10月 11 04:56 nfs102.txt

##192.168.1.102 client ##################################
login as: root
root@192.168.1.102’s password:
[root@lovn ~]# rpm -qa | grep nfs
nfs-utils-lib-1.1.5-4.el6.i686
nfs-utils-1.2.3-15.el6.i686
nfs4-acl-tools-0.3.3-5.el6.i686

[root@lovn ~]# showmount -e 192.168.1.100
clnt_create: RPC: Port mapper failure – Unable to receive: errno 113 (No route to host)
[root@lovn ~]#
[root@lovn ~]# rpcinfo -p 192.168.1.100
rpcinfo: can’t contact portmapper: RPC: Remote system error – No route to host
[root@lovn ~]# ping 192.168.1.100
PING 192.168.1.100 (192.168.1.100) 56(84) bytes of data.
64 bytes from 192.168.1.100: icmp_seq=1 ttl=64 time=1.62 ms
64 bytes from 192.168.1.100: icmp_seq=2 ttl=64 time=3.82 ms
64 bytes from 192.168.1.100: icmp_seq=3 ttl=64 time=2.98 ms
64 bytes from 192.168.1.100: icmp_seq=4 ttl=64 time=0.381 ms
^C
— 192.168.1.100 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3046ms
rtt min/avg/max/mdev = 0.381/2.205/3.825/1.312 ms
[root@lovn ~]#

#192.168.1.100关闭防火墙后
[root@lovn ~]# showmount -e 192.168.1.100
Export list for 192.168.1.100:
/home/nfs 192.168.1.0/24
[root@lovn ~]#

#创建挂载点
[root@lovn ~]# mkdir /mnt/nfsdata
#挂载
[root@lovn ~]# mount -t nfs 192.168.1.100:/home/nfs /mnt/nfsdata
[root@lovn ~]#
#检查
[root@lovn ~]# mount
/dev/mapper/vg_lovn-lv_root 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)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.2_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
192.168.1.100:/home/nfs on /mnt/nfsdata type nfs (rw,vers=4,addr=192.168.1.100,clientaddr=192.168.1.102)
[root@lovn ~]#

[root@lovn ~]# ll /mnt/nfsdata/
总用量 4
-rw-r–r–. 1 root root 27 10月 11 04:48 nfs100.txt
[root@lovn ~]# cat /mnt/nfsdata/nfs100.txt
hello,nfs on 192.168.1.100
[root@lovn ~]#

#写文件测试
[root@lovn ~]# echo "hello,nfs server from 192.168.1.102" >> /mnt/nfsdata/nfs102.txt
-bash: /mnt/nfsdata/nfs102.txt: 权限不够
[root@lovn ~]#

#服务器端文件夹开放权限后
[root@lovn ~]# echo "hello,nfs server from 192.168.1.102" >> /mnt/nfsdata/nfs102.txt
[root@lovn ~]# ll /mnt/nfsdata/
总用量 8
-rw-r–r–. 1 root root 27 10月 11 04:48 nfs100.txt
-rw-r–r–. 1 nfsnobody nfsnobody 36 10月 11 04:56 nfs102.txt

常见错误及解决:

Cannot register service: RPC

1
2
3
4
5
6
7
8
9
[root@lovn ~]# service nfs restart
Shutting down NFS mountd: [ OK ]
Shutting down NFS daemon: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).
[FAILED]
1
2
3
#解决方法:
[root@lovn ~]# service portmap start
#要先启动portmap才行

mount: 192.168.1.100:/home/nfs failed, reason given by server: Permission denied

#解决方法:
#把该客户端的ip加入服务端的/etc/exports
#或者服务端的和客户端规则要统一,要么都使用主机名(注意每台机器的hosts文件),要么都使用IP

客户端挂载超时

#解决方法:

1
2
[root@lovn ~]# service portmap restart
[root@lovn ~]# service nfs restart

Error: RPC MTAB does not exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@lovn ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
Starting RPC idmapd: Error: RPC MTAB does not exist.

#解决方法:
#手动执行
[root@lovn ~]# mount -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs/
#需要时加入开机启动时,加入下面两行到/etc/fstab
rpc_pipefs /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0
nfsd /proc/fs/nfsd nfsd defaults 0 0