NFS服务基础

NFS服务部署:

(一)NFS环境准备:

操作系统

角色

IP地址

CentOS Linux release 7.3.1611 (Core)

NFS服务端

192.168.152.138

CentOS Linux release 7.3.1611 (Core)

NFS客户端

192.168.152.139

 

(二)架构图

 

(三)NFS服务器操作系统及内核版本

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
[root@localhost ~]# uname -m
x86_64
[root@localhost ~]# 

(四)安装前准备

查看nfs-utilsrpcbind包是否安装

[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]#

#补充NFS安装方式(3种):

1)方法1yum -y install nfs-utils rpcbind

2)方法2rpm包安装:例如rpm -ivh nfs-utils*

3)方法3LANG=EN

           yum grouplist|grep -i nfs

           yum groupinstall “NFS file server” -y

 

(五)使用方法1安装所需软件包

检查软件是否安装:

[root@localhost ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-38.el7.x86_64
nfs-utils-1.3.0-0.33.el7.x86_64

(六)启动NFS-server的服务

# 先启动rpc服务,再启动nfs服务
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl status rpcbind
  rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
   Active: active (running) since 日 2017-11-05 18:05:08 CST; 22min ago
 Main PID: 2361 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─2361 /sbin/rpcbind -w

11月 05 18:05:08 localhost.localdomain systemd[1]: Starting RPC bind service...
11月 05 18:05:08 localhost.localdomain systemd[1]: Started RPC bind service.
[root@localhost ~]# ps -ef | grep -v grep | grep rpcbind
rpc        2361      1  0 18:05 ?        00:00:00 /sbin/rpcbind -w
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# 

(七)设置开机自启动

[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl list-unit-files | grep nfs.service
nfs.service                                 enabled 

(八)配置NFS服务端

配置文件路径:/etc/exports,默认为空

[root@localhost ~]#  ll /etc/exports
-rw-r--r--. 1 root root 0 Jan 12  2010 /etc/exports
[root@localhost ~]# cat /etc/exports
[root@localhost ~]# 

编辑配置文件

# 添加如下内容
[root@localhost ~]# cat /etc/exports
/data 192.168.152.138/24(rw,sync,all_squash)

# 创建共享目录/data
[root@localhost ~]# mkdir -p /data    
[root@localhost ~]# ls -ld /data/      
drwxr-xr-x. 2 root root 4096 Apr  3 05:23 /data/
# 更改属主属组为nfsnobody
[root@localhost ~]# chown -R nfsnobody.nfsnobody /data/ 
[root@localhost ~]# ls -ld /data/
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Apr  3 05:23 /data/

(九)重新加载NFS服务(优雅重启)

# systemctl reload nfs 等于 exportfs -r
[root@localhost ~]# systemctl reload nfs 
[root@localhost ~]# cat /var/lib/nfs/etab 
/data   192.168.152.138/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
[root@localhost ~]# showmount -e 192.168.152.138
Export list for 192.168.152.138:
/data 192.168.152.138/24
# 看到共享目录
[root@localhost ~]# showmount -e localhost
Export list for localhost:
/data 192.168.152.138/24
[root@localhost ~]# 

 

(十)客户端安装

# 安装客户端
[root@localhost ~]# yum install nfs-utils rpcbind -y 

# 挂载前检查是否有需要挂载的信息,是否能够挂载
[root@localhost ~]# showmount -e 192.168.152.138
Export list for 192.168.152.138:
/data 192.168.152.138/24

# 挂载并查看挂载后的结果
[root@localhost ~]# mount -t nfs 192.168.152.138:/data/ /mnt/
[root@localhost ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/cl-root     17G  1.4G   16G   9% /
devtmpfs               478M     0  478M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M   13M  476M   3% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda1             1014M  139M  876M  14% /boot
tmpfs                   98M     0   98M   0% /run/user/0
192.168.152.138:/data   17G  3.7G   14G  22% /mnt
[root@localhost ~]# grep mnt /proc/mounts 
192.168.152.138:/data /mnt nfs4 rw,relatime,vers=4.1,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.152.139,local_lock=none,addr=192.168.152.138 0 0
# 截止到现在说明NFS客户端已经挂载成功。可以进入/mnt写入实验数据,然后去看看NFS Server端的/data/bbs里是否有从NFS客户端/mnt目录传过来的数据。
# 测试读写数据
# 可读
[root@localhost mnt]# ls /mnt/
dazhaxie.txt
# 可写
[root@localhost mnt]# mkdir /mnt/test
# 用户和用户组都是nfsnobody。默认所有的客户端写入文件和目录都会被压缩成默认的uid为65534的nfsnobody用户。
[root@localhost mnt]# ls -l /mnt/
total 4
-rw-r--r--. 1 root      root      4 Nov  5 18:44 dazhaxie.txt
drwxr-xr-x. 2 nfsnobody nfsnobody 6 Nov  5 18:44 test

# 加入rc.local 实现开机自动挂载
[root@localhost mnt]# echo "/bin/mount -t nfs 192.168.152.138:/data/ /mnt" >> /etc/rc.local 
[root@localhost mnt]# echo "# mount by luchuangao" >> /etc/rc.local 
[root@localhost mnt]# tail -2 /etc/rc.local 
/bin/mount -t nfs 192.168.152.138:/data/ /mnt
# mount by luchuangao

关闭NFS

systemctl stop nfs
systemctl disable rpcbind.service
systemctl stop rpcbind.service
# Warning: Stopping rpcbind.service, but it can still be activated by: rpcbind.socket
systemctl disable rpcbind.socket
systemctl stop rpcbind.socket

 

 

 

 

posted @ 2017-11-05 18:50  luchuangao  阅读(1800)  评论(0编辑  收藏  举报