记录一次内网搭建SMB服务经验之谈,互相学习互相进步!

首先,我先说一下SMB是啥服务,或者说是啥协议,SMB是windows默认自带的共享文件夹的协议;就是你在windows上,右键文件或文件夹,然后选择共享该文件或文件夹时候,共享走的协议!

Snipaste_2022-04-18_23-19-58.png

内网环境分析

首先内网服务器环境是 CentOS Linux release 7.9.2009 (Core),然后IP是 192.168.1.111,这个IP我在光猫层面就给固定死了,所以也就是这个内网服务器IP地址永远是 192.168.1.111,之所以没在系统里设置因为偶尔会折腾废了会重装系统,等于我有需要重新配置一遍固定IP,所以为了方便,我直接在光猫层面通过网卡地址直接固定IP了!

Snipaste_2022-04-18_23-23-53.png

再然后就是为了方便我自己部署项目,内网服务器装了宝塔面板和docker,当然这里我只需要提一嘴有宝塔面板即可,因为待会操作需要在宝塔面板里开放端口号,docker跟本博文内容无关,就不细讲了!

我的需求

实现内网文件共享方式有很多,并且在我有宝塔面板的情况下就更简单的,最简单粗暴的就是直接开个FTP账号,但是FTP存在一个问题,就是如果是文件夹有中文名,在windows自带的资源管理器里,通过协议访问(如:ftp://192.168.1.111)这样,中文会乱码,而且一定会要求输入账号密码,本身我内网有完全可以不需要账号密码的;因为家里除了我也没人懂这个;外网又连不进来(虽然有公网IP,但这个公网IP各位都懂48小时运营商自动给你掉线重置,我想也没有哪个大哥闲着无聊天天想办法弄我家公网IP然后黑进来,没啥意义),所以我现在要实现的需求最好的解决方案,在windows下,不需要额外装第三方软件的,就是smb协议了,也就是上面说的共享文件夹,而且一定程度也可以交互也和windows资源管理器同步,也可以直接匿名使用,可以满足我的所有诉求!

准备工作

首先,我们开放445端口,这是smb协议的默认端口,当然我这里是从宝塔面板里面开放的,如果你没装宝塔,那就自己通过执行命令开放端口,当然,这不在本篇博文解释范围内,因为我就喜欢傻瓜式操作!

Snipaste_2022-04-18_23-31-23.png

端口开放后,然后确定下诉求,我这个内网小服务器本来是做软路由的,结果没软起来,后来就纯当本地Web服务器用了,小机器,一共2个硬盘,一个mSATA的固态,一个我从就笔记本下拆下来的2.5寸1T硬盘,很显然,我折腾这个的目的就是做文件备份,所以很显然是存到1T硬盘里的,因为这里我已经吧硬盘挂载好了,所以这里简单配个图,也不展示具体怎么分区格式化盘挂载的了;

Snipaste_2022-04-18_23-35-18.png

搭建SMB服务

其实搭建SMB服务很简单,主要是搞配置文件分配权限这里恶心人;首先装smb很简单,就一句话即可;

yum install -y samba samba-client samba-common

装完了默认就会有配置文件,配置文件路径:/etc/samba/smb.conf,然后编辑这个配置文件,怎么编辑我就不说了,这都是linux基本功;主要说一下配置文件和分享一下我的配置;(我的诉求是匿名直接使用,就是在windows下直接双反斜杠+IP就可以使用,不需要输入账号密码!)

默认配置

# 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

完整配置文件

# This is the main Samba configuration file. For detailed information about the
# options listed here, refer to the smb.conf(5) manual page. Samba has a huge
# number of configurable options, most of which are not shown in this example.
#
# The Samba Wiki contains a lot of step-by-step guides installing, configuring,
# and using Samba:
# https://wiki.samba.org/index.php/User_Documentation
#
# In this file, lines starting with a semicolon (;) or a hash (#) are
# comments and are ignored. This file uses hashes to denote commentary and
# semicolons for parts of the file you may wish to configure.
#
# NOTE: Run the "testparm" command after modifying this file to check for basic
# syntax errors.
#
#---------------
# Security-Enhanced Linux (SELinux) Notes:
#
# Turn the samba_domain_controller Boolean on to allow a Samba PDC to use the
# useradd and groupadd family of binaries. Run the following command as the
# root user to turn this Boolean on:
# setsebool -P samba_domain_controller on
#
# Turn the samba_enable_home_dirs Boolean on if you want to share home
# directories via Samba. Run the following command as the root user to turn this
# Boolean on:
# setsebool -P samba_enable_home_dirs on
#
# If you create a new directory, such as a new top-level directory, label it
# with samba_share_t so that SELinux allows Samba to read and write to it. Do
# not label system directories, such as /etc/ and /home/, with samba_share_t, as
# such directories should already have an SELinux label.
#
# Run the "ls -ldZ /path/to/directory" command to view the current SELinux
# label for a given directory.
#
# Set SELinux labels only on files and directories you have created. Use the
# chcon command to temporarily change a label:
# chcon -t samba_share_t /path/to/directory
#
# Changes made via chcon are lost when the file system is relabeled or commands
# such as restorecon are run.
#
# Use the samba_export_all_ro or samba_export_all_rw Boolean to share system
# directories. To share such directories and only allow read-only permissions:
# setsebool -P samba_export_all_ro on
# To share such directories and allow read and write permissions:
# setsebool -P samba_export_all_rw on
#
# To run scripts (preexec/root prexec/print command/...), copy them to the
# /var/lib/samba/scripts/ directory so that SELinux will allow smbd to run them.
# Note that if you move the scripts to /var/lib/samba/scripts/, they retain
# their existing SELinux labels, which may be labels that SELinux does not allow
# smbd to run. Copying the scripts will result in the correct SELinux labels.
# Run the "restorecon -R -v /var/lib/samba/scripts" command as the root user to
# apply the correct SELinux labels to these files.
#
#--------------
#
#======================= Global Settings =====================================

[global]

# ----------------------- Network-Related Options -------------------------
#
# workgroup = the Windows NT domain name or workgroup name, for example, MYGROUP.
#
# server string = the equivalent of the Windows NT Description field.
#
# netbios name = used to specify a server name that is not tied to the hostname,
#                maximum is 15 characters.
#
# interfaces = used to configure Samba to listen on multiple network interfaces.
# If you have multiple interfaces, you can use the "interfaces =" option to
# configure which of those interfaces Samba listens on. Never omit the localhost
# interface (lo).
#
# hosts allow = the hosts allowed to connect. This option can also be used on a
# per-share basis.
#
# hosts deny = the hosts not allowed to connect. This option can also be used on
# a per-share basis.
#
        workgroup = MYGROUP
        server string = Samba Server Version %v

;       netbios name = MYSERVER

;       interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24
;       hosts allow = 127. 192.168.12. 192.168.13.

# --------------------------- Logging Options -----------------------------
#
# log file = specify where log files are written to and how they are split.
#
# max log size = specify the maximum size log files are allowed to reach. Log
# files are rotated when they reach the size specified with "max log size".
#

        # 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

# ----------------------- Standalone Server Options ------------------------
#
# security = the mode Samba runs in. This can be set to user, share
# (deprecated), or server (deprecated).
#
# passdb backend = the backend used to store user information in. New
# installations should use either tdbsam or ldapsam. No additional configuration
# is required for tdbsam. The "smbpasswd" utility is available for backwards
# compatibility.
#

        security = user
        passdb backend = tdbsam


# ----------------------- Domain Members Options ------------------------
#
# security = must be set to domain or ads.
#
# passdb backend = the backend used to store user information in. New
# installations should use either tdbsam or ldapsam. No additional configuration
# is required for tdbsam. The "smbpasswd" utility is available for backwards
# compatibility.
#
# realm = only use the realm option when the "security = ads" option is set.
# The realm option specifies the Active Directory realm the host is a part of.
#
# password server = only use this option when the "security = server"
# option is set, or if you cannot use DNS to locate a Domain Controller. The
# argument list can include My_PDC_Name, [My_BDC_Name], and [My_Next_BDC_Name]:
#
# password server = My_PDC_Name [My_BDC_Name] [My_Next_BDC_Name]
#
# Use "password server = *" to automatically locate Domain Controllers.

;       security = domain
;       passdb backend = tdbsam
;       realm = MY_REALM

;       password server = <NT-Server-Name>

# ----------------------- Domain Controller Options ------------------------
#
# security = must be set to user for domain controllers.
#
# passdb backend = the backend used to store user information in. New
# installations should use either tdbsam or ldapsam. No additional configuration
# is required for tdbsam. The "smbpasswd" utility is available for backwards
# compatibility.
#
# domain master = specifies Samba to be the Domain Master Browser, allowing
# Samba to collate browse lists between subnets. Do not use the "domain master"
# option if you already have a Windows NT domain controller performing this task.
#
# domain logons = allows Samba to provide a network logon service for Windows
# workstations.
#
# logon script = specifies a script to run at login time on the client. These
# scripts must be provided in a share named NETLOGON.
#
# logon path = specifies (with a UNC path) where user profiles are stored.
#
#
;       security = user
;       passdb backend = tdbsam

;       domain master = yes
;       domain logons = yes

        # the following login script name is determined by the machine name
        # (%m):
;       logon script = %m.bat
        # the following login script name is determined by the UNIX user used:
;       logon script = %u.bat
;       logon path = \\%L\Profiles\%u
        # use an empty path to disable profile support:
;       logon path =

        # various scripts can be used on a domain controller or a stand-alone
        # machine to add or delete corresponding UNIX accounts:

;       add user script = /usr/sbin/useradd "%u" -n -g users
;       add group script = /usr/sbin/groupadd "%g"
;       add machine script = /usr/sbin/useradd -n -c "Workstation (%u)" -M -d /nohome -s /bin/false "%u"
;       delete user script = /usr/sbin/userdel "%u"
;       delete user from group script = /usr/sbin/userdel "%u" "%g"
;       delete group script = /usr/sbin/groupdel "%g"


# ----------------------- Browser Control Options ----------------------------
#
# local master = when set to no, Samba does not become the master browser on
# your network. When set to yes, normal election rules apply.
#
# os level = determines the precedence the server has in master browser
# elections. The default value should be reasonable.
#
# preferred master = when set to yes, Samba forces a local browser election at
# start up (and gives itself a slightly higher chance of winning the election).
#
;       local master = no
;       os level = 33
;       preferred master = yes

#----------------------------- Name Resolution -------------------------------
#
# This section details the support for the Windows Internet Name Service (WINS).
#
# Note: Samba can be either a WINS server or a WINS client, but not both.
#
# wins support = when set to yes, the NMBD component of Samba enables its WINS
# server.
#
# wins server = tells the NMBD component of Samba to be a WINS client.
#
# wins proxy = when set to yes, Samba answers name resolution queries on behalf
# of a non WINS capable client. For this to work, there must be at least one
# WINS server on the network. The default is no.
#
# dns proxy = when set to yes, Samba attempts to resolve NetBIOS names via DNS
# nslookups.

;       wins support = yes
;       wins server = w.x.y.z
;       wins proxy = yes

;       dns proxy = yes

# --------------------------- Printing Options -----------------------------
#
# The options in this section allow you to configure a non-default printing
# system.
#
# load printers = when set you yes, the list of printers is automatically
# loaded, rather than setting them up individually.
#
# cups options = allows you to pass options to the CUPS library. Setting this
# option to raw, for example, allows you to use drivers on your Windows clients.
#
# printcap name = used to specify an alternative printcap file.
#

        load printers = yes
        cups options = raw

;       printcap name = /etc/printcap
        # obtain a list of printers automatically on UNIX System V systems:
;       printcap name = lpstat
;       printing = cups

# --------------------------- File System Options ---------------------------
#
# The options in this section can be un-commented if the file system supports
# extended attributes, and those attributes are enabled (usually via the
# "user_xattr" mount option). These options allow the administrator to specify
# that DOS attributes are stored in extended attributes and also make sure that
# Samba does not change the permission bits.
#
# Note: These options can be used on a per-share basis. Setting them globally
# (in the [global] section) makes them the default for all shares.

;       map archive = no
;       map hidden = no
;       map read only = no
;       map system = no
;       store dos attributes = yes


#============================ 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

# Un-comment the following and create the netlogon directory for Domain Logons:
;       [netlogon]
;       comment = Network Logon Service
;       path = /var/lib/samba/netlogon
;       guest ok = yes
;       writable = no
;       share modes = no

# Un-comment the following to provide a specific roaming profile share.
# The default is to use the user's home directory:
;       [Profiles]
;       path = /var/lib/samba/profiles
;       browseable = no
;       guest ok = yes

# A publicly accessible directory that is read only, except for users in the
# "staff" group (which have write permissions):
;       [public]
;       comment = Public Stuff
;       path = /home/samba
;       public = yes
;       writable = no
;       printable = no
;       write list = +staff
  1. 首先,默认配置尽量不要改,不懂就不要改;
  2. 全局 [global] 配置加一个 map to guest = Bad User
  3. 然后新加一个节点,这个节点就是你共享的文件夹信息,参考配置如下
[nas]
        path = /data/
        public = true
        browseable = yes
        writable = yes
        create mode = 0644
        force create mode = 0644
        directory mode = 0755
        force directory mode = 0755

你也不要问我这些都表示啥意思,反正你就按照我这个复制粘贴,100%能用!(当然!path = /data/ 这个你得根据自己实际需求改动)

然后,你需要修改对应的共享目录权限,比如我这里是 /data/,我就需要执行如下命令给予权限

chmod -R 777 /data/

如果你不给权限,那么就直接查(浏览),不能增删改(新建文件、文件夹等操作)

然后开启(重启)下 smb 服务就好了!就这么简单!命令如下:

service smb restart

最后肯定就是开机自启动了,一句话:chkconfig smb on 即可!简单NAS备份做好了,寒酸一点不丢人,效果达到就可以!

2022年4月21日 补充

如果需要在公网环境下使用(如:有公网IP),那就最好有个账号密码机制,否则基本就是裸奔了!如何让samba使用账号机制呢?很简单,你只需要自己在系统里建立个账户,就是 useradd 用户名,然后使用 smbpasswd 用户名 即可设置该账号在 smb 协议下的密码,然后下面分享一下我的配置(需要走账号机制的配置)

# 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
        map to guest = Bad User

[hdd16t_01]
        path = /data/hdd16t_01
        valid users = huangjunjie
        create mask = 0755
        directory mask = 0755
        writeable = true

[hdd16t_02]
        path = /data/hdd16t_02
        valid users = huangjunjie
        create mask = 0755
        directory mask = 0755
        writeable = true