手動搭建NFS服務器

手動搭建NFS服務器

主機 IP 名稱
nfs白色 192.168.100.100 服務端
nfs黑色 192.168.100.129 客戶端

首先需要把主機端和客戶端關閉防火牆
手動搭建nfs服務器
實驗環境
關閉防火牆關閉setlinux

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service 
[root@localhost ~]# setenforce 0

實驗一
開放/nfs/shared目錄,供所有用戶查閱資料
服務端配置
安裝nfs-utils,開啓,並加入到開機自啓

[root@localhost ~]#yum -y install nfs-utils
[root@localhost ~]# systemctl start nfs.service 
[root@localhost ~]# systemctl enable nfs.service

新建/nfs/shared目錄

[root@localhost ~]# mkdir /nfs/shared -p

編輯/etc/exports

[root@localhost ~]# cat /etc/exports
/nfs/shared *(ro,sync)

重新讀取exports文件重啓nfs服務

[root@localhost ~]# exportfs -ar
[root@nfs-server ~]# systemctl restart nfs.service 

客戶端
查看nfs服務器上的共享目錄

[root@localhost ~]#showmount -e 192.168.100.129
Export list for 192.168.100.129
/nfs/shared 192.168.100.129/24

將共享目錄掛載到mnt目錄下

[root@nfs-client ~]# mount -t nfs 192.168.100.129:/nfs/shared /mnt/    

查看

[root@nfs-client ~]# df -h
Filesystem                 1K-blocks     Used Available Use% Mounted on
/dev/sda2                    8897536  1605740   7291796  19% /
devtmpfs                      490012        0    490012   0% /dev
tmpfs                         499860        0    499860   0% /dev/shm
tmpfs                         499860     6856    493004   2% /run
tmpfs                         499860        0    499860   0% /sys/fs/cgroup
/dev/sda1                    1038336   121688    916648  12% /boot
tmpfs                          99972        0     99972   0% /run/user/0
192.168.100.129:/nfs/shared  19383296 12597504   6785792  65% /mnt

測試

[root@localhost ~]#]# mkdir aaa
mkdir: cannot create directory ‘aaa’: Read-only file system //沒有寫入權限
[root@localhost ~]# cat aaa  //只有讀取權限

實驗二
開放/nfs/upload目錄爲192.168.100.0網段的數據上傳目錄,
客戶端
新建/nfs/upload

[root@localhost ~]#mkdir /nfs/upload
新建用戶和用戶組uid和gid都是300
[root@localhost ~]# groupadd nfs-upload -g 300
[root@localhost ~]#useradd nfs-upload -u 300 -g 300
將upload目錄的屬主和屬組加入到nfs-upload
[root@localhost ~]# chown -R nfs-upload.nfs-upload /nfs/upload/
修改/etc/exports配置文件
[root@localhost ~]#cat /etc/exports
/nfs/shared *(ro,sync)
/nfs/upload 192.168.100.0/24(rw,sync,anonuid=300,anongid=300)

重新讀取配置文件

[root@localhost ~]#exportfs -ar

重啓nfs服務

[root@nfs-server ~]# systemctl restart nfs.service

客戶端
查看nfs服務器上的共享目錄

[root@localhost ~]# showmount -e 192.168.100.100
Export list for 192.168.100.100:
/nfs/shared *
/nfs/upload 192.168.100.0/24

將共享目錄掛載到nfs目錄上

[root@localhost ~]# mkdir /nfs
[root@localhost ~]#mount -t nfs 192.168.100.33:/nfs/upload /nfs

測試
檢查nfs屬組和屬主

[root@localhost ~]# ll /nfs/
total 0
drwxr-xr-x 2 300 300 16 Sep  5 12:33 haha

在客戶端的nfs目錄下新建lala目錄

[root@localhost ~]# mkdir /nfs/lala //有寫入權限
[root@localhost ~]# echo "123.com" >> haha/hh //在服務端的haha目錄寫入文件
[root@localhost ~]#cat /nfs/haha/hh  //有讀取權限

自動掛載
卸載nfs目錄掛載

[root@localhost ~]# umount /nfs/

在/etc/fstab寫入配置文件
然後重新加載配置文件

[root@localhost ~]# mount -a
查看
[root@localhost ~]# df
Filesystem                 1K-blocks     Used Available Use% Mounted on
/dev/sda2                    8897536  1605740   7291796  19% /
devtmpfs                      490012        0    490012   0% /dev
tmpfs                         499860        0    499860   0% /dev/shm
tmpfs                         499860     6828    493032   2% /run
tmpfs                         499860        0    499860   0% /sys/fs/cgroup
/dev/sda1                    1038336   121688    916648  12% /boot
tmpfs                          99972        0     99972   0% /run/user/0
192.168.100.100:/nfs/upload  1855904 6615552   119452 36% /nfs
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章