网络共享存储服务NFS
作者:Eric 微信:loveoracle11g
环境准备
服务器系统 | 角色 | IP |
RHEL 7.5 x86-64 | NFS服务端 | 192.168.10.201 |
RHEL 7.5 x86-64 | NFS客户端 | 192.168.10.202 |
基础环境配置 修改nfs服务端主机名 [root@zhouwanchun ~]# hostname zhouwanchun [root@zhouwanchun ~]# hostname nfs-server [root@zhouwanchun ~]# bash [root@nfs-server ~]# 修改nfs客户端主机名 [root@zhouwanchun ~]# hostname zhouwanchun [root@zhouwanchun ~]# hostname nfs-client [root@zhouwanchun ~]# bash [root@nfs-client ~]# 关闭filrewalld防火墙 [root@nfs-server ~]# systemctl disable firewalld.service [root@nfs-server ~]# systemctl stop firewalld.service [root@nfs-client ~]# systemctl disable firewalld.service [root@nfs-client ~]# systemctl stop firewalld.service 关闭selinux [root@nfs-server ~]# vim /etc/sysconfig/selinux # 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 # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@nfs-server ~]# setenforce 0 [root@nfs-server ~]# getenforce Permissive [root@nfs-client ~]# vim /etc/sysconfig/selinux # 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 # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@nfs-client ~]# setenforce 0 [root@nfs-client ~]# getenforce Permissive 安装nfs-server 安装nfs服务主程序 [root@nfs-server ~]# mount /dev/cdrom /mnt mount: /dev/sr0 is write-protected, mounting read-only [root@nfs-server ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot /dev/sr0 iso9660 3.5G 3.5G 0 100% /mnt [root@nfs-server ~]# cd /etc/yum.repos.d/ [root@nfs-server yum.repos.d]# ls [root@nfs-server yum.repos.d]# vim local.repo [local] name=localyum baseurl=file:///mnt enabled=1 gpgcheck=0 [root@nfs-server yum.repos.d]# yum clean all [root@nfs-server yum.repos.d]# yum makecache [root@nfs-server yum.repos.d]# yum repolist all [root@nfs-server ~]# yum -y install nfs-utils rpcbind [root@nfs-server ~]# rpm -qa nfs-utils nfs-utils-1.3.0-0.el7.x86_64 [root@nfs-server ~]# rpm -qa rpcbind rpcbind-0.2.0-23.el7.x86_64 启动rpcbind服务,并查看rpc注册信息 [root@nfs-server ~]# systemctl start rpcbind [root@nfs-server ~]# systemctl enable rpcbind [root@nfs-server ~]# rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper 100000 3 udp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 34752 status 100024 1 tcp 43718 status 100005 1 udp 20048 mountd 100005 1 tcp 20048 mountd 100005 2 udp 20048 mountd 100005 2 tcp 20048 mountd 100005 3 udp 20048 mountd 100005 3 tcp 20048 mountd 100003 3 tcp 2049 nfs 100003 4 tcp 2049 nfs 100227 3 tcp 2049 nfs_acl 100003 3 udp 2049 nfs 100003 4 udp 2049 nfs 100227 3 udp 2049 nfs_acl 100021 1 udp 47777 nlockmgr 100021 3 udp 47777 nlockmgr 100021 4 udp 47777 nlockmgr 100021 1 tcp 58593 nlockmgr 100021 3 tcp 58593 nlockmgr 100021 4 tcp 58593 nlockmgr 100011 1 udp 875 rquotad 100011 2 udp 875 rquotad 100011 1 tcp 875 rquotad 100011 2 tcp 875 rquotad 启动nfs-server [root@nfs-server ~]# systemctl enable nfs-server.service [root@nfs-server ~]# systemctl start nfs-server.service [root@nfs-server ~]# ps -elf | egrep "rpc|nfs" 1 S root 490 2 0 60 -20 - 0 rescue 19:55 ? 00:00:00 [rpciod] 5 S rpc 2535 1 0 80 0 - 9975 poll_s 20:25 ? 00:00:00 /sbin/rpcbind -w 5 S rpcuser 2541 1 0 80 0 - 11639 poll_s 20:25 ? 00:00:00 /sbin/rpc.statd 1 S root 2781 1 0 80 0 - 11202 poll_s 20:28 ? 00:00:00 /usr/sbin/rpc.mountd 1 S root 2788 2 0 60 -20 - 0 rescue 20:28 ? 00:00:00 [nfsd4] 1 S root 2789 2 0 60 -20 - 0 rescue 20:28 ? 00:00:00 [nfsd4_callbacks] 1 S root 2793 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2794 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2795 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2796 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2797 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2798 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2799 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2800 2 0 80 0 - 0 svc_ge 20:28 ? 00:00:00 [nfsd] 1 S root 2804 1 0 80 0 - 5337 ep_pol 20:28 ? 00:00:00 /usr/sbin/rpc.idmapd 1 S root 2805 1 0 80 0 - 28571 poll_s 20:28 ? 00:00:00 /usr/sbin/rpc.rquotad 0 R root 2874 1983 0 80 0 - 27010 - 20:33 pts/0 00:00:00 egrep --color=auto rpc|nfs [root@nfs-server ~]# ps -elf | egrep "rpcbind" 5 S rpc 2535 1 0 80 0 - 9975 poll_s 20:25 ? 00:00:00 /sbin/rpcbind -w 0 R root 2903 1983 0 80 0 - 27010 - 20:35 pts/0 00:00:00 egrep --color=auto rpcbind nfs服务端配置共享目录 创建需要共享的目录 关机init 0 添加一块硬盘1T [root@nfs-server ~]# init 0
[root@zhouwanchun ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000991e3 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 9414655 4194304 82 Linux swap / Solaris /dev/sda3 9414656 30386175 10485760 83 Linux Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@zhouwanchun ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x84a9402f. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-2147483647, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2147483647, default 2147483647): Using default value 2147483647 Partition 1 of type Linux and of size 1024 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@zhouwanchun ~]# partprobe /dev/sdb [root@zhouwanchun ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=256 agcount=4, agsize=67108800 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=268435200, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=131071, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@zhouwanchun ~]# mkdir /nfsdisk [root@zhouwanchun ~]# ls -ld /nfsdisk drwxr-xr-x 2 root root 6 Jul 21 20:44 /nfsdisk [root@zhouwanchun ~]# blkid /dev/sda1: UUID="7b89c031-a44e-4439-b211-04d152e026f6" TYPE="xfs" /dev/sda2: UUID="c1e88ade-3953-44c9-a00f-0c8487e53279" TYPE="swap" /dev/sda3: UUID="7e4bd525-73c9-46d9-8233-c6e79d30f5e4" TYPE="xfs" /dev/sdb1: UUID="67ec42ec-efef-4b50-b9ba-0e12062d1ed8" TYPE="xfs" [root@zhouwanchun ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Tue May 15 18:02:26 2018 # # 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=7e4bd525-73c9-46d9-8233-c6e79d30f5e4 / xfs defaults 1 1 UUID=7b89c031-a44e-4439-b211-04d152e026f6 /boot xfs defaults 1 2 UUID=c1e88ade-3953-44c9-a00f-0c8487e53279 swap swap defaults 0 0 UUID=67ec42ec-efef-4b50-b9ba-0e12062d1ed8 /nfsdisk xfs defaults 0 0 [root@zhouwanchun ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot [root@zhouwanchun ~]# mount -a [root@zhouwanchun ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot /dev/sdb1 xfs 1.0T 33M 1.0T 1% /nfsdisk [root@nfs-server ~]# cd /nfsdisk/ [root@nfs-server nfsdisk]# ls [root@nfs-server nfsdisk]# vim test.txt look :wq 编辑nfs主配置文件,共享/nfsdisk目录至192.168.10.0网段,可读可写,统一用户。 [root@zhouwanchun ~]# vim /etc/exports /nfsdisk 192.168.10.0/24(rw,sync,all_squash) [root@zhouwanchun ~]# systemctl restart nfs-server.service [root@zhouwanchun ~]# cat /var/lib/nfs/etab /nfsdisk 192.168.10.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash) [root@zhouwanchun ~]# grep "65534" /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@zhouwanchun ~]# ls -ld /nfsdisk drwxr-xr-x 2 root root 6 Jul 21 20:44 /nfsdisk [root@zhouwanchun ~]# chown -R nfsnobody:nfsnobody /nfsdisk [root@zhouwanchun ~]# ls -ld /nfsdisk drwxr-xr-x 2 nfsnobody nfsnobody 6 Jul 21 20:44 /nfsdisk NFS服务端配置好了,最后重启一下服务 [root@zhouwanchun ~]# systemctl restart nfs-server.service [root@zhouwanchun ~]# showmount -e 192.168.10.201 Export list for 192.168.10.201: /nfsdisk 192.168.10.0/24 NFS客户端安装挂载相关指令 安装相关服务,仅启动rpcbind服务即可 [root@nfs-client ~]# yum -y install nfs-utils rpcbind [root@nfs-client ~]# rpm -qa | grep nfs-utils nfs-utils-1.3.0-0.el7.x86_64 [root@nfs-client ~]# rpm -qa | grep rpcbind rpcbind-0.2.0-23.el7.x86_64 [root@nfs-client ~]# systemctl enable rpcbind [root@nfs-client ~]# systemctl start rpcbind NFS客户端查看远程服务器提供的可挂载信息 [root@nfs-client ~]# which showmount /usr/sbin/showmount [root@nfs-client ~]# rpm -qf /usr/sbin/showmount nfs-utils-1.3.0-0.el7.x86_64 这下知道周老师为啥要安装这个包了吧 [root@nfs-client ~]# showmount -e 192.168.10.201 Export list for 192.168.10.201: /nfsdisk 192.168.10.0/24 [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 客户端挂载nfs存储 使用默认挂载方式,将远程nfs共享的/nfsdisk目录挂载到本地的/nfsdiskclient目录 [root@nfs-client ~]# mkdir /nfsdiskclient [root@nfs-client ~]# ls -ld /nfsdiskclient drwxr-xr-x. 2 root root 6 Jul 21 21:14 /nfsdiskclient [root@nfs-client ~]# mount -t nfs 192.168.10.201:/nfsdisk /nfsdiskclient [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 192.168.10.201:/nfsdisk nfs4 1.0T 33M 1.0T 1% /nfsdiskclient [root@nfs-client ~]# ls -ld /nfsdiskclient drwxr-xr-x. 2 nfsnobody nfsnobody 21 Jul 21 21:20 /nfsdiskclient [root@nfs-client ~]# cd /nfsdiskclient/ [root@nfs-client nfsdiskclient]# touch test01.txt [root@nfs-client nfsdiskclient]# ll total 4 -rw-r--r--. 1 nfsnobody nfsnobody 0 Jul 21 21:25 test01.txt -rw-r--r--. 1 root root 5 Jul 21 21:20 test.txt [root@nfs-client ~]# cat /nfsdiskclient/test.txt look [root@nfs-server ~]# ls -l /nfsdisk/ total 4 -rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 21 21:25 test01.txt -rw-r--r-- 1 root root 5 Jul 21 21:20 test.txt 开机挂载 [root@nfs-client ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Tue May 15 18:02:51 2018 # # 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=5c8d5949-9b42-4e90-b1df-4324565e4114 / xfs defaults 1 1 UUID=d1075ca8-9c29-4b2b-9181-74914dc42a44 /boot xfs defaults 1 2 UUID=1f00ce4a-a6a6-41ab-a99b-90d8e7a3c300 swap swap defaults 0 0 192.168.10.201:/nfsdisk /nfsdiskclient nfs defaults 0 0 [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 192.168.10.201:/nfsdisk nfs4 1.0T 33M 1.0T 1% /nfsdiskclient [root@nfs-client ~]# umount /nfsdiskclient [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot [root@nfs-client ~]# mount -a [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 2.9G 7.2G 29% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 192.168.10.201:/nfsdisk nfs4 1.0T 33M 1.0T 1% /nfsdiskclient NFS自动挂载 直接就是 直接挂载好 间接就是 你使用才会自动挂载 mount /dev/cdrom /mnt 58 df -Th 59 cd /etc/yum.repos.d/ 60 ls 61 vim local.repo 62 yum clean all 63 yum makecache 64 yum repolist all 客户端安装autofs自动挂载 [root@nfs-client ~]# yum -y install autofs [root@nfs-client ~]# systemctl enable autofs [root@nfs-client ~]# systemctl start autofs [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 192.168.10.201:/nfsdisk nfs4 1.0T 33M 1.0T 1% /nfsdiskclient [root@nfs-client ~]# umount /nfsdiskclient [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot [root@nfs-client ~]# rpm -qc autofs /etc/auto.master /etc/auto.misc /etc/auto.net /etc/auto.smb /etc/autofs_ldap_auth.conf /etc/sysconfig/autofs /usr/lib/systemd/system/autofs.service [root@nfs-client ~]# vim /etc/auto.master.d/nfs.autofs /- /etc/auto.nfs_direct [root@nfs-client ~]# vim /etc/auto.nfs_direct /nfsdiskclient -rw,sync,soft 192.168.10.201:/nfsdisk [root@nfs-client ~]# systemctl restart autofs.service [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot [root@nfs-client ~]# mount /etc/auto.nfs_direct on /nfsdiskclient type autofs (rw,relatime,fd=18,pgrp=1027,timeout=300,minproto=5,maxproto=5,direct) [root@nfs-client ~]# ls -l /nfsdiskclient/ total 4 -rw-r--r-- 1 nfsnobody nfsnobody 0 Jul 21 21:25 test01.txt -rw-r--r-- 1 root root 5 Jul 21 21:20 test.txt [root@nfs-client ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 xfs 10G 3.0G 7.1G 30% / devtmpfs devtmpfs 990M 0 990M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 8.8M 989M 1% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 116M 382M 24% /boot 192.168.10.201:/nfsdisk nfs4 1.0T 33M 1.0T 1% /nfsdiskclient