一、简介
samba是在Linux和Unix系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB协议是一种在局域网上共享文件和打印机的通信协议,它为局域网内不同的计算机之间提供文件及打印机等服务。Samba服务监听着137/udp,138/udp139/tcp,445/tcp端口,通过结合NetBIOS广播协议及SMB协议,使得samba服务能够实现局域网内的windowns、Linux主机的文件共享。
二、samba服务器的搭建
1、samba服务的安装
首先安装samba服务:
[root@samba ~]# yum install -y samba
.......
Installed:
samba.x86_64 0:4.6.2-12.el7_4
Dependency Installed:
avahi-libs.x86_64 0:0.6.31-17.el7 cups-libs.x86_64 1:1.6.3-29.el7 libldb.x86_64 0:1.1.29-1.el7
libtalloc.x86_64 0:2.1.9-1.el7 libtdb.x86_64 0:1.3.12-2.el7 libtevent.x86_64 0:0.9.31-2.el7_4
libwbclient.x86_64 0:4.6.2-12.el7_4 pytalloc.x86_64 0:2.1.9-1.el7 samba-client-libs.x86_64 0:4.6.2-12.el7_4
samba-common.noarch 0:4.6.2-12.el7_4 samba-common-libs.x86_64 0:4.6.2-12.el7_4 samba-common-tools.x86_64 0:4.6.2-12.el7_4
samba-libs.x86_64 0:4.6.2-12.el7_4
Complete!
关闭firewalld及设置selinux为permissive,避免它们对测试效果造成影响:
[root@samba ~]# systemctl stop firewalld
[root@samba ~]# setenforce 0
2、samba服务的程序及配置文件
安装完成后,samba服务生产的相关的程序及配置文件如下:
主配置文件:/etc/samba/smb.conf
主程序:nmbd:NetBIOS name server 及smbd:SMB/CIFS services
Unit文件:smb.service和nmb.service
其主配置文件/etc/samba/smb.conf由samba-common程序包提供,主要包括以下几个部分:
Global Settings
NetworkRelated Options
Logging Options
Standalone Server Options
Domain Members Options
Domain Controller Options
Browser Control Options
Printing Options
File System Options
Share Definitions
对于/etc/samba/smb.conf文件常见参数及分类如下:
- 全局配置:
[global]
workgroup = MYGROUP #工作组名
server string = Samba Server Version %v #服务器信息介绍
netbios name = MYSERVER #用netbios名来指定服务
interfaces = [interface1 interface2...|address1 address2...] #用于让samba服务监听多个网络接口或IP
hosts allow = [address1 address2...] #指定允许访问的主机IP
log file = /var/log/samba/log.%m #指定日志存放路径,%m为来访的主机名
max log size = 50 #定义日志文件最大容量为50K
security=user #设置samba服务的安全认证方式为user
passdb backend=tdbsam #定义用户后台的类型为tdbsam,其他类型还有smbpasswd、ldapsam
load prints = yes # #设置是否共享打印机
cups options = raw #打印机选项
- 共享文件系统配置的配置大体有三类:
[homes]:为每个samba用户定义其是否能够通过samba服务访问自己的家目录;
[printers]:定义打印服务;
[shared_fs]:定义共享的文件系统;
其常用指令有:
comment=STRING:注释;
path=/PATH/TO/FILENAME:当前共享所映射的文件系统路径;
browseable=YES:是否可浏览,指是否可被用户查看;
guest ok=YES:是否允许来宾账号访问;
browseable = No:是否公开目录
writable=YES:是否可写;
read only = no|yes :是否为只读
write list=/PATH/TO/user_list:拥有写权限的用户列表;
其它可用参数,可通过/etc/samba/smb.conf.example文件或命令man smb.conf
来查看。
此处对/etc/samba/smb.conf配置文件添加如下配置:
[root@samba ~]# vim /etc/samba/smb.conf
[shared_dir]
comment=This is a new share directory
path=/samba_dir
writable=yes
[root@samba ~]# mkdir /samba_dir #创建共享文件
配置完成后可使用testparm命令来校验/etc/samba/smb.conf文件的配置是否正确:
[root@samba ~]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[samba_dir]"
Processing section "[data]"
Loaded services file OK.
WARNING: 'workgroup' and 'netbios name' must differ.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
workgroup = SAMBA
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw
[homes]
comment = Home Directories
browseable = No
inherit acls = Yes
read only = No
valid users = %S %D%w%S
[printers]
comment = All Printers
path = /var/tmp
browseable = No
printable = Yes
create mask = 0600
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
create mask = 0664
directory mask = 0775
write list = root
[samba_dir]
comment = This is a new share directory
path = /samba_dir
guest ok = Yes
read only = No
samba服务默认的验证模式为user,因此我们还需要创建用户的数据库。
3、创建samba用户数据库
samba帐号必须要存在于Linux系统中(/etc/passwd),但其密码的却是单独维护的。创建samba用户数据库需要使用命令pdbedit,pdbedit命令是用于管理smb服务的账号信息数据库其用法格式为:
pdbedit [options] account
常用选项有:
-a USERNAME:创建samba用户;
-x:删除samba用户;
-L:列出samba用户列表;
-Lv:列出用户详细信息列表;
除了pdbedit命令之外,我们也可以使用smbpasswd命令来管理创建samba用户,其语法格式如下:
smbpasswd [options] USERNAME
常用选项有:
-a:添加账号
-x:删除账号
-d:禁用账号
-e:启用账号
接着我们就来创建samba用户:
[root@samba ~]# useradd samba
[root@samba ~]# pdbedit -a samba
new password:
retype new password:
Unix username: samba
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2841389940-495581649-2996202120-1000
Primary Group SID: S-1-5-21-2841389940-495581649-2996202120-513
Full Name:
Home Directory: \\samba\samba
HomeDir Drive:
Logon Script:
Profile Path: \\samba\samba\profile
Domain: SAMBA
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 10:06:39 EST
Kickoff time: Wed, 06 Feb 2036 10:06:39 EST
Password last set: Mon, 07 May 2018 07:34:53 EDT
Password can change: Mon, 07 May 2018 07:34:53 EDT
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@samba ~]# useradd magedu
[root@samba ~]# smbpasswd -a magedu
New SMB password:
Retype new SMB password:
Added user magedu.
4、启动smb服务,测试访问
[root@samba ~]# systemctl start smb
由上图所示,samba用户登录后无法在samba_dir共享文件夹下创建目录,这是因samba_dir共享目录的属主和属组均为root,其他用户没有写权限
[root@samba ~]# ll /samba_dir/ -d
drwxr-xr-x. 2 root root 6 May 7 07:19 /samba_dir/
我们将目录的属组改为samba,并添加写权限:
[root@samba ~]# usermod -G samba magedu #将samba添加为magedu账号的附加组
[root@samba ~]# chown :samba /samba_dir/ #修改共享目录的属组为samba
[root@samba ~]# chmod g+w /samba_dir/ #给samba增加写权限
[root@samba ~]# ll -d /samba_dir/
drwxrwxr-x. 2 root samba 6 May 7 07:19 /samba_dir/
[root@samba ~]# systemctl restart smb
此时再次测试:
在Linux系统上挂载samba共享目录:
[root@test ~]# mount -t cifs //192.168.0.84/samba_dir /mnt -o username=samba,password=123456 #挂载samba共享目录
[root@test ~]# mount
......
//192.168.0.84/samba_dir on /mnt type cifs (rw,relatime,vers=1.0,cache=striorcegid,addr=192.168.0.84,unix,posixpaths,serverino,acl,rsize=1048576,wsiz
[root@test ~]# cd /mnt/
[root@test mnt]# ll
总用量 0
drwxr-xr-x. 2 1502 1502 0 5月 7 2018 magedu_test
[root@test mnt]# touch samba_files #创建文件成功
[root@test mnt]# rm magedu_test/
rm: 无法删除"magedu_test/": 是一个目录
[root@test mnt]# rmdir magedu_test/ #删除文件成功,
[root@test mnt]# ll
总用量 0
-rw-r--r--. 1 1501 1501 0 4月 24 15:16 samba_files
另外,在linux系统中访问samba文件系统还可以使用smbclient命令工具:
[root@test ~]# yum install samba-client -y #安装smbclient命令工具
[root@test ~]# smbclient -L 192.168.0.84 -U samba #查看samba服务器的共享情况
Enter SAMBA\samba's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
samba_dir Disk This is a new share directory
IPC$ IPC IPC Service (Samba 4.6.2)
samba Disk Home Directories
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@test ~]# smbclient //192.168.0.84/samba_dir -U samba #交互式访问samba共享目录
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> lcd /etc/
smb: \> put fstab
putting file fstab as \fstab (75.7 kb/s) (average 75.7 kb/s) #成功上传文件
smb: \> ls
. D 0 Mon May 7 20:28:07 2018
.. DR 0 Mon May 7 19:19:53 2018
samba_files N 0 Tue Apr 24 15:16:13 2018
fstab A 465 Mon May 7 20:28:07 2018
hello.txt A 0 Mon May 7 20:28:49 2018
17811456 blocks of size 1024. 16554288 blocks available
smb: \> rm hello.txt #成功删除文件
smb: \> ls
. D 0 Mon May 7 20:29:00 2018
.. DR 0 Mon May 7 19:19:53 2018
samba_files N 0 Tue Apr 24 15:16:13 2018
fstab A 465 Mon May 7 20:28:07 2018
17811456 blocks of size 1024. 16554264 blocks available
smb: \>
三、综合案例
1、创建一个共享data,路径为/var/ftp/data,要求仅centos和gentoo用户能上传,此路径对其他用户不可见;
[root@samba ~]# mkdir -pv /var/ftp/data
mkdir: created directory ‘/var/ftp’
mkdir: created directory ‘/var/ftp/data’
[root@samba ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[data]
comment=ftp
path = /var/ftp/data
browseable = No #不公开data目录
writable=yes
directory mask = 0775
create mask = 0664
write list=centos,gentoo #设置只允许centos、gentoo访问data共享目录
[root@samba ~]# testparm #运行testparm命令检查/etc/samba/smb.conf配置文件是否有错误
[root@samba ~]# useradd centos -s /sbin/nologin
[root@samba ~]# useradd gentoo -s /sbin/nologin
[root@samba ~]# smbpasswd -a centos
New SMB password:
Retype new SMB password:
Added user centos.
[root@samba ~]# smbpasswd -a gentoo
New SMB password:
Retype new SMB password:
Added user gentoo.
[root@samba ~]# setfacl -m u:centos:rwx /var/ftp/data/ #设置用户centos对/var/ftp/data的目录权限为rwx
[root@samba ~]# setfacl -m u:gentoo:rwx /var/ftp/data/ #设置用户gentoo对/var/ftp/data的目录权限为rwx
[root@samba ~]# getfacl /var/ftp/data/
getfacl: Removing leading '/' from absolute path names
# file: var/ftp/data/
# owner: root
# group: root
user::rwx
user:centos:rwx
user:gentoo:rwx
group::r-x
mask::rwx
other::r-x
[root@samba ~]# systemctl restart smb