samba服务
samba简介
samba服务可以实现跨平台(linux和unix windows)共享文件。
SMB(server message block信息服务块)。在局域网上共享文件的一种通信协议。
SMB协议是C/S协议。客户机访问服务机的共享文件系统。
samba监听端口有:
TCP | UTP |
---|---|
139,445 | 137,138 |
- tcp端口对应smbd服务,提供服务器共享文件和资源的访问
- udp端口对应nmdb服务,用于提供NetBIOS主机名的解析
samba进程有:
进程 | 对应协议 |
---|---|
nmbd | netbios |
smbd | cifs |
winbindd+ladp | WindowsAD活动目录 |
samba账户
账户 | 密码 |
---|---|
系统真实存在的用户 | 通过samba服务cmd命令设置的密码 |
smbpasswd命令:
[root@node2 ~]# yum -y install samba-*
//常见选项
[root@node2 ~]# smbpasswd -h
-a add user
-d disable user
-e enable user
-n set no password
-W use stdin ldap admin password
-w PASSWORD ldap admin password
-x delete user
//添加samba账户
[root@node2 ~]# useradd test #创建新用户或使用已有用户
[root@node2 ~]# smbpasswd -a test
New SMB password:
Retype new SMB password:
Added user test.
samba配置文件
-
/etc/samba/smb.conf
[global]#全局配置,作用于整个samba服务器 workgroup = SAMBA security = user #安全级别user,server,domain passdb backend = tdbsam printing = cups printcap name = cups load printers = yes cups options = raw [homes]#宿主用户目录设置,设置默认linux目录共享,客户端请求访问通过后自动映射到该目录 comment = Home Directories valid users = %S, %D%w%S#有效用户,输入存在用户名 browseable = No #可浏览 read only = No #只读 inherit acls = Yes # [printers]#打印机设置 comment = All Printers path = /var/tmp printable = Yes create mask = 0600 browseable = No [print$]#目录相关配置 comment = Printer Drivers path = /var/lib/samba/drivers write list = @printadmin root force group = @printadmin create mask = 0664 directory mask = 0775
Samba安全级别
安全级别 | 作用 |
---|---|
user | 基于本地的验证(本地账户) |
server | 由另一台指定的服务器对用户身份进行认证(不常用) |
domain | 由域控进行身份验证 |
//配置文件语法验证
[root@node2 ~]# testparm /etc/samba/smb.conf
samba访问
环境准备:
服务的ip | 客户机ip |
---|---|
192.168.94.143(node2) | 192.168.94.142(node0) |
准备工作:
//启动服务器端服务,关闭selinux和防火墙
[root@node2 ~]# systemctl restart smb.service
[root@node2 ~]# systemctl restart nmb.service
//客户端安装samba客户端
[root@node0 ~]# yum -y install samba-client
//查看共享目录
[root@node0 ~]# smbclient -L 192.168.94.143 -U test
Enter SAMBA est's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.12.3)
test Disk Home Directories
SMB1 disabled -- no workgroup available
访问
- 交互式访问
//交互式访问。无法直接访问
//验证是否访问的test家目录
[test@node2 ~]$ touch test #家目录创建空文件test
[root@node0 ~]# smbclient //192.168.94.143/test -U test
Enter SAMBA est's password:
Try "help" to get a list of possible commands. #通过help命令查找
smb: > ls test
test N 0 Sat Apr 3 13:42:39 2021
17811456 blocks of size 1024. 16091996 blocks available
- 基于挂载的访问
//将test家目录写入挂载点信息
[root@node0 ~]# echo "//192.168.94.143:/test /opt cifs defaults,username=test,password=123456 0 0" > /etc/fstab
//重新读取配置文件
[root@node0 ~]# mount -a
//成功挂载,正常读写文件
[root@node0 ~]# df -h|grep test
//192.168.94.143:/test 17G 1.7G 16G 10% /opt
[root@node0 opt]# touch a
[root@node0 opt]# ll
total 0
-rwxr-xr-x 1 root root 0 Apr 3 14:58 a
-rwxr-xr-x 1 root root 0 Apr 3 13:42 test
//指定公共目录共享
[root@node2 ~]# cat /etc/samba/smb.conf.example
... ...#按照下面格式写
; [public]
; comment = Public Stuff
; path = /home/samba
; public = yes
; writable = no
; printable = no
; write list = +staff