linux开启samba共享(给Windows客户端网络共享图文教程)以及解决遇到的坑

2021-06-08 949点热度 0人点赞 1条评论

Linux服务器也是可以开启共享给Windows的客户端的(通过Samba/SMB共享的方式)

检查和安装

## 使用rpm检查samba是否安装,rpm -qa|grep samba
## 如果没有显示,就说明没有安装,我这边已经安装好了
[root@localhost ~]# rpm -qa|grep samba
samba-winbind-3.6.23-51.el6.x86_64
samba-3.6.23-51.el6.x86_64
samba-winbind-clients-3.6.23-51.el6.x86_64
samba-client-3.6.23-51.el6.x86_64
samba-common-3.6.23-51.el6.x86_64
samba4-libs-4.2.10-15.el6.x86_64

## 使用yum安装samba
[root@localhost ~]# yum install -y samba

如果是Ubuntu系统,使用apt安装

sudo apt-get install samba
sudo apt-get install smbclient

配置共享

标题内容

注意,CentOS下检查是否关闭了Selinux,不然会遇到奇奇怪怪的问题

vim /etc/selinux/config

# 检查配置文件是否是SELINUX=disabled,不是的话,修改成如下。并reboot重启电脑
SELINUX=disabled

Samba的配置文件路径是:/etc/samba/smb.conf

设置用户名密码共享

无用户名的共享方式因为不安全,基本上都是废弃了deprecated

  1. 修改/etc/samba/smb.conf的security = user
    找到配置文件smb.conf的section: [global]下security = user,如果security = 其他或者被注释了,都修改成取消取消注释,并且是security = user
  2. 新建需要共享的文件夹
    mkdir /opt/share
    chmod 777 /opt/share
  3. 添加Client用户和密码
    # 新增一个Linux用户(如果不存在的话)
    useradd terry
    
    # 设置这个用户的Samba密码
    smbpasswd -a terry
    New SMB password:
    Retype new SMB password:
    
    # 这里列出smbpasswd的用法
    # 增加用户user
    smbpasswd -a user
    
    # 冻结用户user,这个用户不能再登录
    smbpasswd -d user
    
    # 恢复冻结的用户user,让冻结的用户可以再使用
    smbpasswd -e user
    
    # 删除用户user
    smbpasswd -x user
  4. 添加共享配置:
    在smb.conf末尾添加如下配置:

    # 客户端显示的共享目录的名字
    [share]
        comment = sharefolder
        path = /opt/share
        valid users = terry
        # 多个user用逗号隔开
        # valid users=terry,tom
        browseable = yes
        read only = yes
        create mask = 0777
        directory mask = 0777
        public = yes
        writable = yes
        available = yes
    
        # 安全起见,还可以设置IP地址白名单,这里是只允许192.168.0.x的客户端登录
        # hosts deny = ALL
        # hosts allow = 192.168.0.
  5. 重新启动samba
    systemctl restart samba
    #or 
    systemctl restart smbd
    #or
    systemctl restart smb

     

  6. 现在到Windows下,开始-运行-输入:

    或者资源管理器,映射网络驱动器就就可以了

遇坑排查

  1. Windows 10以上客户端打开共享时候,遇到错误:
    因为文件共享不安全,所以你不能连接到文件共享。此共享需要过时的SMB1协议,而此协议是不安全的,可能会使你的系统遭受攻击。 你的系统需要SMB2或更高版本。关于如何解决此问题的信息,请参见
    https://go.microsoft.com/fwlink/?linkeid=85174

    看到网上有说如何修改windows配置文件,让Windows继续使用SMB1,我认为不是一个很好的方案
    经查询和实践,可以让Linux服务器使用SMB2,修改/etc/samba/smb.conf配置文件,在section: [global]下,增加

    min protocol = SMB2
    client min protocol = SMB2
    protocol = SMB2

    如图:

    最后记得重启smb: systemctl restart smb/systemctl restart smbd

  2. 设置Samba密码的时候,提示错误:
    [root@localhost opt]# sudo smbpasswd -a terry
    New SMB password:
    Retype new SMB password:
    Failed to add entry for user terry.

    那是因为首先Linux下terry用户不存在,需要先新建用户terry(useradd),如下

    [root@localhost opt]# useradd terry
    [root@localhost opt]# smbpasswd -a terry
    New SMB password:
    Retype new SMB password:
    Added user terry.

     

 

admin

这个人很懒,什么都没留下

文章评论

  • heat stamp plastic

    What's up to every body, it's my first pay a quick visit of this blog;
    this web site contains awesome and truly good data designed for readers.

    2021-06-08
  • 您需要 登录 之后才可以评论