centos下samba服务器的安装与配置

本文详细介绍如何在CentOS 6.6系统上安装Samba服务并进行基本配置,包括搭建文件共享服务,实现Linux与Windows之间的文件互访。通过两个实际案例演示不同级别的访问控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简介

  Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,最大的功能就是可以用于Linux与windows系统直接的文件共享和打印共


二、系统环境

系统平台:CentOS release 6.6 (Final)

Samba版本:samba-3.6.23-35.el6_8.x86_64

Samba Server IP:192.168.1.104

防火墙已关闭/iptables: Firewall is not running.

SELINUX=disabled

三、安装Samba服务

1、在可以联网的机器上使用yum工具安装,如果未联网,则挂载系统光盘进行安装。

# yum install samba samba-client samba-swat -y

有依赖关系的包samba-common、samba-winbind-clients、libsmbclient将自动安装上去。

2、查看安装状况

3、安装包说明

samba-common-3.5.10-125.el6.x86_64               //主要提供samba服务器的设置文件与设置文件语法检验程序testparm
samba-client-3.5.10-125.el6.x86_64                    //客户端软件,主要提供linux主机作为客户端时,所需要的工具指令集
samba-swat-3.5.10-125.el6.x86_64                    //基于https协议的samba服务器web配置界面
samba-3.5.10-125.el6.x86_64                            //服务器端软件,主要提供samba服务器的守护程序,共享文档,日志的轮替,开机默认选项

Samba服务器安装完毕,会生成配置文件目录/etc/samba和其它一些samba可执行命令工具,/etc/samba/smb.conf是samba的核心配置文件,/etc/init.d/smb是samba的启动/关闭文件。

4、启动Samba服务器

可以通过/etc/init.d/smb start/stop/restart来启动、关闭、重启Samba服务,启动SMB服务如下所示:

[python]  view plain  copy
  1. [root@localhost ~]# /etc/init.d/smb start  
  2. Starting SMB services:                                     [  OK  ]  

5、查看samba的服务启动情况

# service smb status

[python]  view plain  copy
  1. [root@localhost ~]# service smb status  
  2. smbd (pid  1288is running...  

6、设置开机自启动

# chkconfig --level 35 smb on             //在3、5级别上自动运行samba服务

四、配置Samba服务

Samba的主配置文件为/etc/samba/smb.conf

主配置文件由两部分构成

  • Global Settings (55-245行)

  该设置都是与Samba服务整体运行环境有关的选项,它的设置项目是针对所有共享资源的。

  • Share Definitions (246-尾行)

  该设置针对的是共享目录个别的设置,只对当前的共享资源起作用。


默认的smb.conf有很多个选项和内容,比较繁琐,这里以具体的案例一和案例二来具体配置下,先备份一下自己的smb.conf文件,然后重新建立一个smb.conf。

# cp -p /etc/samba/smb.conf    /etc/samba/smb.conf.orig


案例一、公司现有一个工作组workgroup,需要添加samba服务器作为文件服务器,并发布共享目录/share,共享名为public,此共享目录允许所有员工访问。

a. 修改samba的主配置文件如下:

[python]  view plain  copy
  1. #======================= Global Settings =====================================  
  2.   
  3. [global]                                                  //该设置与Samba服务整体运行环境有关,它的设置项目针对所有共享资源         
  4.   
  5. # ----------------------- Network Related Options -------------------------  
  6. #  
  7. # workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH  
  8. #  
  9. # server string is the equivalent of the NT Description field  
  10. #  
  11. # netbios name can be used to specify a server name not tied to the hostname  
  12.   
  13.         workgroup = WORKGROUP                             //定义工作组,也就是windows中的工作组概念  
  14.         server string = David Samba Server Version %v     //定义Samba服务器的简要说明  
  15.         netbios name = DavidSamba                         //定义windows中显示出来的计算机名称  
  16.   
  17. # --------------------------- Logging Options -----------------------------  
  18. #  
  19. # Log File let you specify where to put logs and how to split them up.  
  20.   
  21.         log file = /var/log/samba/log.%m                  //定义Samba用户的日志文件,%m代表客户端主机名  
  22.                                                           //Samba服务器会在指定的目录中为每个登陆主机建立不同的日志文件  
  23. # ----------------------- Standalone Server Options ------------------------  
  24. #  
  25. # Scurity can be set to user, share(deprecated) or server(deprecated)  
  26.   
  27.         security = share                                  //共享级别,用户不需要账号和密码即可访问  
  28.   
  29. #============================ Share Definitions ==============================  
  30.   
  31. [public]                                                  //设置针对的是共享目录个别的设置,只对当前的共享资源起作用  
  32.         comment = Public Stuff                            //对共享目录的说明文件,自己可以定义说明信息  
  33.         path = /share                                     //用来指定共享的目录,必选项  
  34.         public = yes                                      //所有人可查看,等效于guest ok = yes  

b. 建立共享目录

上面设置了共享目录为/share,下面就需要建立/share目录:由于要设置匿名用户可以下载或上传共享文件,所以要给/share目录授权为nobody权限。

[python]  view plain  copy
  1. [root@localhost ~]# mkdir /share/  
  2. [root@localhost ~]# cd /share/  
  3. [root@localhost share]# touch a.txt b.txt  
  4. [root@localhost share]# chown -R nobody:nobody /share/  
  5. [root@localhost share]# ls -l  
  6. total 0  
  7. -rw-r--r-- 1 nobody nobody 0 Jul 17 16:50 a.txt  
  8. -rw-r--r-- 1 nobody nobody 0 Jul 17 16:50 b.txt  

c. 重启smb服务

[ruby]  view plain  copy
  1. [root@localhost ~]# /etc/init.d/smb restart  
  2. Shutting down SMB services:                                [  OK  ]  
  3. Starting SMB services:                                     [  OK  ]  

d. 测试smb.conf配置是否正确

[python]  view plain  copy
  1. [root@localhost share]# /etc/init.d/smb restart  
  2. Shutting down SMB services:                                [  OK  ]  
  3. Starting SMB services:                                     [  OK  ]  
  4. [root@localhost share]# testparm  
  5. Load smb config files from /etc/samba/smb.conf  
  6. rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)  
  7. Processing section "[homes]"  
  8. Processing section "[printers]"  
  9. Processing section "[public]"  
  10. WARNING: The security=share option is deprecated  
  11. Loaded services file OK.  
  12. Server role: ROLE_STANDALONE  
  13. Press enter to see a dump of your service definitions  
  14.   
  15. [global]  
  16.         netbios name = DAVIDSAMBA  
  17.         server string = David Samba Server Version %v  
  18.         security = SHARE  
  19.         log file = /var/log/samba/log.%m  
  20.         max log size = 50  
  21.         client signing = required  
  22.         idmap config * : backend = tdb  
  23.         cups options = raw  
  24.   
  25. [homes]  
  26.         comment = Home Directories  
  27.         read only = No  
  28.         browseable = No  
  29.   
  30. [printers]  
  31.         comment = All Printers  
  32.         path = /var/spool/samba  
  33.         printable = Yes  
  34.         print ok = Yes  
  35.         browseable = No  
  36.   
  37. [public]  
  38.         comment = Public Stuff  
  39.         path = /share  
  40.         guest ok = Yes  

e. 访问Samba服务器的共享文件

  • 在Linux下访问Samba服务器的共享文件

[python]  view plain  copy
  1. [root@localhost share]# smbclient //127.0.0.1/public  
  2. WARNING: The security=share option is deprecated  
  3. Enter root's password:  
  4. Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]  
  5. Server not using user level security and no password supplied.  
  6. smb: \> ls  
  7.   .                                   D        0  Sun Jul 17 16:50:24 2016  
  8.   ..                                 DR        0  Sun Jul 17 16:50:04 2016  
  9.   a.txt                                        0  Sun Jul 17 16:50:24 2016  
  10.   b.txt                                        0  Sun Jul 17 16:50:24 2016  
  11.   
  12.                 53087 blocks of size 131072. 43757 blocks available  
  13. smb: \>  

  • 在windows下访问Samba服务器的共享文件




案例二、公司现有多个部门,因工作需要,将TS部的资料存放在samba服务器的/php 目录中集中管理,以便TS人员浏览,并且该目录只允许TS部员工访问。

a. 添加php部组和用户 建立用户的同时加入到相应的组中的方式:useradd -g 组名 用户名

[python]  view plain  copy
  1. [root@localhost ~]# groupadd php  
  2. [root@localhost ~]# useradd -g php phper1  
  3. [root@localhost ~]# useradd -g php phper2  
  4. [root@localhost ~]# passwd phper1  
  5. Changing password for user phper1.  
  6. New password:  
  7. BAD PASSWORD: it is too simplistic/systematic  
  8. BAD PASSWORD: is too simple  
  9. Retype new password:  
  10. passwd: all authentication tokens updated successfully.  
b. 在根目录下建立/php 文件夹
[python]  view plain  copy
  1. [root@localhost ~]# mkdir /php  
  2. [root@localhost ~]# cd /php/  
  3. [root@localhost php]# touch ci.txt tp.txt  
  4. [root@localhost php]# ls  
  5. ci.txt  tp.txt  
c. 将刚才建立的两个帐户添加到samba的账户中
[python]  view plain  copy
  1. [root@localhost php]# smbpasswd -a phper1  
  2. New SMB password:  
  3. Retype new SMB password:  
  4. Added user phper1.  
  5. [root@localhost php]# smbpasswd -a phper2  
  6. New SMB password:  
  7. Retype new SMB password:  
  8. Added user phper2.  
d. 修改主配置文件如下:就增加[php]处内容
[python]  view plain  copy
  1. #======================= Global Settings =====================================  
  2.   
  3. [global]  
  4.   
  5. # ----------------------- Network Related Options -------------------------  
  6. #  
  7. # workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH  
  8. #  
  9. # server string is the equivalent of the NT Description field  
  10. #  
  11. # netbios name can be used to specify a server name not tied to the hostname  
  12.   
  13.         workgroup = WORKGROUP  
  14.         server string = David Samba Server Version %v  
  15.         netbios name = DavidSamba  
  16.   
  17. # --------------------------- Logging Options -----------------------------  
  18. #  
  19. # Log File let you specify where to put logs and how to split them up.  
  20.   
  21.         log file = /var/log/samba/log.%m  
  22.   
  23. # ----------------------- Standalone Server Options ------------------------  
  24. #  
  25. # Scurity can be set to user, share(deprecated) or server(deprecated)  
  26.   
  27.         security = user                                   //用户级别,由提供服务的Samba服务器负责检查账户和密码  
  28.   
  29. #============================ Share Definitions ==============================  
  30.   
  31. [homes]                                                   //设置用户宿主目录  
  32.         comment = Home Directories  
  33.         browseable = no  
  34.         writable = yes  
  35. ;       valid users = %S  
  36. ;       valid users = MYDOMAIN\%S  
  37.   
  38. [public]  
  39.         comment = Public Stuff  
  40.         path = /share  
  41.         public = yes  
  42.   
  43. [php]  
  44.         comment = php depart  
  45.         path = /php  
  46.         valid users = @php  
e. 重新加载配置

[python]  view plain  copy
  1. [root@localhost ~]# service smb reload  
  2. Reloading smb.conf file:                                   [  OK  ]  
f. 到windows客户端验证,访问\\192.168.1.104,点击php文件夹,提示输入用户名和密码,在此输入phper1验证,如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值