小知识:使用Samba在Linux服务器上搭建共享文件服务的方法

最近我们的小团队需要在服务器上共分出一个共享文件夹用于大家存放公共的资源文档, 大家想啊,这肯定很简单呀,在windows下面只要创建相关的windows account,共享某个文件夹,把读/写权限给我们创建的account的,就完成了共享,但在linux下面就没有这么美好了,网上查阅资源资料多指向通过samba完成共享任务,但一些blog只介绍了怎么做,但没有为什么这么 做,搭建工作且不太顺利,对linux算不上熟悉,走了很多弯路,所以通过这篇blog深入理解其中的每一步。

samba的简介

samba是在linux和unix系统上实现smb协议的一个免费软件,由服务器及客户端程序构成。这些是废话….. 来看点有意思的。作者tridgwell申请使用smbserver ( server message block 的简写 ) 注册这个软件的商标, 因为smb 是没有意义的文字而没有办法注册。然后他就翻字典,看到samba一遍正好包含smb几个字母 ,这这个词也是我们熟知的拉丁舞蹈的名称,然后就有了三八这个名字。(自百科)

搭建samba共享目录, 如果需要使用用户名/密码的形式访问共享目录,我们需要先创建linux的user,然后通过smbpasswd创建samba用户(用户名需要一致),原文在这里:

to provide authentication on a standalone host, you have to create the accounts locally on the operating system and additionally in the samba database. by default, samba uses the tdbsam back end and stores the database in the /usr/local/samba/private/passdb.tdb file. optionally set a different location in the smb.conf file using the passdb backend parameter. see the smb.conf 5 man page for details(from https://wiki.samba.org/index.php/setting_up_samba_as_a_standalone_server).

搭建需要用户名验证的共享目录

1. 创建共享目录的用户,  我们这里使用来组(group)来演示

?
1
2
3
4
groupadd smbgrp
useradd fielshare -s /sbin/nologin -g smbgrp -p <password>
#创建同名的smb用户, 这里的密码和local用户的密码是完全独立的,我们最后用的通过smbpasswd创建的用户
smbpasswd -a fielshare

2. 创建需要共享的工作目录,设置好文件夹的权限

?
1
2
3
mkdir -p /srv/samba/secure
chmod -r 0770 /srv/samba/secure
chown -r root:smbgrp /srv/samba/secure

搭建samba共享目录, 如果需要使用用户名/密码的形式访问共享目录,我们需要先创建linux的user,然后通过smbpasswd创建samba用户(用户名需要一致),原文在这里:

3. 修改安全上文

?
1
chcon -t samba_share_t /srv/samba/secure

这条命令是selinux(详见security-enhanced linux)下面的命令, 作用提把/srv/samba/securel切换到samba的上下文中。

4. 修改配置文件smb.conf

修改配置文件之前 ,我们先做好备份工作,以防不测。

?
1
cp /etc/samba/smb.conf /etc/samba/smb.conf.orig

在这里我们有以下事情需要做:

1.在[global] section下修改workgroup为workgroup (就是我的电脑=>属性=> 计算机名看到的工作级的名字)

%小知识:使用Samba在Linux服务器上搭建共享文件服务的方法-猿站网-插图

2.设置[global] 下的netbios name, 这个可以是任意,就是我们在我的芳邻下看到的计算机名称

3. 确定 [global] 下security设置为user

4.添加共享目录的配置
?
1
2
3
4
5
6
7
8
9
10
11
#为暴露在我的芳邻里点进去看到的文件夹名称
[share]
  comment = secure file server share
# 为需要共享的目录
  path = /srv/samba/secure
# 可访问的用户,多用户用空格隔开, 以@开头为用户组
  valid users = @smbgrp
# 关闭匿名访问,设置为no
  guest ok = no
  writable = yes
  browsable = yes

整个smb.conf文件如下:

?
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
26
27
28
29
30
31
32
33
34
# 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 = workgroup
  netbios name = centos
  security = user
  passdb backend = tdbsam
  printing = cups
printcap name = cups
  load printers = no
  cups options = raw
[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
 [share]
  comment = secure file share
  path = /srv/samba/secure
  valid users = @smbgrp
  guest ok = no
  writable = yes
  browsable = yes
  browseable = yes

  注意smb.conf默认会有[home]节点,如果不是不想得一个和用户名同名的文件夹,请删除它。

  完成编辑, 保存配置文件,

%小知识:使用Samba在Linux服务器上搭建共享文件服务的方法-1猿站网-插图

  执行testparm后会得到下面相似的结果,就是说配置文件没有问题

?
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
26
27
28
29
30
31
32
33
34
35
36
[root@localhost software]# testparm
load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum windows limit (16384)
processing section “[printers]”
processing section “[print$]”
processing section “[share]”
loaded services file ok.
server role: role_standalone
press enter to see a dump of your service definitions
# global parameters
[global]
load printers = no
netbios name = centos-share
printcap name = cups
security = user
idmap config * : backend = tdb
cups options = raw
[printers]
browseable = no
comment = all printers
create mask = 0600
path = /var/tmp
printable = yes
[print$]
comment = printer drivers
create mask = 0664
directory mask = 0775
force group = @printadmin
path = /var/lib/samba/drivers
write list = @printadmin root
[share]
comment = secure file share
path = /home/share
read only = no
valid users = @smbgrp
[root@localhost software]#

5. 重启samba服务, 打开我的电脑进行测试

?
1
2
systemctl restart smb.service
systemctl restart nmb.service

由于测试机和linux主机不在同一个网络,我的芳邻里面找不到我配置的芳邻 centos-share, 这里我通过ip直接访问

6. 别忘了添加防火墙,不然你是看不到你的芳邻的

?
1
2
firewall-cmd –permanent –zone=public –add-service=samba
firewall-cmd –reload

%小知识:使用Samba在Linux服务器上搭建共享文件服务的方法-2猿站网-插图

%小知识:使用Samba在Linux服务器上搭建共享文件服务的方法-3猿站网-插图

总结

这里只演示了使用了用户名的验证模式来共享文件夹,主要是针对windows的,对这一块不熟悉的同学可以自行尝试匿名共享。在设置过程中,我接触到以前没有接触到东西selinux,这一块还是有很多的东西的。对于samba的使用介绍网上有不少文章的,写这遍博客的目的也算是多个视角来告诉大家如何使用。

原文链接:https://www.cnblogs.com/rhino/archive/2019/05/14/share-folder-with-samba.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:详解Linux下你所不知道的7个SSH命令用法

2023-3-16 7:28:19

建站知识

小知识:linux用户组以及权限总结

2023-3-16 7:44:53

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索