CentOS7 下編譯安裝 Samba,什麼是 SMB/CIFS 協議

1、關於 Samba

1. SMB

(1)定義python

SMB (server message block):是一種用來訪問網絡中文件、打印機和其餘共享網絡資源的應用層通訊協議。主要功能是使網絡上的機器可以共享計算機文件、打印機、串行端口和通信等資源。提供經認證的進程間通訊機制,主要運行在windows系統下ios

(2)特性程序員

  1. SMB 能夠以多種方式運行在會話層(或更低):
  • 傳輸層:直接運行在 TCP 上 port 445
  • 會話層:基於 NetBIOS API,運行在不一樣的傳輸層
    • (1)UDP ports 137, 138 & TCP ports 137, 139 (NetBIOS over TCP/IP)
    • (2)一些傳統協議,例如 NBF, IPX/SPX.
  1. SMB 的 "進程間通訊" (IPC) 系統提供 命名管道s 機制,它使得程序員能夠方便的實現繼認可證。
  2. SMB 簽名 Server Message Block version 2 (SMB2) 旨在經過將SMB signals合併爲一個數據包來減輕這個性能限制。
  3. SMB 支持機會鎖 - 一種特殊的鎖,來提高性能。
  4. SMB 服務是 Microsoft's Distributed File System 實現的基礎。

(3)歷史windows

SMB / CIFS / SMB1
CIFS (Common Internet File System):SMB2以前的SMB協議。centos

SMB 2.0
2006年,Microsoft 隨着 Windows Vista 的發佈 引入了新的SMB版本 (SMB 2.0 or SMB2)安全

SMB 2.1
SMB 2.1, 隨 Windows 7 和 Server 2008 R2 引入, 主要是經過引入新的機會鎖機制來提高性能。網絡

SMB 3.0
SMB 3.0 (previously named SMB 2.2) 在Windows 8 和 Windows Server 2012 中引入,帶來幾項重要的變化:
(1)the SMB Direct Protocol (SMB over remote direct memory access [RDMA])
(2)SMB Multichannel (multiple connections per SMB session)
(3)SMB Transparent Failoversession

SMB 3.0.2
隨 Windows 8.1 和 Windows Server 2012 R2引入。在這些版本和之後的版本中,能夠選擇性地禁用較早的SMB版本1,以提升安全性。less

SMB 3.1.1
隨 Windows 10 和 Windows Server 2016 引入。這個版本除了SMB3中添加的AES 128 CCM 加密以外,還支持AES 128 GCM 加密,並使用SHA-512散列實現預認證完整性檢查。在使用SMB2.x 以上版本的客戶端進行鏈接時,SMB 3.1.1還強制要求進行安全協商。

2. Samba

(1)定義

Samba 實現了 SMB 協議,是適用於 Linux 和 Unix 的標準 Windows 互操做性程序套件。Samba 是免費軟件,遵循 GNU General Public License(通用公共許可證)。提供安全、穩定、快速的文件和打印服務 。

(2)官方描述

Samba is the standard Windows interoperability suite of programs for Linux and Unix.

Samba is Free Software licensed under the GNU General Public License, the Samba project is a member of the Software Freedom Conservancy.

Since 1992, Samba has provided secure, stable and fast file and print services for all clients using the SMB/CIFS protocol.

Samba is an important component to seamlessly integrate Linux/Unix Servers and Desktops into Active Directory environments. It can function both as a domain controller or as a regular domain member.

 

2、yum 安裝 Samba

1. 安裝 Samba

(1)查看CentOS 版本

# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

(2)經過 yum 安裝 Samba

# yum -y install samba samba-client samba-common

2. 查看版本

# rpm -qi samba
Name        : samba
Epoch       : 0
Version     : 4.7.1
Release     : 9.el7_5
Architecture: x86_64
...

3. 查看配置文件

默認配置文件爲:/etc/samba/smb.conf

cat /etc/samba/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 = 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

示例文件爲:/etc/samba/smb.conf.example

cat /etc/samba/smb.conf.example

#==== Global Settings ====
[global]
# ---- Network-Related Options ----
    workgroup = MYGROUP
    server string = Samba Server Version %v
;   netbios name = MYSERVER
;   interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24
;   hosts allow = 127. 192.168.12. 192.168.13.

# ---- Logging Options ----
    log file = /var/log/samba/log.%m
    max log size = 50

# ---- Standalone Server Options ----
    security = user
    passdb backend = tdbsam

# ---- Printing Options ----
    load printers = yes
    cups options = raw

#==== Share Definitions ====

[homes]
    comment = Home Directories
    browseable = no
    writable = yes
;   valid users = %S
;   valid users = MYDOMAIN\%S

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

...

4. 啓動服務

4.1. 修改配置文件

# vi /etc/samba/smb.conf

[global]
        workgroup = WORKGROUP
        netbios name = 137
        security = user
        map to guest = bad user

[anshare]
        comment = test share file
        path = /samba/anshare
        # 這裏限定只能經過 andy 訪問
        valid users = andy
        browseable = yes
        writeable = yes
        guest ok = yes
        
[nobodyshare]
        comment = nobody share file
        path = /samba/share
        browseable = yes
        writeable = yes
        guest ok = yes

4.2. 新增共享文件夾

mkdir /samba/anshare
mkdir /samba/share
//容許 nobodyshare 匿名訪問,所以須要修改 share 的用戶組
chown -R nobody:nobody /samba/share

注:這裏的共享目錄可使用 Ceph 塊設備掛載的文件夾,關於如何使用塊設備,請參考 塊設備快速入門

4.3. 新增用戶

// -d 指定用戶登入時的啓始目錄
# useradd -d /home/andy
// 設置密碼
# passwd andy

4.4. 新增 smb 用戶

# smbpasswd -a andy
New SMB password: //這裏輸入aa
Retype new SMB password:
Added user andy.

注:smbpasswd 命令的經常使用方法

smbpasswd -a 增長用戶(要增長的用戶必須以是系統用戶)
smbpasswd -d 凍結用戶,就是這個用戶不能在登陸了
smbpasswd -e 恢復用戶,解凍用戶,讓凍結的用戶能夠在使用
smbpasswd -x 刪除用戶

4.5. 關閉防火牆

# systemctl stop firewalld
# setenforce 0

4.6. 啓動服務

# systemctl enable smb & systemctl enable nmb

# systemctl restart smb & systemctl restart nmb

5. 本地客戶端驗證

# smbclient -L localhost -U andy
Enter WORKGROUP\andy's password: //這裏輸入aa

    Sharename       Type      Comment
    ---------       ----      -------
    anshare         Disk      test share file
    IPC$            IPC       IPC Service (Samba 4.7.1)
Reconnecting with SMB1 for workgroup listing.

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
    WORKGROUP            DESKTOP-H5VOITT

6. Windows 鏈接

(1)在Windows中訪問共享文件夾

\\192.168.0.137

(2)進入共享文件夾

  • 訪問 ansahre ,輸入 smb 用戶名和密碼:andy/aa
  • 能夠匿名訪問 nobodysahre

(3)發現沒有權限寫入

懷疑是共享目錄的權限問題:

# ll /samba
drwxr-xr-x. 2 root   root    6 11月  6 17:51 anshare
drwxr-xr-x. 2 root   root    6 11月  6 17:51 share

修改文件夾權限:

chmod 777 anshare
chmod 777 share

(4)新建文件而後在 Linux 查看

# ll /samba/anshare
drwxr-xr-x. 2 andy andy 6 11月  6 17:55 asd
-rwxr--r--. 1 andy andy 4 11月  6 17:56 asd.txt

# ll /samba/share
-rwxr--r--. 1 andy andy 6 11月  6 18:05 aaa.txt

 

3、編譯安裝 Samba

若是須要得到最新的 samba ,須要本身編譯安裝 samba 源碼。

1. 下載

官網 下載最新的穩定版,這裏咱們下載 Samba 4.7.11。

先卸載以前經過 yum 安裝的 Samba:

# yum -y remove samba samba-client samba-common

2. 安裝 gcc 及依賴包

yum -y install gcc perl python-devel libacl-devel openldap-devel pam-devel

3. 檢查系統環境並生成MakeFile

./configure

4. 編譯

make

5. 編譯安裝

make install

6. 查看版本

軟件的默認安裝目錄

# pwd
/usr/local/samba

執行 samba -V 查看版本

# /usr/local/samba/sbin/samba -V
Version 4.7.11

7. 查看配置文件

配置文件的全路徑爲:/usr/local/samba/etc/smb.conf
注意 smb.conf 默認是沒有的,須要手動建立,示例文件爲:samba-4.7.11/examples/smb.conf.default

cat smb.conf.default
cp smb.conf.default /usr/local/samba/etc/smb.conf

能夠發現,配置文件的結構以下:

#==== Global Settings ====
[global]
   workgroup = MYGROUP
   server string = Samba Server
   server role = standalone server
   log file = /usr/local/samba/var/log.%m
   max log size = 50
   dns proxy = no

#==== Share Definitions ====
[homes]
   comment = Home Directories
   browseable = no
   writable = yes

;[netlogon]
;[Profiles]
[printers]
   comment = All Printers
   path = /usr/spool/samba
   browseable = no
   guest ok = no
   writable = no
   printable = yes

;[tmp]
;[public]
;[fredsprn]
;[fredsdir]
;[pchome]
;[public]
;[myshare]

這段配置默認把用戶的家目錄共享。

8. 啓動服務

8.1. 修改配置以下

vi /usr/local/samba/etc/smb.conf

[global]
   workgroup = MYGROUP
   server string = Samba Server
   server role = standalone server
   log file = /usr/local/samba/var/log.%m
   max log size = 50
   dns proxy = no

[myshare]
   comment = mary's stuff
   path = /usr/somewhere/shared
   valid users = mary
   public = no
   writable = yes
   printable = no
   create mask = 0765

8.2. 建立共享文件夾

mkdir -p /usr/somewhere/shared

8.3. 建立用戶

# useradd -d /home/mary -m mary
# passwd mary

8.4. 建立 smb 用戶

# /usr/local/samba/bin/smbpasswd -a mary
New SMB password: //這裏輸入mm
Retype new SMB password:
Added user mary.

獲得 smb用戶名:mary,smb密碼:mm

8.5. 關閉防火牆

systemctl stop firewalld

8.6. 啓動 Samba

# /usr/local/samba/sbin/smbd 
# /usr/local/samba/sbin/nmbd

9. 本地客戶端驗證

# /usr/local/samba/bin/smbclient -L localhost -U mary

Enter MYGROUP\mary's password: 

    Sharename       Type      Comment
    ---------       ----      -------
    myshare         Disk      mary's stuff
    IPC$            IPC       IPC Service (Samba Server)
Reconnecting with SMB1 for workgroup listing.

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
    MYGROUP              SAMBA

10. Windows 鏈接

(1)在Windows中訪問共享文件夾

\\192.168.0.137

(2)輸入 smb 用戶名和密碼

(3)肯定後能夠看到 myshare

(4)發現沒有權限寫入

(5)修改共享目錄權限
懷疑是共享目錄的權限問題:

# ll /usr/somewhere
drwxr-xr-x. 2 root root 6 11月  6 10:54 shared

修改目錄權限:

# chmod 777 /usr/somewhere/shared/

(6)完成後能夠進行寫入了

參考連接
在CentOS 7中Samba服務安裝和配置
centos7編譯安裝samba共享服務

相關文章
相關標籤/搜索