CentOS 下NFS服务搭建与配置

1. NFS服务端搭建

1.1. 安装NFS和rpc 【服务端、客户端都安装】

[root@backup ~]# rpm -qa nfs-utils rpcbind
[root@backup ~]# yum install nfs-utils rpcbind -y    #nfs需要的安装包
[root@backup ~]# rpm -qa nfs-utils rpcbind
nfs-utils-1.2.3-64.el6.x86_64
rpcbind-0.2.0-11.el6_7.x86_64

1.2. 查看用户信息

[root@nfs01 ~]# tail /etc/passwd
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
oldboy:x:500:500::/home/oldboy:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin        #yum安装rpc服务时创建的
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin        #yum安装rpc服务时创建的
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin    #yum安装nfs服务时创建的

1.3. 配置exports

[root@nfs01 ~]# mkdir /data
[root@nfs01 ~]# ll -d /data/
drwxr-xr-x. 3 root root 4096 Apr 11 09:49 /data/
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data/   
[root@nfs01 ~]# ll -d /data/                     
drwxr-xr-x. 3 nfsnobody nfsnobody 4096 Apr 11 09:49 /data/
[root@nfs01 ~]# cat /etc/exports   
# share /data for web created by zhangliang at 2016-05-21
/data  172.16.1.0/24(rw,sync)
#172.16.1.0/24(rw,sync) 没有空格

1.4. 启动rpcbind服务

[root@nfs01 ~]# /etc/init.d/rpcbind start   
[root@nfs01 ~]# netstat -anp | grep 'rpc'   
tcp        0      0 0.0.0.0:111                0.0.0.0:*                  LISTEN      1346/rpcbind       
tcp        0      0 0.0.0.0:38420              0.0.0.0:*                  LISTEN      1368/rpc.statd     
tcp        0      0 :::13894                    :::*                        LISTEN      1368/rpc.statd     
tcp        0      0 :::111                      :::*                        LISTEN      1346/rpcbind       
udp        0      0 0.0.0.0:673                0.0.0.0:*                              1346/rpcbind       
udp        0      0 127.0.0.1:703              0.0.0.0:*                              1368/rpc.statd     
udp        0      0 0.0.0.0:15306              0.0.0.0:*                              1368/rpc.statd     
udp        0      0 0.0.0.0:111                0.0.0.0:*                              1346/rpcbind       
udp        0      0 :::673                      :::*                                    1346/rpcbind       
udp        0      0 :::50537                    :::*                                    1368/rpc.statd     
udp        0      0 :::111                      :::*                                    1346/rpcbind       
unix  2      [ ACC ]    STREAM    LISTENING    10120  1346/rpcbind        /var/run/rpcbind.sock
unix  2      [ ]        DGRAM                    10207  1368/rpc.statd     
[root@nfs01 ~]# 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
    100024    1  udp  15306  status
    100024    1  tcp  38420  status
#### 由上可知,暂时只有自己的端口服务,没有其他的

1.5. 启动NFS

[root@nfs01 ~]# /etc/init.d/nfs start   
Starting NFS services:                                    [  OK  ]
Starting NFS quotas:                                      [  OK  ]
Starting NFS mountd:                                      [  OK  ]
Starting NFS daemon:                                      [  OK  ]
正在启动 RPC idmapd:                                      [确定]
[root@nfs01 ~]# 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
    100024    1  udp  15306  status
    100024    1  tcp  38420  status
    100011    1  udp    875  rquotad
    100011    2  udp    875  rquotad
    100011    1  tcp    875  rquotad
    100011    2  tcp    875  rquotad
    100005    1  udp  11473  mountd
    100005    1  tcp  62369  mountd
    100005    2  udp  17528  mountd
    100005    2  tcp  47308  mountd
    100005    3  udp  11312  mountd
    100005    3  tcp  51724  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  25181  nlockmgr
    100021    3  udp  25181  nlockmgr
    100021    4  udp  25181  nlockmgr
    100021    1  tcp  20093  nlockmgr
    100021    3  tcp  20093  nlockmgr
    100021    4  tcp  20093  nlockmgr

1.6. 加入开始自启动

1.6.1. 使用 /etc/rc.local

[root@nfs01 ~]# tail /etc/rc.local   
# 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

# start rpc and nfs server
/etc/init.d/rpcbind start
/etc/init.d/nfs start

1.6.2. 使用chkconfig

[root@nfs01 ~]# chkconfig rpcbind on   
[root@nfs01 ~]# chkconfig nfs on   
[root@nfs01 ~]# ls /etc/rc.d/rc3.d/* | grep -E 'rpc|nfs' 
/etc/rc.d/rc3.d/K61nfs-rdma
/etc/rc.d/rc3.d/K69rpcsvcgssd
/etc/rc.d/rc3.d/S13rpcbind
/etc/rc.d/rc3.d/S14nfslock
/etc/rc.d/rc3.d/S19rpcgssd
/etc/rc.d/rc3.d/S30nfs

查看rpc服务和NFS服务的开机启动顺序

[root@nfs01 ~]# head /etc/init.d/rpcbind
#! /bin/sh
#
# rpcbind      Start/Stop RPCbind
#
# chkconfig: 2345 13 87                    #运行级别  开机顺序  关机顺序【其中 2345 指的是 运行级别】
# 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.
#
# processname: rpcbind
[root@nfs01 ~]# head /etc/init.d/nfs   
#!/bin/sh
#
# nfs          This shell script takes care of starting and stopping
#              the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file sharing across networks.
#              This service provides NFS server functionality, which is \
#              configured via the /etc/exports file.
# probe: true

1.7. 查看有哪些参数生效

[root@nfs01 ~]# cat /var/lib/nfs/etab
/data  172.16.1.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)

1.8. 检查是否成功

[root@nfs01 ~]# showmount -e 172.16.1.31   
Export list for 172.16.1.31:
/data 172.16.1.0/24

2. NFS客户端配置

2.1. 开始rpcbind服务

[root@web01 ~]# /etc/init.d/rpcbind start       
Starting rpcbind:                                          [  OK  ]

2.2. 检查共享信息

[root@web01 ~]# showmount -e 172.16.1.31   
Export list for 172.16.1.31:
/data 172.16.1.0/24

2.3. NFS挂载

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt   

2.4. 查看挂载信息

[root@web01 ~]# df -h        #有时可能会被卡主
Filesystem        Size  Used Avail Use% Mounted on
/dev/sda3          8.8G  1.5G  6.9G  18% /
tmpfs              495M    0  495M  0% /dev/shm
/dev/sda1          190M  40M  141M  23% /boot
172.16.1.31:/data  8.8G  1.5G  6.9G  18% /mnt

或者

[root@web01 ~]# cat /proc/mounts                    # 优先使用,监控时使用该命令
rootfs / rootfs rw 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
devtmpfs /dev devtmpfs rw,relatime,size=490920k,nr_inodes=122730,mode=755 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /dev/shm tmpfs rw,relatime 0 0
/dev/sda3 / ext4 rw,relatime,barrier=1,data=ordered 0 0
/proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0
/dev/sda1 /boot ext4 rw,relatime,barrier=1,data=ordered 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
172.16.1.31:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0

2.5. 测试

在客户端和服务端之间测试

  1、客户端创建文件并且输入数据,在服务端是否可以查看

  2、服务端创建文件并且输入数据,在客户端是否可以查看

  3、在服务端删除客户端创建的文件

  4、在客户端删除服务端创建的文件

2.6. 加入开机自启动

[root@web01 mnt]# tail -3 /etc/rc.local   
# mount nfs
mount -t nfs 172.16.1.31:/data /mnt

猜你喜欢

转载自www.linuxidc.com/Linux/2018-08/153348.htm