天天看點

linux檔案服務之Samba

1、Samba   

Samba服務 

          Samba是在Linux和UNIX系統上實作SMB協定的一個免費軟體,由伺服器及用戶端程式構成。SMB(Server Messages Block,資訊服務塊)是一種在區域網路上共享檔案和列印機的一種通信協定,它為區域網路内的不同計算機之間提供檔案及列印機等資源的共享服務。SMB協定是客戶機/伺服器型協定,客戶機通過該協定可以通路服務          器上的共享檔案系統、列印機及其他資源。通過設定“NetBIOS over TCP/IP”使得Samba不但能與區域網路絡主機分享資源,還能與全世界的電腦分享資源。

作用:共享目錄(smb協定)

軟體:samba 服務端, samba-client 用戶端 

配置檔案:/etc/samba/smb.conf 

服務:smb, nmb 

端口:smb ---> 139/tcp,  445/tcp    提供檔案共享功能

 nmb ---> 137/udp,  138/udp 提供解析計算機名稱

配置檔案:/etc/samba/smb.conf

全局配置

[global]

        workgroup = MYGROUP>>>設定工作組名稱

        server string = Samba Server Version %v>>>顯示samba軟體版本資訊

interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24>>>samba服務監聽的IP位址

hosts allow = 127. 192.168.12. 192.168.13.>>>設定僅允許哪些主機可通路

hosts deny = 192.168.12.  192.168.1.1/24>>>拒絕哪些主機可通路

security = user>>> 基于使用者認證的通路 

share>>> 匿名通路

共享目錄配置

[共享名稱]

comment = >>> 描述資訊

path = /bj>>> 指定目錄名稱

browseable = yes >>> 可下載下傳檔案

writable = yes >>> 可上傳檔案

public = yes >>> 允許所有使用者通路 

write list = user1>>> 僅允許user1可上傳檔案 

示例: 

環境描述:

Linux    192.168.122.105Centos 7.2 檔案共享伺服器

Windows/Linux 用戶端 

需求:

通過samba軟體将本地的/caiwu目錄共享, 用戶端可通過martin使用者通路,僅允許其下載下傳檔案  

1) 關閉SELinux, 防火牆 

[root@file-server ~]# setenforce 0

[root@file-server ~]# getenforce 

Permissive

[root@file-server ~]# vim /etc/sysconfig/selinux 

[root@file-server ~]# systemctl stop firewalld.service 

[root@file-server ~]# systemctl disable firewalld.service 

2) 安裝軟體  

[root@file-server ~]# yum install -y samba samba-client

3) 編輯配置檔案,共享/caiwu目錄

[root@file-server ~]# mkdir /caiwu

[root@file-server ~]# touch /caiwu/{1..5}.mp3

[root@file-server ~]# vim /etc/samba/smb.conf 

        [caiwu]

        comment = It is a test

        path = /caiwu

        browseable = yes

4) 建立共享使用者 

[root@file-server ~]# useradd martin

[root@file-server ~]# smbpasswd -a martin

New SMB password:

Retype new SMB password:

Added user martin.

[root@file-server ~]# pdbedit -L>>> 檢視共享使用者

martin:1001:

[root@file-server ~]# 

5) 啟動服務 

[root@file-server ~]# systemctl start smb

[root@file-server ~]# systemctl enable smb

[root@file-server ~]# ss -antp | grep smbd

LISTEN     0      50           *:139                      *:*                   users:(("smbd",pid=2804,fd=38))

LISTEN     0      50           *:445                      *:*                   users:(("smbd",pid=2804,fd=37))

LISTEN     0      50          :::139                     :::*                   users:(("smbd",pid=2804,fd=36))

LISTEN     0      50          :::445                     :::*                   users:(("smbd",pid=2804,fd=35))

6) 測試通路 

Windows用戶端: 

\\192.168.122.105

取消使用者宿主目錄的共享

[root@file-server ~]# vim /etc/samba/smb.conf 

#[homes]

#       comment = Home Directories

#       browseable = no

#       writable = yes

[root@file-server ~]# systemctl restart smb

Linux用戶端:

[root@client ~]# yum install -y samba-client

[root@client ~]# smbclient //192.168.122.105/caiwu -U martin

配置允許martin使用者可上傳檔案  

1) 編輯配置檔案 

[root@file-server ~]# vim /etc/samba/smb.conf 

[caiwu]

...

writable = yes 

[root@file-server ~]# systemctl restart smb

2) 設定目錄的本地權限  

[root@file-server ~]# setfacl -m u:martin:rwx /caiwu/

示例: 

通過samba軟體将本地的/shichang目錄共享,允許martin使用者下載下傳檔案,允許admin使用者上傳檔案  

1) 建立目錄,建立共享使用者 

[root@file-server ~]# mkdir /shichang

[root@file-server ~]# touch /shichang/{1..5}.jpg

[root@file-server ~]# 

[root@file-server ~]# useradd admin

[root@file-server ~]# smbpasswd -a admin

New SMB password:

Retype new SMB password:

Added user admin.

[root@file-server ~]# 

[root@file-server ~]# pdbedit -L

martin:1001:

admin:1002:

[root@file-server ~]# 

2) 編輯配置檔案

[root@file-server ~]# vim /etc/samba/smb.conf 

[shichang]

    path = /shichang

    browseable = yes

    write list = admin

[root@file-server ~]# systemctl restart smb

[root@file-server ~]# chown admin /shichang/

[root@file-server ~]# ls -ldh /shichang/

drwxr-xr-x. 2 admin root 66 2月  21 12:00 /shichang/

[root@file-server ~]# 

3) 測試通路 

清除windows的共享緩存 

net use * /del 

windows設定網絡映射驅動器通路共享  

本文轉自 北冥有大魚  51CTO部落格,原文連結:http://blog.51cto.com/lyw168/1957466,如需轉載請自行聯系原作者

繼續閱讀