smb文件共享
来源:互联网 发布:手机联系人恢复软件 编辑:程序博客网 时间:2024/05/26 05:53
SMB文件共享
通用Internet文件系统(CIFS)也称为服务器信息块(SMB),是适用于MicrosoftWindows服务器和客户端的标准文件和打印机共享系统。Samba服务可用于将Linux文件系统作为CIFS/SMB网络文件共享进行共享,并将Linux打印机作为CIFS/SMB打印机共享进行共享。
Samba服务的组成部分
- 软件包:
Samba-common – Samba的支持文件
Samba-client – 客户端应用程序
Samba – 服务器应用程序 - 服务名称:smb nmb
- 服务端口:通常使用TCP/445进行所有连接。还使用UDP137、UDP138和TCP/139进行向后兼容
- 主配置文件:/etc/samba/smb.conf
/etc/samba/smb.conf: [global]部分
workgroup
hosts allow 用于指定Windows工作组或网络域名。
hosts allow是用逗号、空格或制表符分隔的允许访问服务的主机集合。如果在[global]部分中指定, 将适用于所有服务,无论单个服务是否具有不同的设置。可以按名称或IP号指定主机。例如,可以使用allow hosts = 192.168.0. 表示允许一个网络。
安全性
该选项影响客户端如何响应Samba,并且在smb.conf文件中是最重要的设置之一。
security = user
客户端必须通过有效的用户名和密码登录。
security = domain
只有当计算机已添加到NT域时才能正常工作。它要求将encrypted passwords 参数设置为yes。在该模式中,Samba通过将用户名/密码传递至Windows NT主域控制器或备份域控 制器来进行验证,与Windows NT Server采用的方式完全一样。请注意,有效的UNIX用户以及域控制器上的账户必须仍然存在,以允许Samba具有有效的UNIX账户,可用以映射文件访问。您必须设置 passwd server参数,为Samba提供服务器,以验证密码。
security = server
Samba将通过将用户名/密码传递至另一个SMB服务器来尝试进行验证。必须设置password server参数, 为samba提供服务器, 以验证密码。
security = ads
Samba在ADS realm中作为域成员。如要在该模式下操作,运行Samba的计算机需要安装并配置Kerberos,并且需要使用net实用程序将Samba加入ADS realm。
/etc/samba/smb.conf:其他部分
[homes]
此共享(默认情况下启用)是一个特别共享,通过CIFS使用户的主目录可。它包括browseable= no, 因此在用户进行身份验证之前,将不会显示为可用共享。共享名称可以指定为homes(在这种情况下, Samba服务器会将其转换为用户的主目录路径)或username。
[printers]
默认情况下也可用,将共享当前可用的打印机 。
[share]
如果您想要设置其他共享,请将共享名称放置在括号中,如上所示。共享需要至少一个path参数。
SMB用户
useradd
security = user需要UNIX和Samba账户信息。添加用户(最好使用与Samba账户一样的名称),或在/etc/samba/smbusers中放置条目(有一些示例)。如果您创建仅Samba用户,请将UNIX密码设置为/sbin/nologin。
[root@serverX ~]# useradd -s /sbin/nologin wxh
smbpasswd
如果没有Samba密码服务器,则必须在本地计算机上创建身份验证数据。使用smbpasswd创建Samba账户和密码。
如果smbpasswd只传递一个用户名而不带任何选项,它将尝试更改账户密码。传递-a选项将添加账户并设置密码。
[root@serverX ~]# smbpasswd -a wxh
New SMB password: westos
Retype new SMB password: westos
Added user wxh.
具体如下:
1.先安装服务[root@client ~]# yum install samba samba-common samba-client.x86_64 -y[root@client ~]# systemctl start smb nmb[root@client ~]# systemctl enable smb nmb2.添加用户[root@client ~]# smbpasswd -a student ##添加student用户New SMB password: ##输入smb当前用户密码Retype new SMB password: ##确认密码Added user student.[root@client ~]# pdbedit -L ##列出所有用户名student:1000:User student[root@client ~]# pdbedit -x student ##删除用户名
保护SMB
samba_enable_home_dirs和use_samba_home_dirs SELinux布尔值
samba_enable_home_dirs布尔值允许本地Linux主目录作为CIFS文件共享导出至其他系统。另一方面 use_samba_home_dirs布尔值允许挂载远程CIFS文件共享并将其用作本地Linux主目录。
setsebool -P samba_enable_home_dirs on
samba_share_t用于共享用户自定义samba共享
chcon -R -t samba_share_t /smbshare
(或) semanage fcontext -a -t samba_share_t ‘/smbshare(/.*)?’
restorecon -RvvF /smbshare
samba_export_all_ro 和 samba_export_all_rw
用于共享系统目录
setsebool -P samba_export_all_ro on
setsebool -P samba_export_all_rw on
访问CIFS共享
连接到CIFS文件共享的四个基本方法:
1. 图形访问CIFS共享转至 “网络” --> “连接服务器”。填写以下字段:Server Address : 172.25.0.11Userame: wxhPassword: westos2. 命令行FTP方式访问CIFS共享:[root@server0 ~]# smbclient -L server0.example.com -U wxhEnter wxh's password: westosDomain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]SharenameType---------IPC$smbsharewxhComment----IPCDiskDisk-------IPC Service (Samba Server Version 4.1.1)testHome Directories[root@server0 ~]# smbclient //server0.example.com/smbshare -U wxhEnter wxh's password: westosDomain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls. D 0 Thu Dec 18 17:27:27 2014.. D 0 Thu Dec 18 11:37:20 2014testfileN12 Thu Dec 18 11:38:03 20143. 手动挂载CIFS共享[root@server0 ~]# mount -o username=wxh //server0.example.com/smbshare /mnt/wxh4. 永久挂载CIFS共享将以下行添加到/etc/fstab://server0.example.com/smbshare /mnt/wxh cifs credentials=/root/userpasswd 0 0创建/root/userpasswd:user=wxhpass=westos
共享目录的基本设定
[root@localhost ~]# vim /etc/samba/smb.conf
[haha] 共享名称
comment = let me see see 对共享目录的描述
path = 共享目录的绝对路径
workgroup = WESTOS
1.允许smb用户可以访问自己的家目录[root@client ~]# setsebool -P samba_enable_home_dirs on ##允许本地linux主目录作为cifs文件共享导出至其他系统[root@client ~]# getsebool -a | grep sambasamba_create_home_dirs --> offsamba_domain_controller --> offsamba_enable_home_dirs --> onsamba_export_all_ro --> offsamba_export_all_rw --> offsamba_portmapper --> offsamba_run_unconfined --> offsamba_share_fusefs --> offsamba_share_nfs --> offsanlock_use_samba --> offuse_samba_home_dirs --> offvirt_sandbox_use_samba --> offvirt_use_samba --> off[root@samba-server ~]# smbclient //172.25.254.20/student -U student ##在另一台主机检测Enter student's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls . D 0 Mon May 22 03:44:30 2017 .. D 0 Sun Jun 4 08:04:11 2017 .mozilla DH 0 Mon May 22 03:43:07 2017 .bash_logout H 18 Wed Jan 29 07:45:18 2014 .bash_profile H 193 Wed Jan 29 07:45:18 2014 .bashrc H 231 Wed Jan 29 07:45:18 2014 59824 blocks of size 131072. 34815 blocks availablesmb: \> exit
当共享目录为用户自建立目录
[root@client ~]# mkdir /smbshare[root@client ~]# touch /smbshare/file{1..3}[root@client ~]# vim /etc/samba/smb.conf [haha]comment = let me see seepath = /smbshareworkgroup = WESTOS[root@client ~]# semanage fcontext -a -t samba_share_t '/smbshare(/.*)?'[root@client ~]# restorecon -RvvF /smbsharerestorecon reset /smbshare context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0restorecon reset /smbshare/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0restorecon reset /smbshare/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0restorecon reset /smbshare/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0检测:[root@samba-server ~]# smbclient //172.25.254.20/haha -U student Enter student's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls . D 0 Wed Jun 7 03:33:28 2017 .. D 0 Wed Jun 7 03:33:19 2017 file1 N 0 Wed Jun 7 03:33:28 2017 file2 N 0 Wed Jun 7 03:33:28 2017 file3 N 0 Wed Jun 7 03:33:28 2017 59824 blocks of size 131072. 34810 blocks availablesmb: \>
当共享目录为系统建立目录
[root@client ~]# vim /etc/samba/smb.conf [haha]comment = let me see seepath = /varworkgroup = WESTOS[root@client ~]# setsebool -P samba_export_all_ro on ##只读共享[root@client ~]# setsebool -P samba_export_all_rw on ##读写共享检测:[root@samba-server ~]# smbclient //172.25.254.20/haha -U student Enter student's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls . D 0 Wed Jun 7 02:42:30 2017 .. D 0 Wed Jun 7 03:33:19 2017 lib D 0 Wed Jun 7 03:15:02 2017 run D 0 Wed Jun 7 02:58:28 2017 lock D 0 Wed Jun 7 03:15:12 2017 log D 0 Wed Jun 7 02:43:32 2017 adm D 0 Thu Mar 13 07:51:26 2014 cache D 0 Sun Jun 4 08:56:45 2017 db D 0 Sun Jun 4 09:34:57 2017 empty D 0 Mon May 22 04:22:51 2017 games D 0 Thu Mar 13 07:51:26 2014 gopher D 0 Thu Mar 13 07:51:26 2014 local D 0 Thu Mar 13 07:51:26 2014 mail D 0 Sun Jun 4 08:04:11 2017 nis D 0 Thu Mar 13 07:51:26 2014 opt D 0 Thu Mar 13 07:51:26 2014 preserve D 0 Thu Mar 13 07:51:26 2014 spool D 0 Wed Jun 7 02:56:56 2017 tmp D 0 Wed Jun 7 02:58:31 2017 yp D 0 Thu Mar 13 07:51:26 2014 kerberos D 0 Tue Mar 11 15:30:43 2014 var D 0 Mon May 22 03:53:38 2017 crash D 0 Wed Apr 30 05:39:35 2014 account D 0 Mon May 22 04:23:06 2017 gdm D 0 Mon May 22 04:58:14 2017 59824 blocks of size 131072. 34813 blocks available smb: \> exit
5.samba的配置参数
允许匿名用户访问
guest ok =yes
map to guest = bad user
具体如下:
[root@client ~]# vim /etc/samba/smb.conf [haha]comment = let me see seepath = /smbshareworkgroup = WESTOSguest ok = yesmap to guest = bad user[root@samba-server ~]# smbclient //172.25.254.20/haha Enter root's password: ##直接回车 不用输密码,可以匿名访问Anonymous login successfulDomain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls . D 0 Wed Jun 7 03:33:28 2017 .. D 0 Wed Jun 7 03:33:19 2017 file1 N 0 Wed Jun 7 03:33:28 2017 file2 N 0 Wed Jun 7 03:33:28 2017 file3 N 0 Wed Jun 7 03:33:28 2017 59824 blocks of size 131072. 34810 blocks availablesmb: \>
访问控制
hosts allow = #仅允许
hosts deny = #仅拒绝
在Samba中使用hosts allow、hostsdeny参数时,该参数可以出现在全局配置部分,用于允许或拒绝可连接到Samba服务器的客户端,也可以出现在具体的共享资源配置中,用于允许或拒绝可访问该资源的客户端。(1)全局配置中hosts deny指定客户端,此时无法访问Samba服务器任何共享资源。(2)全局配置中hosts,allow指定客户端,分以下几种情况。①:如具体共享资源中只指定了hosts deny且与全局配置不冲突时,客户端可以访问具体共享资源。②:如具体共享资源中只指定了hosts allow且是全局配置的子集时,只有具体共享资源中指定的客户端可以访问。③:如具体共享资源中即指定了hosts allow又指定了hosts deny时,首先根据hosts allow与hosts deny生效规则得出具体共享资源允许或拒绝的客户端,再根据上面两条规则得出最终的结果。(1)如果hosts deny与hosts allow发生冲突时,hosts allow优先。(2)如果只有hosts allow,除了hosts allow中指定的客户端外其他所有客户端都不能访问。(3)如果只有hosts deny,除了hosts deny中指定的客户端外其他所有客户都可以访问。
valid users = westos #当前共享的有效用户
valid users = @westos #当前共享的有效用户为westos组
valid users = +westos #当前共享的有效用户为westos组
具体如下:
[root@client ~]# vim /etc/samba/smb.conf[haha]comment = let me see seepath = /smbshareworkgroup = WESTOSvalid users = westos检测:[root@samba-server ~]# smbclient //172.25.254.20/haha -U studentEnter student's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]tree connect failed: NT_STATUS_ACCESS_DENIED ##不能访问[root@samba-server ~]# smbclient //172.25.254.20/haha -U westosEnter westos's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1] ##可以访问smb: \> ls . D 0 Wed Jun 7 03:33:28 2017 .. D 0 Wed Jun 7 03:33:19 2017 file1 N 0 Wed Jun 7 03:33:28 2017 file2 N 0 Wed Jun 7 03:33:28 2017 file3 N 0 Wed Jun 7 03:33:28 2017 59824 blocks of size 131072. 34814 blocks availablesmb: \>
[root@client ~]# vim /etc/samba/smb.conf [haha]comment = let me see seepath = /smbshareworkgroup = WESTOSvalid users = +westos[root@client ~]# useradd lee -g westos ##先建立一个lee用户,属于westos组[root@client ~]# pdbedit -a lee ##再将lee加入到samba用户中检测:[root@samba-server ~]# smbclient //172.25.254.20/haha -U lee ##lee用户可以访问Enter lee's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]smb: \> ls . D 0 Wed Jun 7 03:33:28 2017 .. D 0 Wed Jun 7 03:33:19 2017 file1 N 0 Wed Jun 7 03:33:28 2017 file2 N 0 Wed Jun 7 03:33:28 2017 file3 N 0 Wed Jun 7 03:33:28 2017 59824 blocks of size 131072. 34814 blocks availablesmb: \> exit[root@samba-server ~]# smbclient //172.25.254.20/haha -U student ##student用户仍然不能访问Enter student's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]tree connect failed: NT_STATUS_ACCESS_DENIED
读写控制
1.所有用户均可写
[root@client ~]# chmod o+w /smbshare/[root@client ~]# setsebool -P samba_export_all_rw on[root@client ~]# vim /etc/samba/smb.conf writable = yes检测:[root@samba-server ~]# mount -o username=westos,password=redhat //172.25.254.20/haha /mnt[root@samba-server ~]# ls /mntfile1 file2 file3[root@samba-server ~]# cd /mnt[root@samba-server mnt]# touch file4[root@samba-server mnt]# lsfile1 file2 file3 file4
2.设定指定用户可写
write list = student #可写用户
write list = +student #可写用户组
write list = @student
admin users = westos #共享的超级用户指定
具体如下:
可写用户[root@client ~]# vim /etc/samba/smb.conf write list = westos检测:[root@samba-server ~]# mount -o username=lee,password=redhat //172.25.254.20/haha /mnt[root@samba-server ~]# cd /mnt[root@samba-server mnt]# touch file0touch: cannot touch ‘file0’: Permission denied ##没有权限,不能建立文件
可写用户组[root@client ~]# vim /etc/samba/smb.conf write list = +westos检测:[root@samba-server ~]# mount -o username=lee,password=redhat //172.25.254.20/haha /mnt [root@samba-server ~]# cd /mnt[root@samba-server mnt]# touch file0 ##可以创建文件[root@samba-server mnt]# lsfile0 file1 file2 file3 file4
共享的超级用户指定
smb多用户挂载
在客户端做,博主的客户端就是samba-server:
安装cifs-utils软件包,它包含了cifscreds命令:
[root@samba-server ~]# yum install cifs-utils.x86_64 -y
创建smb多用户挂载目录:
这里使用root目录
建立smb认证文件:
[root@samba-server ~]# vim /root/haha
username=student
password=redhat #服务端的密码
编辑/etc/fstab,永久挂载smb共享:
//server0/smbshare /mnt/multiuser cifs credentials=/root/smb-multiuser.txt,multiuser,
sec=ntlmssp 0 0
mount -a
具体如下:
[root@samba-server ~]# vim /root/hahausername=studentpassword=redhat[root@samba-server ~]# chmod 600 /root/haha[root@samba-server ~]# yum install cifs-utils.x86_64 -y[root@samba-server ~]# mount -o credentials=/root/haha,multiuser,sec=ntlmssp //172.25.254.20/haha /mnt#credentials=/root/haha 指定挂载所用到的用户文件#multiuser 支持多用户认证#sec=ntlmssp 认证方式为标准smb认证方式[root@samba-server ~]# ls /mntfile0 file1 file2 file3 file4 file6[root@samba-server ~]# su - studentLast login: Wed May 31 06:36:16 EDT 2017 on pts/1[student@samba-server ~]$ ls /mntls: cannot access /mnt: Permission denied ##因为没有作smb的认证所以无法访问smb共享[student@samba-server ~]$ cifscreds add -u westos 172.25.254.20 ##添加smb认证Password: ##smb用户westos认证的密码[student@samba-server ~]$ ls /mntfile0 file1 file2 file3 file4 file6
- 共享文件下载--SMB
- 共享文件上传--SMB
- smb协议共享文件
- smb文件共享实现
- smb文件共享
- SMB文件共享
- SMB 文件共享
- Linux Windows 文件共享smb
- 小型ANET linux-smb文件共享
- smb服务器建立共享文件账号
- 基于SMB协议的共享文件读写
- IOS_使用SMB实现局域网文件共享
- iOS_使用SMB实现局域网文件共享
- Android smb共享文件的下载
- java基于SMB协议读取共享文件
- SMB实现共享文件(上传、下载)
- SMB协议的共享文件读写
- ubuntu安装smb与windows共享文件
- [YTU]_2384 ( 矩形类中运算符重载【C++】)
- 冒泡排序--Java版
- Python 文件判断 用户输入 文件名和路径 判断是否存在
- JSP数据交互
- 通过扫描就能获得信息Vin码识别技术
- smb文件共享
- 矢量资源工具Vector Asset Studio
- yocto
- Java时延
- Webpack按需打包Lodash的几种方式
- Linux 下多线程的消费者-生产者模型
- Power Strings (kmp中next数组的运用)
- C语言指针的形象理解-----/*自己编写*/
- 记一次MySQL使用外网IP连接超时