说明参考
https://www.lisenet.com/2016/samba-server-on-rhel-7/

安装

1
#yum install -y samba samba-client

服务
1
#systemctl enable smb nmb

加防火墙
1
2
#firewall-cmd --permanent --add-service=samba
#firewall-cmd --reload

安装工具
1
#yum -y install policycoreutils-python.x86_64

创建共享目录

1
#mkdir /var/samba/user/samba/

配置权限
1
2
3
#groupadd sambauser
#chgrp sambauser /var/samba/user/samba/
#chmod 755 /var/samba/user/samba/

检查一下默认的SELinux
1
#ls -dZ /var/samba/user/samba/

将samba_share_t上下文类型应用于组共享
1
#semanage fcontext -a -t samba_share_t "/var/samba/user/samba/"

让samba可以创建新的主目录
1
#setsebool -P samba_create_home_dirs=1

要使用Samba共享NFS文件系统
1
#setsebool -P samba_share_nfs=1

写入权限
1
#setsebool -P allow_smbd_anon_write=1

配置Samba

1
#vim /etc/samba/smb.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[group]
comment = Group Share
path = /var/samba/user/samba/%U/
writable = yes
browseable = yes
printable = no
guest ok = no
write list = @sambauser
read list = @sambauser
valid users = @sambauser
create mask = 0770
directory mask = 0770

测试配置
1
#testparm -s

输出Loaded services file OK
其它输出与配置文件一致则配置正确
查看目录
1
#ls -dZ /var/samba/user/samba/

修改目录
1
#chcon -t samba_share_t /var/samba/user/samba/

创建samba用户
1
useradd -s /sbin/nologin -g sambauser samba

samba创建密码
1
passwd samba

centos7上需要给samba创建密码
1
smbpasswd -a samba

启动服务

1
#systemctl start smb nmb

使用脚本sambadduser.sh创建测试用户dev1
1
#./sambadduser.sh x "dev1 dev1"

sambadduser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
if [ ! "$2" ];then
echo "arg2 is empty"
exit 0
fi
n=`echo $2 | cut -d ' ' -f 1`
p=`echo $2 | cut -d ' ' -f 2`

useradd -s /sbin/nologin -g sambauser $n
(echo $p;echo $p) | smbpasswd -s -a $n
mkdir -m 0770 "/var/samba/user/samba/${n}"
chown "${n}:sambauser" "/var/samba/user/samba/${n}"


测试用户dev1
1
#smbclient //localhost/samba -U dev1

使用脚本smbdeluser.sh删除测试用户dev1
1
#./smbdeluser.sh x "dev1"

smbdeluser.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash

if [ ! "$2" ];then
echo "arg2 is empty"
exit 0
fi
smbpasswd -x $2
userdel -r $2
rm -rf /var/samba/user/samba/$2/