如题

检查是否安装

1
2
3
4
[root@AndyCentOS7Basic ~]# rpm -qi samba
package samba is not installed
[root@AndyCentOS7Basic ~]# rpm -qa | grep samba
[root@AndyCentOS7Basic ~]#

安装

1
2
[root@AndyCentOS7Basic ~]# yum -y install samba samba-client
...

安装好后,再次查看

1
2
3
4
5
6
7
8
[root@AndyCentOS7Basic school-docs]# rpm -qa | grep samba
samba-common-libs-4.6.2-12.el7_4.x86_64
samba-client-libs-4.6.2-12.el7_4.x86_64
samba-libs-4.6.2-12.el7_4.x86_64
samba-client-4.6.2-12.el7_4.x86_64
samba-common-4.6.2-12.el7_4.noarch
samba-common-tools-4.6.2-12.el7_4.x86_64
samba-4.6.2-12.el7_4.x86_64

另外一台电脑,没选择安装 samba-client

1
2
3
4
5
6
7
8
9
[root@AndyCentOS7Basic ~]# yum -y install samba
...
[root@AndyCentOS7Basic ~]# rpm -qa | grep samba
samba-common-libs-4.6.2-12.el7_4.x86_64
samba-client-libs-4.6.2-12.el7_4.x86_64
samba-libs-4.6.2-12.el7_4.x86_64
samba-4.6.2-12.el7_4.x86_64
samba-common-4.6.2-12.el7_4.noarch
samba-common-tools-4.6.2-12.el7_4.x86_64

配置

1
2
3
4
5
6
7
8
9
[root@AndyCentOS7Basic ~]# cd /etc/samba/
[root@AndyCentOS7Basic samba]# ll
total 20
-rw-r--r-- 1 root root 20 Nov 28 00:21 lmhosts
-rw-r--r-- 1 root root 667 Nov 28 00:21 smb.conf
-rw-r--r-- 1 root root 11327 Nov 28 00:21 smb.conf.example

[root@AndyCentOS7Basic samba]# mv smb.conf smb.conf.origin
[root@AndyCentOS7Basic samba]# vim 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
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = no
cups options = raw

map to guest = Bad User
[school-docs]
path = /opt/school-docs
public = no
create mask = 0765
writable = yes
valid users = apg
printable = no

[apgHome]
path = /opt/apg
public = no
writable = yes
valid users = apg
printable = no
create mask = 0765

[anonymous]
comment = anonymous
path = /opt/anonymous
public = yes
writeable = yes
create mask = 0644

设置开机启动

1
2
[root@AndyCentOS7Basic ~]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.

创建用户

1
2
3
4
5
6
7
8
9
10
11
12
[root@AndyCentOS7Basic samba]# groupadd gsmb
[root@AndyCentOS7Basic samba]# useradd aipeigo -g gsmb -s /sbin/nologin
[root@AndyCentOS7Basic samba]# smbpasswd -a aipeigo
New SMB password:
Retype new SMB password:
Added user aipeigo.

# 指定主目录的方式创建另外一个用户
[root@AndyCentOS7Basic samba]# useradd -g gsmb -d /opt/apg -s /sbin/nologin apg
[root@AndyCentOS7Basic samba]# smbpasswd -a apg
New SMB password:apg
Retype new SMB password:apg

创建共享目录并设置权限

1
2
3
4
5
6
7
mkdir -p /opt/school-docs
cd /opt/
chown aipeigo:gsmb -R school-docs/
chmod 777 school-docs

mkdir -p /opt/anonymous
chown nobody:nobody anonymous/

smb 启动、重启等命令

  1. systemctl start smb # 启动
  2. systemctl restart smb #重启
  3. systemctl enable smb #修改为开机启动
  4. systemctl status smb #查看状态
  5. 使用 testparm 测试 samba 配置是否正确

备注:win 7 与 win 10 专业版可正常访问,win 10 教育版无法访问

解决办法,参见:windows 10 访问 CentOS 7 的 SMB 共享文件

遇到的操作错误

Failed to add entry for user aipeigo

1
2
3
4
[root@AndyCentOS7Basic samba]# smbpasswd -a aipeigo
New SMB password:
Retype new SMB password:
Failed to add entry for user aipeigo

这是因为没有加相应的系统账号

1
2
groupadd gsmb
useradd aipeigo -g gsmb -s /sbin/nologin

You need permission to perform this action

进入共享目录,创建文件或文件夹,提示没有权限

1
You need permission to perform this action

解决:禁用 SELinux
vim /etc/selinux/config
设置 SELINUX 为:SELINUX=disabled
然后重启服务器