0%

samba服务的搭建与管理

Smaba是一个局域网文件共享服务,主要功能是在Linux和Windows之间共享文件。samba使用起来非常的方便,可以用Windows的文件管理器直接打开Samba共享的文件。本文主要介绍了Samba服务的搭建与管理,以及Windows管理Samba连接。

安装

直接使用包管理器安装即可。

1
2
3
4
# Ubuntu
apt-get install samba
# CentOS
yum install samba

配置文件

Samba的配置文件是/etc/samba/smb.conf,配置文件由若干个section组成,每个section包含若干个参数。section开头由section name和方括号构成,例如[section name]。参数类似于name = value。有关配置文件的更多介绍,可以参考smb.conf.5。下面列出了主要section及其参数。

除了[global],每个section都是一个共享资源,共享资源的名字是section的名字。[homes][printers]是两个特殊的共享资源,分别用于共享家目录和打印机。

global

global section中的参数适用于整个服务,或者是某些未定义参数的默认值。

homes

共享名会被更改为用户名。如果path参数没有指定,则path默认指向登录用户的家目录。这是为用户共享家目录最快最简单的方法。如果在homes中指定了访客访问权限,则所有主目录都将对所有客户端可见,而无需密码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Un-comment the following (and tweak the other settings below to suit)
# to enable the default home directory shares. This will share each
# user's home directory as \\server\username
[homes]
comment = Home Directories
browseable = yes

# By default, the home directories are exported read-only. Change the
# next parameter to 'no' if you want to be able to write to them.
read only = no

# File creation mask is set to 0700 for security reasons. If you want to
# create files with group=rw permissions, set next parameter to 0775.
create mask = 0600

# Directory creation mask is set to 0700 for security reasons. If you want to
# create dirs. with group=rw permissions, set next parameter to 0775.
directory mask = 0700

# By default, \\server\username shares can be connected to by anyone
# with access to the samba server.
# Un-comment the following parameter to make sure that only "username"
# can connect to \\server\username
# This might need tweaking when using external authentication schemes
valid users = %S

printers

TODO

自定义section

可按如下模板新增一个共享文件夹。

1
2
3
4
5
6
7
8
9
10
[share name]
comment = 任意注释字符串
path = /path/to/share
write list = user1[,user2]
force group = group
force user = user
create mask = 0664
valid users = user1[,user2]
browseable = yes
read only = No

用户管理

可以使用smbpasswd命令管理samba的用户。如果没有打开访客访问,则必须要登录用户才能访问samba服务。常用的选项如下:

  • -a 添加用户。如果不指定用户名,则使用当前登录用户。
  • -d 关闭用户
  • -e 使能用户
  • -x 删除用户

命令pdbedit提供了更多管理用户的选项。

  • -L 列出所有的用户

服务管理

更改配置文件之后,需要重启服务配置才能生效。samba的服务名是smbd,可以用service或systemctl来重启。

1
2
service smbd restart
systemctl restart smbd

开机自启动samba服务。

1
systemctl enable smbd

samba服务正常运行后,可以使用指令smbstatus查看samba服务的状态,主要包含客户端的连接信息。

Windows管理Samba

访问Samba

在Windows文件管理器的地址栏输入\\ip即可访问Samba服务。

管理Samba连接

cmd输入指令net use可以查看当前已建立连接的Samba,需要重点管理远程一列。

1
2
3
4
5
6
7
8
9
C:\Users\admin>net use
会记录新的网络连接。


状态 本地 远程 网络

-------------------------------------------------------------------------------
OK W: \\192.168.50.1\share Microsoft Windows Network
命令成功完成。

如果想断开某个连接,可以使用net use [远程] /del命令,例如net use \\192.168.50.1\share /del。命令net use * /del删除所有的Samba连接。

Smaba凭据

进入控制面板->用户账户->管理Windows凭据,则可以看到登录Samba服务所使用的用户名。如果想使用其他用户登录Samba服务,删除凭据后重启访问即可,这时会要求重新输入用户名和密码。

参考资料