nfs的基本设定

来源:互联网 发布:2016版excel数据有效性 编辑:程序博客网 时间:2024/06/12 05:06

一、安装并开启nfs服务

服务端:

[root@server ~]# yum install nfs-utils -y

[root@server ~]# systemctl start nfs

客户端:

[root@client ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

原因:

在服务端没有加入火墙策略

   
[root@server ~]# firewall-cmd --permanent --add-service=nfs
success
[root@server ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@server ~]# firewall-cmd --permanent --add-service=mountd
success
[root@server ~]# firewall-cmd --reload
success
[root@server ~]# systemctl restart firewalld.service

测试:

[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:    ##此时服务端还没有共享的目录


做出共享目录,或者把服务端的某一目录共享给外界使用

一、安装并开启nfs服务

服务端:

[root@server ~]# yum install nfs-utils -y

[root@server ~]# systemctl start nfs

客户端:

[root@client ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

原因:

在服务端没有加入火墙策略

   
[root@server ~]# firewall-cmd --permanent --add-service=nfs
success
[root@server ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@server ~]# firewall-cmd --permanent --add-service=mountd
success
[root@server ~]# firewall-cmd --reload
success
[root@server ~]# systemctl restart firewalld.service

测试:

[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:    ##此时服务端还没有共享的目录


二、做出共享目录,或者把服务端的某一目录共享给外界使用

[root@server ~]# mkdir /zhaoyan/nfs -p
[root@server ~]# cat /etc/exports            ##写入共享的某一目录
/zhaoyan/nfs *(sync)                                ##表示将次目录进行共享,并且对所有人开放
[root@server ~]# exportfs -rv                  ##在此服务中,不能重启此服务,否则会进行客户端访问的卡顿,
exporting *:/zhaoyan/nfs                          ##用此来进行策略的更新

测试:

[root@client ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:
/zhaoyan/nfs *               

并进行挂载使用:

[root@client ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/

三、实现客户端的自动挂载与自动卸载功能

此在客户端进行实现:

在客户端进行安装此软件

[root@client ~]# yum install autofs -y
开启此软件,会自动增加一个/net的目录,
[root@client ~]# systemctl start autofs
[root@client ~]# cd /net/
[root@client net]# ls                                     ##/net此目录虽不显示,但可以直接进去共享的服务器的ip
[root@client net]# cd 172.25.71.1
[root@client 172.25.71.1]# ls
zhaoyan

关闭此目录时:/etc/sysconfig/autofs此文件会默认卸载时间时300秒,可以进行修改

[root@client ~]# systemctl stop autofs
[root@client ~]# cd /net/
-bash: cd: /net/: No such file or directory

##开启此服务就可以进入挂载的目录中,关闭此服务实现卸载,实现自动卸载与挂载功能

四、修改挂载的目录

[root@client ~]# vim /etc/auto.master
  5 # For details of the format look at autofs(5).
  6 #
  7 /misc   /etc/auto.misc
  8 /zhaoyan/linux /etc/auto.nfs       ##此为添加的策略,/zhaoyan/linux写入的是,挂载点的上层目录
  9 #                                                 ##将写入的要挂载的服务端写入此文件,该文件没有,需要自己建立

[root@client nfs]# cat /etc/auto.nfs
nfs -rw,noatime 172.25.71.1:/zhaoyan/nfs           ##要挂载的点为/zhaoyan/linux/nfs

测试:(在此服务开启的情况下)

[root@client nfs]# pwd
/zhaoyan/linux/nfs


退出此目录:


##此实验中的/zhaoyan/linux/nfs是客户端自己打开此服务建立的,与/net/目录是一样的,如果自己建立/zhaoyan/linux/nfs目录的话,会与系统自己建立的目录进行冲突。

五、客户端自行进行建立与删除文件

服务端:

因为客户端将此共享目录挂载在自己的目录上,若自行建立文件,实际是在服务端的该共享目录上建立文件的,所以需要服务端分配所要的权限

[root@server ~]# chmod 777 /zhaoyan/nfs/                       ##给共享的此目录一个777的权限
[root@server ~]# ls -ld /zhaoyan/nfs/
drwxrwxrwx. 2 root root 6 Dec 10 06:53 /zhaoyan/nfs/

[root@server ~]# cat /etc/exports                                      ##在此目录中写入给一个读写的权力

/zhaoyan/nfs *(sync,rw)
测试:

172.25.71.1:/zhaoyan/nfs  10473984 3149568   7324416  31% /zhaoyan/linux/nfs        ##已经成功挂载
[root@client nfs]# touch file                          ##建立文件没有报错
[root@client nfs]# ls
file


六、对建立的人给相应的用户

[root@client nfs]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file           ##默认将建立的用户压缩成为nfsnobody
服务端:

[root@server ~]# useradd zhaoyan                       ##建立一个用户
[root@server ~]# id zhaoyan
uid=1001(zhaoyan) gid=1001(zhaoyan) groups=1001(zhaoyan)
[root@server ~]# vim /etc/exports
[root@server ~]# exportfs -rv
exporting *:/zhaoyan/nfs
[root@server ~]# cat /etc/exports
/zhaoyan/nfs *(sync,rw,anonuid=1001,anongid=1001) ##让客户端建立文件的人使用的身份是1001


测试:

[root@client ~]# cd /zhaoyan/linux/nfs
[root@client nfs]# ls
file
[root@client nfs]# touch file1
[root@client nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file
-rw-r--r-- 1      1001      1001 0 Dec 10 07:32 file1

在客户端显示使用的是1001的身份,表示此客户端1001的uid并没有人使用

[root@client nfs]# cat /etc/passwd| grep 1001
[root@client nfs]#

[root@client nfs]# useradd haha
[root@client nfs]# cat /etc/passwd| grep 1001
haha:x:1001:1001::/home/haha:/bin/bash

此时在此进行测试:

[root@client nfs]# touch file2
[root@client nfs]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 10 07:23 file
-rw-r--r-- 1 haha      haha      0 Dec 10 07:32 file1                        ##发现建立文件的用用户均成为了haha用户
-rw-r--r-- 1 haha      haha      0 Dec 10 07:36 file2


让172.25.71.2这台主机在使用root用户登陆时,不进行身份转换

[root@server ~]# cat /etc/exports
/zhaoyan/nfs 172.25.71.2(sync,rw,no_root_squash) *(sync,ro)

[root@server ~]# exportfs -rv
exporting 172.25.71.2:/zhaoyan/nfs
exporting *:/zhaoyan/nfs

测试:

no_root_squash      ##相当于该客户端的root用户对该共享目录有极大的权限,极度不安全的一个参数

###在nfs时,经常在挂载时就出现这样的问题,此问题出现的原因是服务端的系统版本与所下载的软件的版本是不一致的,这样就会出现挂载不上的问题

[root@client ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/

mount.nfs: an incorrect mount option was specified

原创粉丝点击