1.SMB 文件共享:
用 Internet 文件系统 (CIFS) 也称为服务器信是适用于MicrosoftWindows 服务器和客户端的标准文件和打印机共享系统息块 (SMB)。Samba 服务可用于将 Linux 文件系统作为 CIFS/SMB 网络文件共享进行共享 , 并将 Linux 打印机作为 CIFS/SMB 打印机共享进行共享
SMB 服务的组成部分:
软件包 : Samba-common – Samba 的支持文件
Samba-client – 客户端应用程序
Samba – 服务器应用程序
服务名称 : smb nmb
服务端口 : 通常使用 TCP/445 进行所有连接。还使用UDP137 、 UDP138 和 TCP/139 进行向后兼容
主配置文件 : /etc/samba/smb.conf
主要的命令:
smbpasswd -a username #建立samba用户(必须为本地真实存在的用户)
pdbedit -x username #删除samba用户
pdbedit -L #查看samba用户
smbclient -L //172.25.254.230 #匿名用户访问,-L表示列出信息
smbclient -L //172.25.254.230 -U username #本地用户访问
smbclient //172.25.254.230/student -U student #本地用户登陆
实验:
准备工作: 重置两个虚拟机 分别设定ip 配置yum源 更改主机名: 将desktop改为client.example.com
将server 改为server.example.com
在服务端(server)
yum search samba
yum install samba samba-client samba-common -y
systemctl start smb
systemctl enable smb.service
systemctl stop firewalld
systemctl disable firewalld
netstat -antlupe | grep smb #查看samba的端口
在客户端(client)
yum install samba-client -y
smbclient -L //172.25.254.230 #匿名用户访问
在服务端:
id student ##必须是本地真实存在的用户,才能成为samba用户
id westos
smbpasswd -a student #添加samba用户 -a 表示添加
smbpasswd -a westos #添加失败
useradd westos
smbpasswd -a westos #添加成功
pdbedit -L #查看存在的samba用户
pdbedit -x student #删除samba用户
pdbedit -L
smbpasswd -a student
pdbedit -L
在客户端:
smbclient -L //172.25.254.230 -U student #本地用户访问,-L表示列出
smbclient //172.25.254.230/student -U student #本地用户登陆
在服务端:
getsebool -a | grep samba #查看到samba家目录状态为off
setsebool -P samba_enable_home_dirs on #打开家目录
在客户端:
smbclient //172.25.254.230/westos -U westos
2.SMB 挂载:
在客户端:
smbclient -L //172.25.254.230 -U student
说明:由于samba 是 Disk(硬盘) 不是shell 所以shell里的命令都无法使用
解决方案:
方法一(将shell里的命令上传,便可使用shell里的命令)
在服务端:
cd /home/student
ls
在客户端:
cd /etc ##只有切换到哪个路径下才能在里面上传的文件
smbclient //172.25.254.230/student -U student
smb: \> !ls
smb: \> put passwd #上传成功
putting file passwd as \passwd (1000.0 kb/s) (average 1000.0 kb/s)
smb: \> quit
在服务端:
ls #passwd上传成功
在客户端:
smbclient //172.25.254.230/student -U student
smb: \> put /bin/ls #上传失败
NT_STATUS_OBJECT_PATH_NOT_FOUND opening remote file \/bin/ls
smb: \> quit
cd /bin/
smbclient //172.25.254.230/student -U student
smb: \> put ls #上传成功
putting file ls as \ls (3104.3 kb/s) (average 3104.3 kb/s)
smb: \> touch file #但还是不能建立文件
touch: command not found
smb: \> quit
在服务端:
ls #ls上传成功
方法二(直接挂载之后便可以使用shell里的命令,这种方法比较简单方便)
(1)临时性挂载
在客户端:
cd
mount //172.25.254.230/student /mnt/ -o username=student,password=student #临时挂载
df
cd /mnt #相当于进入到230主机里的/home/student目录里
ls #便可查看到230主机里的东西
touch file{1..10}
在服务端:
在客户端:
cd
smbclient //172.25.254.230/student -U student
smb: \> rm *
smb: \> quit
在服务端:
(2)永久性挂载(在客户端)
方法一:
cd
df
umount /mnt/ #卸载
df
vim /etc/fstab #永久性挂载,但这种方式会影响服务的启动
##################
mount -a #重新加载
df
方法二:
vim /etc/fstab
##################
umount /mnt
df
vim /etc/rc.d/rc.local #永久性挂载且不会影响服务的启动
#################
chmod +x /etc/rc.d/rc.local #给一个可执行权限
reboot
ssh root@172.25.254.130
df
##如果还是没有挂载上,则执行:
mount -o username=student,password=student //172.25.254.230/student /mnt/
df
##若可以临时挂载,说明写入脚本的命令没有问题,问题在于虚拟主机。所以此时做实验应在自己下载好的虚拟机里作实验。
3.
(1)组的设定
在客户端:
smbclient -L //172.25.254.230/
在服务端:
rpm -qc samba-common
vim /etc/samba/smb.conf #编辑samba主配置文件
#################
systemctl restart smb.service
在客户端:
smbclient -L //172.25.254.230/
(2)白名单的设定
在服务端:
vim /etc/samba/smb.conf
##############
systemctl restart smb.service
##只有130可以登陆
在客户端:
smbclient -L //172.25.254.230/
在真机里:
yum install samba samba-client samba-common -y
smbclient -L //172.25.254.230/
(3)黑名单的设定
在服务端:
vim /etc/samba/smb.conf
############
systemctl restart smb.service
##除了230外都能登陆
在客户端:
smbclient -L //172.25.254.230/
在真机中:
smbclient -L //172.25.254.230/
4.SMB 共享目录
(1)共享自己建立的目录
在服务端:
mkdir /westos
vim /etc/samba/smb.conf
###############
systemctl restart smb.service
semanage fcontext -a -t samba_share_t '/westos(/.*)?' #修改安全上下文
restorecon -FvvR /westos/ #刷新
ls -Zd /westos/ #查看安全上下文是否更改成功
vim /etc/samba/smb.conf
################
systemctl restart smb.service
touch /westos/file{1..3}
在客户端:
smbclient -L //172.25.254.230/ #便可查看到DIR目录
smbclient //172.25.254.230/DIR -U student
(2)共享本地目录
在服务端:
vim /etc/samba/smb.conf
###############
systemctl restart smb.service
touch /mnt/linux{1..3}
在客户端:
smbclient -L //172.25.254.230/
smbclient //172.25.254.230/mnt -U student
在服务端:
setenforce 0
在客户端:
smbclient //172.25.254.230/mnt -U student
##若在服务端更改Selinux的状态为强制(setenforce 1 )则此时在客户端 ls 又会查看不到任何文件,
说明 SMB 共享本地目录 会受到 Selinux 的影响
5.SMB权限管理
(1)目录是否可被浏览
在服务端:
vim /etc/samba/smb.conf
###########
在客户端:
smbclient -L //172.25.254.230
(2)用户是否可写
1.对所有用户可写
在服务端:
vim /etc/samba/smb.conf
##############
systemctl restart smb.service
在客户端:
mount //172.25.254.230/DIR /mnt -o username=student,password=student
df
cd /mnt
ls
touch file #不能建立
在服务端:
chmod 777 /westos/ #给权限
在客户端:
touch file #建立成功
ls
(3).设定可写用户列表
在服务端:
vim /etc/samba/smb.conf
##############
在客户端:
cd
umount /mnt/ #卸载
mount //172.25.254.230/DIR /mnt -o username=westos,password=westos
touch /mnt/file4 #失败
umount /mnt/
mount //172.25.254.230/DIR /mnt -o username=student,password=student
touch /mnt/file4 #成功(因为仅允许student用户可写)
ls
(4).设定可写用户组列表
在服务端:
vim /etc/samba/smb.conf
systemctl restart smb.service
id westos
在客户端:
umount /mnt/
mount //172.25.254.230/DIR /mnt -o username=westos,password=westos
cd /mnt/
touch file5 #建立失败,因为只允许对studnet组可写
在服务端:
usermod -G student westos #给westos用户添加附加组student
id westos
在客户端:
umount /mnt/
mount //172.25.254.230/DIR /mnt -o username=westos,password=westos
cd /mnt/
touch file5 #建立成功
ls
(4).指定超级用户
在服务端:
chmod 755 /westos/ #将权限改为超级用户可以执行
ls -ld /westos/ #查看权限
在客户端:
cd
umount /mnt/
mount //172.25.254.230/DIR /mnt -o username=westos,password=westos
touch /mnt/linux #无法建立文件,因为westos不是超级用户
在服务端:
vim /etc/samba/smb.conf #编辑主配置文件
##################
systemctl restart smb.service #重启服务
在客户端:
umount /mnt/
mount //172.25.254.230/DIR /mnt -o username=westos,password=westos
touch /mnt/linux #建立成功
ls /mnt/
6.AMB多用户挂载,用户认证
实验(在客户端)
useradd test
cd /mnt
ls
cd
su - test
cd /mnt
ls ##test用户可以看到root用户的文件,这样安全性能很低
logout
umount /mnt
yum install cifs-utils.x86_64 -y #安装加密服务
vim /root/smbpass
##############
mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.230/DIR /mnt
#多用户挂载,ntlmssp为认证方式,multiuser 为多用户登陆
su - test
cd /mnt
ls #操作被拒绝,无法查看文件
cifscreds add -u westos 172.25.254.230 #建立认证(普通用户test通过SMB用户westos登陆认证)
ls #操作被拒绝
cifscreds add -u westos 172.25.254.221 #再次认证
cifscreds clearall #清除认证信息
cifscreds add -u westos 172.25.254.221 认证westos用户,
ls #此时便可以看到文件
touch /mnt/file6 #同时可以建立文件
ll /mnt #文件所属用户root,所属组为test,因为在建立认证时,SMB用户westos在[DIR]中是以root身份登陆的
7.匿名用户的访问
在客户端:
smbclient //172.25.254.230/DIR #匿名用户不能访问
在服务端:
vim /etc/samba/smb.conf
################
在客户端:
smbclient //172.25.254.230/DIR #此时匿名用户可以访问
umount /mnt
mount //172.25.254.230/DIR /mnt -o username=gust,password="" #匿名用户免密挂载
df