-
网络文件系统:在互联网中共享服务器中文件资源。
-
使用nfs服务需要安装:nfs-utils 以及 rpcbind
-
nfs-utils : 提供nfs服务的程序
-
rpcbind :管理nfs所有进程端口号的程序
nfs的部署
1.客户端和服务端都安装nfs-utils和rpcbind
#安装nfs的软件rpcbind和nsf-utils
[root@server ~]# dnf install rpcbind -y[root@server ~]# dnf install nsf-utils -y#关闭网络和内核防护墙(get查看状态)
[root@server ~]# setenforce 0
[root@server ~]# getenforce
Permissive#开启服务
[root@server ~]# systemctl enable --now rpcbind
[root@server ~]# systemctl enable --now nfs-server.service(客户端无需此步骤)
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
#检查状态
[root@server ~]# systemctl status nfs-server.service
(安装方法2
[root@client ~]# dnf install nfs-utils -y安装依赖关系:rpcbind x86_64
)
2.服务器设置
#编辑共享目录配置文件(即/etc/exports)
[root@server ~]# mkdir /nfs_share (创建共享的目录,名字随便)
)
[root@server ~]# vim /etc/exports
...
/nfs_share 172.25.254.200(ro,sync)# ro表示read only只读 sync表示同步,就是编辑了什么内容立即同步给对方
...#更新状态(刷新服务端后客户端可以挂载共享目录)
[root@server ~]# exportfs -rv#在/nfs_share中创建一个文件
[root@server ~]# echo "hello nfs" > /nfs_share/hellonfs
3.客户端设置
# 查看服务端是否开启了共享
[root@client ~]# showmount -e 192.168.25.100
Export list for 192.168.25.100:
/nfs_share 192.168.25.200 # 这里表示服务端将自己的/nfs_share 共享给了192.168.25.200# 将服务端的共享目录挂在到本地即可使用共享目录
[root@client ~]# mount 192.168.25.100:/nfs_share /mnt
(将/nfs_share 挂载到/mnt)
#检查(查看共享目录中是否存在服务端共享的文件)
[root@client ~]# ls /mnt
hellonfs
[root@client ~]# cat /mnt/hellonfs
hello nfs
[root@client ~]#
自动挂载客户端
安装 autofs即可自动挂载nfs服务器共享出来的目录:
-
当我们进入到/net/xxx.xxx.xxx.xxx目录中,autofs会自动将该ip中的共享目录挂载到/net/xxx.xxx.xxx.xxx路径下
-
当我们超过300秒不去对共享目录做任何操作后,会自动卸载。
# 安装autofs
[root@Client /]# dnf install autofs -y# 启动autofs
[root@Client /]# systemctl start autofs.service# 进入/net/192.168.25.100
[root@Client /]# cd net
[root@Client net]# ls
# 进入该ip对应的目录时,自动将该ip的共享目录挂载到/net/172.25.254.100/nfs_share
[root@Client net]# cd 172.25.254.100
[root@Client 172.25.254.100]# ls
nfs_share
[root@Client nfs_share]# pwd (用于显示当前工作目录的绝对路径)
/net/172.25.254.100/nfs_share# 修改自动卸载的超时时间
[root@Client ~]# vim /etc/autofs.conf
....
# minutes to be consistent with earlier autofs releases.
#
timeout = 300 该时间就是超时长,可以修改为用户需要的超时长
#
# master_wait - set the default maximum number of retries (actual
....
指定自动挂载的目录
1.修改auto.matser配置文件,确定主目录的挂载位置,及其相关的子目录配置文件
[root@Client nfs_share]# vim /etc/auto.master11 # options are explicitly given.12 #13 /net -hosts14 /nfs /etc/autofs.nfs_share #/nfs 主目录挂载位置 /etc/autofs.nfs_share 子目录的配置文件15 #16 # Include /etc/auto.master.d/*.autofs
2.编辑子目录的配置文件 /etc/autofs.nfs_share
[root@Client nfs_share]# vim /etc/autofs.nfs_share
...nfs_share -rw 172.25.254.100:/nfs_share#nfs_share表示要挂载在:/nfs/nfs_share
#-rw:表示可读可写
#172.25.254.100:/nfs_share 表示文件服务器上共享目录位置信息
...
3.重启自动挂载服务
[root@Client nfs_share]# systemctl restart autofs.service
# 读取远程文件服务器上的内容
[root@Client nfs_share]# ls /nfs
nfs_share
[root@Client nfs_share]# cat /nfs/nfs_share/hellonfs.txt
hellonfs