NFS服务的搭建与配置

NFS的服务器介绍:

NFS是Network File System的缩写
 NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本
 NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。
 NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致

NFS的原理图:

46ea7283942e292e8676fee228f1e0f4215.jpg

NFS服务器的安装与配置:

yum install -y nfs-utils rpcbind
 vim /etc/exports //加入如下内容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
 保存配置文件后,执行如下准备操作
 mkdir /home/nfstestdir
 chmod 777 /home/nfstestdir
 systemctl start rpcbind
 systemctl start nfs
 systemctl enable rpcbind
 systemctl enable nfs

 

[root@aming01 ~]# vim /etc/exports  //编辑配置文件信息
[root@aming01 ~]# cat /etc/exports
/home/nfstestdir  192.168.88.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[root@aming01 ~]# mkdir  /home/nfstestdir  //创建共享的目录
[root@aming01 ~]# chmod 777 /home/nfstestdir/
[root@aming01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      944/nginx: master p
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      890/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      988/master          
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      944/nginx: master p
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      890/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      988/master          
tcp6       0      0 :::3306                 :::*                    LISTEN      1326/mysqld         
[root@aming01 ~]# systemctl start nfs     //启动nfs
[root@aming01 ~]# ps aux|grep nfs
root       2675  0.0  0.0      0     0 ?        S<   15:19   0:00 [nfsd4_callbacks]
root       2681  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2682  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2683  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2684  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2685  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2686  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2687  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2688  0.0  0.0      0     0 ?        S    15:19   0:00 [nfsd]
root       2722  3.0  0.0 112724   988 pts/0    R+   15:19   0:00 grep --color=auto nfs
[root@aming01 ~]#

NFS配置选项:

 rw 读写
 ro 只读
 sync 同步模式,内存数据实时写入磁盘
 async 非同步模式
 no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
 root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
 all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
 anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

客户端挂载:

yum install -y nfs-utils
 showmount -e 192.168.133.130 //该ip为NFS服务端ip
 mount -t nfs 192.168.133.130:/home/nfstestdir /mnt
 df -h
 touch /mnt/aminglinux.txt
 ls -l /mnt/aminglinux.txt //可以看到文件的属主和属组都为1000

401dde23917b9dbb1fc227b275bf4610959.jpg

45537297e4416e3de14ef8ede9ddc88ef06.jpg

d07656f595b581335cfb4d415cbd486ca2c.jpg

 

exportfs的命令

 常用选项
 -a 全部挂载或者全部卸载
 -r 重新挂载
 -u 卸载某一个目录
 -v 显示共享目录
 以下操作在服务端上
vim /etc/exports //增加
/tmp/ 192.168.133.0/24(rw,sync,no_root_squash)
 exportfs -arv //不用重启nfs服务,配置文件就会生效

[root@aming01 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        38G  4.1G   34G   11% /
devtmpfs        479M     0  479M    0% /dev
tmpfs           489M     0  489M    0% /dev/shm
tmpfs           489M  6.6M  482M    2% /run
tmpfs           489M     0  489M    0% /sys/fs/cgroup
/dev/sda1       252M  100M  153M   40% /boot
tmpfs            98M     0   98M    0% /run/user/0
[root@aming01 ~]# exportfs -arv
exporting 192.168.88.0/24:/home/nfstestdir
[root@aming01 ~]# vim /etc/exports
[root@aming01 ~]# cat /etc/exports
/home/nfstestdir  192.168.88.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp   192.168.88.129(rw,sync,no_root_squash)
[root@aming01 ~]# exportfs -arv
exporting 192.168.88.129:/tmp
exporting 192.168.88.0/24:/home/nfstestdir
[root@aming01 ~]# ls /tmp/

 

 以下操作在客户端
 mkdir /aminglinux
 mount -t nfs -onolock 192.168.133.130:/tmp /aminglinux
 touch /aminglinux/test.txt
 ls -l !$
 -oremount,nfsvers=3

[root@aming02 mnt]# showmount -e 192.168.88.128
Export list for 192.168.88.128:
/home/nfstestdir 192.168.88.0/24
/tmp             192.168.88.129
[root@aming02 mnt]# mount -t nfs 192.168.88.128:/tmp/  /mnt/
[root@aming02 mnt]# ls /mnt/

 

客户端文件属主属组nobody

 NFS 4版本会有该问题
 客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主、属组为nobody
 客户端挂载时加上 -o nfsvers=3
 客户端和服务端都需要
 vim /etc/idmapd.conf //
 把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3964315/blog/3053867

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值