当前实例客户端与服务器配置在同一网段,服务器:192.168.1.1,客户端:192.168.1.2
1、NFS 服务端的安装与配置
1.1 安装服务
安装 NFS 服务的相关软件包(默认已安装)
执行指令 yum -y install nfs-utils rpcbind
1.2 创建文件及目录
创建共享目录及相应的测试文件→将共享目录(如:/usr/share/nfsfile)的所有者修改为 nfsnobody,以便 nfsnobody 用户能够对该共享目录有写权限
[root@localhost ~] mkdir /usr/share/nfsfile/
[root@localhost ~] echo "welcome 192.168.1.1 test.com" > /usr/share/nfsfile/readme.txt
[root@localhost ~] ls -l | grep nfsfile
[root@localhost ~] ls -l /usr/share | grep nfsfile
drwxr-xr-x. 2 root root 6 5月 10 13:43 nfsfile
[root@localhost ~] chown nfsnobody /usr/share/nfsfile/
[root@localhost ~] ls -l /usr/share | grep nfsfile
drwxr-xr-x. 2 nfsnobody root 24 5月 24 08:38 nfsfile
1.3 编辑配置文件
编辑、加载、查看NFS服务的配置文件
[root@localhost ~] echo "/usr/share/nfsfile 192.168.1.0/24(rw,sync,root_squash)" > /etc/exports
[root@localhost ~] exportfs -r // 重新加载配置文件,使新配置生效
[root@localhost ~] exportfs -v // 查看配置结果
/usr/share/nfsfile 192.168.1.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,root_squash,no_all_squash)
在exports文件中定义NFS系统的输出目录(即共享目录)、 访问权限和允许访问的主机等参数,格式如下:
- 配置行格式:被共享目录的绝对路径 客户端1(权限参数) 客户端2(权限参数) ……
格式说明:
① 同一共享目录的客户端(权限参数)可以有多个(以空格隔开);
② “客户端”的指定方式有以下几种:
客户端的IP地址:如,192.168.0.100。
客户端的IP网段:如,192.168.1.0/24或192.168.1.0/255.255.255.0。
可解析的主机名/完全合格域名:如,nfs.xyz.com、localhost等(指定的 主机名或域名必须在/etc/hosts文件或DNS服务器中能解析出IP地址)。
可解析的特定子域中的所有主机:如,* .xyz.com、server[1-30].xyz.com。
所有主机:*
③“权限参数”必须用英文的圆括号括起,且与前面的客户端”符号不能留空;圆括号内的“权限参数”可以有多个,前后两个用英文逗号隔开。
1.4 启动NFS服务
启动NFS服务并设置开机自动启动
[root@localhost ~] systemctl start nfs-server.service
[root@localhost ~] systemctl enable nfs-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
1.5 开放防火墙
开放防火墙的 nfs、rpc-bind、mountd 流量,允许外 部主机访问
[root@localhost ~] firewall-cmd --permanent --add-service=nfs
success
[root@localhost ~] firewall-cmd --permanent --add-service=rpc-bind
success
[root@localhost ~] firewall-cmd --permanent --add-service=mountd
success
[root@localhost ~] firewall-cmd --reload
success
2、使用 Linux 客户端访问
使用 Linux 客户端访问 NFS 服务器的共享文件
2.1 安装软件包
确保 Linux 系统的 NFS 客户端已安装 rpcbind
软件包,并启动了其服务(RHEL7中已默认安装并启动,客户端不需要启动 nfs 服务)。
2.2 查看共享目录
在 NFS 客户端使用 showmount 命令查看NFS服务器中导出的共享目录
命令格式:showmount [参数] [NFS服务器的地址]
常用参数有:
-e——查询/显示NFS服务器中可用的共享目录。
-a——查询/显示NFS服务器上的共享目录和所有连接客户端的信息。
-d——只显示被本机挂载的共享目录的信息。
-v——显示showmount命令程序的版本号。
[root@client ~] showmount -e 192.168.1.1
Export list for 192.168.1.1:
/usr/share/nfsfile 192.168.1.0/24
2.3 临时挂载共享目录
在 NFS 客户端创建挂载点目录→将服务器端的共享目录临时挂载到本地的挂载点目录→挂载成功后,访问共享目录内的文件(如查看挂载点目录内 readme 文件的内容)。
[root@client ~] mkdir /nfsfile
[root@client ~] mount -t nfs 192.168.1.1:/usr/share/nfsfile/ /nfsfile/
[root@client ~] cat /nfsfile/readme.txt
welcome 192.168.1.1 test.com
2.4 开机自动挂载共享资源
若客户端希望开机时自动将 NFS 服务器端的共享资源挂载到本地主机,则可以在客户端的 /etc/fstab 文件的末尾添加挂载信息→验证对共享资源地读、写访问。
[root@client ~] echo "192.168.1.1:/usr/share/nfsfile /nfsfile nfs defaults 0 0" > /etc/fstab // 编辑自动挂载文件
[root@client ~] umount /nfsfile // 从挂载点卸载共享目录
[root@client ~] mount -a // 使文件/etc/fstab中设置的所有挂载设备立即生效
[root@client ~] touch /nfsfile/test2 // 验证挂载到/nfsfile/下的共享资源是否可写
[root@client ~] ls -l /nfsfile/test2 // 验证挂载到/nfsfile/下的共享资源是否可读
-rw-r--r--. 1 nfsnobody nfsnobody 0 12月 17 20:50 /nfsfile/test
3、使用 Windows 客户端访问
使用 Windows 客户端访问 NFS 服务器中的共享文件
由于目前使用笔记本是 ”家庭版本“ 没有 NFS 服务暂就没写步骤 ,后期会补上来的 > _ <