在 CentOS 7 中运行 Samba

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

这是一个困扰我整整一天的问题。我的环境如下:

  • 运行 Windows 7(64 位)
  • 使用 VirtualBox 5 以 Linux guest 身份运行 CentOS 7

我一直在寻找在 CentOS 来宾中创建 Samba 共享的解决方案,因此我能够从我的 Windows 7 主机系统访问正在运行的 CentOS 来宾中的文件夹。

我将 VirtualBox 配置为使用两个网络适配器(NAT 和 Host-Only)。

我在 CentOS 客户机上安装了 Samba,并将其配置为独立服务器。我创建了一个名为“数据”的 Samba 共享。在此处查看完整的 smb.conf,Samba 共享在文件末尾定义:


 [global]  
      workgroup = WORKGROUP  
      server string = Samba Server Version %v  
      dns proxy = no  
      # log files split per-machine:  
      log file = /var/log/samba/log.%m  
      # maximum size of 50KB per log file, then rotate:  
      max log size = 50  
      security = user  
      passdb backend = tdbsam  
      load printers = yes  
      cups options = raw  
      server role = standalone server  
      encrypt passwords = true  
      guest ok = yes  
      usershare allow guests = yes  
      obey pam restrictions = yes  
      unix password sync = yes  
      passwd program = /usr/bin/passwd %u  
      passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .  
      pam password change = yes  
      map to guest = bad user  
 #============================ Share Definitions ==============================  
 [homes]  
      comment = Home Directories  
      browseable = no  
      writable = yes  
 ;     valid users = %S  
 ;     valid users = MYDOMAIN\%S  
 [printers]  
      comment = All Printers  
      path = /var/spool/samba  
      browseable = no  
      guest ok = no  
      writable = no  
      printable = yes  
 [data]  
      comment = Data Folder  
      path = /data  
      guest ok = yes  
      browseable = yes  
      create mask = 0777  
      directory mask = 0777  
      writable = yes  
      force create mode = 777  
      force directory mode = 777  
      force security mode = 777  
      force directory security mode = 777


此 Samba 配置适用于 Ubuntu 14.04。 VirtualBox 来宾操作系统完美。但无论我做什么,这个配置在 CentOS 中都不起作用。当我尝试使用 \\centos-guest\data\ 从 Windows 主机访问 Samba 共享时,我总是收到“权限被拒绝”错误

事实证明,问题是正在运行的防火墙和 SELinux(安全增强型 Linux)。要禁用两者,请在 CentOS 中执行以下步骤:

  1. systemctl 停止防火墙
  2. systemctl 禁用 firewalld
  3. vi /etc/selinux/配置

 [global]  
      workgroup = WORKGROUP  
      server string = Samba Server Version %v  
      dns proxy = no  
      # log files split per-machine:  
      log file = /var/log/samba/log.%m  
      # maximum size of 50KB per log file, then rotate:  
      max log size = 50  
      security = user  
      passdb backend = tdbsam  
      load printers = yes  
      cups options = raw  
      server role = standalone server  
      encrypt passwords = true  
      guest ok = yes  
      usershare allow guests = yes  
      obey pam restrictions = yes  
      unix password sync = yes  
      passwd program = /usr/bin/passwd %u  
      passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .  
      pam password change = yes  
      map to guest = bad user  
 #============================ Share Definitions ==============================  
 [homes]  
      comment = Home Directories  
      browseable = no  
      writable = yes  
 ;     valid users = %S  
 ;     valid users = MYDOMAIN\%S  
 [printers]  
      comment = All Printers  
      path = /var/spool/samba  
      browseable = no  
      guest ok = no  
      writable = no  
      printable = yes  
 [data]  
      comment = Data Folder  
      path = /data  
      guest ok = yes  
      browseable = yes  
      create mask = 0777  
      directory mask = 0777  
      writable = yes  
      force create mode = 777  
      force directory mode = 777  
      force security mode = 777  
      force directory security mode = 777


之后,重新启动来宾操作系统,您就可以在 Windows 主机上访问 CentOS 来宾的 samba 共享。

但请注意,您在 Linux 来宾操作系统上禁用了防火墙!

如果这不是您想要的,您应该寻找替代方法。

相关文章