NFS服务配置

 引言:NFS服务经常会用到,它用于网络上共享存储。举例来说,
        假如由三台机器ABC,他们急需要访问一个目录,且目录
   中都是图片,传统的做法就是把这些图片分别放到ABC中
   但使用NFS服务,只需要把图片放到A上,然后A共享给B,
   和C。
17.1服务端配置NFS
        前提:安装俩个包(nfs-utils和rpcbind)
        不过当使用yum工具安装nfs-utils时会一并安装上rpcbind
[root@ns1 ~]# yum install -y nfs-utils
已加载插件:fastestmirror, security
设置安装进程
Determining fastest mirrors
file:///mnt/repodata/repomd.xml: [Errno 14] Could not open/read file:///mnt/repodata/repomd.xml
尝试其他镜像。
包 1:nfs-utils-1.2.3-75.el6.x86_64 已安装并且是最新版本
无须任何处理
[root@ns1 ~]# 
配置NFS只需要编辑配置文件/etc/exports,如下所示
添加这一行
/home/nfstestdir 172.16.1.0/16(rw,sync,all_squash,anonuid=1000,anongid=1000)
这个配置文件就一行,共分为三部分,第一部分是本地要共享出去的目录,
第二段是允许访问的主机(可以是一个IP,也可以是一个IP段),第三部分
就是小括号里面的一些权限选项。


 rw:表示读/写
 ro:表示只读
 sync:同步模式,表示内存中的数据实时写入磁盘
 async:非同步模式,表示把内存中的数据定期写入磁盘
 no_root_squash:加上这个选项后,root用户就会对共享的目录拥有至高的权限控制。
 root_squash:与上面这个选项相对应,表示root用户对共享目录的权限不高,只有普通用户的权限
 all_squash:表示不管使用NFS的用户是谁,其身份都会被限定为一个指定的普通用户身份
 anonuid/anongid:要和root_squash以及all_squash选项一同使用,用于指定使用NFS的用户被限定
 后的uid和gid,但前提是本机的/etc/passwd中存在相应的uid和gid


编辑好配置文件后创建相关目录并启动NFS服务,如下所示:(这是centos6.9版本)
[root@ns1 ~]# service nfs restart
关闭 NFS 守护进程:                              [确定]
关闭 NFS mountd:                                [确定]
关闭 NFS quotas:                                [确定]
Shutting down RPC idmapd:                        [确定]
启动 NFS 服务:                                  [确定]
关掉 NFS 配额:                                  [确定]
启动 NFS mountd:                                [确定]
启动 NFS 守护进程:                              [确定]
正在启动 RPC idmapd:                            [确定]


centos7版本的命令如下:
# systemctl start rpcbind
# systemctl start nfs
# systemctl enable rpcbind
# systemctl enable nfs
注意:centos7版本的命令在6.9当中是不能用的。


17.2客户端挂载NFS
打开另一台虚拟机,我的俩台虚拟机IP地址分别为172.16.1.118和172.16.1.119
其中提供NFS服务的是172.16.1.118.在客户端挂载NFS之前,我们需要先查看服务端
共享了那些目录
[root@ns1 ~]# showmount -e 172.16.1.118
Export list for 172.16.1.118:
/home/nfstestdir 172.16.1.0/16
[root@ns1 ~]# 


然后在客户端上(172.16.1.119)上挂载NFS,
[root@ns2 ~]# mount -t nfs 172.16.1.118:/home/nfstestdir /mnt
[root@ns2 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              18G  2.1G   15G  13% /
tmpfs                 491M     0  491M   0% /dev/shm
/dev/sda1             488M   33M  430M   8% /boot
172.16.1.118:/home/nfstestdir
  18G  2.1G   15G  13% /mnt
[root@ns2 ~]# 


使用df -h 可以看到增加了一个/mnt分区,他就是NFS共享目录,进入/mnt目录下,并创建测试文件
[root@ns2 ~]# cd /mnt/
[root@ns2 mnt]# touch aminglinux.txt
touch: 无法创建"aminglinux.txt": 权限不够
这是因为在服务端上创建的/home/nfstestdir目录权限不合适,挂载后相当于被限制为uid为1000的用户
解决该问题需要在服务端上修改/home/nfstestdir目录权限
[root@ns1 ~]# chmod 777 /home/nfstestdir/
然后在到客户端上创建测试文件:
[root@ns2 mnt]# touch pylinux.txt
[root@ns2 mnt]# ls -l
总用量 0
-rw-r--r--. 1 nobody nobody 0 4月  24 17:10 pylinux.txt
[root@ns2 mnt]#


17.3 命令exportfs
命令:exportfs
选项:
-a:表示全部挂载或者卸载
-r:表示重新挂载
-u:表示卸载某一个目录
-v:表示显示共享的目录


当改变/etc/exports配置文件后,使用exports命令挂载不需要重启NFS服务,接下来首先修改
服务器的配置文件,如下所示
[root@ns1 ~]# vim /etc/exports  //添加一行
/tmp/172.16.1.0/16(rw,sync,no_root_squash)
然后在服务端上执行命令
[root@ns1 ~]# exportfs -arv
exportfs: No options for /tmp/172.16.1.0/16(rw,sync,no_root_squash) : suggest (sync) to avoid warning
exporting 172.16.1.0/16:/home/nfstestdir
exporting :/tmp/172.16.1.0/16(rw,sync,no_root_squash)
exportfs: Failed to stat /tmp/172.16.1.0/16(rw,sync,no_root_squash): No such file or directory 


然后在客户端上执行如下命令:
[root@ns2 ~]# mkdir /pylinux
[root@ns2 ~]# mount -t nfs -o nolock 172.16.1.118:/tmp/ /pylinux/
[root@ns2 ~]# 


你还可以要把挂载的NFS目录写入到客户端上的/etc/fstab文件中,挂载时只需要执行mount -a
在/etc/fstab文件增加一行,
oot@ns1 ~]# vim /etc/fstab 


172.16.1.118:/tmp/      /pylinux               nfs     defaults,nolock 0 0


在客户端上执行一下命令
[root@ns2 ~]# umount /pylinux/
umount: /pylinux/: not mounted
[root@ns2 ~]# mount -a
[root@ns2 ~]# cd /pylinux/
[root@ns2 pylinux]# touch 1.txt
[root@ns2 pylinux]# ls -l 1.txt 
-rw-r--r--. 1 root root 0 4月  24 17:40 1.txt
[root@ns2 pylinux]# 
可以看到1.txt的所有者和所有属组全部为root
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值