zoukankan      html  css  js  c++  java
  • samba服务

    samba服务

    samba简介

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

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

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

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

    1. Samba监听端口有:

    TCP UDP
    139 137
    445 138

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

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

    2. samba进程:

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

    3. samba用户:

    帐号 密码
    都是系统用户 /etc/passwd Samba服务自有密码文件 通过smbpasswd -a USERNAME命令设置

    4. smbpasswd命令:

    • -a Sys_User //添加系统用户为samba用户并为其设置密码
    • -d //禁用用户帐号
    • -e //启用用户帐号
    • -x //删除用户帐号
    #安装samba
    [root@localhost ~]# yum -y install samba-*
    
    #创建samba用户
    [root@localhost ~]# useradd tom
    [root@localhost ~]# smbpasswd -a tom
    New SMB password:(这里我输入的是1)
    Retype new SMB password:(这里我输入的是1)
    Added user tom.
    

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

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

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

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

    6. 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 ~]# 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
    
            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
    
    #添加一行错误信息进去,进行测试,显示未知参数abc
    [root@localhost ~]# testparm 
    Load smb config files from /etc/samba/smb.conf
    Unknown parameter encountered: "abc"
    Ignoring unknown parameter "abc"
    Loaded services file OK.
    Server role: ROLE_STANDALONE
    
    Press enter to see a dump of your service definitions
    
    # Global parameters
    [global]
    	printcap name = cups
    	security = USER
    	workgroup = SAMBA
    	idmap config * : backend = tdb
    	cups options = raw
    
    
    [homes]
    	browseable = No
    	comment = Home Directories
    	inherit acls = Yes
    	read only = No
    	valid users = %S %D%w%S
    
    
    [printers]
    	browseable = No
    	comment = All Printers
    	create mask = 0600
    	path = /var/tmp
    	printable = Yes
    
    
    [print$]
    	comment = Printer Drivers
    	create mask = 0664
    	directory mask = 0775
    	force group = @printadmin
    	path = /var/lib/samba/drivers
    	write list = @printadmin root
    

    samba访问

    访问的两种方式:

    • 交互式访问
    • 挂载式访问

    环境说明:

    service客户端IP client服务端IP
    192.168.110.40 192.168.100.30

    准备工作:

    //交互式数据访问
    smbclient -L HOST -U USERNAME
    smbclient //SERVER/shared_name -U USERNAME
    
    //在客户端服务端上关闭selinx和防火墙
    setenforce 0
    systemctl stop firewalld
    
    //在服务端操作
    #安装samba服务
    [root@service ~]# yum -y install samba-*
    
    #修改配置文件,删除打印机的相关配置
    [root@service ~]# cat /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
    
    	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
    
    #启动smb nmb服务
    [root@service samba]# systemctl start smb nmb
    
    #查看端口号139 445
    [root@service samba]# ss -antl
    State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  
    LISTEN   0        50               0.0.0.0:139           0.0.0.0:*     
    LISTEN   0        128              0.0.0.0:22            0.0.0.0:*     
    LISTEN   0        50               0.0.0.0:445           0.0.0.0:*     
    LISTEN   0        50                  [::]:139              [::]:*     
    LISTEN   0        128                 [::]:22               [::]:*     
    LISTEN   0        50                  [::]:445              [::]:* 
    
    #创建tom用户
    [root@service ~]# smbpasswd -a tom
    New SMB password:(输入1)
    Retype new SMB password:(输入1)
    Added user tom.
    
    //在客户端操作
    #安装samba-client包
    [root@client ~]# yum -y install samba-client
    
    #查看samba服务器有哪些共享资源
    [root@client ~]# smbclient -L 192.168.110.40 -U tom
    Enter SAMBA	om's password: (输入1)
    
    	Sharename       Type      Comment
    	---------       ----      -------
    	IPC$            IPC       IPC Service (Samba 4.12.3)
    	tom             Disk      Home Directories
    SMB1 disabled -- no workgroup available
    

    使用交互式访问

    #交互式访问某共享资源
    [root@client ~]# smbclient //192.168.110.40/tom -U tom
    Enter SAMBA	om's password: (输入1)
    Try "help" to get a list of possible commands.
    smb: > ls
      .                                   D        0  Mon Apr  5 22:23:33 2021
      ..                                  D        0  Mon Apr  5 22:03:02 2021
      .bash_logout                        H       18  Fri Aug 30 13:30:21 2019
      .bash_profile                       H      141  Fri Aug 30 13:30:21 2019
      .bashrc                             H      312  Fri Aug 30 13:30:21 2019
      .bash_history                       H        8  Mon Apr  5 22:23:33 2021
    
    		49209880 blocks of size 1024. 47271672 blocks available
    smb: > 
    
    #在服务端使用tom用户在家目录下创建abc
    [root@service ~]# su - tom
    Last login: Mon Apr  5 22:23:24 CST 2021 on pts/1
    [tom@service ~]$ touch abc
    [tom@service ~]$ ls
    abc
    
    #在客户端查看,发现有abc文件
    smb: > ls
      .                                   D        0  Mon Apr  5 22:41:26 2021
      ..                                  D        0  Mon Apr  5 22:03:02 2021
      .bash_logout                        H       18  Fri Aug 30 13:30:21 2019
      .bash_profile                       H      141  Fri Aug 30 13:30:21 2019
      .bashrc                             H      312  Fri Aug 30 13:30:21 2019
      .bash_history                       H        8  Mon Apr  5 22:23:33 2021
      abc                                 N        0  Mon Apr  5 22:41:26 2021
    
    		49209880 blocks of size 1024. 47271672 blocks available
    
    #?查看有哪些命令,quit退出
    smb: > ?
    ?              allinfo        altname        archive        backup         
    blocksize      cancel         case_sensitive cd             chmod          
    chown          close          del            deltree        dir            
    du             echo           exit           get            getfacl        
    geteas         hardlink       help           history        iosize         
    lcd            link           lock           lowercase      ls             
    l              mask           md             mget           mkdir          
    more           mput           newer          notify         open           
    posix          posix_encrypt  posix_open     posix_mkdir    posix_rmdir    
    posix_unlink   posix_whoami   print          prompt         put            
    pwd            q              queue          quit           readlink       
    rd             recurse        reget          rename         reput          
    rm             rmdir          showacls       setea          setmode        
    scopy          stat           symlink        tar            tarmode        
    timeout        translate      unlock         volume         vuid           
    wdel           logon          listconnect    showconnect    tcon           
    tdis           tid            utimes         logoff         ..             
    !              
    smb: > quit
    

    基于挂载的方式访问

    #基于挂载的方式访问
    mount -t cifs //SERVER/shared_name /挂载到本地的什么目录 -o
    username=USERNAME,password=PASSWORD
    
    #修改配置,挂载到media目录下
    [root@client ~]# vim /etc/fstab 
    
    //192.168.110.40:/tom /media cifs defaults,username=tom,password=1 0 0
    
    #挂载
    [root@client ~]# mount -a
    [root@client ~]# df -h
    文件系统				容量	 已用  可用   已用% 挂载点
    Filesystem             Size  Used Avail Use% Mounted on
    devtmpfs               883M     0  883M   0% /dev
    tmpfs                  901M     0  901M   0% /dev/shm
    tmpfs                  901M  8.6M  892M   1% /run
    tmpfs                  901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root   17G  1.6G   16G  10% /
    /dev/sda1             1014M  179M  836M  18% /boot
    tmpfs                  181M     0  181M   0% /run/user/0
    //192.168.110.40:/tom   47G  1.9G   46G   4% /media
    
    #测试
    [root@client ~]# cd /media/
    [root@client media]# ls
    abc
    [root@client media]# touch 123
    [root@client media]# ll
    total 0
    -rwxr-xr-x. 1 root root 0 Apr  5 23:03 123
    -rwxr-xr-x. 1 root root 0 Apr  5 22:41 abc
    
    #在服务端使用tom用户创建456目录
    [tom@service ~]$ mkdir 456
    [tom@service ~]$ ls
    123  456  abc
    
    #在客户端查看,有456目录
    [root@client media]# ll
    total 0
    -rwxr-xr-x. 1 root root 0 Apr  5 23:03 123
    drwxr-xr-x. 2 root root 0 Apr  5 23:04 456
    -rwxr-xr-x. 1 root root 0 Apr  5 22:41 abc
    

    练习

    配置用户认证共享:

    #使用yum命令安装samba服务器
    [root@service ~]# yum -y install samba-*
    
    //映射共享目录:
    #创建用户jerry
    [root@service ~]# useradd -M jerry
    
    #为jerry用户创建smb共享密码
    [root@service ~]# smbpasswd -a jerry
    New SMB password:(输入1)
    Retype new SMB password:(输入1)
    Added user jerry.
    
    #假设这里映射jerry用户为share用户,那么就要在/etc/samba/smbusers文件中添加如下内容
    [root@service ~]# echo 'jerry = share' > /etc/samba/smbusers
    [root@service ~]# cd /etc/samba/
    [root@service samba]# ls
    lmhosts  smb.conf  smb.conf.example  smbusers
    [root@service samba]# cat smbusers 
    jerry = share
    
    #在全局配置中添加如下内容
    [root@service ~]# 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  //添加此行内容
            passdb backend = tdbsam
    
            printing = cups
            printcap name = cups
            load printers = yes
            cups options = raw
            
    #创建一个共享目录jerry
    [root@service ~]# mkdir /opt/jerry
    [root@service ~]# chown -R jerry.jerry /opt/jerry/
    [root@service ~]# ll /opt/
    total 0
    drwxr-xr-x. 2 jerry jerry 6 Apr  6 00:02 jerry
    
    #配置共享
    [root@service ~]# cat >> /etc/samba/smb.conf <<EOF
    [jerry]
    comment = jerry
    path = /opt/jerry
    browseable = yes
    guest ok = yes
    writable = yes
    write list = share
    public = yes
    EOF
    [root@service ~]# tail -8 /etc/samba/smb.conf
    [jerry]
    comment = jerry
    path = /opt/jerry
    browseable = yes
    guest ok = yes
    writable = yes
    write list = share
    public = yes
    
    #重启服务
    [root@service ~]# systemctl restart nmb smb
    
    #在客户机查看samba服务器有哪些共享资源
    [root@client ~]# smbclient -L 192.168.110.40 -U share
    Enter SAMBAshare's password: (输入1)
    
    	Sharename       Type      Comment
    	---------       ----      -------
    	jerry           Disk      jerry
    	IPC$            IPC       IPC Service (Samba 4.12.3)
    SMB1 disabled -- no workgroup available
    
    #将samba服务器的共享资源jerry挂载到客户机本地
    [root@client ~]# mount -t cifs //192.168.110.40/jerry /opt -o username=share,password=1
    [root@client ~]# df -h
    Filesystem              Size  Used Avail Use% Mounted on
    devtmpfs                883M     0  883M   0% /dev
    tmpfs                   901M  8.6M  892M   1% /run
    tmpfs                   901M     0  901M   0% /sys/fs/cgroup
    /dev/mapper/rhel-root    17G  1.6G   16G  10% /
    tmpfs                   181M     0  181M   0% /run/user/0
    /dev/sda1              1014M  179M  836M  18% /boot
    //192.168.110.40/jerry   47G  1.9G   46G   4% /opt
    
    #在客户机上进入共享目录创建新文件
    [root@client ~]# cd /opt/
    [root@client opt]# touch 123
    [root@client opt]# mkdir 456
    [root@client opt]# ls
    123  456
    
    #到服务器上验证
    [root@service ~]# cd /opt/jerry/
    [root@service jerry]# ls
    123  456
    

    在window查看共享目录

    win+r打开window运行,输入服务端的ip地址

    image

    点击确认
    image

    输入用户密码
    image

    查看共享文件
    image

    点击Jerry用户的文件夹
    image

    可以看到刚才创建的文件

    image

  • 相关阅读:
    NodeJS、NPM安装配置步骤(windows版本)
    23种设计模式全解析
    js阻止浏览器默认事件
    js获取不同浏览器盒子宽度高度
    H5之重力感应篇
    JS中的call()和apply()方法
    html学习笔记
    less(css)语言快速入门
    power designer简单教程
    Strom开发配置手册
  • 原文地址:https://www.cnblogs.com/leixixi/p/14619473.html
Copyright © 2011-2022 走看看