linux中nfs、iscsi的共享方式、用autofs自动挂载的方法

#####################nfs专用于linux的传送
server:

[root@server ~]# yum install nfs-utils                   ###下载nfs
[root@server ~]# systemctl stop smb.service               ###关闭samba因为会冲突
[root@server ~]# systemctl disable smb.service
[root@server /]# systemctl start nfs-server              ##开启nfs
[root@server /]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'
[root@server /]# systemctl stop firewalld
[root@server /]# systemctl start firewalld
[root@server /]# firewall-cmd --permanent --add-service=nfs                    ##防火墙加nfs
success
[root@server /]# firewall-cmd --permanent --add-service=rpc-bind               ##防火墙加rpc-bind
success
[root@server /]# firewall-cmd --permanent --add-service=mountd                  ##防火墙加mountd
success
[root@server /]# firewall-cmd --reload                                          ##重新加载
success
[root@server /]# vim /etc/exports                                               ##共享什么文件
westos_nfs           *(sync)[root@server /]# exportfs -rv                                                 ##刷新
exporting *:/westos_nfs
[root@server /]# touch /westos_nfs/file{1..9}


client:

[root@desktop ~]# showmount -e 172.25.254.200					##发现共享文件
Export list for 172.25.254.200:
/westos_nfs *
[root@desktop ~]# mount 172.25.254.200:/westos_nfs /mnt[root@desktop mnt]# yum install autofs -y                                    ##下载自动挂载
[root@desktop mnt]# systemctl start autofs[root@desktop mnt]# cd /net
[root@desktop net]# ls
[root@desktop net]# cd 172.25.254.200
[root@desktop 172.25.254.200]# ls
westos_nfs
[root@desktop 172.25.254.200]# df
文件系统                      1K-块    已用    可用 已用% 挂载点
/dev/vda1                  10473900 3216724 7257176   31% /
devtmpfs                     469332       0  469332    0% /dev
tmpfs                        484920      80  484840    1% /dev/shm
tmpfs                        484920   12764  472156    3% /run
tmpfs                        484920       0  484920    0% /sys/fs/cgroup
/dev/mapper/vg0-vo           483670    2339  451840    1% /home
172.25.254.200:/westos_nfs 10473984 3224064 7249920   31% /mnt[root@desktop ~]# vim /etc/autofs.conf                           ##更改自动挂载的时间     configuration overrides this and sets the timeout to 5minutes to be consistent with earlier autofs releases.#
TIMEOUT= 3                                                            ##改为3秒
#NEGATIVE_TIMEOUT - set the default negative timeout forfailed mount attempts (default 60).#
#NEGATIVE_TIMEOUT=60
[root@desktop ~]# systemctl restart autofs.service                       ##重起服务

#####################在客户端 nfs.挂载路径的修改
client138:

[root@desktop ~]# vim /etc/sysconfig/autofs      ##这个是我们要修改的挂载时间路径[root@desktop ~]# vim /etc/auto.master           ##主配置文件
13 /net    -hosts
14 /mnt    /etc/auto.nfs                         ##挂载地点的上一级,副配置文件
[root@desktop ~]# vim /etc/auto.nfs              ##建立副配置文件
westos                   172.25.254.238:/westos_nfs   ##挂在地点的最后,和可以挂载的服务端文件   
[root@desktop /]# systemctl restart autofs.service    ##重起后
[root@desktop mnt]# cd westos
[root@desktop westos]# ls                           可直接从/mnt/westos中找到共享文件
file[1..3]


######在客户端对 读写和nfs版本号的修改。仍是通过客户端
client138:

[root@desktop westos_nfs]# vim /etc/auto.nfs       ##进入副配置文件,添加读写和版本参数
  westos    -ro,vers=3               172.25.254.238:/westos_nfs
[root@desktop westos]# systemctl restart autofs.service   ##重起
[root@desktop westos]# cd ..                            ##退出
[root@desktop ~]# cd /mnt/westos                          ##重新进入
[root@desktop westos]# mount                             ##挂载子资料改变
172.25.254.238:/westos_nfs on /mnt/westos type nfs (ro,relatime,vers=3,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.25.254.238,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.25.254.238)


################nfs,上面是怎么连接到服务端,那么怎么在客户端对服务端的共享文件操作呢?
########一共有三个开关。二个在服务端,一个在客户端
client:

[root@desktop westos]# vim /etc/auto.nfs                           ####客户端打开端口
  westos    -rw,vers=3               172.25.254.238:/westos_nfs    ##这里的ip是服务端的


server:

[root@server /]# chmod 777 /westos_nfs/                            ##文件的权限
[root@server /]# ls -ld /westos_nfs/
drwxrwxrwx. 2 root root 23 Aug 14 21:39 /westos_nfs/
[root@server /]# vim /etc/exports                                   ##发布共享时的给与共享权限
/westos_nfs     *(sync) 172.25.254.138(rw)
[root@server /]# exportfs -rv
exporting 172.25.254.138:/westos_nfs
exporting *:/nfs_westos

#######当文件可读可写之后我们想要用户创建文件时有一定的身份,客户端建立文件是黑户建立的,我们可以指定他的身份。主要是在服务端设定
server:

[root@server /]# vim /etc/exports                                                          ##进入配置文件
/westos_nfs     *(sync) 172.25.254.138(rw,anonuid=1001,anongid=1001,no_root_squash)      ##ip之后括号里面依次是:读写,uid,gid,客户有root身份建立文件
[root@server /]# exportfs -rv                   ##刷新
exporting 172.25.254.138:/westos_nfs
exporting *:/nfs_westos
exporting *:/westos_nfs
[root@server /]# 


client128: ###下面是依次实验的结果

[root@desktop westos]# touch file2
[root@desktop westos]# ls
file1  file[1..3]  file2
[root@desktop westos]# touch file3
[root@desktop westos]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 14 22:43 file1
-rw-r--r-- 1 root      root      0 Aug 14 21:39 file[1..3]
-rw-r--r-- 1      1001 nfsnobody 0 Aug 14 22:47 file2
-rw-r--r-- 1      1001 nfsnobody 0 Aug 14 22:49 file3
[root@desktop westos]# touch file4
[root@desktop westos]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 14 22:43 file1
-rw-r--r-- 1 root      root      0 Aug 14 21:39 file[1..3]
-rw-r--r-- 1      1001 nfsnobody 0 Aug 14 22:47 file2
-rw-r--r-- 1      1001 nfsnobody 0 Aug 14 22:49 file3
-rw-r--r-- 1 root      root      0 Aug 14  2019 file4

################################一种新的传送方式他可以给与网络磁盘的分享targetcli################
server:238

[root@server ~]# fdisk /dev/vdb                                     ###磁盘分区分一个区
[root@server ~]# yum install targetcli -y                          ###下载软件
[root@server ~]# targetcli
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> /backstores/block create westos:storage1 /dev/vdb1                ###建立物理分享存储单元
Created block storage object westos:storage1 using /dev/vdb1.
/> iscsi/ create iqn.2019-08.com.westos:storage1                      ##建立分享的单元空间
Created target iqn.2019-08.com.westos:storage1.                 
Created TPG 1.
/> ls
o- / ............................................................................... [...]
  o- backstores .................................................................... [...]
  | o- block ........................................................ [Storage Objects: 1]
  | | o- westos:storage1 ..................... [/dev/vdb1 (2.0GiB) write-thru deactivated]    ###
  | o- fileio ....................................................... [Storage Objects: 0]
  | o- pscsi ........................................................ [Storage Objects: 0]
  | o- ramdisk ...................................................... [Storage Objects: 0]
  o- iscsi .................................................................. [Targets: 1]
  | o- iqn.2019-08.com.westos:storage1 ......................................... [TPGs: 1]   ####
  |   o- tpg1 ..................................................... [no-gen-acls, no-auth]
  |     o- acls ................................................................ [ACLs: 0]
  |     o- luns ................................................................ [LUNs: 0]
  |     o- portals .......................................................... [Portals: 0]
  o- loopback ............................................................... [Targets: 0]
/> iscsi/iqn.2019-08.com.westos:storage1/tpg1/luns create /backstores/block/westos:storage1 ###建立存储单元与分享单元的联系
Created LUN 0.
/> iscsi/iqn.2019-08.com.westos:storage1/tpg1/acls create iqn.2019-08.com.westos:westoskey1 ###建立客户端联系的钥匙
Created Node ACL for iqn.2019-08.com.westos:westoskey1
Created mapped LUN 0.
/> iscsi/iqn.2019-08.com.westos:storage1/tpg1/portals create 172.25.254.238                ##将自己的端口用作分享端口
Using default IP port 3260
Created network portal 172.25.254.238:3260.
/> exit                                                                                         ##退出
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
[root@server ~]# firewall-cmd --permanent --add-port=3260/tcp                                   ###防火墙允许端口通过
success

客户端client138:

[root@desktop ~]vim /etc/iscsi/initiatorname.iscsi                                  ###配置文件的做法,复制服务端acl的钥匙到里面去
InitiatorName=iqn.2019-08.com.westos:westoskey1
[root@desktop ~]# iscsiadm -m discovery -t st -p 172.25.254.238                     ###查看谁在共享
172.25.254.238:3260,1 iqn.2019-08.com.westos:storage1
[root@desktop ~]# iscsiadm -m node -T iqn.2019-08.com.westos:storage1 -p 172.25.254.238 -l    ###将共享连接
Logging in to [iface: default, target: iqn.2019-08.com.westos:storage1, portal: 172.25.254.238,3260] (multiple)
Login to [iface: default, target: iqn.2019-08.com.westos:storage1, portal: 172.25.254.238,3260] successful.
[root@desktop ~]# fdisk -l                                                      ##查看共享磁盘
[root@desktop ~]# fdisk /dev/sda                                                 ##磁盘分区
[root@desktop ~]# mkfs.xfs /dev/sda1 -f                                         ##文件配置写入
[root@desktop ~]# mount /dev/sda1 /mnt                                             ##挂在上去
[root@desktop ~]# blkid                                                           ##系统可识别硬盘(不做自动挂载可以不看)
/dev/sda1: UUID="08ae07d4-4dd9-4b63-9e47-01cff590742e" TYPE="xfs" 
[root@desktop ~]# vim /etc/fstab                                                  ##设置自动挂载配置文件
[root@desktop ~]# mount -a
[root@desktop ~]# umount /mnt                                                      ###解除挂在
[root@desktop ~]# fdisk /dev/sda                                                    ###删除分区
[root@desktop ~]# iscsiadm -m node -T iqn.2019-08.com.westos:storage1 -u             ##与服务端断开
[root@desktop ~]# iscsiadm -m node -T iqn.2019-08.com.westos:storage1 -o delete       ###删除本机所存信息
###########注意:1、载客户端给共享硬盘分区保存后不要partprobe。2、在断开连接时一定要先把挂载去了
server238:
[root@server ~]# targetcli                                              ###载服务端清除targetcli
/> clearconfig confirm=true                                             ####用help可以看到这条命令可以删除
All configuration cleared


#########################本次实验完毕

上一篇:d-规则


下一篇:20145330第六周《Java学习笔记》