Samba简单配置

一、Samba简介

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

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

SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

安装

yum install samba samba-client

二、配置文件

配置文件在/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]
        # 工作组,可以自定义,但需要和Windows上一致
        workgroup = SAMBA

        # 安全级别,有以下几种:
        # security = user #这里指定samba的安全等级。关于安全等级有四种:
        # share:用户不需要账户及密码即可登录samba服务器
        # user:由提供服务的samba服务器负责检查账户及密码(默认)
        # server:检查账户及密码的工作由另一台windows或samba服务器负责
        # domain:指定windows域控制服务器来验证用户的账户及密码。
        security = user

        # 用户名和密码存储方式(tdbsam、smbpasswd、ldapsam)
        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 = root
        create mask = 0664
        directory mask = 0775

三、共享一个只读的匿名目录

修改/etc/samba/smb.conf配置文件:

# 把工作组修改和Windows上的一致
        workgroup = WORKGROUP

        #修改安全级别:
        security = share

# 共享的名字
[test]
        comment = share all
        path = /tmp/sambadir

        # 是否允许在工作组可见
        browseable = yes

        # 是否公开
        public = yes

        # 是否可写
        writable = no

然后创建共享的目录,并配置权限:

mkdir -p /tmp/sambadir/test
chmod 777 /tmp/sambadir/test
echo "123131" > /tmp/sambadir/test/1.tx

启动程序,放行防火墙:

systemctl start smb

firewall-cmd --zone=public --add-service=samba --permanent
firewall-cmd --reload

四、centos7上启动服务报错

执行systemctl status smb查看状态,发现并没有成功启动:

● smb.service - Samba SMB Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 四 2018-05-03 01:30:09 CST; 2s ago
  Process: 50237 ExecStart=/usr/sbin/smbd $SMBDOPTIONS (code=exited, status=1/FAILURE)
 Main PID: 50237 (code=exited, status=1/FAILURE)

5月 03 01:30:09 web01 systemd[1]: Starting Samba SMB Daemon...
5月 03 01:30:09 web01 systemd[1]: smb.service: main process exited, code=exited, status=1/FAILURE
5月 03 01:30:09 web01 systemd[1]: Failed to start Samba SMB Daemon.
5月 03 01:30:09 web01 systemd[1]: Unit smb.service entered failed state.
5月 03 01:30:09 web01 systemd[1]: smb.service failed.

可以执行smbd -F -S获取报错信息:

smbd version 4.6.2 started.
Copyright Andrew Tridgell and the Samba Team 1992-2017
WARNING: Ignoring invalid value 'share' for parameter 'security'
error opening config file '/etc/samba/smb.conf'

从错误信息中得知,security部分配置错误,百度之后得到原因。
原来,samba4较之前的SAMBA 3有一个重大的变化是:security不再支持share,参数需要做调整。

安装的版本信息如下:

rpm -qa |grep samba
samba-4.6.2-12.el7_4.x86_64

此时需要将:

security=share

改为:

security=user
map to guest =Bad User

然后再启动:

systemctl restart smb

五、在Windows上访问

在本地电脑上打开资源管理器,并在地址栏输入服务器IP地址:

\\192.168.237.6

即可看到以下页面:

进入之后能看到有一个test目录,以及test目录中的1.txt测试文件。
试着在其中写入信息然后保存,收到报错。

这是因为我们在samba配置文件中配置了只读权限。

标签: 文件服务

添加新评论