NFS客户端:
[root@oldboy ~]#yum install nfs-utils rpcbind -y NFS客户端
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.2.3-54.el6 will be installed
--> Processing Dependency: nfs-utils-lib >= 1.1.0-3 for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: keyutils >= 1.4-4 for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libtirpc for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libgssglue.so.1(libgssapi_CITI_2)(64bit) for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libgssglue for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libevent for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libtirpc.so.1()(64bit) for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libnfsidmap.so.0()(64bit) for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libgssglue.so.1()(64bit) for package: 1:nfs-utils-1.2.3-54.el6.x86_64
--> Processing Dependency: libevent-1.4.so.2()(64bit) for package: 1:nfs-utils-1.2.3-54.el6.x86_64
---> Package rpcbind.x86_64 0:0.2.0-11.el6 will be installed
--> Running transaction check
---> Package keyutils.x86_64 0:1.4-5.el6 will be installed
---> Package libevent.x86_64 0:1.4.13-4.el6 will be installed
---> Package libgssglue.x86_64 0:0.1-11.el6 will be installed
---> Package libtirpc.x86_64 0:0.2.1-10.el6 will be installed
---> Package nfs-utils-lib.x86_64 0:1.1.5-9.el6_6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================
Package Arch Version Repository
Size
===================================================
Installing:
nfs-utils x86_64 1:1.2.3-54.el6 base 326 k
rpcbind x86_64 0.2.0-11.el6 base 51 k
Installing for dependencies:
keyutils x86_64 1.4-5.el6 base 39 k
libevent x86_64 1.4.13-4.el6 base 66 k
libgssglue x86_64 0.1-11.el6 base 23 k
libtirpc x86_64 0.2.1-10.el6 base 79 k
nfs-utils-lib x86_64 1.1.5-9.el6_6 updates 68 k
Transaction Summary
===================================================
Install 7 Package(s)
Total download size: 652 k
Installed size: 1.7 M
Is this ok [y/N]: y
Downloading Packages:
(1/7): keyutils-1.4-5.el6.x | 39 kB 00:00
(2/7): libevent-1.4.13-4.el | 66 kB 00:00
(3/7): libgssglue-0.1-11.el | 23 kB 00:00
(4/7): libtirpc-0.2.1-10.el | 79 kB 00:00
(5/7): nfs-utils-1.2.3-54.e | 326 kB 00:01
(6/7): nfs-utils-lib-1.1.5- | 68 kB 00:00
(7/7): rpcbind-0.2.0-11.el6 | 51 kB 00:00
---------------------------------------------------
Total 147 kB/s | 652 kB 00:04
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : libgssglue-0.1-11.el6.x86_64 1/7
Installing : libtirpc-0.2.1-10.el6.x86_64 2/7
Installing : rpcbind-0.2.0-11.el6.x86_64 3/7
Installing : keyutils-1.4-5.el6.x86_64 4/7
Installing : libevent-1.4.13-4.el6.x86_64 5/7
Installing : nfs-utils-lib-1.1.5-9.el6_6.x 6/7
Installing : 1:nfs-utils-1.2.3-54.el6.x86_ 7/7
Verifying : libevent-1.4.13-4.el6.x86_64 1/7
Verifying : rpcbind-0.2.0-11.el6.x86_64 2/7
Verifying : keyutils-1.4-5.el6.x86_64 3/7
Verifying : 1:nfs-utils-1.2.3-54.el6.x86_ 4/7
Verifying : nfs-utils-lib-1.1.5-9.el6_6.x 5/7
Verifying : libtirpc-0.2.1-10.el6.x86_64 6/7
Verifying : libgssglue-0.1-11.el6.x86_64 7/7
Installed:
nfs-utils.x86_64 1:1.2.3-54.el6
rpcbind.x86_64 0:0.2.0-11.el6
Dependency Installed:
keyutils.x86_64 0:1.4-5.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-10.el6
nfs-utils-lib.x86_64 0:1.1.5-9.el6_6
Complete!
[root@backup ~]# rpm -qa "nfs-utils|rpcbind|portmap" NFS服务端
nfs-utils-1.2.3-54.el6.x86_64
nfs-utils-lib-1.1.5-9.el6_6.x86_64
rpcbind-0.2.0-11.el6.x86_64
[root@oldboy ~]#rpm -qa "nfs-utils|rpcbind|portmap" NFS客户端
nfs-utils-lib-1.1.5-9.el6_6.x86_64
rpcbind-0.2.0-11.el6.x86_64
nfs-utils-1.2.3-54.el6.x86_64
NFS服务端:
[root@backup ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
[root@backup ~]# ps -ef|grep rpc
rpc 3169 1 0 08:58 ? 00:00:00 rpcbind
root 3173 2900 0 08:58 pts/0 00:00:00 grep rpc
[root@backup ~]# /etc/init.d/rpcbind status
rpcbind (pid 3169) is running...
[root@backup ~]# rpcinfo -p localhost
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
[root@backup ~]# /etc/init.d/rpcbind stop
Stopping rpcbind: [ OK ]
[root@backup ~]# rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused
[root@backup ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
[root@backup ~]# rpcinfo -p localhost
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
[root@backup ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@backup ~]# rpcinfo -p localhost
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
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100005 1 udp 32923 mountd
100005 1 tcp 38492 mountd
100005 2 udp 44186 mountd
100005 2 tcp 52464 mountd
100005 3 udp 42052 mountd
100005 3 tcp 37846 mountd
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 58384 nlockmgr
100021 3 udp 58384 nlockmgr
100021 4 udp 58384 nlockmgr
100021 1 tcp 49852 nlockmgr
100021 3 tcp 49852 nlockmgr
100021 4 tcp 49852 nlockmgr
[root@backup ~]# chkconfig rpcbind on
[root@backup ~]# chkconfig nfs on
[root@backup ~]# chkconfig --list nfs
nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@backup ~]# chkconfig --list rpcbind
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@backup ~]# more /etc/init.d/rpcbind
#! /bin/sh
#
# rpcbind Start/Stop RPCbind
#
# chkconfig: 2345 13 87
# description: The rpcbind utility is a server that
converts RPC program \
# numbers into universal addresses. It
must be running on the \
# host to be able to make RPC calls on
a server on that machine.
#
[root@backup ~]# more /etc/init.d/nfs
#!/bin/sh
#
# nfs This shell script takes care of sta
rting and stopping
# the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file s
haring across networks.
# This service provides NFS server fun
ctionality, which is \
# configured via the /etc/exports file
通过查看以上两个服务的启动脚本,得出,启动时先启动rpcbind,然后是nfs;关闭时先关闭NFS,然后关闭rpcbind.
NFS客户端:
[root@oldboy ~]#/etc/init.d/rpcbind start 客户端只需要启动rpcbind
Starting rpcbind: [ OK ]
[root@oldboy ~]#/etc/init.d/rpcbind status
rpcbind (pid 2924) is running...
[root@oldboy ~]#chkconfig rpcbind on
[root@oldboy ~]#chkconfig --list rpcbind
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
NFS服务端:
[root@backup ~]# ll /etc/exports
-rw-r--r--. 1 root root 0 Jan 12 2010 /etc/exports
[root@backup ~]# cat /etc/exports
[root@backup ~]# vi /etc/exports
#shared backup for bbs by oldboy at 20150711
/backup/NFS 192.168.0.0/24(rw,sync)
~
~
~
~
"/etc/exports" 2L, 81C written
[root@backup ~]# cat /etc/exports
#shared backup for bbs by oldboy at 20150711
/backup/NFS 192.168.0.0/24(rw,sync)
[root@backup ~]# /etc/init.d/nfs reload和exportfs -r是等价的 优雅平滑重启NFS服务,不影响服务使用,提升客户体验
[root@backup ~]# showmount -e localhost 服务端查看共享
Export list for localhost:
/backup/NFS 192.168.0.0/24
NFS客户端:
[root@oldboy ~]#showmount -e 192.168.0.114 客户端查看共享
Export list for 192.168.0.114:
/backup/NFS 192.168.0.0/24
客户端查看不到NFS服务端共享的解决方法:
1.ping 服务端IP
2.telnet 服务端IP 111(rpcbind服务监听端口)
[root@oldboy ~]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.6G 2.2G 5.1G 31% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
[root@oldboy ~]#mount -t nfs 192.168.0.114:/backup/NFS /mnt
[root@oldboy ~]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.6G 2.2G 5.1G 31% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
192.168.0.114:/backup/NFS
7.6G 4.7G 2.6G 65% /mnt
[root@backup ~]# ll /backup/NFS NFS服务端共享的目录要给其他用户给“写权限”,以免客户端写数据时出现“无权限”的错误
total 4
drwxrwxrwx 5 nobody nobody 4096 Apr 13 00:00 data
[root@oldboy ~]#ll /mnt NFS客户端
total 4
drwxrwxrwx 5 nobody nobody 4096 Apr 13 00:00 data
[root@backup ~]# cat /var/lib/nfs/etab
/backup/NFS 192.168.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534)
[root@backup ~]# grep 65534 /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
提高NFS服务端共享目录的安全性操作
[root@backup ~]# chmod 755 /backup
[root@backup ~]# chown -R nfsnobody.nfsnobody /backup
NFS客户端:
[root@oldboy mnt]#pwd
/mnt
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
[root@oldboy mnt]#touch oldboy.log
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 11 10:32 oldboy.log
[root@oldboy mnt]#rm -f oldboy.log
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
NFS服务端:
[root@backup ~]# chmod 755 /backup/NFS
[root@backup ~]# ll /backup
total 16
drwxrwxrwx 3 nfsnobody nfsnobody 4096 Apr 7 00:41 LAMP
drwxrwxrwx 3 nfsnobody nfsnobody 4096 Apr 7 00:39 LNMP
drwxrwxrwx 3 nfsnobody nfsnobody 4096 Apr 7 00:35 mysql
drwxr-xr-x 3 nfsnobody nfsnobody 4096 Jul 11 10:32 NFS
NFS客户端:
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
[root@oldboy mnt]#touch dd.log
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 11 10:37 dd.log
[root@oldboy mnt]#rm -f dd.log
[root@oldboy mnt]#ll
total 4
drwxrwxrwx 5 nfsnobody nfsnobody 4096 Apr 13 00:00 data
[root@oldboy mnt]#echo "mount -t nfs 192.168.0.114:/backup/NFS /mnt” \
>>/etc/rc.local 加入开机启动
[root@oldboy mnt]#cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
mount -t nfs 192.168.0.114:/backup/NFS /mnt
三、NFS服务的相关功能组件介绍
NFS服务端:
[root@backup ~]# /etc/init.d/rpcbind status
rpcbind (pid 3239) is running...
[root@backup ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 3284) is running...
nfsd (pid 3300 3299 3298 3297 3296 3295 3294 3293) is running...
rpc.rquotad (pid 3279) is running...
[root@backup ~]# ps -ef|grep -E "rpc|nfs"
rpc 3239 1 0 09:01 ? 00:00:00 rpcbind rpc服务 rpcbind==>portmap(C5.8)远程过程调用服务
root 3270 2 0 09:01 ? 00:00:00 [rpciod/0]
root 3279 1 0 09:01 ? 00:00:00 rpc.rquotad 磁盘配额进程
root 3284 1 0 09:01 ? 00:00:00 rpc.mountd 权限管理进程
root 3291 2 0 09:01 ? 00:00:00 [nfsd4]
root 3292 2 0 09:01 ? 00:00:00 [nfsd4_callbacks]
root 3293 2 0 09:01 ? 00:00:00 [nfsd] NFS主进程
root 3294 2 0 09:01 ? 00:00:00 [nfsd]
root 3295 2 0 09:01 ? 00:00:00 [nfsd]
root 3296 2 0 09:01 ? 00:00:00 [nfsd] NFS主进程
root 3297 2 0 09:01 ? 00:00:00 [nfsd]
root 3298 2 0 09:01 ? 00:00:00 [nfsd]
root 3299 2 0 09:01 ? 00:00:00 [nfsd]
root 3300 2 0 09:01 ? 00:00:00 [nfsd] NFS主进程
root 3327 1 0 09:01 ? 00:00:00 rpc.idmapd
root 9165 6649 0 20:56 pts/1 00:00:00 grep -E rpc|nfs
[root@backup ~]# /etc/init.d/nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down NFS services: [ OK ]
Shutting down RPC idmapd: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@backup ~]# ps -ef|egrep "rpc|nfs"
rpc 3239 1 0 09:01 ? 00:00:00 rpcbind
root 3270 2 0 09:01 ? 00:00:00 [rpciod/0]
root 9268 1 0 20:58 ? 00:00:00 rpc.rquotad
root 9273 1 0 20:58 ? 00:00:00 rpc.mountd
root 9280 2 0 20:58 ? 00:00:00 [nfsd4]
root 9281 2 0 20:58 ? 00:00:00 [nfsd4_callbacks]
root 9282 2 0 20:58 ? 00:00:00 [nfsd]
root 9283 2 0 20:58 ? 00:00:00 [nfsd]
root 9284 2 0 20:58 ? 00:00:00 [nfsd]
root 9285 2 0 20:58 ? 00:00:00 [nfsd]
root 9286 2 0 20:58 ? 00:00:00 [nfsd]
root 9287 2 0 20:58 ? 00:00:00 [nfsd]
root 9288 2 0 20:58 ? 00:00:00 [nfsd]
root 9289 2 0 20:58 ? 00:00:00 [nfsd]
root 9316 1 0 20:58 ? 00:00:00 rpc.idmapd
root 9365 6649 0 21:08 pts/1 00:00:00 egrep rpc|nfs
[root@backup ~]# cat /var/lib/nfs/etab
/backup/NFS 192.168.0.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534)
NFS客户端:
[root@oldboy mnt]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 7.6G 2.2G 5.1G 31% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
192.168.0.114:/backup/NFS
7.6G 4.7G 2.6G 65% /mnt
[root@oldboy mnt]#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)
/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)
192.168.0.114:/backup/NFS on /mnt type nfs (rw,vers=4,addr=192.168.0.114,clientaddr=192.168.0.131)
[root@backup ~]# cat /etc/exports
#shared backup for bbs by oldboy at 20150711
/backup/NFS(共享目录)(可以访问共享目录的网段或主机、域名)192.168.0.0/24(rw,sync)(访问的客户端权限以及数据是否从内存缓存同步到磁盘)
四、配置NFS生产重要技巧
1.确保所有服务器对NFS共享目录具备相同的权限
a.all_squash把所有客户端都压缩成匿名用户
b.就是anonuid,anongid指定的UID和GID的用户
2.所有的客户端和服务端都需要有一个相同的UID和GID的用户,即nfsnobody(UID必须相同)
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-07/120502.htm
