CentOS6.6NFS安装部署

1.部署环境

[root@linux-nfs-server ~]# uname  -r 系统内核信息
2.6.32-504.el6.x86_64

2.角色分配

主机名 IP 角色
linux-nfs-server 192.168.0.21 服务端
linux-nfs-client-1 192.168.0.22 客户端

3.开始部署

3.1 NFS所需软件

    1. nfs-utils这个是NFS服务的主程序,包括rpc.nfsd、rpc.mountd两个daemons
    1. rpcbind RPC主程序
#服务端客户端都安装
[root@linux-nfs-server ~]# yum install nfs-utils rpcbind -y 

#服务端操作即可,客户端不用操作
[root@linux-nfs-server ~]# /etc/init.d/rpcbind start   #启动rpcbind服务
Starting rpcbind:                                          [  OK  ]

[root@linux-nfs-server ~]# /etc/init.d/rpcbind status  #检查启动状态
rpcbind (pid  25976) is running... 

#rpcbind主端口
[root@linux-nfs-server ~]# lsof -i :111
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 25976  rpc    6u  IPv4  30324      0t0  UDP *:sunrpc 
rpcbind 25976  rpc    8u  IPv4  30327      0t0  TCP *:sunrpc (LISTEN)
rpcbind 25976  rpc    9u  IPv6  30329      0t0  UDP *:sunrpc 
rpcbind 25976  rpc   11u  IPv6  30332      0t0  TCP *:sunrpc (LISTEN)

#查看rpcbind的端口映射表,可以看到此时只有自己的端口号,NFS还没启动
[root@linux-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

#启动NFS
[root@linux-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  ]

#查看NFS主端口
[root@linux-nfs-server ~]# netstat -lntup |grep 2049
tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 :::2049                     :::*                        LISTEN      -                   
udp        0      0 0.0.0.0:2049                0.0.0.0:*                               -                   
udp        0      0 :::2049                     :::*              


#此时rpcinfo 查询端口映射表,可以发现NFS向rpcbind注册了自己的端口
[root@linux-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  38110  mountd
    100005    1   tcp  59198  mountd
    100005    2   udp  49969  mountd
    100005    2   tcp  56130  mountd
    100005    3   udp  37731  mountd
    100005    3   tcp  34686  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  52081  nlockmgr
    100021    3   udp  52081  nlockmgr
    100021    4   udp  52081  nlockmgr
    100021    1   tcp  35816  nlockmgr
    100021    3   tcp  35816  nlockmgr
    100021    4   tcp  35816  nlockmgr                

     #把nfs启动命令写入开机自启动文件中  
 [root@linux-nfs-server ~]# echo -e "/etc/init.d/rpcbind start \n/etc/init.d/nfs start" >> /etc/rc.local 

    #操作之后查看结果,保持良好的运维习惯
 [root@linux-nfs-server ~]# tail -2 /etc/rc.local 
 /etc/init.d/rpcbind start 
 /etc/init.d/nfs start
提示:启动NFS前必须先启动rpcbind否则将出错

4.NFS服务端配置

4.1 NFS配置文件格式

/data 192.168.0.1/24(rw,sync)

第一列 第二列 第三列
/data 共享的目录 192.168.0.1/24 共享给谁,一般是一个网段 (rw,sync)共享权限
  #NFS主配置文件,默认是空的
 [root@linux-nfs-server ~]# cat /etc/exports 

#添加如下配置
[root@linux-nfs-server ~]# vim /etc/exports 
/data   192.168.0.1/24(rw,sync)

#查看
[root@linux-nfs-server ~]# cat /etc/exports 
/data   192.168.0.1/24(rw,sync)

#平滑重启,共享目录必须存在,否则启动报错
[root@linux-nfs-server ~]# /etc/init.d/nfs reload

#查询本机共享的目录
[root@linux-nfs-server ~]# showmount  -e localhost
Export list for localhost:
/data 192.168.0.1/24

#在本机挂载共享目录,看看是否可以挂载成功
[root@linux-nfs-server ~]# mount -t nfs 192.168.0.21:/data /mnt

#查看挂载结果
[root@linux-nfs-server ~]# df -h
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           7.1G  1.5G  5.3G  22% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   27M  153M  15% /boot
192.168.0.21:/data  7.1G  1.5G  5.3G  22% /mnt    #挂载成功了

#在/data 目录下创建一个文件,是在data目录下哟
[root@linux-nfs-server ~]# touch /data/a.txt

#然后在/mnt下查看,可以发现,/mnt目录下多了一个a.txt文件
[root@linux-nfs-server ~]# ls -l /mnt/
total 0
-rw-r--r-- 1 root root 0 May  1 14:26 a.txt

#虽然可以读,但是在/mnt进行写操作时,发现权限错误
[root@linux-nfs-server ~]# touch /mnt/b.txt
touch: cannot touch /mnt/b.txt: Permission denied

#原因:客户端访问nfs的时候都会被解析成uid为65534,gid为65534的匿名用户nfsnobody
#解决:修改/data目录所属用户组,nfsnobody这个虚拟用户在安装nfs完成后,自动创建的
[root@linux-nfs-server ~]# chown  -R nfsnobody.nfsnobody /data

5.客户端配置

#查看服务端共享的目录,可以查到
[root@linux-nfs-client-1 ~]# showmount  -e 192.168.0.21
Export list for 192.168.0.21:
/data 192.168.0.1/24

[root@linux-nfs-client-1 ~]# mount -t nfs 192.168.0.21:/data /mnt  #挂载共享目录

#可以看到已经挂载成功了
[root@linux-nfs-client-1 ~]# df -h 
Filesystem          Size  Used Avail Use% Mounted on
/dev/sda3           7.1G  1.5G  5.3G  22% /
tmpfs               238M     0  238M   0% /dev/shm
/dev/sda1           190M   27M  153M  15% /boot
192.168.0.21:/data  7.1G  1.5G  5.3G  22% /mnt

[root@linux-nfs-server ~]# touch  /data/aaa.txt   #服务端创建文件

#此时客户端也有这个文件了
[root@linux-nfs-client-1 ~]# ls -l /mnt/
total 0
-rw-r--r-- 1 root root 0 May  2 08:31 aaa.txt

#卸载挂载点
[root@linux-nfs-client-1 ~]# umount /mnt  

#提示:如果卸载的时候提示umount /mnt: device is busy需要退出挂载目录进行卸载,或者NFS Server宕机了需要强制卸载
#umount -lf /mnt。注意如果NFS服务端宕机了用df –h可能无法查看挂载点,可以使用cat /proc/mounts 查看挂载点

提示:有人说客户端需要启动rpcbind才能挂载,但是我并没有启动,直接使用mount挂载的,也就说客户端不需要启动rpcbind也可以挂载,当然其他系统我不知道,但是CentOS6.6,貌似不用启动

6.NFS相关文件和命令

NFS配置文件

文件名 描述
/etc/exports NFS主配置文件,默认为空
/var/lib/nfs/etab 该文件记录了服务端NFS共享目录的的所有参数权限情况
/proc/mounts 该文件可以查看 NFS客户端挂载情况,当然也可以df -h 查看

NFS命令

文件名 描述
/usr/sbin/exportfs NFS管理命令,该命令可以做到无需NFS配置文件实现共享

7.NFS配置参数

7.1 服务端配置参数

参数 描述
rw 表示可读写权限
ro 表示只读权限
sync 同步写入磁盘,优点:数据安全不会丢失。缺点:性能差。
async 异步写入磁盘,优点:性能高,数据先写入内存。缺点:数据有可能丢失,数据同步会有延迟,数据短时间内可能会不一致。
no_root_squash 访问服务端共享目录的用户是root的话,那么他对该共享目录就具有root权限
root_squash 访问服务端共享目录的用户是root的话,那么会被解析成匿名用户,同时UID和GID会变成nfsnobody
all_squash 不管访问共享目录的用户身份是什么,它的权限都将被解析成匿名用户,同时它的UID和GID都会变成nfsnobody账号身份

7.2 客户端常用挂载参数

参数 描述
ro 只读的方式挂载
rw 读写的方式挂载
atime 在每一次数据访问时,同步更新每次访问的inode时间,是默认选项,在高并发的情况下,建议加上 noatime来取消这个默认选项来提升IO性能。起到优化IO的目的
noexec 在挂载文件系统中不允许直接执行二进制的程序,注意仅对二进制程序有效,shell,php还是可以执行的
nodiratime 不更新文件系统上的directory inode访问时间,高并发环境,建议应用该选项,提升IO性能
nosuid 不允许在文件系统上做suid的权限
nodev 不允许在文件系统上创建设备

8.NFS基本优化

#/proc/sys/net/core/rmem_default 该文件指定了接收套接字缓冲区大小的缺省值(以字节为单位)
#/proc/sys/net/core/rmem_max    该文件指定了接收套接字缓冲区大小的最大值(以字节为单位)
#/proc/sys/net/core/wmem_default该文件指定了发送套接字缓冲区大小的缺省值(以字节为单位)
#/proc/sys/net/core/wmem_max   该文件指定了发送套接字缓冲区大小的最大值(以字节为单位)
#内核优化
[root@linux-nfs-server ~]# cat >>/etc/sysctl.conf<<EOF
> net.core.wmem_default = 8388608
> net.core.rmem_default = 8388608
> net.core.rmem_max = 16777216
> net.core.wmem_max = 16777216
> EOF

[root@linux-nfs-server ~]# sysctl  -p   


#客户端挂载优化
[root@linux-nfs-client-1 ~]# mount -t nfs -o noatime,nodiratime,nodev,nosuid,noexec,rw 192.168.0.21:/data /mnt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容