linux下配置samba服务器
1、 什么是samba
Samba服务类似于windows上的共享功能,可以实现在Linux上共享文件,windows上访问,当然在Linux上也可以访问到。
是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。

2、安装配置samba
安装命令:yum install -y samba samba-client

[root@samba-server ~]# egrep -v "^$|#|;" /etc/samba/smb.conf
[global]

workgroup = MYGROUP
server string = Samba Server Version %v

log file = /var/log/samba/log.%m
max log size = 50

security = user
passdb backend = tdbsam
load printers = yes
cups options = raw

[homes]
comment = Home Directories
browseable = no
writable = yes
[printers]
comment = All Printers
path = /var/spool/samba
browseable = no
guest ok = no
writable = no
printable = yes

1.配置文件解释:
[global] 定义全局的配置,workgroup用来定义工作组,相信如果您安装过windows的系统,你会对这个workgroup不陌生。一般情况下,需要我们把这里的MYGROUP改成WORKGROUP(windows默认的工作组名字)。
security = user #这里指定samba的安全等级。
关于安全等级有四种:
share:用户不需要账户及密码即可登录samba服务器
user:由提供服务的samba服务器负责检查账户及密码(默认)
server:检查账户及密码的工作由另一台windows或samba服务器负责
domain:指定windows域控制服务器来验证用户的账户及密码。
passdb backend = tdbsam #passdb backend(用户后台)
samba有三种用户后台:smbpasswd, tdbsam和ldapsam。
load printers:加载打印机
cups options = raw 打印机相关设置;

2.samba配置文件参数介绍

smbpasswd:该方式是使用smb工具smbpasswd给系统用户(真实用户或者虚拟用户)设置一个Samba密码,客户端就用此密码访问Samba资源。smbpasswd在/etc/samba中,有时需要手工创建该文件。
tdbsam: 使用数据库文件创建用户数据库。数据库文件叫passdb.tdb,在/etc/samba中。passdb.tdb用户数据库可使用 smbpasswd -a 创建Samba用户,要创建的Samba用户必须先是系统用户。也可使用pdbedit创建Samba账户。
pdbedit参数很多,列出几个主要的:
pdbedit -a username:新建Samba账户。
pdbedit -x username:删除Samba账户。
pdbedit -L:列出Samba用户列表,读取passdb.tdb数据库文件。
pdbedit -Lv:列出Samba用户列表详细信息。
pdbedit -c “[D]” -u username:暂停该Samba用户账号。
pdbedit -c “[]” -u username:恢复该Samba用户账号。
ldapsam:基于LDAP账户管理方式验证用户。首先要建立LDAP服务,设置 “passdb backend = ldapsam:ldap://LDAP Server”
load printers 和 cups options 两个参数用来设置打印机相关。
除了这些参数外,还有几个参数需要你了解:
netbios name = MYSERVER # 设置出现在网上邻居中的主机名
hosts allow = 127. 192.168.12. 192.168.13. 172.16.# 用来设置允许的主机,如果在前面加 ”;” 则表示允许所有主机
log file = /var/log/samba/%m.log #定义samba的日志,这里的%m是上面的netbios name
max log size = 50 # 指定日志的最大容量,单位是K
[homes] 该部分内容共享用户自己的家目录,也就是说,当用户登录到samba服务器上时实际上是进入到了该用户的家目录,用户登陆后,共享名不是homes而是用户自己的标识符,对于单纯的文件共享的环境来说,这部分可以注视掉。
[printers] 该部分内容设置打印机共享。

3.samba实践一
要求共享一个目录,任何人都可以访问,即不用输入密码即可访问,要求只读;

[root@samba-server samba]# cp /etc/samba/smb.conf /etc/samba/smb.conf.ori
[root@samba-server samba]# egrep -v "^$|#|;" /etc/samba/smb.conf.ori >smb.conf

在配置文件/etc/samba/smb.conf中做如下修改(红颜色字体为修改的内容)
[root@samba-server samba]# cat /etc/samba/smb.conf
[global]
workgroup =WORKGROUP
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
security = share
passdb backend = tdbsam
load printers = yes
cups options = raw
[homes]
comment = Home Directories
browseable = no
writable = yes
[printers]
comment = All Printers
path = /var/spool/samba
browseable = no
guest ok = no
writable = no
printable = yes
[share]

      comment = share all
        path = /tmp/samba
        browseable = yes
        public = yes
        writable = no

mkdir /tmp/samba
chmod 777 /tmp/samba
touch /tmp/samba/sharefile
echo "111111" > /tmp/samba/sharefile
testparm 测试配置文件
/etc/init.d/smb start
netstat -lntup|grep smb

[root@samba-server samba]# netstat -lntup|grep smb
tcp 0 0 0.0.0.0:139 0.0.0.0: LISTEN 3338/smbd
tcp 0 0 0.0.0.0:445 0.0.0.0:
LISTEN 3338/smbd
tcp 0 0 :::139 ::: LISTEN 3338/smbd
tcp 0 0 :::445 :::
LISTEN 3338/smbd

启动:/etc/init.d/smb start
检查配置的smb.conf是否正确 testparm
测试:windows机器浏览器输入 file://192.168.56.17/share
或者运行栏输入: \192.168.56.17 或 \192.168.56.17 \share

浏览器显示:

root@samba-server samba]# testparm
Load smb config files from /etc/samba/smb.conf
Processing section "[homes]"
Processing section "[printers]"
Processing section "[share]"
WARNING: The security=share option is deprecated 有警告,但是不影响测试
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions

[global]
server string = Samba Server Version %v
security = SHARE
log file = /var/log/samba/log.%m
max log size = 50
client signing = required
idmap config * : backend = tdb
cups options = raw

[homes]
comment = Home Directories
read only = No
browseable = No

[printers]
comment = All Printers
path = /var/spool/samba
printable = Yes
print ok = Yes
browseable = No

[share]
comment = share all
path = /tmp/samba
guest ok = Yes

4.Samba实践二
共享一个目录,使用用户名和密码登录后才可以访问,要求可以读写;
[root@samba-server samba]# cat /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
security = user
passdb backend = tdbsam
load printers = yes
cups options = raw
#[homes]

comment = Home Directories

browseable = no

writable = yes

#[printers]

comment = All Printers

path = /var/spool/samba

browseable = no

guest ok = no

writable = no

printable = yes

#[share]
comment = share all
path = /tmp/samba
browseable = yes
public = yes
writable = no
[myshare]
comment = share for users
path = /samba
browseable = yes
writable = yes
public = no

[root@samba-server /]# mkdir /samba
[root@samba-server /]# chmod 777 /samba/
[root@samba-server /]# useradd test01
[root@samba-server /]# useradd test02

[root@samba-server /]# pdbedit -a test02
new password:
retype new password:
Unix username: test02
NT username:
Account Flags: [U ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1001
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test02
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test02\profile
Domain: SAMBA-SERVER
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Fri, 05 Aug 2016 17:33:23 CST
Password can change: Fri, 05 Aug 2016 17:33:23 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

[root@samba-server /]# pdbedit -a test01
new password:
retype new password:
Passwords do not match!
[root@samba-server /]# pdbedit -a test01
new password:
retype new password:
Unix username: test01
NT username:
Account Flags: [U ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1000
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test01
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test01\profile
Domain: SAMBA-SERVER
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Fri, 05 Aug 2016 17:33:07 CST
Password can change: Fri, 05 Aug 2016 17:33:07 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

5.Samba实践三
samba服务端,先暂停用户,账号标记为[DU],正常状态为[U];客户端访问,提示账号不可用;

[root@samba-server samba]# pdbedit -c [D] -u test01
Unix username: test01
NT username:
Account Flags: [DU ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1000
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test01
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test01\profile
Domain: SAMBA-SERVER
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Fri, 05 Aug 2016 17:55:14 CST
Password can change: Fri, 05 Aug 2016 17:55:14 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
session setup failed: NT_STATUS_ACCOUNT_DISABLED

samba服务端恢复用户,账号标记为[U];客户端访问,恢复正常;

[root@samba-server samba]# pdbedit -c [] -u test01
[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \>

smb: \> ls
. D 0 Fri Aug 5 17:38:07 2016
.. DR 0 Fri Aug 5 17:31:37 2016
contest 10 Fri Aug 5 17:38:07 2016

    41119 blocks of size 131072. 13988 blocks available

[root@samba-server samba]# pdbedit -c [D] -u test02
Unix username: test02
NT username:
Account Flags: [DU ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1001
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test02
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test02\profile
Domain: SAMBA-SERVER
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Fri, 05 Aug 2016 17:33:23 CST
Password can change: Fri, 05 Aug 2016 17:33:23 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
session setup failed: NT_STATUS_ACCOUNT_DISABLED

[root@samba-server samba]# pdbedit -c [] -u test02|grep -i Account
Account Flags: [U ]
Account desc:

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> ls
. D 0 Fri Aug 5 17:38:07 2016
.. DR 0 Fri Aug 5 17:31:37 2016
contest 10 Fri Aug 5 17:38:07 2016

    41119 blocks of size 131072. 13988 blocks available

smb: \> q

[root@samba-server ~]# pdbedit -L
test01:504:
删除用户:
[root@samba-server ~]# pdbedit -x test01
[root@samba-server ~]# pdbedit -L
[root@samba-server samba]# pdbedit -x test02
[root@samba-server ~]#

[root@samba-server ~]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
session setup failed: NT_STATUS_LOGON_FAILURE

6.Linux访问samba共享文件
参考文档:
http://mofansheng.blog.51cto.com/8792265/1654871

[root@samba-client ~]# yum install -y samba-client

安装客户端软件 yum install -y samba-client
命令格式:smbclient //IP/共享名 -U 用户名
如:smbclient //127.0.0.1/share 如果是匿名访问可以省略掉-U

[root@samba-client ~]# mount -t cifs //192.168.56.17/myshare /mnt -o username=test01,password=123456
[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password: 123456
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> q

mount -t cifs //192.168.0.22/myshare /mnt 匿名访问不需要-o参数;
如执行命令提示参数有错,还需要安装cifs-utils包;

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password: 1234 输错密码报错
session setup failed: NT_STATUS_LOGON_FAILURE

输入正确密码后进入,put上传文件,get下载文件;上传的文件smb服务器中查看所属主、组为user1;user1在共享文件夹可以新建目录,删除文件;help可以列出当前命令行可以执行的命令;

必须先在samba-client当前目录下创建文件,才能上传对应的文件到samba-server端的共享目录下/share 下,而且通过test01 用户登陆的上传的文件到samba-server 服务端的share下,显示该文件的用户和用户组为test01.

[root@samba-client ~]# echo 123 >1.sql
[root@samba-client ~]# ls -ls
total 8
4 -rw-r--r-- 1 root root 4 Aug 5 18:51 1.sql
4 -rw-r--r-- 1 root root 10 Aug 5 18:48 contest

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> put 1.sql
putting file 1.sql as \1.sql (0.8 kb/s) (average 0.8 kb/s)

[root@samba-server samba]# ll
total 8
-rwxr--r-- 1 test01 test01 4 Aug 5 18:51 1.sql
-rw-r--r-- 1 root root 10 Aug 5 17:38 contest

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \>get contest
getting file \contest of size 10 as contest (1.2 KiloBytes/sec) (average 1.2 KiloBytes/sec)

[root@samba-client ~]# ls
contest
[root@samba-client ~]# cat contest
Wujianwei

客户端mount挂载samba共享文件到本地
mount -t cifs //192.168.56.17/myshare /mnt -o username=test01,password=123456

[root@samba-client ~]# mount
/dev/sda5 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda2 on /data type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
//192.168.56.17/myshare on /mnt type cifs (rw,username=test01,password=123456)

7实例共享文件访问权限的控制
http://304076020.blog.51cto.com/7503470/1367740

nginx+lwa
http://lee90.blog.51cto.com/10414478/1833724

编辑samba的配置文件:
comment:注释信息;
path:当前共享所映射的文件系统路径;
browseable:是否可浏览,指是否可被用户查看;
guest ok:是否允许来宾账号访问;
public:是否公开所有用户;
writable:是否可写;
write list:拥有写权限的用户列表;

vim /etc/samba/smb.conf

comment = wordpress 名称
path = /data/application/web 要导出的路径
browseable = yes
writable = yes
2.客户端配置:
挂载:
mount -t cifs -o username=apachepassword=admin //172.16.35.1/wordpress /var/www/html

参考资料:
Samba服务共享的Linux和windows 参数介绍:
http://yuanbin.blog.51cto.com/363003/115761/
http://www.linuxidc.com/Linux/2015-05/117977.htm