Linux nfs 共享目录

/ 技术文章 / 0 条评论 / 467浏览

Linux互相共享目录

将 Smartcart 和 Smartcartpadserver 部署在不同机器时,需要保证两台server用的相同的系统(都是windows或者都是Linux), 否则将有一台机器获取不到图片等静态资源,因为文件路径只有一个配置项 (FileUpload.rootPath/image) 因此,我们需要将两台机器的/mnt/smartcart/image/指向同一目录。 比如,现在我们把Smartcartpadserver上的目录共享出来,挂载到Smartcart上

使用nfs共享目录

安装nfs相关组件

···

yum install nfs-utils nfs-utils-lib

···

创建nfs用户组及用户(不要老想着用root) 创建用户,组;把需要的用户也加到nfs组下面

···

groupadd -g 550 nfswritergroup
useradd -u 550 -g nfswritergroup nfswriter
usermode -a -G nfswritergroup wildfly
usermod -a -G nfswritergroup root
// 给nfswritergroup 读写执行 权限
setfacl -m g:nfswritergroup:rwx /mnt/smartcart

···

将所有匿名用户当做wildfly

···

id wildfly
uid=1002(wildfly) gid=1003(wildfly) groups=1003(wildfly),550(nfswritergroup)

vi /etc/exports
# Share /mnt/smartcart to all, including to root
# Share /mnt/smartcart to all the machines in the domain
# Regard all anonymous as wildfly by default

# for smartcart
/mnt/smartcart        0.0.0.0/0(rw,sync,all_squash,anonuid=1002,anongid=550)

:x

chown -R wildfly:nfswritergroup /mnt/smartcart
chmod -R 775 /mnt/smartcart

···

启动nfs

···

systemctl enable rpcbind
systemctl enable nfs-server

systemctl start rpcbind
systemctl start nfs-server

···

验证

···
showmount -e localhost
···

使用autofs挂载共享目录

创建待挂载目录

···
groupadd wildfly
useradd -g wildfly wildfly

mkdir -p /mnt/smartcart
···

安装配置autofs

autofs可以懒挂载,重启自动挂载

···
yum -y install nfs-utils autofs

vi /etc/auto.master
# data for smartcart
/mnt/smartcart /etc/auto.nfs

vi /etc/auto.nfs
image   -rw,rsize=32768,wsize=32768  10.3.17.43:/mnt/smartcart/image

# enable autofs
systemctl restart rpcbind
systemctl restart autofs
systemctl enable autofs
···

或者用mount命令手动挂载

···

sudo mount -t nfs 10.3.17.43:/mnt/smartcart /mnt/smartcart

···