nfs的基本设定

一、安装并开启nfs服务

服务端:

[[email protected] ~]# yum install nfs-utils -y

[[email protected] ~]# systemctl start nfs

客户端:

[[email protected] ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
nfs的基本设定
原因:

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

   
[[email protected] ~]# firewall-cmd --permanent --add-service=nfs
success
[[email protected] ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[[email protected] ~]# firewall-cmd --permanent --add-service=mountd
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# systemctl restart firewalld.service
nfs的基本设定
nfs的基本设定

测试:

[[email protected] ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:    ##此时服务端还没有共享的目录
nfs的基本设定


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

一、安装并开启nfs服务

服务端:

[[email protected] ~]# yum install nfs-utils -y

[[email protected] ~]# systemctl start nfs

客户端:

[[email protected] ~]# showmount -e 172.25.71.1
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
nfs的基本设定
原因:

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

   
[[email protected] ~]# firewall-cmd --permanent --add-service=nfs
success
[[email protected] ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[[email protected] ~]# firewall-cmd --permanent --add-service=mountd
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# systemctl restart firewalld.service
nfs的基本设定
nfs的基本设定

测试:

[[email protected] ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:    ##此时服务端还没有共享的目录
nfs的基本设定


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

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

nfs的基本设定

测试:

[[email protected] ~]# showmount -e 172.25.71.1
Export list for 172.25.71.1:
/zhaoyan/nfs *               
nfs的基本设定
并进行挂载使用:

[[email protected] ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/
nfs的基本设定
三、实现客户端的自动挂载与自动卸载功能

此在客户端进行实现:

在客户端进行安装此软件

[[email protected] ~]# yum install autofs -y
开启此软件,会自动增加一个/net的目录,
[[email protected] ~]# systemctl start autofs
[[email protected] ~]# cd /net/
[[email protected] net]# ls                                     ##/net此目录虽不显示,但可以直接进去共享的服务器的ip
[[email protected] net]# cd 172.25.71.1
[[email protected] 172.25.71.1]# ls
zhaoyan
nfs的基本设定
关闭此目录时:/etc/sysconfig/autofs此文件会默认卸载时间时300秒,可以进行修改

[[email protected] ~]# systemctl stop autofs
[[email protected] ~]# cd /net/
-bash: cd: /net/: No such file or directory
nfs的基本设定
##开启此服务就可以进入挂载的目录中,关闭此服务实现卸载,实现自动卸载与挂载功能

四、修改挂载的目录

[[email protected] ~]# 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 #                                                 ##将写入的要挂载的服务端写入此文件,该文件没有,需要自己建立

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

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

[[email protected] nfs]# pwd
/zhaoyan/linux/nfs
nfs的基本设定

退出此目录:

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

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

服务端:

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

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

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

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

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

nfs的基本设定

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

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

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

nfs的基本设定

测试:

[[email protected] ~]# cd /zhaoyan/linux/nfs
[[email protected] nfs]# ls
file
[[email protected] nfs]# touch file1
[[email protected] 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
nfs的基本设定

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

[[email protected] nfs]# cat /etc/passwd| grep 1001
[[email protected] nfs]#

[[email protected] nfs]# useradd haha
[[email protected] nfs]# cat /etc/passwd| grep 1001
haha:x:1001:1001::/home/haha:/bin/bash
nfs的基本设定
此时在此进行测试:

[[email protected] nfs]# touch file2
[[email protected] 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

nfs的基本设定

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

[[email protected] ~]# cat /etc/exports
/zhaoyan/nfs 172.25.71.2(sync,rw,no_root_squash) *(sync,ro)

[[email protected] ~]# exportfs -rv
exporting 172.25.71.2:/zhaoyan/nfs
exporting *:/zhaoyan/nfs
nfs的基本设定
测试:

nfs的基本设定

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

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

[[email protected] ~]# mount 172.25.71.1:/zhaoyan/nfs /mnt/

mount.nfs: an incorrect mount option was specified
nfs的基本设定