Linux下的nfs文件系统

###1、nfs文件系统
######(1)只读共享
#######server

[root@client mnt]# yum install nfs-utils -y
[root@client mnt]# systemctl start nfs
[root@server ~]# vim /etc/exports
1 /mnt *(sync,ro) #sync允许数据同步,ro只读
[root@server ~]# exportfs -rv
exporting *:/mnt

#######client

[root@client mnt]# yum install nfs-utils -y
[root@client mnt]# systemctl start nfs
[root@client mnt]# showmount -e 172.25.254.168 #查看共享
Export list for 172.25.254.168:
/mnt *
[root@client mnt]# mount 172.25.254.168:/mnt /mnt #挂载
[root@client ~]# cd /mnt
[root@client mnt]# rm -fr test1
rm: cannot remove ‘test1’: Read-only file system #只读系统

######(2)读写共享
#######server

[root@server ~]# vim /etc/exports
1 /mnt *(sync,rw) #读写
[root@server ~]# exportfs -rv
exporting *:/mnt

#######client

[root@client mnt]# mount 172.25.254.168:/mnt /mnt #挂载
[root@client ~]# rm -fr /mnt/test1
rm: cannot remove ‘/mnt/test1’: Permission denied #拒绝

#######server

[root@server ~]# chmod 777 /mnt/

#######client

[root@client ~]# rm -fr /mnt/test1 
[root@client ~]# touch /mnt/test1 #可写

######(3)不转换用户身份的共享
#######server

[root@server ~]# vim /etc/exports
1 /mnt *(sync,rw,no_root_squash) #表示客户端使用root挂载时,不转换用户身份
[root@server ~]# exportfs -rv

#######client
建立文件,所属用户和组是root
这里写图片描述
######(4)指定用户身份的共享
#######server

[root@server ~]# vim /etc/exports
1 /mnt *(sync,rw,anonuid=1000,anongid=1000) #表示客户端使用student用户身份
[root@server ~]# exportfs -rv

#######client
建立文件,所属用户和组是student
这里写图片描述
######(5)不同主机的多种共享
#######server

[root@server ~]# vim /etc/exports
1 /westos 172.25.254.0/24(sync,ro) 172.25.254.68(sync,rw) #172.25.254.0网段只读,172.25.254.68主机读写
[root@server ~]# exportfs -rv

68主机测试,可写:
这里写图片描述
其他主机测试:
这里写图片描述

###2、autofs
自动挂/卸载文件操作系统
#######server

[root@server ~]# systemctl start nfs
[root@server ~]# systemctl stop firewalld
[root@server ~]# vim /etc/exports
/westos 172.25.254.0/24(sync,rw,no_root_squash) #允许该网段共享/westos目录
[root@server ~]# exportfs -rv

#######client

[root@client ~]# yum install autofs -y
[root@client ~]# systemctl start autofs
[root@client ~]# cd /net #开启autofs会自动出现/net目录
[root@client net]# ls
[root@client net]# cd 172.25.254.168 #切换共享ip
[root@client 172.25.254.168]# ls #可以看到共享目录
westos
[root@client 172.25.254.168]# cd westos/
[root@client westos]# ls
file1 file2 test1 test2 test3
[root@client westos]# df #查看挂载,自动挂载/westos
172.25.254.168:/westos 10473984 3157760 7316224 31% /net/172.25.254.168/westos
[root@client westos]# cd
[root@client ~]# vim /etc/sysconfig/autofs #编辑配置文件
TIMEOUT=10 #10s刷新,表示不使用共享时,10s后自动卸载
[root@client ~]# systemctl restart autofs
[root@client ~]# df #挂载消失

这里写图片描述
######注意测试时,不要频繁df查看挂载,每查看一次,使用共享的时间就会刷新一次
这里写图片描述

###3、指定挂载点

[root@client ~]# vim /etc/auto.master
/misc /etc/auto.misc
/nfs /etc/auto.westos #/nfs是最终挂载点的上层目录
[root@client ~]# vim /etc/auto.westos #挂载规则文件
westos -ro 172.25.254.168:/westos #westos为最终挂载点
[root@client ~]# systemctl restart autofs.service
[root@client ~]# cd /nfs
[root@client nfs]# ls
[root@client nfs]# cd westos
[root@client westos]# df
172.25.254.168:/westos 10473984 3157760 7316224 31% /nfs/westos #最终挂载点为/nfs/westos

这里写图片描述

[root@client westos]# mount #查看挂载详细信息

这里写图片描述

[root@client westos]# cd #注意,要先退出该目录
[root@client ~]# vim /etc/auto.westos
westos -rw,vers=3.0 172.25.254.168:/westos
[root@client ~]# systemctl restart autofs.service
[root@client ~]# cd /nfs
[root@client nfs]# cd westos
[root@client westos]# mount

这里写图片描述

注意,再次修改挂载时,需要出/nfs/westos目录,否则修改后重启无效,无法更改挂载参数

这里写图片描述
###4、ISCSI 名称设定
#######server

[root@server ~]# fdisk /dev/vdb #创建一个设备
[root@server ~]# partprobe
[root@server ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/vdb1 2048 4196351 2097152 83 Linux
[root@server ~]# yum install targetcli -y
[root@server ~]# systemctl start target
[root@server ~]# targetcli
/> ls
o- / ........................................................................ [...]
o- backstores ............................................................. [...]
| o- block ................................................. [Storage Objects: 0]
File: /home/kiosk/Desktop/6.3/nfs文件系统 Page 3 of 4
| o- fileio ................................................ [Storage Objects: 0]
| o- pscsi ................................................. [Storage Objects: 0]
| o- ramdisk ............................................... [Storage Objects: 0]
o- iscsi ........................................................... [Targets: 0]
o- loopback ........................................................ [Targets: 0]
/> /backstores/block create westos:storage1 /dev/vdb1 #修改分区名称,格式:名称 设备
/> /iscsi create iqn.2018-06.com.example:storage1 #限定名称,注意iqn的方式是域名反写
/> 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.2018-06.com.example: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.2018-06.com.example:storage1/tpg1/acls create iqn.2018-06.com.example:westoskey #设置密码
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/luns create /backstores/block/westos:storage1 #关联设备,即创建连接将共享与分区关联起来
/> /iscsi/iqn.2018-06.com.example:storage1/tpg1/portals create 172.25.254.225 #开启服务器端口

#######client

[root@client ~]# yum search iscsi
[root@client ~]# yum install iscsi-initiator-utils.x86_64 -y
[root@client ~]# systemctl start iscsi
[root@client ~]# cd /etc/iscsi/
[root@client iscsi]# vim initiatorname.iscsi #修该密码为服务器端设定密码
InitiatorName=iqn.2018-06.com.example:westoskey
[root@client iscsi]# systemctl restart iscsi
[root@client iscsi]# fdisk -l
[root@client iscsi]# iscsiadm -m discovery -t st -p 172.25.254.225 #发现设备,m表示动作,t表示网络设备类型
[root@client iscsi]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.225 -l #登陆设备,T表示网络设备名称,p表示服务主机,l表示登陆
[root@client iscsi]# fdisk -l #增加了磁盘设备

这里写图片描述
###5、共享磁盘设备的使用
#######挂载

[root@client iscsi]# fdisk /dev/sda 
[root@client iscsi]# partprobe
[root@client iscsi]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sda1 8192 4194303 2093056 83 Linux
[root@client iscsi]# mkfs.xfs /dev/sda1 #格式化
[root@client iscsi]# vim /etc/fstab
/dev/sda1 /mnt xfs defaults,_netdev 0 0 #_netdev表示先激活网络,这时会读取网络共享
[root@client iscsi]# reboot #首次重启会失败,需要poweroff强行关机,再start开机

######/etc/fstab文件编辑时,不加_netdev系统会起不来,可以采用如下方法:
系统引导阶段按上下键,停止引导,‘e’进入编辑:
这里写图片描述
编辑完成,Ctrl+x退出
进入一个shell,执行chroot /sysroot
这里写图片描述
正确编辑文件,两次exit退出即可
#######卸载

[root@client ~]# df
[root@client ~]# umount /mnt
[root@client ~]# vim /etc/fstab #删除自动挂载行
[root@client ~]# yum install tree -y #下载树型显示
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.168 -u #登出
[root@client ~]# fdisk -l #查看,无/dev/sda1设备
[root@client ~]# tree /var/lib/iscsi/ #iscsi相关文件存在
[root@client ~]# systemctl restart iscsi
[root@client ~]# fdisk -l #因为文件存在,所以重启服务后,设备会再次出现
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.168 #登出
[root@client ~]# iscsiadm -m node -T iqn.2018-06.com.example:storage1 -p 172.25.254.168 -o delete #删除相关文件
[root@client ~]# systemctl restart iscsi
[root@client ~]# fdisk -l #无/dev/sda1设备
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值