Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。
SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通讯协议,它为局域网内的不一样计算机之间提供文件及打印机等资源的共享服务。
SMB协议是客户机/服务器型协议,客户机经过该协议能够访问服务器上的共享文件系统、打印机及其余资源。
samba监听端口linux
TCP——13九、445,tcp端口相对应的服务是smbd服务,其做用是提供服务器中文件、打印资源的共享访问;
UDP——13七、138,udp端口相对应的服务是nmbd服务,起做用是提供基于NetBOIS主机名称的解析。ios
samba进程:数据库
进程 | 对应 |
---|---|
nmbd | 对应netbios |
smbd | 对应cifs协议 |
winbindd + ldap | 对应Windows AD 活动目录 |
samba用户:vim
账号 密码 安全
都是系统用户/etc/passwd Samba服务自有密码文件经过smbpasswd -a USERNAME命令设置bash
账号 | 密码 |
---|---|
都是系统用户/etc/passwd | Samba服务自有密码文件经过smbpasswd -a USERNAME命令设置 |
1. smbpasswd :服务器
**重点:** ** -a 添加用户 -d 禁用用户 -e 启用用户 -x 删除用户 只是把smb里面的这个用户删除,不是把普通用户删掉 [root@wlw ~]# yum install -y samba* [root@wlw ~]# useradd www [root@wlw ~]# id www uid=1001(www) gid=1002(www) 组=1002(www) [root@wlw ~]# smbpasswd -a www New SMB password: Retype new SMB password: Added user www.**
Samba安全级别:
Samba服务器的安全级别有三个,分别是user,server,domainapp
安全级别 做用dom
user 基于本地的验证tcp
server 由另外一台指定的服务器对用户身份进行认证
domain 由域控进行身份验证
安全级别 | 做用 |
---|---|
user | 基于本地的验证 |
server | 由另外一台指定的服务器对用户身份进行认证 |
domain | 由域控进行身份验证 |
之前的samba版本支持的安全级别有四个,分别是share,user,server,domain
share是用来设置匿名访问的,但如今的版本已经不支持share了,可是仍是能够实现匿名访问的 只是配置方式变了
samba配置文件:
/etc/samba/smb.conf(主配置文件)
samba三大组成 做用
[global] 全局配置,此处的设置项对整个samba服务器都有效
[homes] 宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。当用户访问服务器中与本身用户名同名的共享目录时,经过验证后将会自动映射到该用户的宿主目录中
[printers] 打印机共享设置
samba三大组成 | 做用 |
---|---|
[global] | 全局配置,此处的设置项对整个samba服务器都有效 |
[homes] | 宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。当用户访问服务器中与本身用户名同名的共享目录时,经过验证后将会自动映射到该用户的宿主目录中 |
[printers] | 打印机共享设置 |
经常使用配置文件参数:
都在两个文件里
[root@wlw ~]# vim /etc/samba/smb.conf smb.conf smb.conf.example [root@wlw ~]# vim /etc/samba/smb.conf.example
参数 | 做用 | |
---|---|---|
workgroup | 表示设置工做组名称 | |
server string | 表示描述samba服务器 | |
security | 表示设置安全级别,其值可为share、user、server、domain | |
passdb backend | 表示设置共享账户文件的类型,其值可为tdbsam(tdb数据库文件)、ldapsam | (LDAP目录认证)、smbpasswd(兼容旧版本samba密码文件) |
comment | 表示设置对应共享目录的注释,说明信息,即文件共享名 | |
browseable | 表示设置共享是否可见 | |
writable | 表示设置目录是否可写 | |
path | 表示共享目录的路径 | |
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命令 [root@wlw ~]# 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$]" Processing section "[wlw]" set_variable_helper(yes #是否全部人浏览): value is not boolean! Error loading services.
测试配置文件是否有语法错误,以及显示最终生效的配置:使用testparm命令
环境说明:
服务器IP | 客户机IP |
---|---|
192.168.66.128 | 192.168.66.130 |
//交互式数据访问
smbclient -L HOST -U USERNAME
smbclient //SERVER/shared_name -U USERNAME
//在客户端安装samba-client包
[root@wan ~]# yum install -y samba-client #客户端只须要下个 samba-客户端就行
//查看samba服务器有哪些资源共享
[root@wan ~]# smbclient -L 192.168.66.128 -U www 或者[root@wan ~]# smbclient //192.168.66.128/www -U www #这是两种方法 Enter SAMBA\www's password: Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers wlw Disk IPC$ IPC IPC Service (Samba 4.6.2) www Disk Home Directories Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] Server Comment --------- ------- Workgroup Master --------- ------- SAMBA WLW [root@wan ~]# smbclient //192.168.66.128/www -U www Enter SAMBA\www's password: Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] smb: \> ls . D 0 Thu Mar 26 15:12:13 2020 .. D 0 Thu Mar 26 15:12:13 2020 .bash_logout H 18 Wed Mar 8 00:13:45 2017 .bash_profile H 193 Wed Mar 8 00:13:45 2017 .bashrc H 231 Wed Mar 8 00:13:45 2017 17811456 blocks of size 1024. 15534504 blocks available smb: \> quit
基于挂载的方式访问
mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD
[root@wan ~]# mkdir /w [root@wan ~]# mount -t cifs //192.168.66.128/www /w -o username=www,password=w [root@wan ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/rhel-root 17G 1.7G 16G 10% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sr0 3.8G 3.8G 0 100% /mnt //192.168.66.128/www 17G 2.2G 15G 13% /w
/etc/fstab写法
//SERVER/shared_name /挂载到本地的什么目录 cifs defaults,username=USERNAME,password=PASSWORD 0 0
[root@wan ~]# vim /etc/fstab //192.168.66.128/www /w cifs defaults,username=www,password=w 0 0
服务端: [root@wlw ~]# systemctl stop firewalld [root@wlw ~]# systemctl disable firewalld [root@wlw ~]# vim /etc/selinux/config 把里面的改为SELINUX=disabled [root@wlw ~]# setenforce 0 [root@wlw ~]# yum install -y samba* [root@wlw ~]# systemctl start smb nmb [root@wlw ~]# ss -antl //看有没有139和445这两个samba的端口 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:139 *:* LISTEN 0 64 *:41771 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:20048 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 50 *:445 *:* LISTEN 0 64 *:2049 *:* LISTEN 0 128 *:56458 *:* LISTEN 0 50 :::139 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::20048 :::* LISTEN 0 128 :::41940 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 50 :::445 :::* LISTEN 0 64 :::2049 :::* LISTEN 0 64 :::43753 :::* [root@wlw ~]# mkdir /wan [root@wlw ~]# useradd www [root@wlw ~]# vim /etc/samba/smb.conf #在最下面本身添加 [www] path = /wan #共享什么文件 guest ok = yes #是否全部人浏览 browseable = yes #打开读的功能 writable = yes #打开写的功能 write list = www #指定哪一个用户能写 hosts allow =192.168.66.130 #只容许这个ip访问 [root@wlw ~]# smbpasswd -a www New SMB password: Retype new SMB password: Added user www. #出现Added user www.就表明成功了
[root@wan ~]# systemctl stop firewalld [root@wan ~]# systemctl disable firewalld [root@wan ~]# vim /etc/selinux/config 把里面的改为SELINUX=disabled [root@wan ~]# setenforce 0 [root@wan ~]# yum install -y samba-client #客户端只须要下个 samba-客户端就行 [root@wan ~]# smbclient -L 192.168.66.128 -U www 或者[root@wan ~]# smbclient //192.168.66.128/www -U www #这是两种方法 Enter SAMBA\www's password: Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers wlw Disk IPC$ IPC IPC Service (Samba 4.6.2) www Disk Home Directories Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] Server Comment --------- ------- Workgroup Master --------- ------- SAMBA WLW [root@wan ~]# smbclient //192.168.66.128/www -U www Enter SAMBA\www's password: Domain=[WLW] OS=[Windows 6.1] Server=[Samba 4.6.2] smb: \> ls . D 0 Thu Mar 26 15:12:13 2020 .. D 0 Thu Mar 26 15:12:13 2020 .bash_logout H 18 Wed Mar 8 00:13:45 2017 .bash_profile H 193 Wed Mar 8 00:13:45 2017 .bashrc H 231 Wed Mar 8 00:13:45 2017 17811456 blocks of size 1024. 15534504 blocks available smb: \> quit [root@wan ~]# mkdir /w [root@wan ~]# mount -t cifs //192.168.66.128/www /w -o username=www,password=w [root@wan ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/rhel-root 17G 1.7G 16G 10% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sr0 3.8G 3.8G 0 100% /mnt //192.168.66.128/www 17G 2.2G 15G 13% /w [root@wan ~]# vim /etc/fstab //192.168.66.128/www /w cifs defaults,username=www,password=w 0 0 [root@wan ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/rhel-root 17G 1.7G 16G 10% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sr0 3.8G 3.8G 0 100% /mnt [root@wan ~]# mount -a mount.nfs: access denied by server while mounting 192.168.66.128:/nfs [root@wan ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/rhel-root 17G 1.7G 16G 10% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sr0 3.8G 3.8G 0 100% /mnt //192.168.66.128/www 17G 2.2G 15G 13% /w
[root@wlw ~]# yum install -y samba* [root@wlw ~]# mkdir /opt/zxc [root@wlw ~]# chmod 777 /opt/zxc [root@wlw ~]# ll /opt/ 总用量 6 drwxrwxrwx. 2 root root 6 3月 26 18:25 zxc [root@wlw ~]# cat >> /etc/samba/smb.conf <<EOF > [wan] > comment = wan > path = /opt/zxc > browseable = yes > guest ok = yes > writable = yes > public = yes > EOF [root@wlw ~]# tail -7 /etc/samba/smb.conf [wan] comment = wan path = /opt/zxc browseable = yes guest ok = yes writable = yes public = yes [root@wlw ~]# systemctl start smb
客户端:
[root@wan ~]# smbclient -L 192.168.66.128 -U 'Bad User' Enter SAMBA\Bad User's password: OS=[Windows 6.1] Server=[Samba 4.6.2] Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers wan Disk wan IPC$ IPC IPC Service (Samba 4.6.2) OS=[Windows 6.1] Server=[Samba 4.6.2] Server Comment --------- ------- Workgroup Master --------- ------- SAMBA WLW [root@wan ~]# mount -t cifs //192.168.66.128/wan /opt/wan/ -o username='Bad User' [root@wan ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/rhel-root 17G 1.7G 16G 10% / devtmpfs 901M 0 901M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sr0 3.8G 3.8G 0 100% /mnt //192.168.66.128/wan 17G 2.2G 15G 13% /opt/wan