如何在 Ubuntu 18 上安装和配置 Samba

什么是桑巴?

Samba 是一个开源软件包,在 GPL(通用公共许可证)下发布。 它允许我们使用 SMB/CIFS 协议跨各种操作系统访问共享网络驱动器和打印机。 Samba 具有客户端和服务器组件。 Samba 使用 SMB 协议,这在从 Microsoft 计算机访问文件服务器上的资产时是必需的。 Samba 还可以用作与 Microsoft Active Directory 兼容的域控制器。

介绍

在本教程中,我们将把我们的 Ubuntu 服务器设置为可以从 Windows 计算机轻松访问的文件存储平台。 我们将使用 Samba 软件来启用此连接。 首先,我们将更新可用于安装的软件包数据库。

root@host:~# apt-get update && apt-get -y upgrade
 Hit:1 https://by.archive.ubuntu.com/ubuntu bionic InRelease
 Hit:2 https://by.archive.ubuntu.com/ubuntu bionic-updates InRelease
 Hit:3 https://by.archive.ubuntu.com/ubuntu bionic-backports InRelease
 Hit:4 https://security.ubuntu.com/ubuntu bionic-security InRelease
 Reading package lists... Done
 Building dependency tree
 Reading state information... Done
 Calculating upgrade... Done
 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@host:~#

桑巴安装

接下来,我们将开始在我们的 Ubuntu 服务器上安装 Samba。

root@host:~# apt-get install samba
 Reading package lists... Done
 Building dependency tree
 Reading state information... Done
The following additional packages will be installed:
   attr ibverbs-providers libcephfs2 libibverbs1 libnl-route-3-200
   libpython-stdlib librados2 python python-crypto python-dnspython python-ldb
   python-minimal python-samba python-tdb python2.7 python2.7-minimal
   samba-common samba-common-bin samba-dsdb-modules samba-vfs-modules
   tdb-tools
Suggested packages:
   python-doc python-tk python-crypto-doc python-gpgme python2.7-doc
   binfmt-support bind9 bind9utils ctdb ldb-tools ntp | chrony smbldap-tools
   winbind heimdal-clients
 The following NEW packages will be installed:
   attr ibverbs-providers libcephfs2 libibverbs1 libnl-route-3-200
   libpython-stdlib librados2 python python-crypto python-dnspython python-ldb
   python-minimal python-samba python-tdb python2.7 python2.7-minimal samba
   samba-common samba-common-bin samba-dsdb-modules samba-vfs-modules
   tdb-tools
 0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.
 Need to get 9.518 kB of archives.
 After this operation, 52,7 MB of additional disk space will be used.
 Do you want to continue? [Y/n] Y
 …
 …
 Processing triggers for mime-support (3.60ubuntu1) ...
 Processing triggers for ureadahead (0.100.0-21) ...
 Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
 Processing triggers for libc-bin (2.27-3ubuntu1) ...
 Processing triggers for systemd (237-3ubuntu10.39) ...
 Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
 Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
 Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
root@host:~#

桑巴配置

接下来,我们将定制 Samba 以满足我们的特定需求。 我们将需要访问安装配置文件的目录。 这些文件位于 /etc/桑巴。

在配置文件中,我们将创建和使用许多默认配置设置。 现在,我们将备份/重命名默认配置文件,以防我们需要将文件恢复为默认设置。

让我们进入那个目录。

root@host:~# cd /etc/samba
root@host:~# /etc/samba#

现在,让我们看一下文件列表。

root@host:~# /etc/samba# ls -l
 total 20
 -rw-r--r-- 1 root root 8 сту 14 19:11 gdbcommands
 -rw-r--r-- 1 root root 9542 сак 26 17:16 smb.conf
 drwxr-xr-x 2 root root 4096 сту 14 19:11 tls
root@host:~# /etc/samba#

如您所见,我们的 配置文件配置 文件位于此处。 我们将通过重命名来制作原始文件的备份副本。 为此,我们将使用“mv”命令,因为我们将暂时创建一个新的 smb.conf 文件。 这 MV 命令可以移动文件并重命名它。

root@host:~# /etc/samba# mv smb.conf smb.conf.old
root@host:~# /etc/samba#
root@host:~# /etc/samba# ls -l
 total 20
 -rw-r--r-- 1 root root 8 сту 14 19:11 gdbcommands
 -rw-r--r-- 1 root root 9542 сак 26 17:16 smb.conf.old
 drwxr-xr-x 2 root root 4096 сту 14 19:11 tls
root@host:~# /etc/samba#

下一步是创建一个新的配置文件。 我们将为此使用“touch”命令。

root@host:~# /etc/samba# touch smb.conf
root@host:~# /etc/samba#

现在,我们将创建一个“smbusers”文件。 此文件存储将访问文件服务器的人员的用户名和密码。 此信息已加密,因此不会有任何安全问题。 这是使用 smb.conf 文件中的设置 ‘encrypt passwords = yes’ 完成的。

root@host:~# /etc/samba# touch smbusers
root@host:~# /etc/samba#
root@host:~# /etc/samba# ls -l
 total 20
 -rw-r--r-- 1 root root 8 сту 14 19:11 gdbcommands
 -rw-r--r-- 1 root root 0 сак 26 17:33 smb.conf
 -rw-r--r-- 1 root root 9542 сак 26 17:16 smb.conf.old
 -rw-r--r-- 1 root root 0 сак 26 17:33 smbusers
 drwxr-xr-x 2 root root 4096 сту 14 19:11 tls
root@host:~# /etc/samba#

现在,我们将创建一个将在网络上共享的文件夹。

root@host:~# /etc/samba# mkdir /opt/FirstTestShare
root@host:~# /etc/samba#

接下来,我们必须应用允许我们的用户读取、写入和修改它的权限集。

root@host:~# /etc/samba# chmod 0777 /opt/FirstTestShare
root@host:~# /etc/samba#

现在我们将使用 nano 编辑器打开并配置 smb.conf。

root@host:~# nano smb.conf

配置文件由几个部分组成。 中心部分称为 [global]. 根据您的需要,可能会添加其他部分,其中可能包括: [Documents], [Files], 和 [Video].

最初,我们将设置主要部分并为我们的服务器提供一个名称。

server string = Test Fileserver Samba

接下来,我们将识别存在于 Windows 网络上的工作组。 工作组被标识为一个唯一的对象,它是给定网络环境中所有计算机都可以看到和使用的公共资源。 工作组的默认名称是 WORKGROUP。

workgroup = WORKGROUP

现在,让我们设置文件服务器角色。 Samba 有多种功能,但今天,我们将使用它作为一个简单的文件服务器。

server role = standalone server

接下来,我们将查看“配置授权”设置。 我们需要启用此选项以允许访问文件服务器、下载文件、读取、修改和删除,仅适用于添加了用户名和密码的用户。 我们将使用该值 用户 – 通过登录名和密码授权。

security = user

在此之后,我们需要指定用户数据库文件。 此文件识别并存储我们为其提供访问权限的用户。

smb passwd file = /etc/samba/smbusers

接下来,我们应该启用“强制启用密码加密”设置。 此设置对于安全问题至关重要,因为使用加密密码会显着降低纯文本密码在流量通过网络时被“嗅探”的能力,例如当用户登录 Samba 共享时。

encrypt passwords = yes

此外,我们还可以禁止访客访问共享。 使用此设置,我们可以禁止访客用户登录和授权尝试。

map to guest = bad user

接下来,我们可以确定应用程序日志的存储方式和位置。 一个数字定义了日志记录级别。 数字越高(从 0 -7),将写入的日志记录级别越详细。 我们收到的有关应用程序操作的信息越多越好。

然而,这也有不利的一面。 为什么设置更高级别的日志记录会不好? 仅仅因为这会产生大量信息并且文件大小会显着增加,从而占用宝贵的磁盘空间。 通常,仅出于调试目的才需要更高的日志记录级别。 如果您是开发人员或需要跟踪错误,则可以将其设置为更高级别,如果您需要基本日志记录,则可以使用中等数量。

logfile = /var/log/samba/log.%m

现在,我们可以建立文件夹的名称。 这将是共享文件夹的可见名称。 我们使用了这个名字。

[FirstTestShare]

我们将查看的下一个设置称为注释。 此注释将是当我们将鼠标悬停在文件夹上时将显示的工具提示值。

comment = Our First Test Share

接下来,必须指出我们将共享的文件夹的路径。

path = /opt/FirstTestShare

使用下一个设置,我们指示该文件夹应该是可见的。 如果未指定此值,则文件夹将被隐藏。

browseable = yes

现在,我们可以允许用户写入共享文件夹。

writeable = yes

接下来,我们可以确定将分配给上传到共享文件夹的文件的权限级别。

create mask = 0775

此外,对于目录。

directory mask = 0775

现在我们已经完成了配置文件,使用以下命令保存它。
ctrl + X 然后 Y 然后输入

保存配置文件后,我们需要重启服务。

root@host:~#/etc/samba# service smbd restart
root@host:~#/etc/samba#

现在,我们可以将服务添加到 systemctl 启动配置中。

root@host:~#/etc/samba# systemctl enable smbd
 Synchronizing state of smbd.service with SysV service script with /lib/systemd/systemd-sysv-install.
 Executing: /lib/systemd/systemd-sysv-install enable smbd
root@host:~#/etc/samba#

接下来,我们可以验证 Samba 是否已启动并正在运行。

service smbd status

重要说明,该服务之前称为 Samba,后来重命名为 smbd 在ubuntu 16. 如果无法启动smbd服务,试试samba命令

现在我们可以在网络环境中看到我们的共享文件夹。 我们将使用 ifconfig 命令定位我们的 IP 地址。

root@host:~# ifconfig
 Command 'ifconfig' not found, but can be installed with:
 apt install net-tools
root@host:~#

如果您收到与上述类似的错误消息,我们只需要安装一个名为 nettools 的附加软件包。

root@host:~#/etc/samba# apt install net-tools
 Reading package lists... Done
 Building dependency tree
 Reading state information... Done
 The following packages were automatically installed and are no longer required:
   efibootmgr fonts-liberation2 fonts-opensymbol gir1.2-geocodeglib-1.0
   gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gir1.2-gudev-1.0
   gir1.2-udisks-2.0 grilo-plugins-0.3-base gstreamer1.0-gtk3
   libboost-date-time1.65.1 libboost-filesystem1.65.1 libboost-iostreams1.65.1
   libboost-locale1.65.1 libcdr-0.1-1 libclucene-contribs1v5
   libclucene-core1v5 libcmis-0.5-5v5 libcolamd2 libdazzle-1.0-0
   libe-book-0.1-1 libedataserverui-1.2-2 libeot0 libepubgen-0.1-1
   libetonyek-0.1-1 libevent-2.1-6 libexiv2-14 libfreerdp-client2-2
   libfreerdp2-2 libfwup1 libgc1c2 libgee-0.8-2 libgexiv2-2 libgom-1.0-0
   libgpgmepp6 libgpod-common libgpod4 liblangtag-common liblangtag1
   liblirc-client0 libllvm8 liblua5.3-0 libmediaart-2.0-0 libmspub-0.1-1
   libodfgen-0.1-1 libqqwing2v5 libraw16 librevenge-0.0-0 libsgutils2-2
   libssh-4 libsuitesparseconfig5 libvncclient1 libwayland-egl1-mesa
   libwinpr2-2 libxapian30 libxmlsec1 libxmlsec1-nss lp-solve
   media-player-info python3-mako python3-markupsafe syslinux syslinux-common
   syslinux-legacy usb-creator-common
 Use 'apt autoremove' to remove them.
 The following NEW packages will be installed:
   net-tools
 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
 Need to get 194 kB of archives.
 After this operation, 803 kB of additional disk space will be used.
 Get:1 https://by.archive.ubuntu.com/ubuntu bionic/main amd64 net-tools amd64 1.60+git20161116.90da8a0-1ubuntu1 [194 kB]
 Fetched 194 kB in 0s (1.737 kB/s)
 Selecting previously unselected package net-tools.
 (Reading database ... 151714 files and directories currently installed.)
 Preparing to unpack .../net-tools_1.60+git20161116.90da8a0-1ubuntu1_amd64.deb ...
 Unpacking net-tools (1.60+git20161116.90da8a0-1ubuntu1) ...
 Setting up net-tools (1.60+git20161116.90da8a0-1ubuntu1) ...
 Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
root@host:~#/etc/samba#

现在,我们可以重复 ifconfig 命令。

root@host:~#/etc/samba# ifconfig
 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 192.168.88.207 netmask 255.255.255.0 broadcast 192.168.88.255
      inet6 fe80::7e73:aaa2:eebd:4266 prefixlen 64 scopeid 0x20<link>
      ether 08:00:27:4a:9e:9d txqueuelen 1000 (Ethernet)
      RX packets 32348 bytes 34851594 (34.8 MB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 13521 bytes 1137635 (1.1 MB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
      inet 127.0.0.1 netmask 255.0.0.0
      inet6 ::1 prefixlen 128 scopeid 0x10<host>
      loop txqueuelen 1000 (Local Loopback)
      RX packets 517 bytes 46151 (46.1 KB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 517 bytes 46151 (46.1 KB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 root@host:~#/etc/samba#

可以看到,我们的 IP 是 192.168.88.207。

视窗连接

要连接到 Windows 上的共享,我们需要进入网络环境并输入我们的 IP 地址。

//192.168.88.207

接下来,因为我们不知道用户名和密码,所以我们将创建它们。 在这里,我们将在文件服务器上本地设置一个用户,然后将它们保存到我们之前创建的 smbusers 文件中。

root@host:~#/etc/samba# useradd -c "samba user n1" -s /sbin/nologin user1
root@host:~#/etc/samba#

我们使用的命令标志在这里被分解。 “-s /sbin/nologin user1”表示用户将无法登录服务器本身。 此设置是可靠的安全措施。

root@host:~#/etc/samba# smbpasswd -a user1
 New SMB password:
 Retype new SMB password:
 Added user user1.
root@host:~#/etc/samba#

现在,我们可以进入文件系统并尝试登录。一旦我们可以登录到文件服务器,我们就可以创建我们的文件夹和文件以及读取其他人的文件和文件夹。 其他用户没有删除我们的任何文件和文件夹所需的权限。 但是,可以使用“创建掩码”和“目录掩码”参数在配置文件中更改此设置。 作为奖励,我们将分析可能出现的问题情况以及如何解决它。

作为一个 example,一位律师在一个组织工作,想查看文件系统中有哪些文件。 作为未经授权的用户,他可以进入共享文件夹并查看文件,但无法更改或编辑它们。 默认情况下,用户 1 和该组中的用户能够编辑和删除文件。

因此,让我们将所有用户包括在同一组中。 我们可以通过创建一个新组来实现这一点。

root@host:~# groupadd buh
root@host:~#

然后我们为 user1 和 user2 设置默认组。

root@host:~# usermod -g buh user1
root@host:~#
root@host:~# usermod -g buh user2
root@host:~#

我们现在可以重新启动 Samba 以应用新的更改。

root@host:~# service smbd restart
root@host:~#

现在,我们可以更改所有文件和文件夹本身的所有权设置。

root@host:~# chown -R user1:buh /opt/FirstTestShare
root@host:~#

现在,user2 将能够编辑 Buh 文件夹中的文件,例如 user1。 您也可以与其他用户一起完成此操作。

这些设置的另一个重要方面是用户可以下载和修改文件,然后将其重新保存到共享文件夹。 但是想象一下这样一种情况,用户打开文件以查看其内容但未能 close 它。 另一个用户想要从其他位置打开此文件。 该文件将被锁定,无法编辑,因为第一个用户已将其打开。 如果另一个用户试图打开该文件,该文件将被打开,但处于只读模式。 他们将能够查看它,但不能编辑它。

在这种情况下,有一个 smbstatus 实用程序可以让我们查看谁打开了文件。

在 Service 部分,我们可以看到有一个 pid 号为 9127 的文件。为了 close 文件并开始处理它,我们可以强制 close 使用带有我们 pid 号的 kill 命令的文件。

sudo kill -9 9127

结论

在本教程中,我们学习了如何安装 Samba 并对其进行自定义以满足我们的需求。 我们创建了用户,设置了用户组来处理和共享文件,最后解决了文件共享过程中可能出现的一些简单问题。

立即与专家交谈!

给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问交谈,了解您今天如何利用这项技术!