实战NFS服务搭建与配置

nfs服务应用:
存储静态文件如:图片(头像logo),视频,附件等。

1. 查看所有机器是否安装了 rpcbind nfs-utils
[root@nfs-server ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-13.el6_9.1.x86_64
nfs-utils-1.2.3-75.el6_9.x86_64

如果没有安装则安装:
yum install nfs-utils rpcbind -y
2.服务端:

[root@nfs-server ~]# /etc/init.d/rpcbind status
rpcbind is stopped
[root@nfs-server ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
[root@nfs-server ~]# /etc/init.d/rpcbind status
rpcbind (pid 46014) is running...

#查看端口和服务验证rpcbind是否启动
[root@nfs-server ~]# lsof -i :111
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 46014 rpc 6u IPv4 43942 0t0 UDP *:sunrpc
rpcbind 46014 rpc 8u IPv4 43944 0t0 TCP *:sunrpc (LISTEN)
rpcbind 46014 rpc 9u IPv6 43945 0t0 UDP *:sunrpc
rpcbind 46014 rpc 11u IPv6 43947 0t0 TCP *:sunrpc (LISTEN)


[root@nfs-server ~]# netstat -lntup|grep rpcbind
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 46014/rpcbind
tcp 0 0 :::111 :::* LISTEN 46014/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 46014/rpcbind
udp 0 0 0.0.0.0:821 0.0.0.0:* 46014/rpcbind
udp 0 0 :::111 :::* 46014/rpcbind
udp 0 0 :::821 :::* 46014/rpcbind


[root@nfs-server ~]# ps -ef|grep rpcbind
rpc 46014 1 0 13:50 ? 00:00:00 rpcbind
root 46031 1341 0 13:53 pts/0 00:00:00 grep rpcbind
如果rpc 服务是停止的则提示:
[root@nfs-server ~]# rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused
此时使用下面命令开启:
[root@nfs-server ~]#/etc/init.d/rpcbind start

3. 启动NFS 服务
[root@nfs-server ~]# /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@nfs-server ~]# 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 16420 mountd
100005 1 tcp 29507 mountd
100005 2 udp 45473 mountd
100005 2 tcp 12943 mountd
100005 3 udp 29388 mountd
100005 3 tcp 22133 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 21445 nlockmgr
100021 3 udp 21445 nlockmgr
100021 4 udp 21445 nlockmgr
100021 1 tcp 27929 nlockmgr
100021 3 tcp 27929 nlockmgr
100021 4 tcp 27929 nlockmgr
4.加人开机自启动
[root@nfs-server ~]# chkconfig nfs on
[root@lnmp ~]# chkconfig rpcbind on
#检查开机自启动加入是否成功
[root@nfs-server ~]# chkconfig --list nfs
nfs 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@nfs-server ~]# chkconfig --list rpcbind
rpcbind 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

#写入开机启动文件/etc/rc.local 中

[root@nfs-server ~]# vi /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
>/etc/udev/rules.d/70-persistent-net.rules
/etc/init.d/rpcbind start
/etc/init.d/nfs start

 

***实战配置服务端 (将data 目录共享给192.168.0.0/24网段的主机,可读写)***
5. 服务器端配置

5.1 服务器端创建共享目录
[root@nfs-server ~]# mkdir /data

5.2 服务器端编辑nfs配置文件
vi /etc/exports
####################################
#share data by alvin at 2018-09-22
/data 192.168.0.0/24(rw,sync,all_squash)
####################################

服务器端检查配置
[root@nfs-server ~]# cat /etc/exports
#share data by alvin at 2018-09-22
/data 192.168.0.0/24(rw,sync,all_squash)
共享的目录名称 共享的网段 权限,同步
5.3 服务器端重启服务
[root@nfs-server ~]# /etc/init.d/nfs reload     # 此处是查看 nfs 中reload 命令情况 reload 相当于 exportfs -r

5.4 服务器端本机自身检查一下共享的情况
[root@nfs-server ~]# showmount -e localhost
Export list for localhost:
/data 192.168.0.0/24

#查看一下,/data目录的权限:
[root@nfs-server ~]# ll -ld /data
drwxr-xr-x 2 root root 4096 Sep 22 15:36 /data

[root@nfs-server ~]# cat /var/lib/nfs/etab    #因为默认的读写权限给的是:anonuid=65534
/data 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,sec=sys,rw,root_squash,no_all_squash)
[root@nfs-server ~]#


#如果不修改,客户端创建文件提示没有权限:
[root@lnmp02 mnt]# touch aaboy.log
touch: cannot touch aaboy.log: Permission denied


修改/data权限
[root@nfs-server ~]# grep 65534 /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@nfs-server ~]# chown -R nfsnobody.nfsnobody /data
[root@nfs-server ~]# ll -ld /data
drwxr-xr-x 2 nfsnobody nfsnobody 4096 Sep 22 15:36 /data

客户端
检查是否安装了rpcbind
[root@Client-3 ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-11.el6.i686
nfs-utils-1.2.3-39.el6.i686
#启动rpc服务并加入开机自启动就可以了
/etc/init.d/rpcbind start
/etc/init.d/rpcbind status
chkconfig rpcbind on
chkconfig --list rpcbind


客户端检查一下共享
[root@lamp01 ~]# showmount -e 192.168.0.15
Export list for 192.168.0.15:
/data 192.168.0.0/24

6.客户端检查并挂载/mnt
[root@lamp01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 25G 2.4G 21G 11% /
tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 969M 70M 849M 8% /boot

[root@lamp01 ~]# mount -t nfs 192.168.0.15:/data /mnt                    #192.168.0.15 是服务器端的IP地址
[root@lamp01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 25G 2.4G 21G 11% /
tmpfs 490M 0 490M 0% /dev/shm
/dev/sda1 969M 70M 849M 8% /boot
192.168.0.15:/data 25G 2.4G 21G 11% /mnt


为了防止客户端重启后不再挂载,需要添加开机自动挂载
echo "/etc/init.d/rpcbind start" >>/etc/rc.local
echo "/bin/mount -t nfs 192.168.0.15:/data /mnt" >>/etc/rc.local

[root@lamp01 ~]# 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
> /etc/udev/rules.d/70-persistent-net.rules
/etc/init.d/rpcbind start
/bin/mount -t nfs 192.168.0.15:/data /mnt

测试验证:

服务器端创建文件
[root@nfs-server ~]# cd /data
[root@nfs-server data]# ll
total 0
[root@nfs-server data]# touch a.txt
[root@nfs-server data]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 22 16:09 a.txt          #服务器端创建的文件,用户和属组是root


客户端删除文件
[root@lnmp02 ~]# ll /mnt
total 0
-rw-r--r-- 1 root root 0 Sep 22 16:09 a.txt
[root@lnmp02 ~]# rm -f /mnt/a.txt
[root@lnmp02 ~]# ll /mnt
total 0
[root@lnmp02 ~]#

服务器端再次查看,发现消失.............
[root@nfs-server data]# ll
total 0
[root@nfs-server data]#

#服务器端创建文件,演示同上省略
[root@lnmp02 ~]# cd /mnt
[root@lnmp02 mnt]# touch b.txt
[root@lnmp02 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Sep 22 16:17 b.txt         # 客户端创建的文件,用户和属组是nfsnobody

posted on 2018-09-22 16:54  飚车之神  阅读(759)  评论(0编辑  收藏  举报

导航