Linux入门教程:CentOS 配置部署 NFS,运维部署 安装 CentOS


对于服务器的文件共享,可以部署一台文件共享服务器,通过 NFS 去挂载使用。

安装

CentOS 6.x

下载安装

yum -y install rpcbind nfs

配置开机启动

chkconfig rpcbind on
chkconfig nfs on

Centos 7.x

下载安装

yum -y install rpcbind nfs-utils

配置开机启动

systemctl enable nfs-server.service
systemctl enable rpcbind.service

服务端配置

配置

在服务端,编辑配置文件 /etc/exports,添加如下行

/home/uploads 192.168.221.101(rw,all_squash,anonuid=0,anongid=0)

配置说明:

192.168.221.101 客户端 ip
rw 读写 ro 只读
all_squash 无论客户端以何用户连接服务端,对服务端而言均为匿名用户
anonuid 匿名用户的 uid
anongid 匿名用户的 gid

查看共享目录的 uid 和 gid,使用命令:

ls -nd /home/nfs

uid 和 gid 最好为客户端和服务端都存在的 uid 和 gid 值。

可用的配置说明列表:

exportfs -r # 生效

若在服务启动后修改了 /etc/exports 配置文件,执行该命令后,无需重启 nfs 服务和 rpcbind 服务。对该命令的说明如下:

-a 全部挂载或卸载 /etc/exports中的内容

-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab

-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)

-v 在export的时候,将详细的信息输出到屏幕上。

防火墙端口配置

NFS 除 111 和 2049 外,其它端口均为随机生成,若 NFS 服务端需要启用防火墙,需要固定其它端口。

参考文章:http://linux.vbird.org/linux_server/0330nfs.php#nfsserver_security

# 先添加默认端口,随后添加额外端口
firewall-cmd  --permanent    --add-port=111/tcp
firewall-cmd  --permanent    --add-port=111/udp
firewall-cmd  --permanent    --add-port=2049/tcp
firewall-cmd  --permanent    --add-port=2049/udp
...

重启防火墙,重启 rpcbind:

rpcinfo -p

启动

查看本机共享目录:

showmount -e

CentOS 6.x

service rpcbind start
service nfs start

CentOS 7.x

systemctl start rpcbind.service
systemctl start nfs-server.service

客户端配置

查看服务端共享目录:

showmount -e server_ip

配置自动挂载:

vim /etc/fstab

添加一行

192.168.221.100:/home/nfs/ /home/test/uploads/ nfs defaults 0 0

在客户端,只需要启动 rpcbind 服务,但仍需要安装 nfs

# CentOS 6.x
chkconfig rpcbind on
service rpcbind start

# CentOS 7.x
systemctl enable rpcbind
systemctl start rpcbind

挂载测试

mount -a

查看挂载

nfsstat -m

错误解决

执行 mount 报错

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

关闭防火墙,或者执行如下:

rpcinfo  -p  192.168.221.100

将列出端口都添加至防火墙规则,放行。

相关内容