Samba服务简述

2019-05-30  本文已影响0人  SRE1

Samba

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

CIFS: Common Internet File System通用网络文件系统,是windows主机之间共享的协议,samba实现了这个协议,所以可以实现wondows与linux之间的文件共享服务。

监听的端口有:tcp/137 udp/137 tcp/139 udp/139
137:是实现NetBIOS协议,实现解析windows之间主机名的解析,实现在windows的网上邻居可以看到Linux的主机名。
139:是实现cifs协议

Samba服务器的搭建

安装samba服务

[root@samba ~]# yum install -y samba
.......
Installed:
  samba.x86_64 0:4.6.2-12.el7_4                                                                                                                           

Dependency Installed:
  avahi-libs.x86_64 0:0.6.31-17.el7              cups-libs.x86_64 1:1.6.3-29.el7                     libldb.x86_64 0:1.1.29-1.el7                        
  libtalloc.x86_64 0:2.1.9-1.el7                 libtdb.x86_64 0:1.3.12-2.el7                        libtevent.x86_64 0:0.9.31-2.el7_4                   
  libwbclient.x86_64 0:4.6.2-12.el7_4            pytalloc.x86_64 0:2.1.9-1.el7                       samba-client-libs.x86_64 0:4.6.2-12.el7_4           
  samba-common.noarch 0:4.6.2-12.el7_4           samba-common-libs.x86_64 0:4.6.2-12.el7_4           samba-common-tools.x86_64 0:4.6.2-12.el7_4          
  samba-libs.x86_64 0:4.6.2-12.el7_4            

Complete!

samba服务的程序及配置文件

主配置文件:/etc/samba/smb.conf
主程序:nmbd:NetBIOS name server 及smbd:SMB/CIFS services
Unit文件:smb.service和nmb.service

主配置文件/etc/samba/smb.conf由samba-common程序包提供,主要包括以下几个部分:

Global Settings
NetworkRelated Options
Logging Options
Standalone Server Options
Domain Members Options
Domain Controller Options
Browser Control Options
Printing Options
File System Options
Share Definitions

全局配置
[global]
    workgroup = MYGROUP  #工作组名
    server string = Samba Server Version %v  #服务器信息介绍
    netbios name = MYSERVER  #用netbios名来指定服务
    interfaces = [interface1 interface2...|address1 address2...]  #用于让samba服务监听多个网络接口或IP
    hosts allow = [address1 address2...]  #指定允许访问的主机IP
    log file = /var/log/samba/log.%m  #指定日志存放路径,%m为来访的主机名
    max log size = 50  #定义日志文件最大容量为50K
    security=user  #设置samba服务的安全认证方式为user
    passdb backend=tdbsam  #定义用户后台的类型为tdbsam,其他类型还有smbpasswd、ldapsam
    load prints = yes  #  #设置是否共享打印机
    cups options = raw  #打印机选项

共享文件系统配置

[homes]:为每个samba用户定义其是否能够通过samba服务访问自己的家目录;
[printers]:定义打印服务;
[shared_fs]:定义共享的文件系统;
常用指令

comment=STRING:注释;
path=/PATH/TO/FILENAME:当前共享所映射的文件系统路径;
browseable=YES:是否可浏览,指是否可被用户查看;
guest ok=YES:是否允许来宾账号访问;
browseable = No:是否公开目录
writable=YES:是否可写;
read only = no|yes :是否为只读
write list=/PATH/TO/user_list:拥有写权限的用户列表;

此处对/etc/samba/smb.conf配置文件添加如下配置:

[root@samba ~]# vim /etc/samba/smb.conf
[shared_dir]
        comment=This is a new share directory
        path=/samba_dir
        writable=yes
[root@samba ~]# mkdir /samba_dir  #创建共享文件

配置完成后可使用testparm命令来校验/etc/samba/smb.conf文件的配置是否正确:

[root@samba ~]# 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 "[samba_dir]"
Processing section "[data]"
Loaded services file OK.
WARNING: 'workgroup' and 'netbios name' must differ.

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
    workgroup = SAMBA
    printcap name = cups
    security = USER
    idmap config * : backend = tdb
    cups options = raw


[homes]
    comment = Home Directories
    browseable = No
    inherit acls = Yes
    read only = No
    valid users = %S %D%w%S


[printers]
    comment = All Printers
    path = /var/tmp
    browseable = No
    printable = Yes
    create mask = 0600


[print$]
    comment = Printer Drivers
    path = /var/lib/samba/drivers
    create mask = 0664
    directory mask = 0775
    write list = root


[samba_dir]
    comment = This is a new share directory
    path = /samba_dir
    guest ok = Yes
    read only = No

samba服务默认的验证模式为user,因此我们还需要创建用户的数据库。

创建samba用户数据库

samba帐号必须要存在于Linux系统中(/etc/passwd),但其密码的却是单独维护的。创建samba用户数据库需要使用命令pdbedit,pdbedit命令是用于管理smb服务的账号信息数据库

格式:pdbedit [options] account

常用选项有:
-a USERNAME:创建samba用户;
-x:删除samba用户;
-L:列出samba用户列表;
-Lv:列出用户详细信息列表;
-t,--password-from-stdin:从标准输出接收字符串作为用户密码;
使用空提示符,然后将密码输入两次

除了pdbedit命令之外,我们也可以使用smbpasswd命令来管理创建samba用户

格式:smbpasswd [options] USERNAME

常用选项有:
-a:添加账号
-x:删除账号
-d:禁用账号
-e:启用账号

smbstatus命令

显示samba服务的相关共享的访问状态信息
-b:显示简要格式信息
-v:显示详细格式信息

查看服务器端共享:smbclient -t SMB_SERVER [-U USERNAME]
交互式文件访问:smbclient //SMB_SERVER/SHARE_NAME [-U USERNAME]
挂载访问:mount -t clfs //MB_SERVER/SHARE_NAME -o username=USERNAME,password=PASSWORD

注意:挂载操作用户,与-o选项中指定用户直接产生映射关系;此时,访问挂载点,是以-o选项中的username指定的用户身份进行;本地用户对指定的路径访问,首先得拥有对应的本地文件系统权限;

创建samba用户:
[root@samba ~]# useradd samba
[root@samba ~]# pdbedit -a samba
new password:
retype new password:
Unix username:        samba
NT username:          
Account Flags:        [U          ]
User SID:             S-1-5-21-2841389940-495581649-2996202120-1000
Primary Group SID:    S-1-5-21-2841389940-495581649-2996202120-513
Full Name:            
Home Directory:       \\samba\samba
HomeDir Drive:        
Logon Script:         
Profile Path:         \\samba\samba\profile
Domain:               SAMBA
Account desc:         
Workstations:         
Munged dial:          
Logon time:           0
Logoff time:          Wed, 06 Feb 2036 10:06:39 EST
Kickoff time:         Wed, 06 Feb 2036 10:06:39 EST
Password last set:    Mon, 07 May 2018 07:34:53 EDT
Password can change:  Mon, 07 May 2018 07:34:53 EDT
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@samba ~]# useradd magedu
[root@samba ~]# smbpasswd -a magedu
New SMB password:
Retype new SMB password:
Added user magedu.

启动smb服务,测试访问

[root@samba ~]# systemctl start smb
输入账号密码后成功登陆 无法在samba_dir目录下创建文件

由上图所示,samba用户登录后无法在samba_dir共享文件夹下创建目录,这是因samba_dir共享目录的属主和属组均为root,其他用户没有写权限

root@samba ~]# ll /samba_dir/ -d
drwxr-xr-x. 2 root root 6 May  7 07:19 /samba_dir/

将目录的属组改为samba,并添加写权限:

[root@samba ~]# usermod -G samba samba  #将samba添加为samba账号的附加组
[root@samba ~]# chown :samba /samba_dir/  #修改共享目录的属组为samba
[root@samba ~]# chmod g+w /samba_dir/  #给samba增加写权限
[root@samba ~]# ll -d /samba_dir/
drwxrwxr-x. 2 root samba 6 May  7 07:19 /samba_dir/
[root@samba ~]# systemctl restart smb
成功创建目录

在Linux系统上挂载samba共享目录

[root@test ~]# mount -t cifs //192.168.0.84/samba_dir /mnt -o username=samba,password=123456  #挂载samba共享目录
[root@test ~]# mount
......
//192.168.0.84/samba_dir on /mnt type cifs (rw,relatime,vers=1.0,cache=striorcegid,addr=192.168.0.84,unix,posixpaths,serverino,acl,rsize=1048576,wsiz
[root@test ~]# cd /mnt/
[root@test mnt]# ll
总用量 0
drwxr-xr-x. 2 1502 1502 0 5月   7 2018 magedu_test
[root@test mnt]# touch samba_files  #创建文件成功
[root@test mnt]# rm magedu_test/
rm: 无法删除"magedu_test/": 是一个目录
[root@test mnt]# rmdir magedu_test/  #删除文件成功,
[root@test mnt]# ll
总用量 0
-rw-r--r--. 1 1501 1501 0 4月  24 15:16 samba_files

在linux系统中访问samba文件系统还可以使用smbclient命令工具:

[root@test ~]# yum install samba-client -y  #安装smbclient命令工具
[root@test ~]# smbclient -L 192.168.0.84 -U samba  #查看samba服务器的共享情况
Enter SAMBA\samba's password: 
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    samba_dir       Disk      This is a new share directory
    IPC$            IPC       IPC Service (Samba 4.6.2)
    samba           Disk      Home Directories
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
[root@test ~]# smbclient //192.168.0.84/samba_dir -U samba #交互式访问samba共享目录
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> lcd /etc/
smb: \> put fstab  
putting file fstab as \fstab (75.7 kb/s) (average 75.7 kb/s)  #成功上传文件
smb: \> ls
  .                                   D        0  Mon May  7 20:28:07 2018
  ..                                 DR        0  Mon May  7 19:19:53 2018
  samba_files                         N        0  Tue Apr 24 15:16:13 2018
  fstab                               A      465  Mon May  7 20:28:07 2018
  hello.txt                           A        0  Mon May  7 20:28:49 2018

        17811456 blocks of size 1024. 16554288 blocks available
smb: \> rm hello.txt   #成功删除文件
smb: \> ls  
  .                                   D        0  Mon May  7 20:29:00 2018
  ..                                 DR        0  Mon May  7 19:19:53 2018
  samba_files                         N        0  Tue Apr 24 15:16:13 2018
  fstab                               A      465  Mon May  7 20:28:07 2018

        17811456 blocks of size 1024. 16554264 blocks available
smb: \>

练习

创建一个共享data,路径为/var/ftp/data,要求仅centos和gentoo用户能上传,此路径对其他用户不可见

[root@samba ~]# mkdir -pv /var/ftp/data
mkdir: created directory ‘/var/ftp’
mkdir: created directory ‘/var/ftp/data’
[root@samba ~]# vim /etc/samba/smb.conf
[global]
        workgroup = SAMBA
        security = user
        passdb backend = tdbsam
        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw
[data]
        comment=ftp
        path = /var/ftp/data
        browseable = No  #不公开data目录
        writable=yes
        directory mask = 0775
        create mask = 0664
        write list=centos,gentoo  #设置只允许centos、gentoo访问data共享目录
[root@samba ~]# testparm  #运行testparm命令检查/etc/samba/smb.conf配置文件是否有错误
[root@samba ~]# useradd centos -s /sbin/nologin 
[root@samba ~]# useradd gentoo -s /sbin/nologin 
[root@samba ~]# smbpasswd -a centos
New SMB password:
Retype new SMB password:
Added user centos.
[root@samba ~]# smbpasswd -a gentoo
New SMB password:
Retype new SMB password:
Added user gentoo.
[root@samba ~]# setfacl -m u:centos:rwx /var/ftp/data/  #设置用户centos对/var/ftp/data的目录权限为rwx
[root@samba ~]# setfacl -m u:gentoo:rwx /var/ftp/data/  #设置用户gentoo对/var/ftp/data的目录权限为rwx
[root@samba ~]# getfacl /var/ftp/data/
getfacl: Removing leading '/' from absolute path names
# file: var/ftp/data/
# owner: root
# group: root
user::rwx
user:centos:rwx
user:gentoo:rwx
group::r-x
mask::rwx
other::r-x
[root@samba ~]# systemctl restart smb

输入\\192.168.0.106\data

手动添加data路径访问
上一篇 下一篇

猜你喜欢

热点阅读