NfS網路共享檔案系統環境搭建

Davis_itpub發表於2020-08-19

服務端:

停止iptables防火牆

[root@nfs-server ~]# service iptables stop

iptables: Flushing firewall rules: [  OK  ]

iptables: Setting chains to policy ACCEPT: filter [  OK  ]

iptables: Unloading modules: [  OK  ]


配置 iptables開機不啟動

[root@nfs-server ~]# chkconfig iptables off

[root@nfs-server ~]# chkconfig iptables --list

iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@nfs-server ~]# 


檢視nfs軟體包是否安裝(服務端、客戶端均操作)

[root@nfs-server ~]# 

[root@nfs-server ~]# rpm -qa |grep nfs

nfs-utils-1.2.3-39.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

nfs-utils-lib-1.1.5-6.el6.x86_64

[root@nfs-server ~]# 


磁碟分割槽:

[root@nfs-server ~]# fdisk -l


Disk /dev/sda: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00029ef0


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          64      512000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              64        1109     8388608   82  Linux swap / Solaris

/dev/sda3            1109        6528    43527168   83  Linux


Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000


[root@nfs-server ~]# fdisk /dev/sdb 

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x0f0318e3.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

         switch off the mode (command 'c') and change display units to

         sectors (command 'u').


Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-13054, default 1): 

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): 

Using default value 13054


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@nfs-server ~]# 



格式化檔案系統:

[root@nfs-server ~]# mkfs.ext4 /dev/sdb1

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

6553600 inodes, 26214055 blocks

1310702 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=4294967296

800 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks: 

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 

        4096000, 7962624, 11239424, 20480000, 23887872


Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 39 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@nfs-server ~]# 


建立網路共享目錄(服務端、客戶端均操作)

[root@nfs-server ~]# mkdir -p /sharedata

[root@nfs-server ~]# 


Mount檔案系統

[root@nfs-server ~]# mount /dev/sdb1 /sharedata/

[root@nfs-server ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        41G  5.2G   34G  14% /

tmpfs          1004M     0 1004M   0% /dev/shm

/dev/sda1       485M   39M  421M   9% /boot

/dev/sdb1        99G  188M   94G   1% /sharedata

[root@nfs-server ~]# 



設定開機自動掛接檔案系統

[root@nfs-server ~]# vi /etc/fstab 


#

# /etc/fstab

# Created by anaconda on Thu Nov 23 14:02:10 2017

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=bdec3845-4891-4ea9-984c-6eb31c67c2f9 /                       ext4    defaults        1 1

UUID=edf2e581-b41a-4c6a-8c79-df0852e93a11 /boot                   ext4    defaults        1 2

UUID=dc7767d6-5ac7-4746-bddb-0cf615e8cba5 swap                    swap    defaults        0 0

/dev/sdb1       /sharedata      ext4    defaults        0 0

tmpfs                   /dev/shm                tmpfs   defaults        0 0

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0

sysfs                   /sys                    sysfs   defaults        0 0

proc                    /proc                   proc    defaults        0 0

[root@nfs-server ~]# 


/sharedata為NFS伺服器共享的目錄,192.168.81.12為需要訪問NFS伺服器/sharedata共享目錄的客戶端伺服器的維護IP

[root@nfs-server ~]# vi /etc/exports 

/sharedata 192.168.81.12(rw,no_root_squash)


[root@nfs-server ~]# 

啟動nfs服務(服務端、客戶端均操作)

[root@nfs-server ~]# service nfs start

Starting NFS services:  [  OK  ]

Starting NFS quotas: [  OK  ]

Starting NFS mountd: [  OK  ]

Stopping RPC idmapd: [  OK  ]

Starting RPC idmapd: [  OK  ]

Starting NFS daemon: [  OK  ]



檢視nfs服務狀態(服務端、客戶端均操作)

[root@nfs-server ~]# service nfs status

rpc.svcgssd is stopped

rpc.mountd (pid 2364) is running...

nfsd (pid 2379 2378 2377 2376 2375 2374 2373 2372) is running...

rpc.rquotad (pid 2360) is running...

[root@nfs-server ~]# 


設定nfs開機自啟動(服務端、客戶端均操作)

[root@nfs-server ~]# 

[root@nfs-server ~]# chkconfig nfs on

[root@nfs-server ~]# 

[root@nfs-server ~]# chkconfig nfs --list

nfs             0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@nfs-server ~]# 



使nfs伺服器重新讀取exports檔案中的設定

[root@nfs-server ~]# 

[root@nfs-server ~]# exportfs -rv

exporting 192.168.81.12:/sharedata

[root@nfs-server ~]# 



檢視nfs共享情況(說明:192.168.81.11為NFS伺服器的維護IP)

[root@nfs-server ~]# 

[root@nfs-server ~]# showmount -e 192.168.81.11

Export list for 192.168.81.11:

/sharedata 192.168.81.12

[root@nfs-server ~]# 

[root@nfs-server ~]# 



在所有的伺服器上修改vim /etc/selinux/config,設定SELINUX=disabled (服務端、客戶端均操作)

[root@nfs-client ~]# vi /etc/selinux/config 


# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disabled

#SELINUX=enforcing

# SELINUXTYPE= can take one of these two values:

#     targeted - Targeted processes are protected,

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted 



[root@nfs-client ~]# 




NFS客戶端設定

在客戶端伺服器上檢視NFS伺服器nfs共享情況

[root@nfs-client ~]# 

[root@nfs-client ~]# showmount -e 192.168.81.11

Export list for 192.168.81.11:

/sharedata 192.168.81.12

[root@nfs-client ~]# 

[root@nfs-client ~]# 

[root@nfs-client ~]# showmount -e 192.168.81.12

Export list for 192.168.81.12:

[root@nfs-client ~]# 


修改/etc/sysconfig/autofs,將BROWSE_MODE改為yes ,其它保持預設即可

[root@nfs-client ~]# vi /etc/sysconfig/autofs

…………………………省略…………………………………………………………

#MOUNT_WAIT=-1

#

# UMOUNT_WAIT - time to wait for a response from umount(8).

#

#UMOUNT_WAIT=12

#

# BROWSE_MODE - maps are browsable by default.

#

BROWSE_MODE="yes"

#

# MOUNT_NFS_DEFAULT_PROTOCOL - specify the default protocol used by

#                              mount.nfs(8). Since we can't identify

#                              the default automatically we need to

#                              set it in our configuration.

#

…………………………省略…………………………………………………………



修改/etc/auto.master檔案;註釋掉:/misc  /etc/auto.misc;增加:/sharedata  /etc/auto.misc  --timeout=10

[root@nfs-client ~]# cat /etc/auto.master 

#

# Sample auto.master file

# This is an automounter map and it has the following format

# key [ -mount-options-separated-by-comma ] location

# For details of the format look at autofs(5).

#

#/misc  /etc/auto.misc

/sharedata  /etc/auto.misc  --timeout=10

#

# NOTE: mounts done from a hosts map will be mounted with the

#       "nosuid" and "nodev" options unless the "suid" and "dev"

#       options are explicitly given.

#

/net    -hosts

#

# Include central master map if it can be found using

# nsswitch sources.

#

# Note that if there are entries for /net or /misc (as

# above) in the included master map any keys that are the

# same will not be seen as the first read key seen takes

# precedence.

#

+auto.master

[root@nfs-client ~]# 



修改/etc/auto.misc

在客戶端上修改/etc/auto.misc檔案,註釋掉:cd  -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom,增加:sharedata    -rw,soft,intr   192.168.81.11:/sharedata,其中192.168.81.11為nfs服務端IP地址

[root@nfs-client ~]# cat /etc/auto.misc

#

# This is an automounter map and it has the following format

# key [ -mount-options-separated-by-comma ] location

# Details may be found in the autofs(5) manpage


#cd             -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

sharedata    -rw,soft,intr   192.168.81.11:/sharedata


# the following entries are samples to pique your imagination

#linux          -ro,soft,intr           ftp.example.org:/pub/linux

#boot           -fstype=ext2            :/dev/hda1

#floppy         -fstype=auto            :/dev/fd0

#floppy         -fstype=ext2            :/dev/fd0

#e2floppy       -fstype=ext2            :/dev/fd0

#jaz            -fstype=ext2            :/dev/sdc1

#removable      -fstype=ext2            :/dev/hdd

[root@nfs-client ~]# 




測試NFS客戶端連線NFS服務端

在客戶端伺服器上執行如下命令mount NFS服務端共享的檔案系統

[root@nfs-client ~] mount 192.168.81.11:/sharedata  /sharedata

[root@nfs-client ~]# 

[root@nfs-client ~]# df -h

Filesystem                Size  Used Avail Use% Mounted on

/dev/sda3                  45G  4.0G   39G  10% /

tmpfs                     927M   72K  927M   1% /dev/shm

/dev/sda1                 485M   39M  422M   9% /boot

192.168.81.11:/sharedata   99G  188M   94G   1% /sharedata

[root@nfs-client ~]# 

[root@nfs-client ~]# 


在客戶端伺服器上複製一個檔案到/sharedata目錄下,然後在NFS伺服器上,進入

/sharedata目錄,檢視複製的檔案是否在/sharedata 目錄下,如果存在,說明配置成功

[root@nfs-client ~]# cp -r /etc/hosts /sharedata/

[root@nfs-client ~]# cp -r /etc/hosts.allow /sharedata/

[root@nfs-client ~]# 

[root@nfs-client ~]# ls -lsa /sharedata/

total 32

 4 drwxr-xr-x.  3 root root  4096 Dec 14 10:32 .

 4 dr-xr-xr-x. 27 root root  4096 Dec 14 09:51 ..

 4 -rw-r--r--.  1 root root   158 Dec 14 09:56 hosts

 4 -rw-r--r--.  1 root root   370 Dec 14 10:33 hosts.allow

16 drwx------.  2 root root 16384 Dec 14 09:36 lost+found

[root@nfs-client ~]# 

[root@nfs-client ~]# 



服務端檢視:

[root@nfs-server sharedata]# ls -lsa /sharedata/

total 32

 4 drwxr-xr-x.  3 root root  4096 Dec 14 10:32 .

 4 dr-xr-xr-x. 29 root root  4096 Dec 14 09:37 ..

 4 -rw-r--r--.  1 root root   158 Dec 14 09:56 hosts

 4 -rw-r--r--.  1 root root   370 Dec 14 10:33 hosts.allow

16 drwx------.  2 root root 16384 Dec 14 09:36 lost+found

[root@nfs-server sharedata]# 

[root@nfs-server sharedata]# 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31520497/viewspace-2712774/,如需轉載,請註明出處,否則將追究法律責任。

相關文章