1. 程式人生 > >NFS筆記(二)NFS伺服器配置例項

NFS筆記(二)NFS伺服器配置例項

一、NFS伺服器配置例項 實驗拓撲 二、實驗要求及環境

2.1實驗環境

NFS伺服器 IP:192.168.8.5 環境: [[email protected] ~]# uname -a Linux server7.ctos.zu 3.10.0-514.el7.centos.plus.i686 #1 SMP Wed Jan 25 12:55:04 UTC 2017 i686 i686 i386 GNU/Linux [[email protected] ~]# cat /etc/redhat-release  CentOS release 7.3.1611 (AltArch) 客戶端IP:192.168.8.7 環境: [

[email protected] ~]# uname -a Linux client-A.ctos.zu 2.6.32-696.el6.i686 #1 SMP Tue Mar 21 18:53:30 UTC 2017 i686 i686 i386 GNU/Linux [[email protected] ~]# cat /etc/redhat-release  CentOS release 6.9 (Final)

2.2、實驗要求

NFS伺服器

1、/home/share 目錄可讀寫,並且不限制使用者身份,共享給192.168.8.0/24網段不的所有主機; 2、/home/zhidata 這個目錄僅共享給192.168.8.7這臺主機,以供該主機上面的zhi這個使用者來使用, 也就是說zhi在192.168.8.5和192.168.8.7上均有賬號,且賬號均為zhi; 3、/home/upload 這個目錄做為192.168.8.0/24網段的資料上傳目錄,其中/home/upload 的使用者和所屬組為nfs-upload這個名字,它的UID和GID均為222; 4、/home/nfs  這個目錄的屬性為只讀,可提供除了網段內的工作站外,向Internet也提供資料內容。 三、伺服器端設定  1、 編輯/etc/exports內容 [

[email protected] etc]# cat /etc/exports /home/share  192.168.8.0/24(rw,no_root_squash) /home/zhidata  192.168.8.7(rw) /home/upload   192.168.8.0/24(rw,all_squash,anonuid=222,anongid=222) /home/nfs     192.168.8.0/24(ro)  *(ro,all_squash) 2、按要求建立每個對應目錄     2.1、建立/home/share目錄 任何人都可以在 /home/share內新增、修改檔案,但     僅有該檔案/目錄的建立者與root能夠刪除自己的目錄或檔案。          [
[email protected]
etc]# mkdir -p  /home/share     [[email protected] etc]# ll -d /home/share/     drwxr-xr-x. 2 root root 6 9月   5 15:50 /home/share/     [[email protected] etc]# chmod 1777  /home/share/     [[email protected] etc]# ll -d /home/share/     drwxrwxrwt. 2 root root 6 9月   5 15:50 /home/share/          2.2、建立/home/nfs           [[email protected] etc]# ll -d /home/nfs/     drwxr-xr-x. 2 root root 6 9月   5 16:52 /home/nfs/          2.3、建立/home/zhidata目錄          [[email protected] etc]# mkdir -v /home/zhidata     mkdir: 已建立目錄 "/home/zhidata"     [[email protected] etc]# ll -d /home/zhidata/     drwxr-xr-x. 2 root root 6 9月   5 16:57 /home/zhidata/     [[email protected] etc]# useradd zhi     [email protected] etc]# passwd zhi     更改使用者 zhi 的密碼 。     新的 密碼:     無效的密碼: 密碼少於 8 個字元     重新輸入新的 密碼:     passwd:所有的身份驗證令牌已經成功更新。     [[email protected] etc]# cat /etc/passwd | grep zhi     zhi:x:1000:1000:zhi:/home/zhi:/bin/bash     [[email protected] etc]# chmod 700 /home/zhidata/     [[email protected] etc]# chown -R zhi:zhi /home/zhidata/     [[email protected] etc]# ll -d /home/zhidata/     drwx------. 2 zhi zhi 6 9月   5 16:57 /home/zhidata/          2.4、建立/home/upload目錄 #先建立對應賬號與組名及UID [[email protected] etc]# groupadd  -g 222 nfs-upload [[email protected] etc]# useradd -g 222 -u 222 -M nfs-upload [[email protected] etc]# cat /etc/passwd|grep nfs rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin nfs-upload:x:222:222::/home/nfs-upload:/bin/bash #建立目錄,修改屬主 [[email protected] etc]# mkdir /home/upload [[email protected] etc]# chown -R nfs-upload:nfs-upload  /home/upload/ [[email protected] etc]# ll -d /home/upload/ drwxr-xr-x. 2 nfs-upload nfs-upload 6 9月   6 08:38 /home/upload/ 2.5、重啟NFS服務 [[email protected] etc]# systemctl restart  nfs [[email protected] etc]# systemctl status  nfs ● nfs-server.service - NFS server and services    Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)    Active: active (exited) since 三 2017-09-06 08:41:40 CST; 8s ago   Process: 9171 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)   Process: 9169 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)   Process: 9168 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)   Process: 9181 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)   Process: 9180 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)  Main PID: 9181 (code=exited, status=0/SUCCESS)    CGroup: /system.slice/nfs-server.service 9月 06 08:41:40 server7.ctos.zu systemd[1]: Starting NFS server and services... 9月 06 08:41:40 server7.ctos.zu systemd[1]: Started NFS server and services. [[email protected] etc]# tail /var/log/messages Sep  6 08:41:40 server7 systemd: Started NFS Mount Daemon. Sep  6 08:41:40 server7 rpc.mountd[9178]: Version 1.3.0 starting Sep  6 08:41:40 server7 systemd: Started NFSv4 ID-name mapping service. Sep  6 08:41:40 server7 systemd: Starting NFS server and services... Sep  6 08:41:40 server7 kernel: NFSD: starting 90-second grace period (net c0c932c0) Sep  6 08:41:40 server7 systemd: Started NFS server and services. Sep  6 08:41:40 server7 systemd: Starting Notify NFS peers of a restart... Sep  6 08:41:40 server7 sm-notify[9198]: Version 1.3.0 starting Sep  6 08:41:40 server7 sm-notify[9198]: Already notifying clients; Exiting! Sep  6 08:41:40 server7 systemd: Started Notify NFS peers of a restart. 2.6、檢視NFS伺服器共享出來目錄 [[email protected] etc]# showmount -e localhost Export list for localhost: /home/nfs     (everyone) /home/upload  192.168.8.0/24 /home/share   192.168.8.0/24 /home/zhidata 192.168.8.7 2.7、關閉防火牆、selinux 2.8檢視配置 [[email protected] ~]# exportfs -v /home/zhidata     192.168.8.7(rw,wdelay,root_squash,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash) /home/share       192.168.8.0/24(rw,wdelay,no_root_squash,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash) /home/upload      192.168.8.0/24(rw,wdelay,root_squash,all_squash,no_subtree_check,anonuid=222,anongid=222,sec=sys,rw,secure,root_squash,all_squash) /home/nfs         192.168.8.0/24(ro,wdelay,root_squash,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash) /home/nfs         <world>(ro,wdelay,root_squash,all_squash,no_subtree_check,sec=sys,ro,secure,root_squash,all_squash) 四、客戶端配置 4.1、客戶端也要安裝nfs-utils和rcpbind軟體包 [[email protected] ~]# yum install nfs-utils rpcbind 4.2、啟動RPC [[email protected] ~]# service rpcbind start Starting rpcbind:                                          [  OK  ] [[email protected] ~]# service rpcbind status rpcbind (pid  1926) is running... [[email protected] ~]# chkconfig --list |grep rpcbind 4.3、客戶端關閉防火牆、selinux [[email protected] ~]# service iptables stop iptables: Setting chains to policy ACCEPT: filter          [  OK  ] iptables: Flushing firewall rules:                         [  OK  ] iptables: Unloading modules:                               [  OK  ] 4.4 測試網路 [[email protected] ~]# ping 192.168.8.5 PING 192.168.8.5 (192.168.8.5) 56(84) bytes of data. 64 bytes from 192.168.8.5: icmp_seq=1 ttl=64 time=2.16 ms 64 bytes from 192.168.8.5: icmp_seq=2 ttl=64 time=0.697 ms 64 bytes from 192.168.8.5: icmp_seq=3 ttl=64 time=0.346 ms 64 bytes from 192.168.8.5: icmp_seq=4 ttl=64 time=0.336 ms 64 bytes from 192.168.8.5: icmp_seq=5 ttl=64 time=0.335 ms 64 bytes from 192.168.8.5: icmp_seq=6 ttl=64 time=0.317 ms 64 bytes from 192.168.8.5: icmp_seq=7 ttl=64 time=0.512 ms 64 bytes from 192.168.8.5: icmp_seq=8 ttl=64 time=0.320 ms ^C --- 192.168.8.5 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 7555ms rtt min/avg/max/mdev = 0.317/0.629/2.169/0.595 ms 4.5 在客戶端檢視NFS伺服器共享出來目錄 [[email protected] ~]# showmount -e 192.168.8.5 Export list for 192.168.8.5: /home/nfs     (everyone) /home/upload  192.168.8.0/24 /home/share   192.168.8.0/24 /home/zhidata 192.168.8.7 4.6、掛載NFS伺服器上/home/nfs目錄到本地/opt/data目錄下 #掛載 [[email protected] opt]# mount -t nfs 192.168.8.5:/home/nfs  /opt/data/ #檢視資訊 [[email protected] data]# df -h Filesystem            Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root                       6.5G  644M  5.6G  11% / tmpfs                 250M     0  250M   0% /dev/shm /dev/sda1             477M   26M  426M   6% /boot 192.168.8.5:/home/nfs                       8.0G  1.6G  6.5G  20% /opt/data [[email protected] data]# mount /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 192.168.8.5:/home/nfs on /opt/data type nfs (rw,vers=4,addr=192.168.8.5,clientaddr=192.168.8.7) #建立檔案測試 [[email protected] data]# touch testabc  touch: cannot touch `testabc': Read-only file system 4.7掛載/home/upload 目錄到客戶端/opt/upload目錄下 #掛載 [[email protected] data]# mkdir /opt/upload [[email protected] data]# mount -t nfs  192.168.8.5:/home/upload   /opt/upload/ #檢視掛載是否成功 [[email protected] data]# df -h Filesystem            Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root                       6.5G  644M  5.6G  11% / tmpfs                 250M     0  250M   0% /dev/shm /dev/sda1             477M   26M  426M   6% /boot 192.168.8.5:/home/nfs                       8.0G  1.6G  6.5G  20% /opt/data 192.168.8.5:/home/upload                       8.0G  1.6G  6.5G  20% /opt/upload                        #客戶端建立nfs-upload使用者 [[email protected] ~]# groupadd  -g 222 nfs-upload [[email protected] ~]# useradd -g 222 -u 222 -m nfs-upload      #測試,可以看到建立的檔案及目錄預設屬主為nfs-upload屬組為nfs-upload [[email protected] upload]$ pwd /opt/upload [[email protected] upload]$ ll total 0 #測試建立檔案 [[email protected] upload]$ touch test123 測試建立目錄 [[email protected] upload]$ mkdir  testdir [[email protected] upload]$ ll -a total 4 drwxr-xr-x  3 nfs-upload nfs-upload   36 Sep  6 10:28 . drwxr-xr-x. 4 root       root       4096 Sep  6 18:22 .. -rw-rw-r--  1 nfs-upload nfs-upload    0 Sep  6 10:28 test123 drwxrwxr-x  2 nfs-upload nfs-upload    6 Sep  6 10:28 testdir # 在客戶端用root使用者建立檔案測試 [email protected] upload]# touch testabc [[email protected] upload]# ll total 0 -rw-r--r-- 1 root       root       0 Sep  6 10:31 nfstest123 -rw-r--r-- 1 nfs-upload nfs-upload 0 Sep  6 10:32 testabc drwxrwxr-x 2 nfs-upload nfs-upload 6 Sep  6 10:28 testdir 可以看到用root使用者建立的檔案屬主仍是nfs-upload #測試刪除檔案 [[email protected] upload]$ rm test123 [[email protected] upload]$ ll total 0 drwxrwxr-x 2 nfs-upload nfs-upload 6 Sep  6 10:28 testdir