1. 目的
利用NFS服务,使得一台Linux机器可以通过网络访问另一台Linux机器上的目录。
2. 实验环境
两台Fedora 31 Linux机器,一台作为服务器(提供共享的目录),另一台作为客户端(用于访问服务器的共享目录)
服务器地址:192.168.1.125
客户端地址:192.168.1.170
3. 服务器配置
3.1 安装NFS工具集
dnf -y install nfs-utils
3.2 配置NFS服务器
打开/etc/idmapd.conf
配置文件,在第5行解除原文件中的注释,将Domain
配置项改为本机地址:/home 192.168.1.170(rw,no_root_squash)
[General]
#Verbosity = 0
# The following should be set to the local NFSv4 domain name
# The default is the host's DNS domain name.
Domain = 192.168.1.125
3.3 配置共享目录和共享的网段范围
打开/etc/exports
文件,添加形如下行的配置:
/home 192.168.1.170(rw,no_root_squash)
上述配置的意思是,将/home
作为共享目录,开放给地址为192.168.1.170
的主机(当然也可以配置成网段),允许这台主机进行读写,使用者如果以root身份登入,那么使用者对这个目录具有root权限(视情况可以配置成root_squash
)。
3.4 开启NFS服务
systemctl enable --now rpcbind nfs-server
3.5 设置防火墙规则
如果服务器开启了防火墙,那么需要新建一条规则,允许其他机器使用本机的NFS服务:
firewall-cmd --add-service=nfs --permanent
4. 客户端配置
4.1 安装NFS工具集
dnf -y install nfs-utils
4.2 配置NFS服务器
打开/etc/idmapd.conf
配置文件,在第5行解除原文件中的注释,将Domain
配置项改为本机地址:/home 192.168.1.170(rw,no_root_squash)
[General]
#Verbosity = 0
# The following should be set to the local NFSv4 domain name
# The default is the host's DNS domain name.
Domain = 192.168.1.125
4.3 运行目录远程挂载命令
# 新建本地挂载点目录
mkdir -p /mnt/remote_home
# 将服务器目录挂载到本地目录
mount -t nfs 192.168.1.125:/home /mnt/remote_home
可以通过df -hT
命令,查看挂载情况:
...
192.168.1.125:/home nfs4 1.8T 108G 1.6T 7% /mnt/remote_home
...
如果想要在系统启动时进行自动挂载,那么需要在/etc/fstab
文件里添加一行配置:
192.168.1.125:/home /mnt/remote_home nfs defaults 0 0