Linux(23)——文件共享
1. Samba
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。
主配置目录: /etc/samba 主配置文件: /etc/smb.conf
安全上下文:samba_share_t
- 安装与启用
服务端:
[root@rhserver Desktop]# dnf install samba samba-common samba-client -y
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=samba
[root@rhserver Desktop]# firewall-cmd --reload
客户端:
[root@rhclient Desktop]# dnf install samba-client -y
测试:
[root@rhclient Desktop]# smbclient -L //192.168.158.129
- 共享目录设置
- 自定义目录
当使用自定义目录时,在主配置文件中声明目录,并设定目录安全上下文标签。
[root@rhserver Desktop]# vim /etc/samba/smb.conf
# 添加内容
[SHARE] #共享名称
comment = share #共享说明
path = /sharedir #共享路径
[root@rhserver Desktop]# mkdir /sharedir
[root@rhserver Desktop]# semanage fcontext -a -t samba_share_t '/sharedir(/.*)?'
[root@rhserver Desktop]# restorecon -RvvF /sharedir/
测试:
[root@rhclient Desktop]# smbclient //192.168.158.129/SHARE -U user1
- 系统目录
当系统目录时,修改目录的安全上下文会使其余服务对该目录的使用受到影响,因此需要开启samba服务的全局sebool。
[root@rhserver Desktop]# vim /etc/samba/smb.conf
# 添加内容
[MNT] #共享名称
comment = local mnt dir #共享说明
path = /mnt #共享路径
[root@rhserver Desktop]# setsebool -P samba_export_all_rw on #开启全局读写
[root@rhserver Desktop]# setsebool -P samba_export_all_ro on #开启全局只读
测试:
[root@rhclient Desktop]# smbclient //192.168.158.129/MNT -U user1
- 用户与访问控制
- 用户使用
samba用户必须是被地存在的用户
[root@rhserver Desktop]# smbpasswd -a user1 #添加用户
[root@rhserver Desktop]# pdbedit -L #查看用户列表
[root@rhserver Desktop]# pdbedit -x user1 #删除用户
-
访问控制
当参数写到 [GLOBAL] 下时,对全局生效;当写至局部目录下时。只针对该目录生效。hosts allow 192.168.158.128 192.168.1. #允许指定IP与网段访问 hosts deny 192.168.158.128 192.168.1. #拒绝指定IP与网段访问
-
配置参数
当参数写到 [GLOBAL] 下时,对全局生效;当写至局部目录下时。只针对该目录生效。writable = yes #是否可写 write list = user #指定用户可写 write list = +user #指定组可写 write list = @user valid users = user #允许访问的用户 valid users = +user|@user #允许访问的用户 browseable = yes|no #是否可以被浏览 admin users = user #指定用户为超级用户使用服务 #### 匿名用户设定 map to guest = bad user #在全局设定中 guest ok = yes #全局或局部目录
未开放写权限:
开放权限,修改配置文件:
通过其余用户挂载失败,设定用户挂载成功,并且创建文件成功。服务端查看文件所有者为root。
- 多用户挂载
挂载后经过认证的用户才可访问服务。
[root@rhserver Desktop]# dnf install cifs-utils -y #安装服务
[root@rhserver Desktop]# vim /root/smbpass #创建认证文件
username=user1
password=abc123
[root@rhserver Desktop]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //192.168.158.129/SHARE /mnt
## 指定挂载方式:认证文件 认证类型 多用户挂载
[root@rhserver Desktop]# cifscreds add -u user2 192.168.158.129 #添加用户
[root@rhserver Desktop]# cifscreds clearall 192.168.158.129 #删除所有用户
配置完成后,未认证成功的用户访问挂载点 /mnt 会被拒绝。
-
autofs 自动挂载卸载
在客户端安装autofs
编辑配置文件:vim /etc/autofs.conf #编辑配置文件 按需修改挂载/卸载策略 Timeout=3 vim /etc/auto.master # 添加内容 自动挂载点的父级目录 # 父目录为根时,使用 /- /- /etc/auto.samba # 二级目录以上 /mnt /etc/auto.samba vim /etc/auto.samba # 编辑挂载策略文件: # 挂载点(父目录为根时,写绝对路径); # 挂载策略; # 目标路径 samba -fstype=cifs,username=user1,password=abc123 ://192.168.158.129/SHARE
配置文件:
自动挂载成功后,超时自动卸载:
2. NFS
Network File System(NFS),比Samba安全性更高的共享服务。动态开放端口。rpc-bind–>mountd–>NFS,配置文件为 /etc/exports。
- 安装启用
[root@rhserver Desktop]# dnf install nfs-utils #安装nfs
[root@rhserver Desktop]# systemctl start nfs-server #启动nfs
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=rpc-bind
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=mountd
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=nfs
[root@rhserver Desktop]# firewall-cmd --reload
测试:
[root@rhclient Desktop]# showmount -e 192.168.158.129
- 共享目录配置
[root@rhserver Desktop]# vim /etc/exports
[root@rhserver Desktop]# exportfs -rv
# 共享目录 共享目标用户(共享参数)
/sharedir *(ro),192.168.158.128(rw)
# 参数
anonuid=1000,anongid=1000 #指定用户身份
# uid与gid显示为实际操作端的用户,指定ID数字不变,实际用户因查看端变动
sync #数据完成更改后同步至服务器
async #数据实时同步至服务器
rw #读写
ro #只读
no_root_squash #root当以用户挂载后 不转换身份
设定文件所有者UID为1234,rw方式挂载:
服务器端UID=1234的用户存在,而客户端不存在此用户:
文件所有者以在服务端的UID为准:
-
autofs 自动挂载卸载
与samba设定步骤相同,挂载策略略有改变。vim /etc/auto.nfs nfs -rw,vers=3 192.168.158.129:/sharedir #当有多个两级共享目录时,自动匹配目录 * -rw,vers=3 192.168.158.129:/sharedir/&
在服务端创建多个共享文件夹:
客户端autofs配置:
自动挂载,超时自动卸载:
3. iscsi
Internet Small Computer System Interface,提供磁盘共享服务,磁盘可被客户端操作。
- 服务端启用
[root@rhserver Desktop]# dnf install targetcli -y
[root@rhserver Desktop]# targetcli
/backstores/block create test_storage1 /dev/vdb1
/iscsi create iqn.2020-08.com.test:strage1
/iscsi/iqn.2021-06.com.test:strage1/tpg1/luns create /backstores/block/test_storage1
/iscsi/iqn.2021-06.com.test:strage1/tpg1/acls create iqn.2021-06.com.test:strage1:testkey
[root@rhserver Desktop]# systemctl restart target
[root@rhserver Desktop]# firewall-cmd --permanent --add-port=3026/tcp
[root@rhserver Desktop]# firewall-cmd --reload
- 客户端启用
[root@rhclient Desktop]# dnf install iscsi-initiator-utils.x86_64 -y
[root@rhclient Desktop]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2021-06.com.test:strage1:testkey
[root@rhclient Desktop]# systemctl restart iscsid
[root@rhclient Desktop]# iscsiadm -m discovery -t st -p 192.168.158.129
[root@rhclient Desktop]# iscsiadm -m node -T iqn.2021-06.com.test:strage1 -p 192.168.158.129 -l
[root@rhclient Desktop]# mkfs.xfs /dev/sda1 #格式化共享磁盘
[root@rhclient Desktop]# mount /dev/sda1 /mnt #挂载使用
自动挂载:
vim /etc/fstab
/dev/sda1 /mnt xfs default,_netdev 0 0
- 删除
客户端:
[root@rhclient Desktop]# iscsiadm -m node -T iqn.2021-06.com.test:strage1 -p 192.168.158.129 -u
[root@rhclient Desktop]# iscsiadm -m node -T iqn.2021-06.com.test:strage1 -p 192.168.158.129 -o delete
服务端:
[root@rhserver Desktop]# targetcli
clearconfig confirm=True