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命令设置
  1. //smbpasswd命令:
  2. -a Sys_User //添加系统用户为samba用户并为其设置密码
  3. -d //禁用用户帐号
  4. -e //启用用户帐号
  5. -x //删除用户帐号
  6. [root@localhost ~]# yum -y install samba-*
  7. [root@localhost ~]# useradd tom
  8. [root@localhost ~]# smbpasswd -a tom
  9. New SMB password:
  10. Retype new SMB password:
  11. 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 表示设置是否为打印机
  1. //测试配置文件是否有语法错误,以及显示最终生效的配置:使用testparm命令
  2. [root@localhost ~]# testparm
  3. Load smb config files from /etc/samba/smb.conf
  4. rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
  5. Processing section "[homes]"
  6. Processing section "[printers]"
  7. Processing section "[print$]"
  8. Loaded services file OK.
  9. Server role: ROLE_STANDALONE

2. samba访问

环境说明:

服务器IP 客户机IP
172.16.12.128 172.16.12.129
  1. //交互式数据访问
  2. smbclient -L HOST -U USERNAME
  3. smbclient //SERVER/shared_name -U USERNAME
  4. //在客户机安装samba-client包
  5. [root@localhost ~]# yum -y install samba-client
  6. //查看samba服务器有哪些共享资源
  7. [root@localhost ~]# smbclient -L 172.16.12.128 -U tom
  8. Enter SAMBA\tom's password:
  9. Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]
  10. Sharename Type Comment
  11. --------- ---- -------
  12. print$ Disk Printer Drivers
  13. IPC$ IPC IPC Service (Samba 4.6.2)
  14. tom Disk Home Directories
  15. Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]
  16. Server Comment
  17. --------- -------
  18. Workgroup Master
  19. --------- -------
  20. //交互式访问某共享资源
  21. [root@localhost ~]# smbclient //172.16.12.128/tom -U tom
  22. Enter SAMBA\tom's password:
  23. Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2]
  24. smb: \> ls
  25. . D 0 Sat Aug 4 13:52:14 2018
  26. .. D 0 Sat Aug 4 12:59:42 2018
  27. .bash_logout H 18 Wed Mar 8 00:13:45 2017
  28. .bash_profile H 193 Wed Mar 8 00:13:45 2017
  29. .bashrc H 231 Wed Mar 8 00:13:45 2017
  30. aa N 0 Sat Aug 4 13:52:14 2018
  31. 17811456 blocks of size 1024. 16665456 blocks available
  32. smb: \> quit //quit退出
  33. //基于挂载的方式访问
  34. mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD
  35. [root@localhost ~]# mkdir /opt/smb
  36. [root@localhost ~]# mount -t cifs //172.16.12.128/tom /opt/smb/ -o username=tom,password=redhat
  37. [root@localhost ~]# df -h
  38. 文件系统 容量 已用 可用 已用% 挂载点
  39. /dev/mapper/rhel-root 17G 1.1G 16G 6% /
  40. devtmpfs 478M 0 478M 0% /dev
  41. tmpfs 489M 0 489M 0% /dev/shm
  42. tmpfs 489M 6.8M 482M 2% /run
  43. tmpfs 489M 0 489M 0% /sys/fs/cgroup
  44. /dev/sda1 1014M 125M 890M 13% /boot
  45. tmpfs 98M 0 98M 0% /run/user/0
  46. /dev/sr0 3.8G 3.8G 0 100% /mnt
  47. //172.16.12.128/tom 17G 1.1G 16G 7% /opt/smb

配置示例

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

服务器IP 客户机IP
192.168.32.125 192.168.32.130

服务端配置示例

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

客户端查看并挂载使用

  1. //安装samba客户端
  2. [root@client ~]# yum -y install samba-client
  3. //在客户端查看samba服务器有哪些共享资源
  4. [root@client ~]# smbclient -L 192.168.32.125 -U lisi
  5. Enter SAMBA\lisi's password:
  6. Sharename Type Comment
  7. --------- ---- -------
  8. print$ Disk Printer Drivers
  9. share_dir Disk share_dir
  10. IPC$ IPC IPC Service (Samba 4.10.4)
  11. tom Disk Home Directories
  12. Reconnecting with SMB1 for workgroup listing.
  13. Server Comment
  14. --------- -------
  15. Workgroup Master
  16. --------- -------
  17. //将samba服务器的共享资源share_dir挂载到客户机本地的/mnt下
  18. //基于挂载的方式访问
  19. `mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o username=USERNAME,password=PASSWORD`
  20. [root@client ~]# mount -t cifs //192.168.32.125/share_dir /mnt -o username=lisi,password=123456
  21. [root@client ~]# df -h
  22. Filesystem Size Used Avail Use% Mounted on
  23. devtmpfs 475M 0 475M 0% /dev
  24. tmpfs 487M 0 487M 0% /dev/shm
  25. tmpfs 487M 14M 473M 3% /run
  26. tmpfs 487M 0 487M 0% /sys/fs/cgroup
  27. /dev/mapper/centos-root 17G 1.4G 16G 8% /
  28. /dev/sda1 1014M 137M 878M 14% /boot
  29. tmpfs 98M 0 98M 0% /run/user/0
  30. //192.168.32.125/share_dir 17G 1.7G 16G 10% /mnt
  31. //永久挂载
  32. [root@localhost ~]# umount /mnt/
  33. 在/etc/fstab中添加以下内容
  34. //192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0
  35. [root@localhost ~]# tail -1 /etc/fstab
  36. //192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0
  37. [root@localhost ~]# mount -a
  38. [root@localhost ~]# tail -1 /etc/fstab
  39. //192.168.32.125/share_dir /mnt cifs defaults,username=lisi,password=123456 0 0
  40. [root@localhost ~]# df -h
  41. Filesystem Size Used Avail Use% Mounted on
  42. devtmpfs 475M 0 475M 0% /dev
  43. tmpfs 487M 0 487M 0% /dev/shm
  44. tmpfs 487M 14M 473M 3% /run
  45. tmpfs 487M 0 487M 0% /sys/fs/cgroup
  46. /dev/mapper/centos-root 17G 1.4G 16G 8% /
  47. /dev/sda1 1014M 137M 878M 14% /boot
  48. tmpfs 98M 0 98M 0% /run/user/0
  49. //192.168.32.125/share_dir 17G 1.7G 16G 10% /mnt

验证

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

在windows上验证

用映射的账号lisi登录

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

net use * /d /y

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

服务器IP 客户机IP
192.168.32.125 192.168.32.130

服务端配置

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

客户端查看并挂载使用

  1. //安装samba客户端
  2. [root@client ~]# yum -y install samba-client
  3. //创建挂载目录
  4. [root@client ~]# mkdir /anon
  5. //查看samba服务器有哪些共享资源
  6. [root@client ~]# smbclient -L 192.168.32.125
  7. Enter SAMBA\root's password:
  8. Sharename Type Comment
  9. --------- ---- -------
  10. print$ Disk Printer Drivers
  11. anony Disk Anonymous sharing
  12. IPC$ IPC IPC Service (Samba 4.10.4)
  13. Reconnecting with SMB1 for workgroup listing.
  14. Server Comment
  15. --------- -------
  16. Workgroup Master
  17. --------- -------
  18. [root@client ~]# smbclient -L 192.168.32.125 -U 'Bad User'
  19. Enter SAMBA\Bad User's password: //直接回车
  20. Sharename Type Comment
  21. --------- ---- -------
  22. print$ Disk Printer Drivers
  23. anony Disk Anonymous sharing
  24. IPC$ IPC IPC Service (Samba 4.10.4)
  25. Reconnecting with SMB1 for workgroup listing.
  26. Server Comment
  27. --------- -------
  28. Workgroup Master
  29. --------- -------
  30. //将samba服务器的共享资源挂载到本地的/opt下,username可以是任意字符,但不能为空
  31. [root@client ~]# mount -t cifs //192.168.32.125/anon /anon -o username='aasds'
  32. [root@client ~]# df -h
  33. Filesystem Size Used Avail Use% Mounted on
  34. devtmpfs 475M 0 475M 0% /dev
  35. tmpfs 487M 0 487M 0% /dev/shm
  36. tmpfs 487M 14M 473M 3% /run
  37. tmpfs 487M 0 487M 0% /sys/fs/cgroup
  38. /dev/mapper/centos-root 17G 1.4G 16G 8% /
  39. /dev/sda1 1014M 137M 878M 14% /boot
  40. tmpfs 98M 0 98M 0% /run/user/0
  41. //192.168.32.125/anony 17G 1.7G 16G 10% /opt
  42. [root@client ~]# umount /opt/
  43. [root@client ~]# mount -t cifs //192.168.32.125/anon /anon -o username='Bad User'
  44. [root@client ~]# df -h
  45. Filesystem Size Used Avail Use% Mounted on
  46. devtmpfs 475M 0 475M 0% /dev
  47. tmpfs 487M 0 487M 0% /dev/shm
  48. tmpfs 487M 14M 473M 3% /run
  49. tmpfs 487M 0 487M 0% /sys/fs/cgroup
  50. /dev/mapper/centos-root 17G 1.4G 16G 8% /
  51. /dev/sda1 1014M 137M 878M 14% /boot
  52. tmpfs 98M 0 98M 0% /run/user/0
  53. //192.168.32.125/anony 17G 1.7G 16G 10% /opt
  54. //永久挂载,写入配置文件
  55. [root@client ~]# umount /anon
  56. //在/etc/fstab中添加以下内容
  57. //192.168.32.125/anon /anon cifs defaults,username=baduser 0 0
  58. [root@client ~]# tail -1 /etc/fstab
  59. //192.168.32.125/anon /anon cifs defaults,username=baduser 0 0
  60. [root@client ~]# mount -a

验证

  1. //在客户端的共享目录下创建一些文件
  2. [root@client ~]# ls /anon/
  3. asd bbb
  4. //在服务端查看
  5. [root@server ~]# ll /opt/anon/
  6. total 4
  7. -rwxr--r--. 1 nobody nobody 12 May 9 09:14 asd //属主属组是匿名用户
  8. drwxr-xr-x. 2 nobody nobody 6 May 9 09:14 bbb
  9. [root@server ~]# cat /opt/anon/asd
  10. 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. c++随机生成树

    分析 当我们写完一道题,自认为它是正解,但是交上去却WA的时候,我们该怎么办呢 当我们已经想出了一道的暴力解法,又想出了一种比较优秀的解法,但不知道这种解法对错与否,我们该怎么办呢 答案显然是对拍 对 ...

  2. Java实现上传文件到指定服务器指定目录(ChannelSftp实现文件上传下载)

    package com.tianyang.task.utils; import java.io.File;import java.io.FileInputStream;import java.io.I ...

  3. day27 面向对象

    day27 面向对象 目录 day27 面向对象 一.面相对象介绍 1 什么是对象 2 类于对象 二.实现面向对象编程 1 先定义类 2 属性访问 2.1 调用dict方法 2.2 类.属性 3 调用 ...

  4. 数据可视化之DAX篇(十四)DAX函数:RELATED和RELATEDTABLE

    https://zhuanlan.zhihu.com/p/64421378 Excel中知名度最高的函数当属VLOOKUP,它的确很有用,可以在两个表之间进行匹配数据,使工作效率大大提升,虽然它也有很 ...

  5. Odoo13之在tree视图左上角添加自定义按钮

    前言 首先展示效果图,如下图所示,在资产设备模块tree视图的左上角添加了一个同步资产的按钮. 要完成按钮的添加,分为四步,分别是: 1.编写xml文件,找到相关模型tree视图,并给模型tree视图 ...

  6. SpringBoot学习笔记(十七:异步调用)

    @ 目录 1.@EnableAsync 2.@Async 2.1.无返回值的异步方法 2.1.有返回值的异步方法 3. Executor 3.1.方法级别重写Executor 3.2.应用级别重写Ex ...

  7. 用python批量处理Excel表格,处理结果又快又好,做办公室最靓的那个仔

    使用python批量处理Excel数据     让你根据Excel上所有人的身份证号码,提取出公司员工的生日 让你每个月都将公司所有人的考勤数据整理一下 类似这样的格式化的重复操作,你还在每次都使用的 ...

  8. Ethical Hacking - POST EXPLOITATION(2)

    MAINTAINING ACCESS - Methods 1. Using a veil-evasion Rev_http_service Rev_tcp_service Use it instead ...

  9. OSCP Learning Notes - Exploit(1)

    Gaining Root with Metasploit Platform: Kali Linux, Kioptrix Level 1 1. Find the IP of Kioptirx nmap ...

  10. 微信小程序开发部署

    一.开发准备 1,想要开发微信小程序,必须要有一个AppId,如果没有可以去注册一个.   https://mp.weixin.qq.com/进入注册页面,点击上方注册.   2,点击选择“小程序”出 ...