安装NFS

214 阅读1分钟

The Network File System (NFS) is a mechanism for storing files on a network. It is a distributed file system that allows users to access files and directories located on remote computers and treat those files and directories as if they were local. From www.ibm.com/docs/en/aix…

nfs.jpg

Server:

#!/bin/sh

echo "#######Step 1: Install nfs and rpcbind#######"
sudo yum install -y nfs-utils
sudo yum install -y rpcbind
sudo systemctl enable rpcbind
sudo systemctl enable nfs
sudo systemctl start rpcbind
sudo systemctl start nfs

echo "#######Step 2: Modify firewall#######"
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --reload

echo "#######Step 3: Create test directories and files#######"
sudo mkdir -p /data/public
sudo mkdir -p /data/protected
sudo bash -c 'echo "Protected" > /data/protected/protected.txt'


echo "#######Step 4: Modify File Policy#######"
if [ `grep '/data/public' /etc/exports | wc -l` == 0 ];then
  sudo bash -c  "cat >> /etc/exports" <<EOF
/data/public 10.0.2.1/24(rw,sync,no_root_squash,no_all_squash)
/data/protected 10.0.2.1/24(ro,sync,no_root_squash,no_all_squash)
EOF
sudo systemctl reload nfs 
fi

Client:

#!/bin/sh

echo "#######Step 1: Install nfs and rpcbind#######"
sudo yum install -y nfs-utils
sudo yum install -y rpcbind

echo "#######Step 2: Create a folder to be mounted#######"
sudo mkdir -p /home/martin/mnt/protected
sudo mkdir -p /home/martin/mnt/public

echo "#######Step 3: Mount nfs#######"
sudo mount 10.0.2.8:/data/protected /home/martin/mnt/protected
sudo mount 10.0.2.8:/data/public /home/martin/mnt/public

echo "#######Step 4: Test#######"
cat /home/martin/mnt/protected/protected.txt
sudo bash -c 'echo "Public" > /home/martin/mnt/public/public.txt'
sudo bash -c 'echo "Public" > /home/martin/mnt/protected/protected.txt'

Client Output:

#######Step 4: Test#######

Protected

bash: /home/martin/mnt/protected/protected.txt: Read-only file system