主机 | IP |
服务端 | 192.168.201.131 |
客户端 | 192.168.201.132 |
1.服务端配置
1.1 安装NFS和rpc
[root@hx1 ~]# yum install -y nfs-utils
[root@hx1 ~]# yum install -y rpcbind
1.2 启动服务和设置开启启动
[root@hx1 ~]# systemctl start rpcbind
[root@hx1 ~]# systemctl enable rpcbind
[root@hx1 ~]# systemctl start nfs-server
[root@hx1 ~]# systemctl enable nfs-server
1.3 配置防火墙放行nfs服务(防火墙关闭情况下忽略此步骤)
[root@hx1 ~]# firewall-cmd --permanent --add-service=nfs
[root@hx1 ~]# firewall-cmd --permanent --add-service=rpc-bind
[root@hx1 ~]# firewall-cmd --permanent --add-service=mountd
[root@hx1 ~]# firewall-cmd --reload
# 也可以放行端口
[root@hx1 ~]# firewall-cmd --permanent --add-port=111/tcp
[root@hx1 ~]# firewall-cmd --permanent --add-port=111/udp
[root@hx1 ~]# firewall-cmd --permanent --add-port=2049/tcp
[root@hx1 ~]# firewall-cmd --permanent --add-port=20048/tcp
[root@hx1 ~]# firewall-cmd --reload
# 如果使用的是 iptables,可以使用以下命令
[root@hx1 ~]# iptables -A INPUT -p tcp --dport 111 -j ACCEPT
[root@hx1 ~]# iptables -A INPUT -p udp --dport 111 -j ACCEPT
[root@hx1 ~]# iptables -A INPUT -p tcp --dport 2049 -j ACCEPT
[root@hx1 ~]# iptables -A INPUT -p tcp --dport 20048 -j ACCEPT
[root@hx1 ~]# service iptables save
1.4 配置共享文件目录,编辑配置文件
# 创建共享目录,然后在/etc/exports配置文件中编辑配置即可
[root@hx1 ~]# mkdir /test1
[root@hx1 ~]# vi /etc/exports
/test1 192.168.201.0/24(rw,no_root_squash)
[root@hx1 ~]# systemctl reload nfs
参数 | 作用 |
---|
ro | 只读 |
rw | 读写 |
root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 |
no_root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 |
all_squash | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 |
sync | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
no_all_squash | 保留共享文件的UID和GID(默认) |
async | 优先将数据保存到内存,然后再写入硬盘;效率更高,但可能会丢失数据 |
2.客户端配置
2.1 安装NFS
[root@hx1 ~]# yum -y install nfs-utils
2.2 查看nfs服务器共享信息
[root@hx1 ~]# showmount -e 192.168.201.132
Export list for 192.168.201.132:
/test1 192.168.201.0/24
2.3 挂载文件系统(持久化挂载)
[root@hx1 ~]# vi /etc/fstab
192.168.201.132:/test1 /test1 nfs noatime,nodiratime,defaults,_netdev 0 0
[root@hx1 ~]# mount -a
[root@hx1 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 68K 1.9G 1% /dev/shm
tmpfs tmpfs 1.9G 12M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 2.2G 48G 5% /
/dev/mapper/centos-home xfs 46G 33M 46G 1% /home
/dev/sda1 xfs 1014M 151M 864M 15% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 iso9660 4.4G 4.4G 0 100% /mnt
192.168.201.132:/test1 nfs4 1014M 32M 982M 4% /test1