SAMBA服务

SAMBA服务

SMB是由IBM服务公司发布的,后来微软将smb进行了增强改名为cifs实现windows上的资源共享,1991年andrew Trigell将cifs协议进行了破解,模拟cifs协议,研发了SAMBA服务让Linux和Windows之间可以实现资源共享。

SAMBA的功能

  1. 共享文件和打印
  2. 实现登录SAMBA用户的身份认证
  3. 可以进行NetBIOS名称解析
  4. 外围设备共享

    计算机网路的管理模式

  5. 工作组(WORKGROUP)
    计算机对等关系,帐号信息各自管理
    计算机的工作组名可以随意更改,加入工作组后才能看到samba
  6. 域(DOMAIN)
    域DOMAIN:C/S结构,帐号信息集中管理,DC,AD
    域比工作组严谨,需要验证后才能加入,加入后受到域的管理

    SAMBA安装

    SAMBA服务器端

    服务器端需要安装samba包

    [root@samba ~]# yum install samba -y

    SAMBA客户端

    samba客户端需要安装samba-client、samba-common、cifs-utils、samba-winbind包

    [root@client ~]# yum install samba-client -y
    [root@client ~]# yum install cifs-utils -y
    [root@client ~]# yum install samba-winbind -y

    samba-clinet:samba客户端软件
    samba-common:通用软件
    cifs-utils:smb客户端工具
    samba-winbind:和AD相关

    SAMBA服务进程

    samba服务启动时会启动两个进程smbd和nmbd
    smbd提供smb服务,使用的端口号为TCP:139,445
    nmbd提供NetBIOS名称解析,使用端口号为UDP:137,138

    SAMBA配置文件

    CentOS7上samba配置有样板文件可以参考修改

    
    [root@samba ~]# ll /etc/samba/
    -rw-r--r-- 1 root root    20 Oct 31  2018 lmhosts
    -rw-r--r-- 1 root root   706 Oct 31  2018 smb.conf              #配置文件
    -rw-r--r-- 1 root root 11327 Oct 31  2018 smb.conf.example      #配置文件样板可以用来参考

[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
[homes]
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的使用
### samba用户的创建
samba用户的创建需要Linux上存在相应的用户
1.创建linux用户
```bash
[root@samba ~]# useradd -s /sbin/nologin smb1
[root@samba ~]# useradd -s /sbin/nologin smb2
[root@samba ~]# useradd -s /sbin/nologin smb3

2.把linux账号加入samba账号,输入密码

[root@samba ~]# smbpasswd -a smb1
New SMB password:
Retype new SMB password:
Added user smb1.
[root@samba ~]# smbpasswd -a smb2
New SMB password:
Retype new SMB password:
Added user smb2.
[root@samba ~]# smbpasswd -a smb3
New SMB password:
Retype new SMB password:
Added user smb3.

3.查看samba账号

[root@samba ~]# pdbedit -L
smb1:1002:
smb3:1004:
smb2:1003:

samba账号的存放位置为/var/lib/samba/private

[root@samba ~]# ll /var/lib/samba/private/
total 832
drwx------ 2 root root     54 May 19 19:16 msg.sock
-rw------- 1 root root 421888 May 19 19:16 passdb.tdb   #存放账号的数据库文件,此库被删除后再次创建用户时会自动创建
-rw------- 1 root root 430080 May 19 19:15 secrets.tdb

4.samba账号的删除
先删除smb账号,再将Linux用户删除

[root@samba ~]# pdbedit -x smb3
[root@samba ~]# pdbedit -L
smb1:1002:
smb2:1003:
[root@samba ~]# userdel smb3

启用samba

在samba服务端启动服务

[root@samba ~]# systemctl start smb
[root@samba ~]# systemctl start nmb

在客户端查看工作组
1.匿名登录查看

[root@client ~]# smbclient -L 192.168.73.110 
Enter SAMBA\root's password: 
Anonymous login successful

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    IPC$            IPC       IPC Service (Samba 4.8.3)
Reconnecting with SMB1 for workgroup listing.
Anonymous login successful

    Server               Comment
    ---------            -------
    SAMBA                Samba 4.8.3

    Workgroup            Master
    ---------            -------
    SAMBA                SAMBA

2.使用smb1登录查看

[root@client ~]# smbclient -L 192.168.73.110 -Usmb1%111111

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    IPC$            IPC       IPC Service (Samba 4.8.3)
    smb1            Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

    Server               Comment
    ---------            -------
    SAMBA                Samba 4.8.3

    Workgroup            Master
    ---------            -------
    SAMBA                SAMBA

samba日志

1.修改配置文件,重启服务

[root@samba ~]# vim /etc/samba/smb.conf
[global]
        workgroup = WORKGROUP
        security = user
        netbios name=smbserver          #服务器名,可以解析为ip,需要启用nmb服务
        passdb backend = tdbsam

        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw
        log file = /var/log/samba/log%I    #添加日志选项,%I以客户端ip区分
        log level = 2                      #记录日志的级别
[root@samba ~]# systemctl restart smb

2.客户端登录

[root@client ~]# smbclient -L 192.168.73.110 -Usmb1%111111

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    IPC$            IPC       IPC Service (Samba 4.8.3)
    smb1            Disk      Home Directories
Reconnecting with SMB1 for workgroup listing.

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

    Workgroup            Master
    ---------            -------
    SAMBA                SAMBA
    WORKGROUP            OOOO

3.服务器端查看日志

[root@samba ~]# ll /var/log/samba/
total 12
drwx------ 4 root root  30 May 19 19:27 cores
-rw-r--r-- 1 root root 902 May 19 21:06 log0.0.0.0
-rw-r--r-- 1 root root   0 May 19 20:03 log192.168.73.111   #带ip的日志
-rw-r--r-- 1 root root 710 May 19 21:06 log.nmbd
-rw-r--r-- 1 root root 168 May 19 19:27 log.smbd
drwx------ 2 root root   6 Oct 31  2018 old

samba访问控制

可以使用hosts allow或者hosts deny实现

hosts allow = 127. 192.168.12. 192.168.13.

可以拒绝整个网段访问,也可以拒绝单个地址的访问

设置某个目录的共享

1.创建需要共享的文件夹

[root@samba ~]# mkdir /data/smbshare1
[root@samba ~]# mkdir /data/smbshare2
[root@samba ~]# touch /data/smbshare1/smb1
[root@samba ~]# touch /data/smbshare2/smb2

2.修改配置文件,重启服务

[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
[root@samba ~]# systemctl restart smb

客户端查询

[root@client ~]# smbclient -L 192.168.73.110
Enter SAMBA\root's password: 
Anonymous login successful

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    smbshare1       Disk                            #已经能查看到共享的目录
    IPC$            IPC       IPC Service (Samba 4.8.3)
Reconnecting with SMB1 for workgroup listing.
Anonymous login successful

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

    Workgroup            Master
    ---------            -------
    WORKGROUP            OOOO

共享目录的读写权限

1.修改服务端配置文件

[smbshare1]
path = /data/smbshare1
readonly = no

2.给予目录读写执行权限

[root@samba ~]# chmod 777 /data/smbshare1
[root@samba ~]# ll /data
drwxrwxrwx  2 root root  18 May 19 21:22 smbshare1
drwxr-xr-x  2 root root  18 May 19 21:22 smbshare2

客户端挂载,写入文件

[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111 
[root@client ~]# touch /data/aa

允许特定的人访问

1.修改配置文件

[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
readonly = no
valid users = smb1      #只允许smb1访问
[root@samba ~]# systemctl restart smb

此时只有smb1用户可以登录,其余用户都不能访问

目录只读但某些用户可写

需要将目录设置为只读,添加可写列表写入可写的用户名单
修改配置

[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
readonly = yes          #将目录设置为制度
write list = smb1       #添加可写的用户
#write list = @g1 +g2   #也可以添加可写的组

匿名用户无需验证访问

需要使用到public,将其设置为yes

[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
public = yes            #添加此项设置为匿名用户也能访问
#readonly = no          #如果需要给匿名用户写权限还需要开启此项 

匿名用户所创建的文件,属主属组为nobody用户,所以匿名用户在Linux上被映射成nobody用户

隐藏共享目录

blowsable选项,把共享目录隐藏,但如果知道共享的名字依旧能使用

[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
public = yes 
read only = no
browsable = no          #yes为不隐藏 no为隐藏

让不同的人看到不同的资源

需要在配置文件的全局中添加config file指定每个用户的配置文件
修改配置文件

[root@samba ~]# vim /etc/samba/smb.conf
[global]
config file = /etc/samba/conf.d/%U  #创建每个用户独立的配置文件
...省略...
[smbshare1]
comment = text
path = /data/smbshare2
read only = no

创建用户个人的配置文件

[root@samba ~]# vim /etc/samba/conf.d/smb1
[smbshare1]
comment = text
path = /data/smbshare2
read only = no
#browsable = no            #也可以设置对单个用户隐藏目录

此时smb1访问smbshare1是访问的是/data/smbshare2,而未指定的用户访问时依旧是主配置文件中的/data/smbshare1

查看samba服务器状态

smbstatus命令可以查看samba服务器状态

[root@samba ~]# smbstatus 

Samba version 4.8.3
PID     Username     Group        Machine                                   Protocol Version  Encryption           Signing              
----------------------------------------------------------------------------------------------------------------------------------------
7826    smb1         smb1         192.168.73.111 (ipv4:192.168.73.111:39708) SMB3_02           -                    partial(AES-128-CMAC)

Service      pid     Machine       Connected at                     Encryption   Signing     
---------------------------------------------------------------------------------------------
IPC$         7826    192.168.73.111 Sun May 19 09:42:18 PM 2019 CST  -            AES-128-CMAC
smbshare1    7826    192.168.73.111 Sun May 19 09:42:18 PM 2019 CST  -            -           

No locked files

直接登录samba目录

可以使用smbclient直接登录samba来使用

[root@client ~]# smbclient //192.168.73.110/smbshare1 -Usmb1%111111
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Sun May 19 21:38:39 2019
  ..                                  D        0  Sun May 19 21:21:42 2019
  smb1                                N        0  Sun May 19 21:22:24 2019
  aa                                  A        0  Sun May 19 21:38:39 2019

        52403200 blocks of size 1024. 52370124 blocks available
smb: \> 

将samba文件夹挂载

挂载samba文件,挂载时需要制定用户名密码

[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111

在CentOS6上用户的加密协议不同所以挂载时需要指定加密协议

[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111,sec=ntlmssp

设置开机自动挂载samba

开机自动挂载需要将配置写入/etc/fstab中

[root@client ~]# vim /etc/fstab 
//192.168.73.110/smbshare1 /data        cifs    credentials=/etc/smb.txt        0 0
定义挂载设备    挂载点  文件系统    挂载时的账户和密码文件

创建存放账户和密码的文件,并设置安全权限

[root@client ~]# vim /etc/smb.txt
username=smb1
password=111111
[root@client ~]# chmod 600 /etc/smb.txt 
[root@client ~]# mount -a
[root@client data]# mount | grep 192.168.73
//192.168.73.110/smbshare1 on /data type cifs (rw,relatime,vers=default,cache=strict,username=smb1,domain=,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.73.110,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1)

转载于:https://blog.51cto.com/11886307/2397517

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值