linux——SMB文件共享及应用实例

SMB文件共享

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置”NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

CIFS协议

1.什么是CIFS协议
CIFS 使用客户/服务器模式。客户程序请求远在服务器上的服务器程序为它提供服务,服务器获得请求并返回响应。CIFS是公共的或开放的SMB协议版本,像SMB协议一样,CIFS在高层运行,而不像TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。
2.CIFS的功能
1.访问服务器本地文件并读写这些文件
2.与其它用户一起共享一些文件块

实验前提:重置两个虚拟机,配置yum源,
主机名:
1.client.example(客户端)
2.server.example(服务端)

一、安装smb服务并访问

服务器端:
[root@server ~]# yum search samba #查找smb文件共享服务所需软件
Loaded plugins: langpacks
============================== N/S matched: samba ==============================
samba-client.x86_64 : Samba client programs
samba-common.x86_64 : Files used by both Samba servers and clients
samba-libs.i686 : Samba libraries
samba-libs.x86_64 : Samba libraries
samba-python.x86_64 : Samba Python libraries
samba-winbind.x86_64 : Samba winbind
samba-winbind-modules.i686 : Samba winbind modules
samba-winbind-modules.x86_64 : Samba winbind modules
samba.x86_64 : Server and Client software to interoperate with Windows machines

  Name and summary matches only, use "search all" for everything.
[root@server ~]# yum install samba-client.x86_64 samba-common.x86_64 samba.x86_64 -y
Loaded plugins: langpacks
Usage: yum [options] COMMAND

List of Commands:

check          Check for problems in the rpmdb
check-update   Check for available package updates
[root@server ~]# systemctl start smb
[root@server ~]# systemctl enable smb
Ln-s'/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@server ~]# netstat -antlupe | grep smb
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      0          68632      3222/smbd           
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      0          68633      3222/smbd           
tcp6       0      0 :::445                  :::*                    LISTEN      0          68630      3222/smbd           
tcp6       0      0 :::139                  :::*                    LISTEN      0          68631      3222/smbd 
客户端访问:
[root@client ~]# yum install samba-client.x86_64 -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package samba-client.x86_64 0:4.1.1-31.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
[root@client ~]# smbclient -L //172.25.254.126
Enter root's password: #无密码 Anonymous login successful Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1] Sharename Type Comment --------- ---- ------- IPC$ IPC IPC Service (Samba Server Version 4.1.1) Anonymous login successful Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1] Server Comment --------- ------- Workgroup Master --------- -------片

《linux——SMB文件共享及应用实例》
《linux——SMB文件共享及应用实例》
《linux——SMB文件共享及应用实例》
《linux——SMB文件共享及应用实例》

二、添加本地用户进行访问

服务器端添加用户(只能添加本地真实存在的用户):
[root@server ~]# id student #查看是否有student用户。
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@server ~]# smbpasswd -a student #添加student到smb服务
New SMB password:
Retype new SMB password:
Added user student.
[root@server ~]# id westos #无westos用户
id: westos: no such user
[root@server ~]# smbpasswd -a westos 
New SMB password:
Retype new SMB password:
Failed to add entry for user westos. #添加不成功
[root@server ~]# useradd westos #创建westos用户
[root@server ~]# smbpasswd -a westos 
New SMB password:
Retype new SMB password:
Added user westos.               #此时添加成功
[root@server ~]# pdbedit -L #查看smb服务允许用户
student:1000:Student User
westos:1001: 
[root@server ~]# pdbedit -x student #删除student用户
[root@server ~]# pdbedit -L
westos:1001:                      #student用户被删除
[root@server ~]# smbpasswd -a student #重新添加student用户
New SMB password:
Retype new SMB password:
Added user student.
[root@server ~]# pdbedit -L
student:1000:Student User
westos:1001:

《linux——SMB文件共享及应用实例》

客户端访问:
[root@client ~]# smbclient //172.25.254.126/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \* selinux禁止
smb: \> quit
客户端配置selinux:
[root@server ~]# getsebool -a | grep smb
smbd_anon_write --> off
[root@server ~]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@server ~]# setsebool -P samba_enable_home_dirs on
[root@server ~]#
客户端访问:
[root@client ~]# smbclient //172.25.254.226/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Thu Jul 10 19:06:52 2014
.. D 0 Fri Jun 1 21:49:35 2018
.bash_logout H 18 Wed Jan 29 07:45<%2

点赞