局域网下使用samba服务在Linux系统与Windows系统直接共享文件是一项很方便的操作。Linux服务器的版本是Ubuntu 18.04.3 LTS。

在终端执行下列指令,查看当前 Ubuntu 的版本号

  1. root@SERVER-214:~# cat /etc/issue
  2. Ubuntu 18.04.3 LTS \n \l

在Ubuntu上安装Samba

  1. 更新 apt

    sudo apt update
    
  2. 使用以下命令安装Samba软件包

    sudo apt install samba
    
  3. 安装完成后,Samba服务将自动启动。检查Samba服务器是否正在运行

    sudo systemctl status smbd
    
  4. 输出如下所示,表明Samba服务处于活动状态并正在运行 ```shell root@SERVER-214:~# sudo systemctl status smbd ● smbd.service - Samba SMB Daemon Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-03-02 13:15:45 UTC; 3min 16s ago Docs: man:smbd(8)

        man:samba(7)
        man:smb.conf(5)
    

    Main PID: 5049 (smbd) Status: “smbd: ready to serve connections…” Tasks: 4 (limit: 1108) CGroup: /system.slice/smbd.service

        ├─5049 /usr/sbin/smbd --foreground --no-process-group
        ├─5068 /usr/sbin/smbd --foreground --no-process-group
        ├─5069 /usr/sbin/smbd --foreground --no-process-group
        └─5079 /usr/sbin/smbd --foreground --no-process-group
    

Mar 02 13:15:45 SERVER-214 systemd[1]: Starting Samba SMB Daemon… Mar 02 13:15:45 SERVER-214 systemd[1]: Started Samba SMB Daemon.


至此,已经安装了Samba并准备对其进行配置。

<a name="67a055b3"></a>
### 配置防火墙

如果你的Ubuntu系统上运行的是防火墙,则需要在端口 `137` 和 `138` 上允许传入 UDP 连接,在端口 `139` 和 `445` 上允许 TCP 连接。

```shell
sudo ufw allow 'Samba'

配置全局Samba选项

在更改Samba配置文件之前,建议创建一个备份

sudo cp /etc/samba/smb.conf{,.backup}

Samba 软件包随附的默认配置文件是为Samba 服务器配置的, 打开文件,并确保服务器角色设置为standalone

sudo vim /etc/samba/smb.conf
...
# Most people will want "standalone sever" or "member server".
# Running as "active directory domain controller" will require first
# running "samba-tool domain provision" to wipe databases and create a
# new domain.
   server role = standalone server
...

默认情况下,Samba 在所有接口上侦听。如果要限制仅从内部网络访问 Samba 服务器,取消注释以下两行并指定要绑定到的接口

...
# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
interfaces = 127.0.0.0/8 eth0

# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
# It is recommended that you enable this feature if your Samba machine is
# not protected by a firewall or is a firewall itself.  However, this
# option cannot handle dynamic or non-broadcast interfaces correctly.
bind interfaces only = yes
...

完成后,运行 testparm实用程序以检查 Samba 配置文件中是否有错误。如果没有语法错误,将看到 Loaded services file OK。 最后,使用以下命令重新启动Samba服务:

How to Install and Configure Samba on Ubuntu 18.04
sudo systemctl restart smbd
sudo systemctl restart nmbd

参考