http://www.centoscn.com/CentosServer/ftp/2015/0722/5896.html
这篇指南介绍了如何在CentOS7中配置匿名和安全的Samba服务器。Samba是一个开源/*软件套件,提供无缝的文件和打印服务SMB/CIFS客户端.Samba是免费的,不像其他的SMB/CIFS的实现着,Samba允许在Linux/Unix服务器和给予Windows的客户端之间协同工作,
我有个一新安装的CentOS7 服务器,这是我要安装Samba服务器的机子。当然,你需要有一个windows机器来检查这台CentOS7的服务器是可被连接的。
我的CentOS7的服务器主机名:server1.example.com ,IP是:192.168.0.100
注意:Windows 机器必须和这台CentOS7在同一个工作组。可以用下面的命令来检查:

为了确保windows机器可以在窗口中处理类似的访问。在终端中添加服务器IP地址:
|
notepad C:\Windows\System32\drivers\etc\hosts
|
在我的例子中是这样保存的:
[...]
192.168.0.100 server1.example.com centos
|
首先我将解释安装Samba和匿名共享的方法。安装Samba运行
|
yum
install
samba samba-client samba-common
|
安装之后请备份一份原始的Samba配置文件:
1
|
mv
/etc/samba/smb
.conf
/etc/samba/smb
.conf.bak
|
然后来新建我们的conf文件:
vi
/etc/samba/smb
.conf
[global]
workgroup = WORKGROUP
server string = Samba Server %
v
netbios name = centos
security = user
map to guest = bad user
dns proxy = no
[Anonymous]
path =
/samba/anonymous
browsable =
yes
writable =
yes
guest ok =
yes
read
only = no
:wq
|
mkdir
-p
/samba/anonymous
systemctl
enable
smb.service
systemctl
enable
nmb.service
systemctl restart smb.service
systemctl restart nmb.service
|
在下面CentOs7的防火墙cmd会阻止Samba的访问,为了摆脱这个,我们运行:
[root@server1 ~]
success
[root@server1 ~]
[root@server1 ~]
success
[root@server1 ~]
|
现在你可以在Windows中访问CentOS7的共享文件了,在命令提示行中输入:

下面是浏览到的文件夹,如果你尝试着去创建一个文件,你会得到一个没有权限的错误信息、


检查这个共享文件夹的权限:
drwxr-xr-x. 2 root root 6 Jul 17 13:41 anonymous
[root@server1 samba]#
我们下面给匿名用户一个权限:
cd
/samba
chmod
-R 0755 anonymous/
chown
-R nobody:nobody anonymous/
ls
-l anonymous/
|
total 0
drwxr-xr-x. 2 nobody nobody 6 Jul 17 13:41 anonymous
[root@server1 samba]#
进一步我们需要配置Selinux
|
chcon -t samba_share_t anonymous/
|
现在我们可以浏览和创建文件了

在服务器上查看:
total 0
-rwxr--r--. 1 nobody nobody 0 Jul 17 16:05 anonymous.txt
[root@server1 samba]#
为了这个,我创建了一个组:smbgrp 和用户srijan通过认证来访问Samba服务器。
groupadd smbgrp
useradd
srijan -G smbgrp
smbpasswd -a srijan
[root@server1 samba]
New SMB password:<--yoursambapasswordRetype new SMB password:<--yoursambapasswordAdded user srijan.
[root@server1 samba]
|
现在在Samba文件夹下创建一个文件夹:Secured ,并且给出权限:
同样的,我们允许Selinux来监听:
cd
/samba
chmod
-R 0777 secured/
chcon -t samba_share_t secured/
|
再次编辑配置文件:
vi
/etc/samba/smb
.conf
[...]
[secured]
path =
/samba/secured
valid
users
= @smbgrp
guest ok = no
writable =
yes
browsable =
yes
systemctl restart smb.service
systemctl restart nmb.service
|
像下面这样测试:
testparm
[root@server1 samba]
Load smb config files from
/etc/samba/smb
.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section
"[Anonymous]"
Processing section
"[secured]"
Loaded services
file
OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions <--ENTER[global]
netbios name = CENTOS
server string = Samba Server %
v
map to guest = Bad User
dns proxy = No
idmap config * : backend = tdb
[Anonymous]
path =
/samba/anonymous
read
only = No
guest ok = Yes
[secured]
path =
/samba/secured
valid
users
= @smbgrp
read
only = No
[root@server1 samba]
|
现在在windows机器中可以使用相应的凭证来查看文件夹。

你的用户srijan同样面对着写入权限的问题,让我们来给出权限:
cd
/samba
chown
-R srijan:smbgrp secured/
|
现在Samba用户在共享的目录中有写入的权限了,开始High吧.

注:
如果你的 chcon命令不成功,请按下面的方式尝试:
|
chcon -h system_u:object_r:forderA
/path/to/B
|