1.简介
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。
Samba提供了基于CIFS的四个服务:文件和打印服务、授权与被授权、名称解析、浏览服务。前两项服务由smbd提供,后两项服务则由nmbd提供。 简单地说,smbd进程的作用是处理到来的SMB软件包,为使用该软件包的资源与Linux进行协商,nmbd进程使主机(或工作站)能浏览Linux服务器。
2.Samba安装与配置
安装samba应用
yum -y install samba samba-client
启动Samba应用
systemctl start smb nmb
查看samba进程
[root@localhost home]# ps -ef | grep -E 'smb|nmb'
root 3284 1 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3285 1 0 8月13 ? 00:00:02 /usr/sbin/nmbd --foreground --no-process-group
root 3289 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3290 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3292 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 11914 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 19570 2586 0 01:20 pts/0 00:00:00 grep --color=auto -E smb|nmb
查看samba端口
[root@localhost home]# netstat -tulpn
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 3337/smbd
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 3337/smbd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 907/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1010/sshd
tcp6 0 0 :::445 :::* LISTEN 3337/smbd
tcp6 0 0 :::3306 :::* LISTEN 868/mysqld
tcp6 0 0 :::139 :::* LISTEN 3337/smbd
tcp6 0 0 :::80 :::* LISTEN 907/nginx: master p
udp 0 0 172.18.180.225:123 0.0.0.0:* 515/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 515/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 515/ntpd
udp 0 0 172.18.191.255:137 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.180.225:137 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:137 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.191.255:138 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.180.225:138 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:138 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:30203 0.0.0.0:* 714/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 714/dhclient
udp6 0 0 :::123 :::* 515/ntpd
udp6 0 0 :::50647 :::* 714/dhclient
修改samba的配置文件,以便windows系统能够直接访问共享文件,这里注意,由于阿里云或者运营商的137,138,139,445端口限制,这里只做本地的测试,阿里云的centos7需要做端口转发(后续讨论)
先备份配置文件
[root@localhost samba]# ll
总用量 24
-rw-r--r--. 1 root root 20 4月 13 23:34 lmhosts
-rw-r--r-- 1 root root 827 8月 13 00:17 smb.conf
-rw-r--r--. 1 root root 706 8月 12 19:55 smb.conf.bak
-rw-r--r--. 1 root root 11327 4月 13 23:34 smb.conf.example然后修改配置文件,
原始文件:
[root@localhost samba]# cat smb.conf.bak
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
修改之后:
[root@localhost samba]# cat smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = WORKGROUP
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[shared]
# 共享文件目录描述
comment = Shared Directories
# 共享文件目录
path = /home/shared/
# 是否允许guest访问
public = no
# 指定管理用户
#admin users = admin
# 可访问的用户组、用户
#valid users = @admin
# 是否浏览权限
browseable = yes
# 是否可写权限
writable = yes
# 文件权限设置
create mask = 0777
directory mask = 0777
force directory mode = 0777
force create mode = 0777
path = /home/shared/就是我们的samba共享文件路径,注意给shared 777权限,
然后adduser shared_smb,再设置samba用户和密码,smbpasswd -a shared_smb
root@localhost home]# ll
总用量 4
drwx------. 14 lry lry 4096 8月 12 19:48 lry
drwxrwxrwx. 2 root root 19 8月 12 21:30 shared
drwx------. 3 shared_smb shared_smb 78 8月 12 21:30 shared_smb
[root@localhost home]# pdbedit -L
shared_smb:1001:
smbpasswd用法:
smbpasswd -a:向smbpasswd文件中添加用户;
-c:指定samba的配置文件;
-x:从smbpasswd文件中删除用户;
-d:在smbpasswd文件中禁用指定的用户;
-e:在smbpasswd文件中激活指定的用户;
-n:将指定的用户的密码置空。
最后在本地win10测试:\\192.168.1.100\shared
查看连接状态:smbstatus
像FTP客户端一样使用smbclient
[root@localhost home]# smbclient //192.168.0.1/tmp -U username%password