安装nfs及相关依赖:sudo yum install nfs-utils* -y
根据需要编辑配置文件:sudo gedit /etc/exports,如果共享目录是/opt/nfs,且希望挂载后对其可读可写,则在里面添加一行:/opt/nfs *(sync,rw),*表示所有IP,如果指定要IP访问,则可以直接替换为指定IP;若希望/opt/nfs只能读不能写则将rw改为ro即可,详细参数,查看手册。
开启nfs相关服务:sudo systemctl enable nfs-server.service
nfs-server检查是否开启:systemctl is-enabled nfs-server.service,会显示enabled
其它机器远程加载时只需:mount x.x.x.x:/opt/nfs /mnt,x.x.x.x是nfs服务的ip地址。
----------------------------------------------------------------------------------------------------------------------
编辑nfs配置文件:
vim /etc/exports
激活exports,可直接用exportfs刷新或者重启服务:
1. exportfs刷新:
# exportfs -avf
exporting 192.168.221.0/24:/path/to/your/directory
2. 重启服务:
# systemctl restart nfs-server.service
查看本机共享出的nfs文件夹:
# showmount -e
Export list for tp:
/path/to/your/directory
换台机器挂载nfs文件夹:
# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
188G 2.7G 176G 2% /
/dev/sda1 99M 23M 71M 25% /boot
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sr0 3.6G 3.6G 0 100% /media
192.168.221.1:/path/to/your/directory 932G 806G 127G 87% /mnt
注意问题:
1. 检查配置没有问题后,仍无法发现nfs共享,可以先临时关闭防火墙(systemctl stop iptables.service)和SELINUX(setenforce 0),再试试看。
2. 客户机上能发现nfs共享,并且可以成功mount,但是没有权限列出mount后的目录,需要加no_root_squash到nfs exports选项里,这样客户机能以root权限来查看nfs共享文件夹。
man exports
root_squash
Map requests from uid/gid 0 to the anonymous uid/gid. Note that
this does not apply to any other uids or gids that might be
equally sensitive, such as user bin or group staff.
no_root_squash
Turn off root squashing. This option is mainly useful for disk‐
less clients.
---------------------------------------------------------------------------------------------------------------------------
Configure NFS Server to share any directories on your Network.
| |
[1] | It's the Configuration on the system you want to build NFS server. |
[root@master ~]#
yum -y install nfs-utils
[root@master ~]#
vi /etc/idmapd.conf
# line 5: uncomment and change to your domain name
Domain =
server.world
[root@master ~]#
vi /etc/exports
# write like below *note
/home 10.0.0.0/24(rw,sync,no_root_squash,no_all_squash)
# *note
/home
⇒ shared directory
10.0.0.0/24
⇒ range of networks NFS permits accesses
rw
⇒ writable
sync
⇒ synchronize
no_root_squash
⇒ enable root privilege
no_all_squash
⇒ enable users' authority
systemctl start rpcbind.service
[root@master ~]#
systemctl start nfs-server.service
[root@master ~]#
systemctl start nfs-lock.service
[root@master ~]#
systemctl start nfs-idmap.service
[root@master ~]#
systemctl enable rpcbind.service
[root@master ~]#
systemctl enable nfs-server.service
[root@master ~]#
systemctl enable nfs-lock.service
[root@master ~]#
systemctl enable nfs-idmap.service
|
[2] | Configuration on NFS clients |
Domain =
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
[root@www ~]#
Filesystem Size Used Avail Use% Mounted on rootfs 16G 832M 14G 6% / devtmpfs 995M 0 995M 0% /dev tmpfs 1003M 0 1003M 0% /dev/shm tmpfs 1003M 39M 964M 4% /run /dev/mapper/VolGroup-lv_root 16G 832M 14G 6% / tmpfs 1003M 39M 964M 4% /run tmpfs 1003M 0 1003M 0% /sys/fs/cgroup tmpfs 1003M 0 1003M 0% /media /dev/vda2 485M 32M 428M 7% /boot master.server.world:/home/ 16G 848M 14G 6% /home
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=65ec32e2-f459-4d63-b8b0-e18124b50f3a /boot ext4 defaults 1 2 /dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
master.server.world:/home /home nfs defaults 0 0