一、安装前准备
Samba服务基于SMB服务消息块协议和CIFS通用互联网文件系统协议,
Samba服务对应的进程有2个:
smbd:提供对服务器中文件、打印资源的共享访问
nmbd:提供基于NetBios主机名称的解析,也就是令客户端可以通过计算机名的方式去访问服务端
Samba服务所使用的端口和协议:
1)Port 137 (UDP) - NetBIOS 名字服务 ; nmbd
2)Port 138 (UDP) - NetBIOS 数据报服务
3)Port 139 (TCP) - 文件和打印共享 ; smbd (基于SMB(Server Message Block)协议,主要在局域网中使用,文件共享协议)
4)Port 389 (TCP) - 用于 LDAP (Active Directory Mode)
5)Port 445 (TCP) - NetBIOS服务在windos 2000及以后版本使用此端口, (Common Internet File System,CIFS,它是SMB协议扩展到Internet后,实现Internet文件共享)
6)Port 901 (TCP) - 用于 SWAT,用于网页管理Samba
[root@localhost yum.repos.d]# getenforce Disabled [root@localhost yum.repos.d]# vim /etc/sysconfig/iptables :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 137 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 138 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
二、安装Samba
[root@localhost yum.repos.d]# rpm -qa|grep samba samba-common-3.6.9-151.el6.i686 samba-winbind-3.6.9-151.el6.i686 samba-winbind-clients-3.6.9-151.el6.i686 samba-3.6.9-151.el6.i686 #说明已经安装过,没安装运行如下 [root@localhost yum.repos.d]# yum -y install samba Loaded plugins: product-id, subscription-manager Updating certificate-based repositories. Unable to read consumer identity Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package samba.i686 0:3.6.9-151.el6 will be installed --> Processing Dependency: samba-winbind-clients = 3.6.9-151.el6 for package: samba-3.6.9-151.el6.i686 ... Dependency Installed: libtalloc.i686 0:2.0.7-2.el6 libtdb.i686 0:1.2.10-1.el6 pkgconfig.i686 1:0.23-9.1.el6 samba-common.i686 0:3.6.9-151.el6 samba-winbind.i686 0:3.6.9-151.el6 samba-winbind-clients.i686 0:3.6.9-151.el6 Complete! [root@localhost yum.repos.d]# chkconfig smb on [root@localhost yum.repos.d]# service smb start Starting SMB services: [ OK ] [root@localhost yum.repos.d]# chkconfig nmb on [root@localhost yum.repos.d]# service nmb start Starting NMB services: [ OK ] [root@localhost yum.repos.d]#
配置samba
Samba服务的主配置文件为/etc/samba/smb.conf,该文件中的内容比较多,其中大多是以“#”开头的注释行,以及以“;”开头的配置样例行。样例行是对配置内容的举例,用户可以参考样例行进行配置。无论是注释行还是样例行,Samba服务器都将予以忽略。
[global] #全局设置:这部分配置项的内容对整个Samba服务器有效
workgroup = MYGROUP
server string = Samba Server Version %v #设置Samba服务器所在的工作组以及对工作组的描述信息。当在Windows客户端上通过网上邻居的方式访问文件共享时,才需要用到工作组,我们现在已经很少去使用网上邻居了,所以这部分信息如何设置都无所谓。
security = user
passdb backend = tdbsam #security用来设置安全级别,passdb backend用来设置用户密码存放位置。
security:安全级别(安全级别中比较常用的是share和user)
share 可匿名访问
user 需由本服务器验证用户名及密码
server 由另一台服务器验证用户名级密码
domain 由windows域控制器验证用户名及密码
[homes]
comment = Home Directories
browseable = no
writable = yes
; valid users = %S
; valid users = MYDOMAIN\%S
#是对用户主目录的默认共享设置,也就是说Samba服务器默认将所有用户的主目录都设为了共享。当在客户端以Samba服务器中某个系统用户的身份登录时,就会看到自己的主目录,并且具有写入权限。当然每个用户只能看到自己的主目录,别人的主目录是无法看到的。
[printers]
comment = All Printers
path = /var/spool/samba
browseable = no
guest ok = no
writable = no
printable = yes
#是打印机的默认共享设置,如果系统中并没有安装打印机,那么可以将这部分内容注释掉,或者无需理会
范例
文件末尾提供了一个范例
# A publicly accessible directory, but read only, except for people in # the "staff" group ; [public] #共享名,可自由定义 ; comment = Public Stuff #对共享的描述信息,可自由定义 ; path = /home/samba #共享目录的路径,必须为绝对路径 ; public = yes #是否允许所有用户访问 ; writable = yes #是否允许写入 ; printable = no #是否需要提供打印共享,这个一般无需设置 ; write list = +staff #允许写入的用户列表。如设置了拒绝写入,这一项也不需设置
创建一个共享目录/home/SambaServer,只允许SambaServer用户访问
[root@localhost yum.repos.d]# cp /etc/samba/smb.conf /etc/samba/smb.confbak [root@localhost yum.repos.d]# vim /etc/samba/smb.conf ..... security = user passdb backend = tdbsam ...... #文末添加以下 [SambaServer] #在Windows网上邻居中看到的共享目录的名字 comment = SambaServer #在Windows网上邻居中看到的共享目录的备注信息 path = /home/SambaServer #共享目录在系统中的位置 public = no #不公开目录 writable = yes #共享目录可以读写 valid users=SambaServer #只允许SambaServer用户访问 #添加访问linux共享目录的账号SambaServer,用户家目录为/home/SambaServer, 用户登录终端设为/bin/false(即使之不能登录系统) [root@localhost home]# useradd SambaServer -d /home/SambaServer -s /bin/false [root@localhost home]# touch /home/SambaServer/samba.txt ##创建测试文件samba.txt [root@localhost home]# echo "This is a test file" > /home/SambaServer/samba.txt [root@localhost home]# chown SambaServer:SambaServer /home/SambaServer -R #将用户SambaServer添加入到Samba用户数据库,并设置登录共享目录的密码为:123456 [root@localhost home]# smbpasswd -a SambaServer New SMB password: Retype new SMB password: Added user SambaServer. [root@localhost home]# /etc/init.d/smb restart Shutting down SMB services: [ OK ] Starting SMB services: [ OK ]
在window运行里输入\\ip就可以弹出输入登陆对话框,输入密码就可以访问/home/SambaServer里的内容
[root@localhost home]# useradd SambaServer1 -d /home/SambaServer1 -s /bin/false [root@localhost home]# smbpasswd -a SambaServer1 New SMB password: Retype new SMB password: Added user SambaServer1. [root@localhost home]# vim /etc/samba/smb.conf [SambaServer] comment = SambaServer path = /home/SambaServer public = no write list = SambaServer1 #只有SambaServer1可以写入数据(需要设置共享目录的权限)SambaServer无法写入数据 #write list = @write #只有write组内用户可以写数据 valid users=SambaServer,SambaServer1 [root@localhost home]# /etc/init.d/smb restart Shutting down SMB services: [ OK ] Starting SMB services: [ OK ]
通过SambaServer和SambaServer1后发现进入后看到的内容不一样,用户成功登录之后,除了能看到设置好的共享目录share之外,还能看到自己的主目录,这就是配置文件中默认的[homes]项所起到的作用。
补充:
允许用户组read里用户可以访问,组成员SambaServer1可以写入数据
[SambaServer] comment = SambaServer path = /home/SambaServer #public = yes 注释掉该行,因为这项的作用就是允许所有Samba用户都拥有访问权限 write list = SambaServer1 valid users=@read
允许/拒绝某些IP访问
通过“hosts allow”或“hosts deny”项设置允许或拒绝某些IP访问共享文件。如果将这两项设置放在[global]全局设置中,那么对所有共享目录都有效;如果只是放在某个共享目录中,则只对该目录生效,如果要同时指定多个IP,它们之间以空格或逗号间隔。注意:如果要指定一个IP段,则需使用类似“192.168.11.”或“172.16.”的形式,这里不持“255.255.255.0”或“/24”这类形式的子网掩码
[SambaServer] comment = SambaServer path = /home/SambaServer public = no writable = yes valid users=SambaServer,SambaServer1 hosts allow = 192.168.100.1 192.168.17. 10.15. #只允许通过hosts allow的ip上通过SambaServer,SambaServer1访问
smbpasswd命令的主要选项:
-h:显示smbpasswd命令的帮助信息
-a:添加指定的Samba用户帐号
-d:禁用指定的用户帐号
-e:启用指定的用户帐号
-x:删除指定的用户帐号
不使用任何命令选项时可以用于修改Samba用户的密码
window下清除登陆信息:
net use 查看信息
net use * /del 删除信息