samba


1. samba简介

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。

在此之前我们已经了解了NFS,NFS与samba一样,也是在网络中实现文件共享的一种实现,但不幸的是,其不支持windows平台,而本章要提到的samba是能够在任何支持SMB协议的主机之间共享文件的一种实现,当然也包括windows。

SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。

SMB协议是C/S型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。

Samba监听端口有:

TCP UDP
139 445 137 138

tcp端口相对应的服务是smbd服务,其作用是提供对服务器中文件、打印资源的共享访问。

udp端口相对应的服务是nmbd服务,其作用是提供基于NetBIOS主机名称的解析。

samba进程:

进程 对应
nmbd 对应netbios
smbd 对应cifs协议
winbindd + ldap 对应Windows AD活动目录

samba用户:

帐号 密码
都是系统用户 /etc/passwd Samba服务自有密码文件 通过smbpasswd -a USERNAME命令设置
//smbpasswd命令:
-a Sys_User //添加系统用户为samba用户并为其设置密码
-d //禁用用户帐号
-e //启用用户帐号
-x //删除用户帐号 [root@localhost ~]# yum -y install samba-*
[root@localhost ~]# useradd tom
[root@localhost ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.

Samba安全级别:

Samba服务器的安全级别有三个,分别是user,server,domain

安全级别 作用
user 基于本地的验证
server 由另一台指定的服务器对用户身份进行认证
domain 由域控进行身份验证

以前的samba版本支持的安全级别有四个,分别是share,user,server,domain

share是用来设置匿名访问的,但现在的版本已经不支持share了,但是还是可以实现匿名访问的

只是配置方式变了

samba配置文件:

  • /etc/samba/smb.conf(主配置文件)
samba三大组成 作用
[global] 全局配置,此处的设置项对整个samba服务器都有效
[homes] 宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。 当用户访问服务器中与自己用户名同名的共享目录时,通过验证后将会自动映射到该用户的宿主目录中
[printers] 打印机共享设置

常用配置文件参数:

参数 作用
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@localhost ~]# 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

2. samba访问

环境说明:

服务器IP 客户机IP
172.16.12.128 172.16.12.129
//交互式数据访问
smbclient -L HOST -U USERNAME
smbclient //SERVER/shared_name -U USERNAME //在客户机安装samba-client包
[root@localhost ~]# yum -y install samba-client //查看samba服务器有哪些共享资源
[root@localhost ~]# smbclient -L 172.16.12.128 -U tom
Enter SAMBA\tom's password:
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2] Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.6.2)
tom Disk Home Directories
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2] Server Comment
--------- ------- Workgroup Master
--------- ------- //交互式访问某共享资源
[root@localhost ~]# smbclient //172.16.12.128/tom -U tom
Enter SAMBA\tom's password:
Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> ls
. D 0 Sat Aug 4 13:52:14 2018
.. D 0 Sat Aug 4 12:59:42 2018
.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
aa N 0 Sat Aug 4 13:52:14 2018 17811456 blocks of size 1024. 16665456 blocks available
smb: \> quit //quit退出 //基于挂载的方式访问
mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD [root@localhost ~]# mkdir /opt/smb
[root@localhost ~]# mount -t cifs //172.16.12.128/tom /opt/smb/ -o username=tom,password=redhat
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17G 1.1G 16G 6% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
//172.16.12.128/tom 17G 1.1G 16G 7% /opt/smb

配置示例

3.搭建用户认证共享服务器

服务器IP 客户机IP
192.168.32.125 192.168.32.130

服务端配置示例

//关闭防火墙和selinux
[root@server ~]# systemctl disable --now firewalld
[root@server ~]# sed -ir '/^SELINUX/s/SELINUX.*/SELINUX=disable/' /etc/selinux/config
[root@server ~]# setenforce 0 //yum安装samba服务器
[root@server ~]# yum -y install samba-* //创建用户tom
[root@server ~]# useradd -M tom
//为tom用户创建一个smb密码,这里设置的密码为123456
[root@server ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom. //用户映射,将tom用户映射为lisi
[root@server ~]# echo 'tom = lisi' > /etc/samba/.smbusers
//在全局配置中添加用户映射的配置
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it. [global]
workgroup = SAMBA
security = user
username map = /etc/samba/.smbusers //添加这一行内容 //创建要共享的目录/opt/share_dir
[root@server ~]# mkdir /opt/share_dir
[root@server ~]# chown -R tom.tom /opt/share_dir/
[root@server ~]# ll /opt/
total 0
drwxr-xr-x. 2 tom tom 6 May 9 04:41 share_dir //在配置文件中配置共享参数
[root@server ~]# cat >> /etc/samba/smb.conf <<EOF
> [share_dir]
> comment = share_dir
> path = /opt/share_dir
> browseable = yes
> valid users = tom
> write list = tom
> EOF //测试配置文件是否有语法错误,以及显示最终生效的配置:使用testparm命令
[root@server ~]# testparm
[root@server ~]# tail -6 /etc/samba/smb.conf
[share_dir]
comment = share_dir //共享描述信息
path = /opt/share_dir //共享目录的路径
browseable = yes //共享是否可见
valid users = tom //设置可以访问的用户和组,组要加@,例如 valid users = root,@root
write list = tom //表示设置允许写的用户和组,组要加@,例如 valid users = root,@root //重新加载smb服务,并设施开机启动
[root@server ~]# systemctl restart smb
[root@server ~]# systemctl enable smb

客户端查看并挂载使用

//安装samba客户端
[root@client ~]# yum -y install samba-client //在客户端查看samba服务器有哪些共享资源
[root@client ~]# smbclient -L 192.168.32.125 -U lisi
Enter SAMBA\lisi's password: Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share_dir Disk share_dir
IPC$ IPC IPC Service (Samba 4.10.4)
tom Disk Home Directories
Reconnecting with SMB1 for workgroup listing. Server Comment
--------- ------- Workgroup Master
--------- ------- //将samba服务器的共享资源share_dir挂载到客户机本地的/mnt下
//基于挂载的方式访问
`mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD` [root@client ~]# mount -t cifs //192.168.32.125/share_dir /mnt -o username=lisi,password=123456
[root@client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 14M 473M 3% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.4G 16G 8% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.32.125/share_dir 17G 1.7G 16G 10% /mnt //永久挂载
[root@localhost ~]# umount /mnt/
在/etc/fstab中添加以下内容
//192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0
[root@localhost ~]# tail -1 /etc/fstab
//192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0 [root@localhost ~]# mount -a
[root@localhost ~]# tail -1 /etc/fstab
//192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 14M 473M 3% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.4G 16G 8% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.32.125/share_dir 17G 1.7G 16G 10% /mnt

验证

//在客户机上创建一些文件
[root@localhost ~]# cd /mnt/
[root@client mnt]# echo 'hello' > aaa
[root@client mnt]# mkdir test
[root@client mnt]# ls
aaa test //在服务器是验证
[root@server ~]# ls /opt/share_dir/
aaa test
[root@localhost ~]# ll /opt/share_dir/
total 4
-rwxr--r--. 1 tom tom 6 May 9 09:08 aaa //映射为了tom用户
drwxr-xr-x. 2 tom tom 6 May 9 09:09 test
[root@server ~]# cat /opt/share_dir/aaa
hello

在windows上验证

用映射的账号lisi登录

windos登录后会有缓存,在cmd命令提示符中输入以下命令可以清除缓存

net use * /d /y

4.搭建匿名用户共享服务器

服务器IP 客户机IP
192.168.32.125 192.168.32.130

服务端配置

//关闭防火墙和selinux
[root@server ~]# systemctl disable --now firewalld
[root@server ~]# sed -ir '/^SELINUX/s/SELINUX.*/SELINUX=disable/' /etc/selinux/config
[root@server ~]# setenforce 0 //yum安装samba服务器
[root@server ~]# yum -y install samba-* /在全局配置中添加如下内容:
[root@localhost ~]# vim /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it. [global]
workgroup = SAMBA
security = user
map to guest = Bad User //添加此行内容 //创建共享目录,让匿名用户有写权限
[root@server ~]# mkdir /opt/anon
[root@server ~]# chmod 777 /opt/anon/
[root@server ~]# ll -d /opt/anon/
drwxrwxrwx. 2 root root 6 May 9 20:07 /opt/anon/ //在配置文件中配置共享参数
[root@server ~]# cat >> /etc/samba/smb.conf <<EOF
> [anon]
> comment = Anonymous sharing
> browseable = yes
> path = /opt/ano
> public = yes
> guest ok = yes
> writeable = yes
> EOF [root@server ~]# tail -7 /etc/samba/smb.conf
[anon]
comment = Anonymous sharing
browseable = yes
path = /opt/anon
public = yes
guest ok = yes
writable = yes //重启服务
[root@server ~]# systemctl restart smb

客户端查看并挂载使用

//安装samba客户端
[root@client ~]# yum -y install samba-client
//创建挂载目录
[root@client ~]# mkdir /anon //查看samba服务器有哪些共享资源
[root@client ~]# smbclient -L 192.168.32.125
Enter SAMBA\root's password: Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
anony Disk Anonymous sharing
IPC$ IPC IPC Service (Samba 4.10.4)
Reconnecting with SMB1 for workgroup listing. Server Comment
--------- ------- Workgroup Master
--------- ------- [root@client ~]# smbclient -L 192.168.32.125 -U 'Bad User'
Enter SAMBA\Bad User's password: //直接回车 Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
anony Disk Anonymous sharing
IPC$ IPC IPC Service (Samba 4.10.4)
Reconnecting with SMB1 for workgroup listing. Server Comment
--------- ------- Workgroup Master
--------- ------- //将samba服务器的共享资源挂载到本地的/opt下,username可以是任意字符,但不能为空
[root@client ~]# mount -t cifs //192.168.32.125/anon /anon -o username='aasds'
[root@client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 14M 473M 3% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.4G 16G 8% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.32.125/anony 17G 1.7G 16G 10% /opt [root@client ~]# umount /opt/
[root@client ~]# mount -t cifs //192.168.32.125/anon /anon -o username='Bad User'
[root@client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 14M 473M 3% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.4G 16G 8% /
/dev/sda1 1014M 137M 878M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.32.125/anony 17G 1.7G 16G 10% /opt //永久挂载,写入配置文件
[root@client ~]# umount /anon //在/etc/fstab中添加以下内容
//192.168.32.125/anon /anon cifs defaults,username=baduser 0 0 [root@client ~]# tail -1 /etc/fstab
//192.168.32.125/anon /anon cifs defaults,username=baduser 0 0
[root@client ~]# mount -a

验证

//在客户端的共享目录下创建一些文件
[root@client ~]# ls /anon/
asd bbb //在服务端查看
[root@server ~]# ll /opt/anon/
total 4
-rwxr--r--. 1 nobody nobody 12 May 9 09:14 asd //属主属组是匿名用户
drwxr-xr-x. 2 nobody nobody 6 May 9 09:14 bbb [root@server ~]# cat /opt/anon/asd
hello world

直接就进入了,没有输入密码

也能创建文件

samba服务及配置的更多相关文章

  1. Linux samba 服务的配置

    今天有个学生问我 samba 服务怎么配置,所以晚上特意研究一下怎么配置这个服务. 过程如下: sudo apt-get install samba samba-common // 安装 samba ...

  2. Samba服务的配置总结

    之前介绍了Linux下Samba服务器部署,这里简单总结下Samba服务参数的配置说明: Samba服务的主配置文件是smb.conf,默认在/etc/samba/目录下.smb.conf含有多个段, ...

  3. centos安装samba服务和配置

    1.samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享 ...

  4. ubuntu 10.04 下 samba 服务的配置

    1. 安装 $ sudo apt-get install samba $ sudo apt-get install samba-common $ sudo apt-get install samb . ...

  5. Centos samba 服务配置

    1背景 转到Linux有段时间了,vim操作还不能应对工程代码,之前一直都是Gnome桌面 + Clion 作开发环境,无奈在服务器上没有这样的环境, 看同事是(Windows)Source Insi ...

  6. linux基础-第十七单元 Samba服务

    Samba的功能 Samba的安装 Samba服务的启动.停止.重启 Samba服务的配置 Samba服务的主配置文件 samba服务器配置实例 Samba客户端设置 windows客户端 Linux ...

  7. 如何搭建samba服务?

    为了日后便于查询,本文所涉及到的所有命令集合如下: chkconfig iptables off #关闭防火墙命令 在Centos7中使用的是chkconfig firewalld off seten ...

  8. 第十七单元 Samba服务

    Samba的功能 Samba的安装 Samba服务的启动.停止.重启 Samba服务的配置 Samba服务的主配置文件 samba服务器配置实例 Samba客户端设置 windows客户端 Linux ...

  9. 利用Linux的Samba服务模拟NT域

    利用Linux的Samba服务模拟NT域 Samba是一个与Windows NT具有相同协议的软件包.我们可以利用Samba服务来模拟 Windows NT域,使用户从Windows计算机上直接使用一 ...

随机推荐

  1. 图文详解Prometheus监控+Grafana+Alertmanager告警安装使用

    一:前言 一个服务上线了后,你想知道这个服务是否可用,需要监控.假如线上出故障了,你要先于顾客感知错误,你需要监控.还有对数据库,服务器的监控,等等各层面的监控. 近年来,微服务架构的流行,服务数越来 ...

  2. requests库入门笔记1

    1.使用requests库发送请求,fiddler无法抓到包:使用浏览器请求相同的url,可以抓到包 在请求参数中添加 proxies参数,如下: proxies = { 'http': 'http: ...

  3. Alink漫谈(十) :线性回归实现 之 数据预处理

    Alink漫谈(十) :线性回归实现 之 数据预处理 目录 Alink漫谈(十) :线性回归实现 之 数据预处理 0x00 摘要 0x01 概念 1.1 线性回归 1.2 优化模型 1.3 损失函数& ...

  4. djangorestframework学习1-通过HyperlinkedModelSerializer,ModelViewSet,routers编写第一个接口

    前提首先安装了django,安装方式:pip install django 1. djangorestftamework安装: pip install djangorestframework 2. 创 ...

  5. JVM 专题二十二:垃圾回收(六)垃圾回收器 (三)

    4. GC日志分析 4.1 日志分析 通过阅读GC日志,我们可以了解Java虚拟机内存分配与回收策略. 内存分配与垃圾回收的参数列表-XX:+PrintGC:输出GC日志.类似-verbose: gc ...

  6. 机器学习实战基础(十):sklearn中的数据预处理和特征工程(三) 数据预处理 Preprocessing & Impute 之 缺失值

    缺失值 机器学习和数据挖掘中所使用的数据,永远不可能是完美的.很多特征,对于分析和建模来说意义非凡,但对于实际收集数据的人却不是如此,因此数据挖掘之中,常常会有重要的字段缺失值很多,但又不能舍弃字段的 ...

  7. Django之实现登录随机验证码

    登录验证码是每个网站登录时的基本标配,网上也有很多相应的文章, 但是从生成验证码到 应用到自己的网站上的全步骤,并没有看到很多, 为了节约大家的时间,我把整体步骤写下来, 即拿即用哈 1. 生成随机验 ...

  8. 一文读懂Java中的动态代理

    从代理模式说起 回顾前文: 设计模式系列之代理模式(Proxy Pattern) 要读懂动态代理,应从代理模式说起.而实现代理模式,常见有下面两种实现: (1) 代理类关联目标对象,实现目标对象实现的 ...

  9. 太实用了!自己动手写软件——GUI编程

    这几天我有一个想法就是将我之前做测试写的一些协议脚本(如:ssh.FTP.SMTP.MySQL.Oracle等)综合在一起做一个密码PJ器,这么多的协议放在一起,每个协议都有自己特殊的参数,如果还是和 ...

  10. 性能测试必备知识(2)- 查看 Linux 的 CPU 相关信息

    做性能测试的必备知识系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1806772.html 查看系统 CPU 信息 cat /proc/ ...