linux中使用nfs共享文件

版权声明:本文为博主原创文章,转载请附上链接。 https://blog.csdn.net/woswod/article/details/80355327

NFS需要使用远程过程调用 (RPC),也就是说,我们并不是只要启动NFS, 还需要启动RPC这个服务

服务器端

CentOS 7.4
ip:172.16.0.1
共享/tmp目录
共享/data目录给172.16.0.2

安装nfs

# yum install rpcbind
# yum install nfs-utils
# service rpcbind start
# service nfs start

设置rpcbind和nfs服务开机启动

# chkconfig nfs on
# chkconfig rpcbind on
也可以设置rpcbind和nfs服务只在系统运行级别35自动启动。
# chkconfig --level 35 rpcbind on
# chkconfig --level 35 nfs on

配置共享文件夹
在/etc/exportfs添加:
/data *(rw,sync,no_root_squash) #共享/data目录
/data 172.16.0.2(rw) #对指定ip共享共享/data目录

参数 作用
ro 只读模式
rw 读写模式
root_squash 当NFS客户端使用root用户访问时,映射为NFS服务端的匿名用户
no_root_squash 当NFS客户端使用root用户访问时,映射为NFS服务端的root用户
sync 同时讲数据写入到内存与硬盘中,保证不丢失数据
async 优先将数据保存到内存,然后再写入硬盘,效率更高,但可能造成数据丢失

修改后不用重启nfs,执行以下命令生效:
# exportfs -a

添加读写权限
chmod -R 666 /data

防火墙中开放端口
RedHat在7中更改了系统软件,不再使用iptables作为系统的防火墙,而是使用了FirewallD,但是为了兼容过去的命令也可以使用iptables来设置防护规则。
需要开放rpc(111端口),nfs(2049端口),nfs挂载端口(892端口),其中111和892是tcp,udp都用。

# iptables -I INPUT -p tcp -m multiport --dports 111,892,2049 -j ACCEPT
# iptables -I INPUT -p udp -m multiport --dports 111,892 -j ACCEPT

上述配置重启后失效,要永久有效,需要修改/etc/sysconfig/iptables-config配置文件,详细请baidu
重启nfs

# service rpcbind restart
# service nfs restart
# showmount -e    //查看自己共享的目录

客户端

CentOS 7.4
ip:172.16.0.2

挂载172.16.0.1:/data到本地/data目录
# mount -t nfs 172.16.0.1:/data /data
开机自动挂载
在/etc/fstab中添加
172.16.0.1:/data /data nfs defaults 0 0
添加读写权限
chmod -R 666 /data
如果还是出现其他权限问题,比如Permission denied等,可以修改文件夹的所有者为nfsnobody
chown nfsnobody.nfsnobody -R /data

猜你喜欢

转载自blog.csdn.net/woswod/article/details/80355327