samba
程序员文章站
2022-05-11 17:32:18
...
samba简介
- Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成
- NFS与samba一样,也是在网络中实现文件共享的一种实现;但NFS不支持windows平台,samba是能够在任何支持SMB协议的主机之间共享文件的一种实现,包括windows
- SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务
samba监听的端口
- TCP:139、445;对应smbd服务,提供对服务器中文件、打印资源的共享访问
- UDP:137、138;对应nmbd服务,提供基于NetBIOS主机名称的解析
samba进程
- nmbd,对应netbios
- smbd,对应cifs协议
- winbindd + ldap,对应Windows AD活动目录
samba用户
- 都是系统用户/etc/passwd
- Samba服务自有密码文件通过smbpasswd -a USERNAME命令设置
smbpasswd [参数] USERNAME
-a //添加系统用户,并为其设置密码
-d //禁用用户
-e //启用用户
-x //删除用户
[[email protected] ~]# yum -y install samba-*
[[email protected] ~]# useradd tom
[[email protected] ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.
Samba安全级别
- user:基于本地的验证
- server:由另一台指定的服务器对用户身份进行认证
- domain:由域控进行身份验证
注:以前的samba版本支持的安全级别有四个,多个share
share是用来设置匿名访问的,现在的版本已经不支持share了,但是还是可以实现匿名访问
samba配置文件
/etc/samba/smb.conf(主配置文件)
- [global]:全局配置,此处的设置项对整个samba服务器都有效
- [homes]:宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。当用户访问服务器中与自己用户名同名的共享目录时,通过验证后将会自动映射到该用户的宿主目录中
- [printers]:打印机共享设置
常用配置文件参数
参数 | 作用 |
---|---|
workgroup | 表示设置工作组名称 |
server string | 表示描述samba服务器 |
security | 表示设置安全级别,其值可为user、server、domain |
passdb backend | 表示设置共享帐户文件的类型,其值可为tdbsam(tdb数据库文件)、ldapsam(LDAP目录认证)、smbpasswd(兼容旧版本samba密码文件);sam保存账户密码信息的 |
comment | 表示设置对应共享目录的注释,说明信息 |
browseable | 表示设置共享是否可见 |
writable | 表示设置目录是否可写 (设置所有人) |
path | 表示共享目录的路径 |
guest ok | 表示设置是否所有人均可访问共享目录 |
public | 表示设置是否允许匿名用户访问(设置匿名用户,guest ok和public都要设置) |
write list | 表示设置允许写的用户和组,组要用@表示,例如 write list = root,@root |
valid users | 设置可以访问的用户和组,例如 valid users = root,@root |
hosts deny | 设置拒绝哪台主机访问,例如 hosts deny = 192.168.72.1 |
hosts allow | 设置允许哪台主机访问,例如 hosts allow = 192.168.72.2 |
printable | 表示设置是否为打印机 |
- 测试配置文件是否有语法错误,以及显示最终生效的配置:使用testparm命令
[[email protected] ~]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Loaded services file OK.
Server role: ROLE_STANDALONE
示例
配置用户认证共享
用一个系统不存在的用户来操作共享目录
服务端:
1、如果系统默认没有samba服务,则安装
yum -y install samba*
2、创建用户,设置smb共享密码
[[email protected] ~]# useradd -M tom
[[email protected] ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.
3、创建要共享目录,并将目录目录属主属组改为tom
mkdir /aaa
chown -R tom.tom /aaa/
4、写配置文件
4.1、映射tom为share用户,需要在/etc/samba/smbusers文件中添加tom = share,然后在/etc/samba/smb.conf的全局配置中加username map = /etc/samba/smbusers
tom = share
[global]
workgroup = SAMBA
security = user
username map = /etc/samba/smbusers //文件名和上面文件名一致,smbusers可自定义
4.2、配置/etc/samba/smb.conf文件中的共享目录
[tom] //共享目录
comment = share directory //说明
path = /aaa //共享目录路径
browseable = Yes //共享目录是否可见,yes可见
guest ok = Yes //是否所有人均可访问共享目录
writable = Yes //共享目录是否可写
write list = share //允许写的用户和组,组前加@
public = Yes //是否允许匿名用户访问,yes允许
5、可用testparm命令查看配置文件是否有语法错误
6、启动服务并开机自启,关闭防火墙和selinux
[[email protected] ~]# systemctl restart smb nmb
[[email protected] ~]# systemctl enable smb nmb
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
客户端:
1、查看samba服务器有哪些共享资源
[[email protected] ~]# smbclient -L 192.168.184.134 -U share
Enter SAMBA\share's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share Disk share directory
IPC$ IPC IPC Service (Samba 4.7.1)
tom Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
SAMBA
2、将服务端共享资源挂载到本地
[[email protected] ~]# mount -t cifs //192.168.184.134/tom /smb/ -o username=tom,password=666666
可写入fstab文件永久挂载
//192.168.184.134/tom /smb cifs defaults,username=share,password=666666 0 0
[[email protected] ~]# df -Th
//192.168.184.134/share cifs 17G 7.8G 9.3G 46% /smb
3、验证
客户机:
[[email protected] ~]# cd /smb/
[[email protected] smb]# ls
[[email protected] smb]# touch aaa
[[email protected] smb]# mkdir bbb
[[email protected] smb]# ls
aaa bbb
服务端:
[[email protected] ~]# cd /aaa/
[[email protected] aaa]# ls
aaa bbb
配置匿名共享
服务端:
1、如果系统默认没有samba服务,则安装
yum -y install samba*
2、创建要共享目录,并将目录权限改为777
[[email protected] ~]# mkdir /aaa
[[email protected] ~]# chmod 777 /aaa
3、修改配置文件
[global]
workgroup = SAMBA
security = user
map to guest = Bad User //添加此行
...
[share]
comment = share
path = /aaa
browseable = yes
guest ok = yes
writable = yes
public = yes
4、启动smb服务,并关闭防火墙、selinux
[[email protected] ~]# systemctl restart smb nmb
[[email protected] ~]# systemctl enable smb nmb
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
客户端:
1、查看samba服务器有哪些共享资源
[[email protected] ~]# smbclient -L 192.168.184.134 -U 'Bad User'
2、挂载
mount -o cifs //192.168.184.134/share /smb -o username='Bad User'
永久挂载
//192.168.184.134/share /smb cifs defaults,username='BadUser' 0 0
3、验证
服务端:
[[email protected] ~]# cd /aaa/
[[email protected] aaa]# ls
aaa bbb
[[email protected] aaa]# ll
总用量 0
-rwxr--r--. 1 nobody nobody 0 4月 16 18:08 aaa
drwxr-xr-x. 2 nobody nobody 6 4月 16 18:08 bbb
客户端:
[[email protected] ~]# cd /smb/
[[email protected] smb]# touch aaa
[[email protected] smb]# mkdir bbb
[[email protected] smb]# ls
aaa bbb
下一篇: scrapy与xpath的坑