Samba是在Linux和UNIX系統上實作SMB協定的一個免費軟體,由伺服器及用戶端程式構成。
以下通過兩個基礎實驗來學習samba,實驗平台為Centos 6.2,環境為:
Samba伺服器
主機名:luffy IP位址:192.168.56.53
用戶端測試機
主機名:zoro IP位址:192.168.56.54
實驗一
假設公司有一台samba伺服器,提供了一個共享名為/share的共享目錄,現在要求隻允許192.168.56.0網段的使用者可以使用自己的samba帳戶登入此目錄,并可讀寫,但不允許動别人的資料。
一、準備工作
1.建立使用者user1、user2,并在根目錄下建立共享檔案夾share及其共享檔案,其中share的檔案夾屬性為777。
建立結果如下:
[root@luffy ~]# cat /etc/passwd
……省略部分内容……
user1:x:502:502::/home/user1:/bin/bash
user2:x:503:503::/home/user2:/bin/bash
[root@ luffy ~]# ls -l /share
total 8
-rw-r--r--. 1 root root 4 May 24 06:41 a
-rw-r--r--. 1 root root 4 May 24 06:41 b
[root@ luffy ~]# ls -ld /share
drwxrwxrwx. 2 root root 4096 May 24 06:41 /share
2.安裝samba軟體
[root@luffy ~]# yum install samba*
Samba*涉及到的元件是samba,samba-common,samba-client
其中samba必需安裝,後面兩個在裝系統時其實已預設安裝(是以其他linux用戶端可以直接使用smbclient指令通路samba的共享檔案夾)。
3.添加samba使用者
Samba中添加的使用者,必須是samba伺服器所在系統中預先建立的,但該使用者登入samba時使用的密碼,需由samba建立并儲存。
使用到的指令是smbpasswd,參數-a表示添加使用者,-x表示删除使用者,-d表示禁用使用者。
下面将user1和user2加入到samba中,
[root@ luffy ~]# smbpasswd -a user1
New SMB password:
Retype new SMB password:
Added user user1.
[root@ luffy ~]# smbpasswd -a user2
Added user user2.
二、配置共享
在伺服器SELinux開啟(Enforcing狀态)的情況下,進行以下實驗。
1.在samba主配檔案/etc/samba/smb.conf末尾添加以下内容:
1
2
3
4
5
6
<code>[share]</code>
<code>comment = sharefile </code><code>#描述資訊</code>
<code>path = </code><code>/share</code> <code>#指定共享檔案夾的路徑</code>
<code>hosts allow = 192.168.56. </code><code>#允許通路該目錄的網段</code>
<code>browseable=</code><code>yes</code> <code>#可浏覽</code>
<code>writable = </code><code>yes</code> <code>#可寫</code>
注,hosts allow字段如果不寫,則表示所有可通路該伺服器的用戶端都可通路該共享目錄。在本實驗和許多實際環境中,都是内網共享,隻有同網段的内網使用者才可通路,“hosts allow=本網段”其實不需要寫。
另外,主配檔案第100行附近,
<code>security = user</code>
<code>#smb passwd file = /etc/samba/smbpasswd</code>
<code>passdb backend = tdbsam</code>
表示,目前(預設)使用的是user認證方式,即用戶端需要有samba中記錄的使用者及其密碼才能登入,如果是security = share,則表示采用匿名登入方式。
配置修改完成後,重新開機samba服務,
[root@luffy ~]# service smb restart
用戶端通路時,報錯:
[root@zoro ~]# smbclient //192.168.56.53/share -U user1
Enter user1's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.6.9-151.el6]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
40317 blocks of size 262144. 31467 blocks available
smb: \> quit
沒有通路權限,是由于請求被selinux攔截了。
(注:如果是出現NT_STATUS_BAD_NETWORK_NAME的報錯,大概是因為主配檔案中定義共享檔案塊時,中括号裡的名字出錯,通路路徑//192.168.56.53/share中的share不是指根目錄中的share檔案夾,而是主配檔案中自定義的[share]中括包内的share。)
2.伺服器端對samba共享目錄修改安全上下文,使用chcon指令
[root@luffy ~]# ls -ldZ /share
drwxr-xr-x. root root system_u:object_r:default_t:s0 /share
[root@luffy ~]# chcon -t samba_share_t /share
drwxr-xr-x. root root system_u:object_r:samba_share_t:s0 /share
用戶端再通路,就正常了
. D 0 Fri May 24 06:41:33 2013
.. DR 0 Fri May 24 06:18:56 2013
a 4 Fri May 24 06:41:26 2013
b 4 Fri May 24 06:41:33 2013
40317 blocks of size 262144. 31466 blocks available
smb: \>
smb: \> rm a
. D 0 Fri May 24 06:45:54 2013
smb: \>quit
從以上操作還可以看到,此時user1可以删除其root建立的檔案a(因為檔案夾share的權限被設成了777)。即user1可以動其他人的檔案,這不符合要求。
3.伺服器端對共享目錄設定特殊權限
[root@luffy ~]# chmod o+t /share
這裡的t,指sticky bit,可以了解為防删除位. 一個檔案是否可以被某使用者删除,主要取決于該檔案所屬的組是否對該使用者具有寫權限。如果沒有寫權限,則這個目錄下的所有檔案都不能被删除,同時也不能添加新的檔案。如果希望使用者能夠添加檔案,但同時不能删除檔案,則可以對檔案使用sticky bit位。設定該位後, 就算使用者對目錄具有寫權限, 也不能删除該檔案。(這話了解起來有點費勁~)
此時用戶端登入便不可以删除其他使用者的檔案了,但删除自己的檔案沒問題。
40317 blocks of size 262144. 31465 blocks available
smb: \> rm b
NT_STATUS_ACCESS_DENIED deleting remote file \b
smb: \> rmdir user1
三、将共享檔案加載到用戶端本地進行操作
用戶端使用smbclient遠端通路共享目錄時,操作指令是有限的(通過help指令可以檢視),touch指令就不可用。為做進一步驗證,要将共享目錄加載到本地,再進行相關操作。
通過mount cifs方式進行加載。
1.先使用user1挂載、操作:
[root@zoro ~]# mount -t cifs 192.168.56.53:/share /mnt/ -o username=user1
Password:
[root@zoro ~]# cd /mnt
[root@zoro mnt]# ls
b
[root@zoro mnt]# mkdir user1
[root@zoro mnt]# vim user1/1
user111 (注,這個是輸入的内容)
[root@zoro mnt]# ll
total 4
-rw-r--r-- 1 root root 4 May 24 06:41 b
drwxr-xr-x 2 502 502 0 May 24 07:02 user1
[root@zoro mnt]# ll user1/
-rw-r--r-- 1 502 502 8 May 24 07:02 1
[root@zoro mnt]# cd
[root@zoro ~]# umount -t cifs 192.168.56.53:/share /mnt/
umount: /mnt: not mounted
2.再用user2挂載、操作:
[root@zoro ~]# mount -t cifs 192.168.56.53:/share /mnt/ -o username=user2%redhat
(注,這行指令,直接把user2的samba密碼redhat也寫出來了)
b user1
[root@zoro mnt]# ls user1/
[root@zoro mnt]# cat user1/1
user111
[root@zoro mnt]# rm -rf user1/
rm: cannot remove `user1/1': Permission denied
[root@zoro mnt]# rm -rf b
rm: cannot remove `b': Permission denied
[root@zoro mnt]#
[root@zoro mnt]# touch 2
可以看到,user2是沒有權限删除或更改其他使用者的檔案的,但可以建立屬于自己的檔案。
實驗二
公司的IT部門有it1、it2兩個成員,他們可以登入到自己部門的共享目錄進行讀寫操作,上傳的檔案的權限為640,且隻允許他們之間讀取檔案,但不可以修改對方的檔案。
1.建立使用者it1、it2和使用者組it,并将it1和it2加入到使用者組it。
2.建立共享目錄/it,将其權限屬性置為777,并使用chcon -t samba_share_t /it指令修改其安全上下文(本實驗仍然在SELinux開啟的情況下進行測試)。
3.将使用者it1、it2加入到samba伺服器
[root@luffy ~]# smbpasswd -a it1
Added user it1.
[root@luffy ~]# smbpasswd -a it2
Added user it2.
1. 在主配檔案/etc/samba/smb.conf末尾添加以下内容:
7
8
9
<code>[it]</code>
<code>comment = it dept.</code>
<code>path = </code><code>/it</code>
<code>browseable=</code><code>yes</code>
<code>writable = </code><code>yes</code>
<code>valid </code><code>users</code> <code>= @it </code><code>#指定允許通路的使用者組為it,注意@符号</code>
<code>create mask = 640 </code><code>#指定新建立的檔案的權限為640</code>
<code>directory mask = 750 </code><code>#指定新建立的檔案夾的權限為750</code>
<code>group = it </code><code>#指定新建立的檔案(夾)的使用者組為it,這裡沒有@</code>
2.用戶端通路
[root@zoro ~]# smbclient //192.168.56.53/it -U user1
tree connect failed: NT_STATUS_ACCESS_DENIED
[root@zoro ~]# smbclient //192.168.56.53/it -U it1
Enter it1's password:
[root@zoro ~]# smbclient //192.168.56.53/it -U it2
Enter it2's password:
. D 0 Mon May 27 05:40:45 2013
.. DR 0 Mon May 27 05:40:45 2013
40317 blocks of size 262144. 31463 blocks available
從以上連接配接可以看出,隻有屬于it使用者組的使用者才有通路/it的權限。
1.使用it1登入、挂載,建立檔案(夾)
[root@zoro ~]# mount -t cifs 192.168.56.53:/it /mnt/ -o username=it1%redhat
[root@zoro mnt]# mkdir it01
[root@zoro mnt]# vim it01/a
this is file a (注,這個是輸入的内容)
[root@zoro mnt]# ls -ld it01/
drwxr-x--- 2 504 506 0 May 27 06:35 it01/
[root@zoro mnt]# ls -ll it01/
-rw-r----- 1 504 506 15 May 27 06:35 a
[root@zoro ~]# umount -t cifs 192.168.56.53:/it /mnt/
注:由于在配置檔案中,對/it的共享設定,指定了create mask = 640和directory mask = 750,是以建立的檔案和檔案夾,都是使用者組成員有讀(執行)權限,其他人沒有任何讀寫權限。
另外,由于/it的共享設定裡有group = it這一行,it1無論是建立檔案或檔案夾,其所屬使用者組都是it1(504)所在的使用者組it(506)。這裡因為是遠端挂載的,使用ll指令隻能看到使用者id。
下面使用it2通路時,由于it2也在it組裡,是以it2對檔案夾it01的r-x權限,對檔案a有r--權限。
2.使用it2登入、挂載,進行驗證
[root@zoro ~]# mount -t cifs 192.168.56.53:/it /mnt/ -o username=it2%redhat
it01
[root@zoro mnt]# ls it01/
a
[root@zoro mnt]# cat it01/a
this is file a
[root@zoro mnt]# rm -rf it01
rm: cannot remove `it01/a': Permission denied
[root@zoro mnt]# rm -rf it01/a
[root@zoro mnt]# touch it01/b
touch: cannot touch `it01/b': Permission denied
以上可以看出,it2可以讀it1的檔案(夾),但卻沒有權限删除和修改(使用vim編輯it01/a時,會被提示不能修改)。
在上述配置檔案中,如果對/it的共享設定,指定了create mask = 640和directory mask = 750,卻沒有添加group = it,那麼it1建立的檔案和檔案夾,其使用者組都是it1,此時it2屬于其他人,是沒有讀寫權限的。
本文轉自Sunshyfangtian 51CTO部落格,原文連結:http://blog.51cto.com/sunshyfangtian/1212270,如需轉載請自行聯系原作者