zoukankan      html  css  js  c++  java
  • 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访问

    环境说明:

    服务器R1的IP客户机R2的IP
    192.168.157.128 192.168.157.129

    配置用户认证共享 

    服务器配置:

    [root@R1 ~]# systemctl stop firewalld
    [root@R1 ~]# setenforce 0
    [root@R1 ~]# yum -y install samba-*
    [root@R1 ~]# useradd -M thl
    [root@R1 ~]# smbpasswd -a thl
    New SMB password:
    Retype new SMB password:
    Added user thl.
    
    //如果映射thl用户为share用户,那就把他写到/etc/samba/smbusers文件中
    [root@R1 ~]# echo 'thl = share' > /etc/samba/smbusers
    [root@R1 ~]# 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
            username map= /etc/samba/smbusers  //添加这行
    
    [root@R1 ~]# mkdir /opt/thl
    [root@R1 ~]# chown -R thl.thl /opt/thl/
    [root@R1 ~]# vim /etc/samba/smb.conf
    [root@R1 ~]# tail -8 /etc/samba/smb.conf
    [thl]
    comment = thl 
    path = /opt/thl
    browseable = yes
    guest ok = yes
    writable = yes
    write list = share
    public = yes
    
    [root@R1 ~]# systemctl start smb
    [root@R1 ~]# systemctl reload smb
    [root@R1 ~]# systemctl enable smb
    [root@R1 ~]#  systemctl restart smb

    客户端配置:

    [root@R2 ~]# yum -y install samba-client
    [root@R2 ~]# smbclient -L 192.168.157.128 -U share
    Enter SAMBAshare's password: 
    
        Sharename       Type      Comment
        ---------       ----      -------
        print$          Disk      Printer Drivers
        thl             Disk      thl
        IPC$            IPC       IPC Service (Samba 4.12.3)
    SMB1 disabled -- no workgroup available
    
    //将samba服务器的共享资源thl挂载到客户机本地
    [root@R2 ~]# mkdir /opt/smb
    [root@R2 ~]# mount -t cifs //192.168.157.128/thl /opt/smb/ -o username=share,password=123
    [root@R2 ~]# df -h
    文件系统                      容量  已用  可用 已用% 挂载点
    devtmpfs                      969M     0  969M    0% /dev
    tmpfs                         984M     0  984M    0% /dev/shm
    tmpfs                         984M  8.6M  975M    1% /run
    tmpfs                         984M     0  984M    0% /sys/fs/cgroup
    /dev/mapper/rhel_redhat-root   17G  1.8G   16G   11% /
    /dev/nvme0n1p1               1014M  146M  869M   15% /boot
    tmpfs                         197M     0  197M    0% /run/user/0
    //192.168.157.128/thl          17G  1.9G   16G   11% /opt/smb
    
    //测试挂载上的共享目录
    [root@R2 ~]# cd /opt/smb/
    [root@R2 smb]# ls
    [root@R2 smb]# touch 123
    [root@R2 smb]# ll
    总用量 0
    -rwxr-xr-x. 1 root root 0 4月   6 00:45 123
    
    //去R1看看效果
    [root@R1 ~]# ls /opt/thl/
    123

    配置匿名共享
    服务器配置:

    [root@R1 ~]# yum -y install samba-*
    [root@R1 ~]# 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@R1 ~]# mkdir /opt/ganyu
    [root@R1 ~]# chmod 777 /opt/ganyu/
    [root@R1 ~]# vim /etc/samba/smb.conf
    [root@R1 ~]# tail -7 /etc/samba/smb.conf
    [ganyu]
        comment = ganyu
        path = /opt/ganyu
        browseable = yes
        guest ok = yes    
        writable = yes
        public = yes
    [root@R1 ~]# systemctl reload smb
    [root@R1 ~]# systemctl restart smb

    客户端配置:

    [root@R2 ~]# yum -y install samba-client
    [root@R2 ~]# smbclient -L 192.168.157.128 -U share
    Enter SAMBAshare's password:   //不用输密码直接回车
    
        Sharename       Type      Comment
        ---------       ----      -------
        print$          Disk      Printer Drivers
        ganyu           Disk      ganyu
        IPC$            IPC       IPC Service (Samba 4.12.3)
    SMB1 disabled -- no workgroup available
    
    //将samba服务器的共享资源ganyu挂载到客户机本地
    [root@R2 ~]#  mount -t cifs //192.168.157.128/ganyu /opt/smb/ -o username='Bad User'
    [root@R2 ~]# df -h
    文件系统                      容量  已用  可用 已用% 挂载点
    devtmpfs                      969M     0  969M    0% /dev
    tmpfs                         984M     0  984M    0% /dev/shm
    tmpfs                         984M  8.6M  975M    1% /run
    tmpfs                         984M     0  984M    0% /sys/fs/cgroup
    /dev/mapper/rhel_redhat-root   17G  1.8G   16G   11% /
    /dev/nvme0n1p1               1014M  146M  869M   15% /boot
    tmpfs                         197M     0  197M    0% /run/user/0
    //192.168.157.128/ganyu        17G  1.9G   16G   11% /opt/smb
    [root@R2 ~]# cd /opt/smb/
    [root@R2 smb]# ls
    [root@R2 smb]# touch 123
    [root@R2 smb]# ls
    123
    [root@R2 smb]# touch 567
    [root@R2 smb]# ls
    123  567
    
    //去R1验证
    [root@R1 ~]# cd /opt/ganyu/
    [root@R1 ganyu]# ls
    123  567
  • 相关阅读:
    常见浏览器的兼容问题以及解决方案 (仅供参考)
    了解浏览器如何工作—渲染引擎1
    维基百科公式不能正常显示
    IDL读取fits文件
    卷积,系统
    硬盘被占用无法拔出问题解决
    Faster RCNN 改进论文及资料
    Pycharm 使用问题一览
    java eclipse 使用随笔
    Images
  • 原文地址:https://www.cnblogs.com/lichouluoyu/p/14618684.html
Copyright © 2011-2022 走看看