NFS–基础–02–实践
1、常用命令
1.1、nfs
# 启动
systemctl start nfs
# 停止
systemctl stop nfs
# 重启
systemctl restart nfs
# 状态
systemctl status nfs
# 开机自启动
systemctl enable nfs
1.2、rpcbind
# 启动
systemctl start rpcbind.service
# 停止
systemctl stop rpcbind.service
# 重启
systemctl restart rpcbind.service
# 状态
systemctl status rpcbind.service
# 开机自启动
systemctl enable rpcbind.service
1.3、httpd
# 启动
systemctl start httpd
# 停止
systemctl stop httpd
# 重启
systemctl restart httpd
# 状态
systemctl status httpd
# 开机自启动
systemctl enable httpd
2、服务端
2.1、安装软件包
# 查看是否安装了 nfs-utils和rpcbind 软件包
rpm -q rpcbind nfs-utils
# 没有则用yum安装
yum install -y rpcbind nfs-utils
2.2、设置共享目录
2.2.1、创建共享目录
# 创建目录
mkdir /nfs/share -p
# 给与权限
chmod 777 /nfs/share
2.2.2、编辑配置文件
# vim /etc/exports
/nfs/share 192.168.187.0/24(rw,sync,no_root_squash)
说明
- /nfs/share 允许192.168.187.0/24的客户端访问。
- rw:表示允许读写,ro表示为只读;
- sync:表示同步写入到内存与硬盘中;
- no_root_squash:表示当客户机以root身份访问时赋予本地root权限
2.3、启动NFS服务程序
# 启动
systemctl start nfs
systemctl start rpcbind.service
# 开机自启动
systemctl enable rpcbind.service
# 开机自启动
systemctl enable nfs
2.3.1、查看rpcbind是否开启
rpcbind服务默认使用111端口
netstat -anpt | grep rpcbind
2.3.2、查看本机发布的NFS共享目录
showmount -e
3、客户端
3.1、安装软件包
# 查看是否安装rpcbind,nfs-utils
rpm -q rpcbind nfs-utils
# 未安装可以使用yum进行安装
yum install -y rpcbind nfs-utils
3.2、启动客户端
# 开启 rpcbind服务
systemctl start rpcbind
# 设置开机自启
systemctl enable rpcbind
3.3、查看服务器端共享的目录,然后进行挂载
3.3.1、查看共享目录
# 服务端IP地址为 192.168.187.181
showmount -e 192.168.187.181
3.3.2、手动挂载并查看是否成功
# 创建目录
mkdir /nfs-client-share
# 手动挂载
mount 192.168.187.181:/nfs/share /nfs-client-share
# 查看是否成功
df -Th
3.3.3、设置自动挂载
# vim /etc/fstab
192.168.187.181:/nfs/share /nfs-client-share nfs defaults,_netdev 0 0
4、测试–验证共享存储功能
在服务端创建文件,在客户端查看
5、测试–网页共享功能
5.1、服务端操作
5.1.1、安装httpd,并设置前端页面内容
# 安装httpd
yum install httpd -y
# 启动
systemctl start httpd
# 设置前端页面内容
echo "NFS 服务端 192.168.187.154" > /var/www/html/index.html
5.1.2、编辑nfs配置文件
# vim /etc/exports
# 共享目录
/var/www/html 192.168.187.0/24(ro)
5.1.3、宣告并查看共享目录
# 宣告共享目录
exportfs -rv
# 查看共享目录
showmount -e
5.1.4、重启服务
# 注意执行顺序,先rpcbind,后nfs
systemctl restart rpcbind.service
systemctl restart nfs
5.2、客户端操作
5.2.1、安装httpd,并设置前端页面内容
# 安装httpd
yum install httpd -y
# 启动
systemctl start httpd
5.2.2、重启服务
# 注意执行顺序,先rpcbind,后nfs
systemctl restart rpcbind.service
systemctl restart nfs
5.2.2、查看服务器端共享的目录,然后进行挂载
# 服务端IP地址为 192.168.187.181
showmount -e 192.168.187.181
# 手动挂载
mount 192.168.187.181:/var/www/html /var/www/html
# 查看是否成功
df -Th