安裝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