一. SMB概述
(1)Samba简介
Samba是在Linux和Unix系统上实现SMB协议的一个免费软件,提供CIFS(Common Internet File System)协议,
由服务器及客户端程序构成,SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,
它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。
(2)Samba服务的组成部分
软件包:
Samba-common ##Samba服务的支持文件
Samba-client ##客户端应用程序
Samba ##服务器应用程序
服务名称 : smb
服务端口 : 通常使用 TCP/445 进行所有连接。还使用UDP137 、 UDP138 和 TCP/139 进行向后兼容
主配置文件 : /etc/samba/smb.conf
二. Samba服务的部署
实验环境:
先将虚拟机重置,以保持干净的实验环境(重置后需要重新设定ip 配置yum源 更改主机名)
服务端/客户端: service 172.25.254.134
客户端: client 172.25.254.234
配置服务端:
1.安装samba服务
##1.安装samba服务软件
[root@service ~]# yum install -y samba samba-common
##2.安装samba客户端软件;便于后续的测试(即该结点既为服务端又为客户端,这样可以节省一个节点资源)
[root@service ~]# yum install -y samba-client
##3.开启服务
[root@service ~]# systemctl start smb
##查看火墙的状态
[root@service ~]# systemctl status firewalld
##4.关闭火墙
[root@service ~]# systemctl stop firewalld
##开机自动关闭火墙
[root@service ~]# systemctl enable firewalld
##查看selinux的状态
[root@service ~]# getenforce
Enforcing
##5.更改selinux的状态为警告模式
[root@service ~]# setenforce 0
[root@service ~]# getenforce
Permissive
2.管理samba用户
(1)添加samba用户
注意:添加的samba用户必须为本地真实存在的用户
[root@service ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@service ~]# id westos
id: westos: no such user
## -a表示添加samb用户
[root@service ~]# smbpasswd -a student
New SMB password: ##设定samb用户密码(与本地用户密码无关)
Retype new SMB password:
Added user student.
##添加samb用户报错,因为系统中没有该用户
[root@service ~]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Failed to add entry for user westos.
##创建用户
[root@service ~]# useradd westos
##添加samb用户成功
[root@service ~]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Added user westos.
##查看系统的samb用户
[root@service ~]# pdbedit -L
student:1000:student
westos:1001:
(2)删除samb用户
##-x表示删除
[root@service ~]# pdbedit -x student
[root@service ~]# pdbedit -L
westos:1001:
配置客户端:
1.安装samba客户端软件
[root@client ~]# yum install -y samba-client
2.samba用户信息浏览
(1)匿名用户浏览
## -L表示列出信息
[root@client ~]# smbclient -L //172.25.254.134
(2)本地samba用户浏览
## westos用户必须为samba用户
[root@client ~]# smbclient -L //172.25.254.134 -U westos
3.本地samba用户访问家目录
(1)以直接登陆的方式 访问本地samba用户家目录
##samba用户:westos 的家目录为 /westos
[root@client ~]# smbclient //172.25.254.134/westos -U westos
Enter westos's password: ##输入samba用户:westos 的密码
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]
smb: \> ls
. D 0 Sat Nov 24 10:39:27 2018
.. D 0 Sat Nov 24 10:39:27 2018
.mozilla DH 0 Sat Nov 17 22:28:52 2018
.bash_logout H 18 Wed Jul 8 19:11:02 2015
.bash_profile H 193 Wed Jul 8 19:11:02 2015
.bashrc H 231 Wed Jul 8 19:11:02 2015
8706048 blocks of size 1024. 5706456 blocks available
##不能建立文件,因为这不是shell环境,不能使用shell命令
smb: \> touch file1
touch: command not found
smb: \> quit
(2)以挂载的方式 访问本地samba用户家目录
[root@client ~]# mount -o username=westos,password=westos //172.25.254.134/westos /mnt
[root@client ~]# df
[root@client ~]# cd /mnt
[root@client mnt]# ls
[root@client mnt]# touch file{1..3}
[root@client mnt]# ls
file1 file2 file3
##此时可在samba服务端的samba用户(westos)的家目录里查看到刚才建立的文件
[root@service ~]# ls -l /home/westos
以挂载的方式 访问本地samb用户家目录的好处在于:可以使用shell命令,对文件进行增删改等操作。
4.samba访问控制
所有更改samba服务配置文件操作均为 即改即生效,也就是说无需重启samba服务
1.更改域名(Domain)
##匿名用户访问;可查看到初始域名
[root@service ~]# smbclient -L //172.25.254.134
##永久性设定行号
[root@service ~]# vim /etc/vimrc
#################
:set nu
[root@service ~]# vim /etc/samba/smb.conf
#################
89 workgroup = WESTOS ##更改samb用户的域名
测试:
##即改即生效,无需重启服务
[root@service ~]# smbclient -L //172.25.254.134
2.允许selinux访问samba服务
##查看selinux的状态
[root@service ~]# getenforce
Permissive
##更改selinux的状态为强制模式
[root@service ~]# setenforce 1
[root@service ~]# getenforce
Enforcing
##此时无法登陆本地samb用户
[root@service ~]# smbclient //172.25.254.134/westos -U westos
##查看解决方案
[root@service ~]# vim /etc/samba/smb.conf
##设定布尔值; 打开samba用户家目录的访问权限
[root@service ~]# setsebool -P samba_enable_home_dirs on
##此时便可以登陆本地samb用户
[root@service ~]# smbclient //172.25.254.134/westos -U westos
3. samba用户访问的黑名单与白名单
@默认所有主机均可访问本地samba用户家目录
#134主机可以登陆samba用户:westos的家目录
[root@service ~]# smbclient //172.25.254.134/westos -U westos
##234主机也可以登陆samba用户:westos的家目录
[root@client ~]# smbclient //172.25.254.134/westos -U westos
(1)设定白名单
[root@service ~]# vim /etc/samba/smb.conf
#################
hosts allow =172.25.254.134 #设定白名单;只允许134主机访问samb用户家目录
测试:
[root@service ~]# smbclient //172.25.254.134/westos -U westos
[root@client ~]# smbclient //172.25.254.134/westos -U westos
(2)设定黑名单
[root@service ~]# vim /etc/samba/smb.conf
#################
; hosts allow = 172.25.254.134 #注释白名单
hosts deny = 172.25.254.134 #设定黑名单;不允许134主机访问samb用户家目录
测试:
[root@service ~]# smbclient //172.25.254.134/westos -U westos
[root@client ~]# smbclient //172.25.254.134/westos -U westos
还原实验环境:
[root@service ~]# vim /etc/samba/smb.conf
5.Samba共享
(1)共享自己建立的目录
##1.建立目录
[root@service ~]# mkdir /westos
[root@service ~]# touch /westos/file{1..3}
##2.共享目录
[root@service ~]# vim /etc/samba/smb.conf
#################
322 [linux] #共享目录名称
323 comment = westos dir share #共享目录说明
324 path = /westos #共享目录的真实路径
##查看目录的安全上下文
[root@service ~]# ls -Zd /westos/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
##3.修改安全上下文
[root@service ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'
[root@service ~]# ls -Zd /westos/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/
##4.刷新
[root@service ~]# restorecon -FvvR /westos/
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@service ~]# ls -Zd /westos/
drwxr-xr-x. root root system_u:object_r:samba_share_t:s0 /westos/
测试:
##匿名用户浏览;可查看到共享的linux目录
[root@service ~]# smbclient -L //172.25.254.134
##本地samba用户登陆;可查看到共享目录中的文件
[root@service ~]# smbclient //172.25.254.134/linux -U westos
(2)共享系统目录
[root@service ~]# ls /mnt
##1.在系统目录/mnt下建立文件
[root@service ~]# touch /mnt/westos{1..3}
##2.共享目录
[root@service ~]# vim /etc/samba/smb.conf
################
326 [opt] ##共享目录名称
327 comment = mnt directory ##共享目录说明
328 path = /mnt ##共享目录真实路径
##查看selinux的状态
[root@service ~]# getenforce
Enforcing
##查看安全上下文
[root@service ~]# ls -Zd /opt
drwxr-xr-x. root root system_u:object_r:usr_t:s0 /opt
由于/mnt为系统目录,如果直接更改安全上下文的话,会影响其他用户的访问,所以我们需要开放samba服务的读写功能
##匿名用户访问
[root@service ~]# smbclient -L //172.25.254.134
##本地用户登陆
[root@service ~]# smbclient //172.25.254.134/opt -U westos
[root@service ~]# getsebool -a | grep samba
##3.设定布尔值;打开samaba服务可读权限
[root@service ~]# setsebool -P samba_export_all_ro on
##4.设定布尔值;打开samaba服务可写权限
[root@service ~]# setsebool -P samba_export_all_rw on
##查看samba服务的访问权限
[root@service ~]# getsebool -a | grep samba
测试:
[root@service ~]# smbclient //172.25.254.134/opt -U westos
6.samba共享目录权限的管理
先添加samba用户:student
[root@service ~]# pdbedit -L
westos:1001:
##添加samba用户
[root@service ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.
##查看系统中的samba用户
[root@service ~]# pdbedit -L
student:1000:
westos:1001:
(1).允许所有用户可写
@默认所有用户均不可写
[root@service ~]# smbclient //172.25.254.134/linux -U westos
##1.允许所有用户可写
[root@service ~]# vim /etc/samba/smb.conf
#################
325 writable = yes #允许所有用户均可写
##此时仍然无法删除文件;此时与samba服务无关,只是因为对文件目录没有权限
[root@service ~]# smbclient //172.25.254.134/linux -U westos
[root@service ~]# ll /westos/
[root@service ~]# ll -d /westos/
##2.允许其他用户对该目录以及目录的内容有读写权限;注意:需要直接给目录权限而不是目录中的文件
[root@service ~]# chmod 777 /westos/
[root@service ~]# ll -d /westos
drwxrwxrwx. 2 root root 30 Nov 24 15:23 /westos
测试:
[root@service ~]# smbclient //172.25.254.134/linux -U westos
(2).允许指定用户可写
[root@service ~]# vim /etc/samba/smb.conf
#################
325 ; writable = yes ##注释
326 write list = westos ##仅允许samba用户:westos可写
测试:
[root@service ~]# smbclient //172.25.254.134/linux -U westos
[root@service ~]# smbclient //172.25.254.134/linux -U student
(3).允许指定用户组可写
[root@service ~]# vim /etc/samba/smb.conf
#################
326 write list = +westos ##仅允许westos用户组可写(+等同于@)
[root@service ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student)
##此时不可写,因为westos不是student用户的附加组
[root@service ~]# smbclient //172.25.254.134/linux -U student
##添加附加组westos到student用户中
[root@service ~]# usermod -G westos student
[root@service ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student),1001(westos)
测试:
[root@service ~]# smbclient //172.25.254.134/linux -U student
(4).仅允许指定用户能访问
[root@service ~]# vim /etc/samba/smb.conf
#################
326 ; write list = +westos
327 valid users = student ##仅允许student用户可访问
测试:
[root@service ~]# smbclient //172.25.254.134/linux -U student
[root@service ~]# smbclient //172.25.254.134/linux -U westos
(5).仅允许指定用户组能访问
[root@service ~]# vim /etc/samba/smb.conf
#################
327 valid users = @student ##仅允许student用户组可访问
[root@service ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
##此时无法访问,因为student不是westos用户的附加组
[root@service ~]# smbclient //172.25.254.134/linux -U westos
[root@service ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
##指定附加组
[root@service ~]# usermod -G student westos
[root@service ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos),1000(student)
测试:
[root@service ~]# smbclient //172.25.254.134/linux -U westos
(6).隐藏目录,但不代表不可用
##默认可以查看到
[root@service ~]# smbclient -L //172.25.254.134 -U westos
[root@service ~]# vim /etc/samba/smb.conf
#################
328 browseable = no ##隐藏目录
测试:
[root@service ~]# smbclient -L //172.25.254.134 -U westos
还原:
[root@service ~]# vim /etc/samba/smb.conf
###########
328 browseable = yes ##更改为yes或者直接注释掉均可
[root@service ~]# smbclient -L //172.25.254.134 -U westos
(7).指定用户身份为超级用户root
[root@service ~]# vim /etc/samba/smb.conf
#################
325 writable = yes
329 admin users = student ##指定student用户身份为超级用户
测试:
[root@service ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@service ~]# smbclient //172.25.254.134/linux -U student
[root@service ~]# ls -l /westos
total 4
-rwxr--r--. 1 root student 1748 Nov 24 16:54 anaconda-ks.cfg
还原:
[root@service ~]# vim /etc/samba/smb.conf
#################
329 ; admin users = student
[root@service ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@service ~]# smbclient //172.25.254.134/linux -U student
[root@service ~]# ls -l /westos
(8).允许匿名用户访问
@默认匿名用户不能访问
[root@service ~]# smbclient //172.25.254.134/linux
[root@service ~]# vim /etc/samba/smb.conf
#################
126 map to guest = bad user ##映射用户;将guest用户映射为无身份的用户
330 guest ok =yes ##允许匿名用户(无身份的用户)访问
测试:
[root@service ~]# smbclient //172.25.254.134/linux
[root@service ~]# ll /westos/
[root@service ~]# id nobody
还原:
[root@service ~]# vim /etc/samba/smb.conf
#################
126 ; map to guest = bad user
330 ; guest ok =yes