環境:CentOS 7.6 (注意:不同linux版本下安裝和啓動方法有差異,假如環境不能聯網就用源碼或rpm方法去安裝吧)
1、安裝
yum install nfs-utils nfs-utils-lib -y
2、修改配置
vim /etc/exports
/pubic 192.168.101.0/24(rw,sync) #允許該網段用戶讀寫,用絕對路徑,sync文件同時寫入硬盤和內存
/mnt *(ro,sync) #允許所有用戶訪問,只讀
(後續修改使配置生效 exportfs -r)
3、重啓服務(按順序)
systemctl restart rpcbind
systemctl restart nfs-server
systemctl restart nfs-lock
systemctl restart nfs-idmap
4、關閉SELinux、關閉或配置防火牆
setenforce 0 (臨時關閉SELinux)-> getenforce (確認) -> vim /etc/selinux/config (修改配置文件永久關閉,SELINUX=disabled)
關閉防火牆
systemctl stop firewalld #關閉
systemctl disable firewalld #禁開機啓動
systemctl status firewalld #檢查狀態是否inactive
配置防火牆
firewall-cmd --permanent --add-port=111/tcp #放開rpc的tcp端口
firewall-cmd --permanent --add-port=2049/tcp #放開nfs的tcp端口
firewall-cmd --permanent --zone=public --add-service=nfs #放開nfs服務
firewall-cmd --reload #重啓
5、測試
showmount -e 127.0.0.1 -> 返回沒報錯
mount -t nfs 127.0.0.1:/mnt /tmp/www/ #掛載nfs目錄到指定目錄,該目錄要提前建好
[root@localhost mnt]# mount -t nfs 192.168.101.228:/mnt /mnt/
mount: wrong fs type, bad option, bad superblock on 192.168.101.228:/mnt,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program)
In some cases useful info is found in syslog - try
dmesg | tail or so.
[root@localhost tmp]# yum install nfs-utils -y #在本地可以mount,同網絡的別機器mount不了就裝下nfs工具就好
......
[root@localhost mnt]# mount -t nfs 192.168.101.228:/mnt /mnt/
[root@localhost tmp]# mount | grep mnt
192.168.101.128:/mnt on /tmp/www type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.101.130,local_lock=none,addr=192.168.101.128)
[root@localhost tmp]# ls www/
test.txt
[root@localhost tmp]#
mount | grep mnt #看是否掛載成功。或者新建個文件在/mnt,然後去/tmp/www下看是否有這文件
umount /tmp/www #解除掛載