首页 > 系统相关 >CentOS7.9 搭建Samba服务器

CentOS7.9 搭建Samba服务器

时间:2022-09-27 22:11:05浏览次数:53  
标签:Samba share 用户 CentOS7.9 mengxin 服务器 MengXin root

一、Samba概述

1.Samba简介

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

  • NFS 与 samba 一样,也是在网络中实现文件共享的一种实现,但不幸的是,其不支持 windows 平台,samba 是能够在任何支持 SMB 协议的主机之间共享文件的一种实现,当然也包括 windows。

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

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

2。Samba监听端口

TCP UDP
139/445 137/138
  • tcp 端口相对应的服务是 smbd 服务,其作用是提供对服务器中文件、打印资源的共享访问
  • udp 端口相对应的服务是 nmbd 服务,其作用是提供基于 NetBIOS 主机名称的解析

3.Samba进程

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

4.Samba 用户

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

5.配置文件常用参数

参数 作用
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.48.100
hosts allow 设置允许哪台主机访问,例如 hosts allow = 192.168.48.200
printable 表示设置是否为打印机

二、搭建Samba服务器

1.安装Samba对应的软件包

//安装Samba软件包,这里安装所有的
[root@MengXin ~]# yum -y install samba*

2.添加用户

//smbpasswd 命令:
    -a Sys_User     //添加系统用户为 samba 用户并为其设置密码
    -d              //禁用用户帐号
    -e              //启用用户帐号
    -x              //删除用户帐号

//建立系统用户以后不需要使用‘passwd’设置系统用户密码
[root@MengXin ~]# useradd mengxin
[root@MengXin ~]# smbpasswd -a mengxin
New SMB password:
Retype new SMB password:
Added user mengxin.

3.创建共享目录

在用户主目录或自定义目录下建立用户的共享文件夹,这里我选择‘/home/mengxin/share’作为用户mengxin的共享文件夹

[root@MengXin mengxin]# mkdir /home/mengxin/share
[root@MengXin mengxin]# touch ./share/mengxin.txt
//根据需要修改共享文件夹的所属及权限
[root@MengXin mengxin]# chown -R mengxin:mengxin share/
[root@MengXin mengxin]# ll
总用量 0
drwxr-xr-x. 2 mengxin mengxin 6 9月  27 20:57 share
[root@MengXin mengxin]#

4.修改配置文件

smb的配置文件为‘/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
# 在文档最后新加一段配置
[mengxin]
        comment = IMMENGXIN
        path = /home/mengxin/share
        public = yes
        browseable = yes
        read only = no

编辑完配置文件后需要在防火墙上允许Samba服务通过,或者直接停止防火墙服务

//允许Samba服务通过
[root@MengXin mengxin]# firewall-cmd --permanent --zone=public --add-service=samba
success
//关闭防火墙服务
[root@MengXin mengxin]# systemctl stop firewalld.service
//重启Samba服务
[root@MengXin mengxin]# systemctl restart smb

5.结果验证

[root@MengXin share]# smbclient //192.168.255.13/mengxin -U mengxin
Enter SAMBA\mengxin's password: 
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Tue Sep 27 21:22:18 2022
  ..                                  D        0  Tue Sep 27 20:57:21 2022
  mengxin.txt                         N        8  Tue Sep 27 21:22:27 2022

                2082816 blocks of size 1024. 2042108 blocks available
smb: \> 

如果你在Windows或者Linux客户端无法访问共享文件夹,请先关闭selinux

[root@MengXin share]# setenforce 0      //临时关闭selinux,永久关闭自行百度

三、Samba的进阶应用

修改配置文件

这里我们新建一个组,并添加几个新用户

[root@MengXin share]# groupadd mgrp
[root@MengXin share]# useradd -g mgrp user1
[root@MengXin share]# useradd -g mgrp user2
[root@MengXin share]# smbpasswd -a user1
New SMB password:
Retype new SMB password:
Added user user1.
[root@MengXin share]# smbpasswd -a user2
New SMB password:
Retype new SMB password:
Added user user2.
[root@MengXin share]# 

修改一下配置文件

[mengxin]
        comment = IMMENGXIN
        path = /home/mengxin/share
        public = yes
        browseable = yes
        read only = no    //文件夹权限可读写
        valib users = user1,mengxin     //允许两个用户访问,上面已经新建
# 设置允许或拒绝的主机,自行新建虚拟机
        hosts allow = 192.168.255.11
        hosts deny = 192.168.255.23

记得重启服务

配置结果验证

这里是允许访问的主机

[root@localhost ~]# smbclient //192.168.255.13/mengxin -U mengxin
Enter SAMBA\mengxin's password: 
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Tue Sep 27 09:22:18 2022
  ..                                  D        0  Tue Sep 27 08:57:21 2022
  mengxin.txt                         N        8  Tue Sep 27 09:22:27 2022

                2082816 blocks of size 1024. 2042052 blocks available
smb: \> 

这里是拒绝访问的主机

[root@MengXin ~]# ip a s ens160
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:62:28:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.255.23/24 brd 192.168.255.255 scope global dynamic noprefixroute ens160
       valid_lft 1361sec preferred_lft 1361sec
    inet6 fe80::61e0:19e8:cc7b:9e5c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@MengXin ~]# smbclient //192.168.255.13/mengxin -U mengxin
Enter SAMBA\mengxin's password: 
tree connect failed: NT_STATUS_ACCESS_DENIED       //返回值显示连接被拒绝
[root@MengXin ~]# 

到这里Samba服务器的配置就结束了,如有错误劳烦指正,非常感谢!!!

标签:Samba,share,用户,CentOS7.9,mengxin,服务器,MengXin,root
From: https://www.cnblogs.com/immengxin/p/16735925.html

相关文章