Samba
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成
SMB(Server Messages Block,信息服务块)
局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务
SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源
Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源
服务器端
yum install samba-client.x86_64 samba-common.x86_64 samba.x86_64 -y
安装 smb 客户端、主程序、smb语法检测等
systemctl start smb
开启 smb
yum install cifs-utlis -y
netstat -antlupe | grep smb
查看 smb 端口
smb 端口为 445 和 139
systemctl start firewalld
开启防火墙
firewall-cmd --permanent --add-service=samba
添加 samba 服务
firewall-cmd --permanent --add-service=samba-client
添加 samba-client 服务
firewall-cmd --reload
firewall-cmd --list-all
查看服务已开启
服务器端
useradd zhang
useradd westos
创建本地用户 zhang 和 westos
smbpasswd -a westos
smbpasswd -a zhang
创建 westos 和 zhang 的 smb 用户密码
pdbedit -L
查看以创建 smb 用户
注意
smb 用户必须是服务器端本地用户
测试端
yum install samba-client -y
安装 smb 客户端
yum install cifs-utlis -y
smbclient -L //172.25.254.130 -U zhang
Enter zhang's password:
查看服务器端 smb 用户 zhang 所共享的资源,需要 smb 用户 zhang 的密码
smbclient //172.25.254.130/zhang -U zhang
Enter zhang's password:
进入 smb 用户 zhang 共享的目录内
!ls
查看进入 smb 客户端前, shell 当前目录
ls
查看 smb 用户 zhang 目录
put f1
当前 shell 所在目录内有 f1 文件
上传至 smb 用户 zhang 目录内
服务器端
cd /home/zhang
切换到用户 zhang 家目录下
ls
测试端
rm f1
删除 f1 文件
ls
服务器端
ls
f1 文件已被删除
测试端
mkdir /mnt/zhang
创建挂载点
mount //172.25.254.130/zhang /mnt/zhang -o username=zhang,password=123
挂载服务器端 smb 用户 zhang 到 /mnt/zhang ,需要提供 smb 用户 zhang 账号密码
cd /mnt/zhang/
切换到 /mnt/zhang/
ls
touch f{1..5}
创建f1–f5文件
ls
服务器端
cd /home/zhang
切换到 /home/zhang/
ls
可查看到客户端操作所建立的 f1–f5 文件
测试端
smbclient -L //172.25.254.130/zhang -U zhang
查看到域名为 [MYGROUP]
服务器端
vim /etc/samba/smb.conf
workgroup = WESTOS
域名修改为 WESTOS
systemctl restart smb
测试端
smbclient -L //172.25.254.130/zhang -U zhang
域名已改变为 WESTOS
服务器端
vim /etc/samba/smb.conf
修改smb配置文件
hosts allow = 172.25.254.131
只允许 172.25.254.131 访问
systemctl restart smb
测试端
smbclient -L //172.25.254.130/zhang -U zhang
172.25.254.130
smbclient -L //172.25.254.130/zhang -U zhang
禁止访问
vim /etc/samba/smb.conf
host deny =172.25.254.131
拒绝 172.25.254.131 访问
systemctl restart smb
vim /etc/samba/smb.conf
hosts allow = 172.25.254.
允许广播域 172.25.254.0 – 172.25.254.255 访问
smb写权限
getenforce
查看SELinux
Disabled
关闭
vim /etc/sysconfig/selinux
SELINUX=enforcing
修改为强制级别
reboot
重启生效
getenforce
Enforing
方法一:
修改目录及目录内容安全上下文
mkdir /westos_smb
创建目录
semanage fcontext -a -t samba_share_t '/westos_smb(/.*?)?'
打开目录及目录内容安全上下文
vim /etc/samba/smb.conf
编辑 samba 配置文件
[WESTOS]
comment = Share Dirctory
备注(自定义)
path = /westos_smb
路径
writable = yes
开启写权限
systemctl restart smb
setfacl -m u:westos:rwx /westos_smb/
用户 westos 设置 acl 权限 对于 /westos_smb 目录
setfacl -m u:zhang:rwx /westos_smb/
用户 zhang 设置 acl 权限 对于 /westos_smb 目录
getfacl /westos_smb
查看 acl 权限
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
挂载
cd /mnt
touch f1
rm -rf W*
方法二:
修改 SEBool
vim /etc/samba/smb.conf
[mnt]
comment = Share System mnt Dirctory
备注(自定义)
path = /mnt
路径
writable = yes
开启写权限
systemctl restart smb
systemctl restart smb
mount //172.25.254.130/mnt /mnt -o username=westos,password=123
挂载
cd /mnt
touch f1
权限不够
getsebool -a | grep samba
查看SEBool
setsebool -P samba_export_all_rw=on
打开权限
getsebool -a | grep samba
查看 samba SEBool 权限
cd /mnt
touch f1
ls
隐藏共享文件目录
vim /etc/samba/smb.conf
[WESTOS]
comment = Share Dirctory
备注(自定义)
path = /westos_smb
路径
browseable = yes
关闭隐藏
systemctl restart smb
smbclient -L //172.25.254.130/WESTOS -U zhang
WESTOS 目录可见
vim /etc/samba/smb.conf
[WESTOS]
comment = Share Dirctory
备注(自定义)
path = /westos_smb
路径
browseable =no
开启隐藏
systemctl restart smb
smbclient -L //172.25.254.130/WESTOS -U zhang
WESTOS 目录隐藏
用户以 root 身份登陆
vim /etc/samba/smb.conf
[WESTOS]
comment = Share Dirctory
备注(自定义)
path = /westos_smb
路径
admin users = westos
root 身份
systemctl restart smb
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
挂载
测试
cd /mnt
touch f1
ll
匿名用户
vim /etc/samba/smb.conf
map to guest = bad user
[WESTOS]
comment = Share Dirctory
path = /westos_smb
writable = yes
guest ok = yes
systemctl restart smb
getenforce
Enforing
当 SELinux 为 Enforing 时需要修改 SEBool 权限
getsebool -a | grep samba
setsebool -P samba_export_all_rw=on
getsebool -a | grep samba
smbclient //172.25.254.130/WESTOS
匿名用户无密码,回车即可
ls
查看 /WESTOS 目录
!ls
查看未开启 smbclient 前,用户当前目录
mount //172.25.254.130/WESTOS /mnt -o username=guest
挂载
touch f2
ll
写权限列表
服务端
vim /etc/smaba/smb.conf
write list = +leon
在写权限列表里添加 leon 用户
leon 用户 拥有写权限
systemctl restart smb
测试端
mount //172.25.254.130/WESTOS /mnt -o username=leon,password=123
cd /mnt
touch f1
权限不够
服务端
chmod 777 /westos_smb
测试端
touch f1
ll
umount /mnt
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
更换 westos 用户
cd /mnt
touch f2
权限不够
因为没有开启 writable (写)权限
在 write list 权限里只有 leon 用户
服务端
vim /etc/smaba/smb.conf
valid users = @leon
在 leon 用户组里的所有用户都拥有写权限
systemctl restart smb
usermod -G leon westos
添加 westos 用户到 leon 用户组里
id westos
测试端
touch f2
再次创建 f2 文件
生成