ITPub博客

首页 > Linux操作系统 > Linux操作系统 > NfS网络共享文件系统环境搭建

NfS网络共享文件系统环境搭建

原创 Linux操作系统 作者:Davis_itpub 时间:2020-08-19 09:38:17 0 删除 编辑

服务端:

停止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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-02-07

  • 博文量
    179
  • 访问量
    83256