#!/bin/bash
what’s nfs ?
网络文件系统(英语:Network File System,缩写作 NFS)是一种分布式文件系统,力求客户端主机可以访问服务器端文件,并且其过程与访问本地存储时一样,它由Sun微系统(已被甲骨文公司收购)开发,于1984年发布[1]。
它基于开放网络运算远程过程调用(ONC RPC)系统:一个开放、标准的RFC系统,任何人或组织都可以依据标准实现它。
Why is it used ?
为了共享文件,因为挂载一个文件系统是可能的,NFS客户端能够像访问本地文件一样访问Nfs 服务器端文件。
How to Setup NFS ?
Step 1 install nfs server
1.1 Installing nfs-utils
sudo su -
yum install nfs-utils
1.2 Choose the directory to share. If not present create one.| 选择共享的目录
mkdir /var/nfs_share_dir
1.3 Add permissions and ownwership privilages to the shared directory. | 添加权限
chmod -R 755 /var/nfs_share_dir
chown nfsnobody:nfsnobody /var/nfs_share_dir
1.4 Start the nfs services. | 启动服务
systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap
1.5 Open the exports file and add these lines. | 配置 exports 文件,添加以下内容
Configuring the exports file for sharing.
vi /etc/exports
# Fill in the the file-shared path and clients details in /etc/exports.
# 192.168.48.101 - Client's IP | 192.168.48.101 (客户端Ip)
/var/nfs_share_dir 192.168.48.101(rw,sync,no_root_squash)
1.6 .Restart the service | 重启服务
systemctl restart nfs-server
1.6 .Only for Centos 7,NFS service override | 配置防火墙
firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --permanent --zone=public --add-service=mountd
firewall-cmd --permanent --zone=public --add-service=rpc-bind
firewall-cmd --reload
Step 2 install nfs client
2.1.Installing nfs-utils
sudo su -
yum install nfs-utils
2.2.Create a mount point | 创建挂载位置
mkdir -p /mnt/nfs/var/nfs_share_dir
2.3 Mounting the filesystem | 挂载文件系统
mount -t nfs 192.168.48.100:/var/nfs_share_dir /mnt/nfs/var/nfs_share_dir
# -t type of filesystem
# 192.168.48.100 server's IP | 192.168.48.100 (服务器端Ip)
2.4.Verify if mounted | 检查是否挂载
$ df -kh
# Filesystem Size Used Avail Use% Mounted on
# /dev/mapper/centos-root 39G 1.1G 38G 3% /
# devtmpfs 488M 0 488M 0% /dev
# tmpfs 494M 0 494M 0% /dev/shm
# tmpfs 494M 6.7M 487M 2% /run
# tmpfs 494M 0 494M 0% /sys/fs/cgroup
# /dev/mapper/centos-home 19G 33M 19G 1% /home
# /dev/sda1 497M 126M 372M 26% /boot
# 192.168.48.100:/var/nfs_share_dir 39G 980M 38G 3% /mnt/nfs/var/nfs_share_dir
2.5 Mounting permanently. | 永久挂载
# Now if the client is rebooted, we need to remount again. So, to mount permanently,we need to configure /etc/fstab file.
# Append this to /etc/fstab 将以下内容添加到文件 /etc/fstab 中去
192.168.48.100:/var/nfs_share_dir /mnt/nfs/var/nfs_share_dir nfs defaults 0 0
2.6 To verify, create a file in the Client-side, and open in server-side.
# Client-side
echo "Client Hello" >> /mnt/nfs/var/nfs_share_dir/testing.txt
# Server-side(192.168.48.100)
$ cat /var/nfs_share_dir/testing.txt
Client Hello
How to use nfs on windows
problems
Hurray! Now client is able to access the files of server.