一紙離人醉 2018-11-28 12:33:51 13246 收藏 13
版權
Samba
Samba是在Linux和UNIX系統上實作SMB協定的一個免費軟體,有服務端和用戶端程式構成。随着Linux的普及,如何共享Linux下的檔案成為使用者關心的問題。其實,幾乎所有的Linux發行套件都提供了一個很好的工具Samba——通過它可以輕松實作檔案共享。
一、SMB檔案共享
通用lnternet檔案系統(CIFS)也稱為伺服器資訊塊(SMB),是适用于MicrosoftWindows伺服器和用戶端的标準檔案和列印機共享系統。
Samba服務可用于将Linux檔案系統作為CIFS/SMB網絡檔案共享進行共享,并将Linux列印機作為CIFS/SMB列印機共享進行共享。
[root@shareserver ~]# ———————服務端 ip:172.25.254.227
[root@client ~]# ——————用戶端 ip:172.25.254.127
1
2
1、安裝以及啟動服務
在服務端:
[root@shareserver ~]# yum install samba samba-client samba-common -y
#samba —— 提供samba服務的主指令;
#samba-client —— 提供測試服務
#samba-common —— 提供管理指令;
[root@shareserver ~]# systemctl start smb #啟動smb服務
[root@shareserver ~]# systemctl enable smb.service #開機啟動smb服務
[root@shareserver ~]# systemctl stop firewalld #關閉火牆
[root@shareserver ~]# systemctl disable firewalld #關閉火牆并且開機不啟動
[root@shareserver ~]# netstat -antlp | grep smb # 檢視samba的端口
3
4
5
6
7
8
9
用戶端進行測試:
[root@client ~]# yum install samba-client -y #安裝
匿名登陸
因為在服務端未設定,是以不用輸入密碼,直接回車,登陸成功
[root@client ~]# smbclient -L //172.25.254.227 #匿名登陸服務端,并檢視資訊,-L表示檢視共享清單
2、添加smb使用者(在服務端)
smb使用者必須是本地使用者!!!
添加使用者student、tom
[root@shareserver ~]# smbpasswd -a student # 把本機使用者student添加到sam伺服器上
New SMB password: # 設定samba使用者的密碼,此密碼與本機使用者的密碼無關
Retype new SMB password: # 确認samba使用者的密碼
Added user student. # 成功
[root@shareserver ~]# smbpasswd -a tom
New SMB password:
Retype new SMB password:
Added user tom.
[root@shareserver ~]# pdbedit -L # 檢視smb中的所有使用者
student:1000:Student User
tom:1001:
10
11
smbpasswd 常用參數:
-a student: 添加使用者student為samba使用者
-d student:禁用samba使用者student
-e student: 啟用samba使用者student
-x student: 删除samba使用者student
可以用 smbpasswd 添加 smb 使用者
還可以用 pdbedit 添加 smb 使用者
pdbedit 常用參數
–a student:建立Samba使用者。
–x student:删除Samba使用者。
–L:列出Samba使用者清單,讀取passdb.tdb資料庫檔案。
–Lv:列出Samba使用者清單的詳細資訊。
–c “[D]” –u student:暫停該Samba使用者的賬号。
–c “[]” –u student:恢複該Samba使用者的賬号。
用戶端測試:
[root@client ~]# smbclient -L //172.25.254.227 -U tom # 指定tom使用者登陸,-U表示指定使用者
可能出現的錯誤:
輸入密碼導緻的錯誤:
[root@client ~]# smbclient //172.25.254.227/tom -U tom # 登陸共享服務端,檢視共享使用者的家目錄資訊時發現被拒絕
服務端samba_enable_home_dirs布爾值的影響:
在服務端排錯:
[root@shareserver ~]# getenforce
Enforcing # selinux與samba服務沖突
[root@shareserver ~]# getsebool -a | grep samba
[root@shareserver ~]# setsebool -P samba_enable_home_dirs on # 分享home目錄即可。
在用戶端測試:
成功:
[root@client ~]# mount -o username=tom,password=123 //172.25.254.227/tom /mnt # 把使用者挂載到/mnt下,友善對共享使用者進行管理
[root@client ~]# df
[root@client ~]# cd /mnt
[root@client mnt]# touch file{1..5}
在服務端tom使用者家目錄檢視:
二、samba的通路控制
1、域名的修改
用戶端:
[root@client mnt]# smbclient -L //172.25.254.227/tom -U tom # 檢視到域名為MYGROUP
服務端:
[root@shareserver ~]# vim /etc/samba/smb.conf # 打開
89 workgroup = TOM #更改
[root@shareserver ~]# systemctl restart smb.service #重新開機smb服務
[root@client mnt]# smbclient -L //172.25.254.227/tom -U tom # 再次檢視,域名修改成功
2、黑白名單(Samba伺服器預設對所有使用者開放權限)
白名單:
[root@shareserver ~]# vim /etc/samba/smb.conf # 設定白名單,即改即生效
95 hosts allow = 172.25.254.227 # ip為172.25.254.227白名單
在主機client(IP=172.25.254.127)為上測試:
失敗
在主機shareserver(IP=172.25.254.227)上通路:
成功
黑名單:
[root@shareserver ~]# vim /etc/samba/smb.conf # 設定黑名單,即改即生效
96 hosts deny = 172.25.254.227 # ip為172.25.254.227黑名單
(如果黑白名單同時存在該ip,則該ip為白名單裡)
恢複環境
[root@shareserver ~]# vim /etc/samba/smb.conf
删除或者注釋的黑白名單
3、共享目錄
(1)共享自己建立的目錄
[root@shareserver ~]# mkdir /smbfile
[root@shareserver ~]# touch /smbfile/hello{1..3}
[root@shareserver ~]# vim /etc/samba/smb.conf # 共享自己建立的目錄
321 [smb] # 這是共享名,可以自己起名字
322 comment = hello # 共享檔案的說明,這個也是自己自定義的
323 path = /smbfile # 共享檔案的絕對路徑
[root@shareserver ~]# systemctl restart smb.service #重新開機smb服務
端測試
[root@client mnt]# smbclient -L //172.25.254.227/smb -U tom # 在共享資訊裡檢視到共享目錄,smb是自己設定的共享目錄名稱
[root@client mnt]# smbclient //172.25.254.227/file -U tom # 在用戶端登入檢視檔案内容時被禁止
在服務端排查原因
[root@shareserver ~]# ls -Zd /smbfile/ # 發現共享目錄的安全上下文不是samba共享
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /linuxfile/
[root@shareserver ~]# semanage fcontext -a -t samba_share_t '/smbfile(/.*)?' # 修改共享目錄的安全上下文
[root@shareserver ~]# restorecon -RvvF /smbfile/ # 重新整理
[root@shareserver ~]# ls -Zd /smbfile/ # 檢視修改成功
drwxr-xr-x. root root system_u:object_r:samba_share_t:s0 /smbfile/
再次在用戶端檢視:
[root@client mnt]# smbclient //172.25.254.227/smb -U tom # 可以檢視
(2)共享系統目錄
[root@shareserver ~]# vim /etc/samba/smb.conf
323 [mnt] # 共享系統目錄的名稱,這個名稱也是任意的
324 comment = shiyan smb
325 path = /mnt # 共享系統目錄的絕對路徑
[root@shareserver ~]# systemctl restart smb.service #重新開機服務
[root@client mnt]# smbclient -L //172.25.254.227/mnt -U tom
[root@client mnt]# smbclient //172.25.254.227/mnt -U tom # 但是登入上之後,也不能檢視内容,這是因為安全上下文不一緻
由于這是系統檔案,如果更改安全上下文,會影響其他使用者的通路,是以我們讓samba服務對讀寫功能權限不做限制
[root@shareserver ~]# getsebool -a | grep samba
[root@shareserver ~]# setsebool -P samba_export_all_ro on # 打開隻讀通路權限
[root@shareserver ~]# setsebool -P samba_export_all_rw on # 打開讀寫通路權限
檢視權限并給該目錄加r(是否可以檢視目錄中有什麼子檔案或者子目錄)權限
chmod +r /mnt ##給mnt添加r權限
在用戶端:
[root@client ~]# smbclient //172.25.254.227/mnt -U tom
4、samba服務的權限管理
(1)所有使用者都可寫
324 writable = yes # 所有使用者都可寫
[root@shareserver ~]# systemctl restart smb #重新開機服務
給共享目錄加可寫權限
[root@shareserver ~]# chmod o+w /smbfile/ # 修改權限
[root@client ~]# smbclient //172.25.254.227/smb -U tom
(2)指定使用者可寫
[root@shareserver ~]# pdbedit -L # 檢視samba服務上的使用者
325 write list = tom # 限定隻有tom使用者可以寫
[root@shareserver ~]# systemctl restart smb #重新開機服務
[root@client ~]# smbclient //172.25.254.227/file -U linux # 用linux使用者登入,可以删除
[root@client ~]# smbclient //172.25.254.227/file -U student # 用其他使用者登入不能删除
(3)指定使用者組可寫
325 write list = @tom
[root@shareserver ~]# systemctl restart smb.service
[root@shareserver ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@shareserver ~]# usermod -G tom student # 把student使用者添加到tom組中
uid=1000(student) gid=1000(student) groups=1000(student),1001(tom)
[root@client ~]# smbclient //172.25.254.227/smb -U student
(4)指定使用者登入
326 valid users = tom # 指定隻有tom使用者可以登陸
[root@shareserver ~]# systemctl restart smb.service
[root@client mnt]# smbclient //172.25.254.227/smb -U student # 用student使用者登入
[root@client mnt]# smbclient //172.25.254.227/smb -U tom # 用其他使用者登入
(5)指定使用者組登入
325 valid users = +student
[root@shareserver ~]# systemctl restart smb #重新開機
[root@shareserver ~]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom)
[root@shareserver ~]# usermod -G student tom # 把tom使用者加入到student組中
uid=1001(tom) gid=1001(tom) groups=1001(tom),1000(student)
[root@client ~]# smbclient //172.25.254.227/smb -U tom
(6)隐藏共享目錄
在用戶端檢視共享目錄:
[root@client ~]# smbclient -L //172.25.254./smb -U tom # 預設共享目錄時可以看見的
327 browseable = no
[root@client ~]# smbclient -L //172.25.254.227/smb -U tom
與前面在用戶端檢視共享目錄,該目錄已被隐藏
(7)匿名使用者登入
[root@client ~]# smbclient //172.25.254.227/smb # 在用戶端檢視,匿名使用者預設不能登入
176 map to guest = bad user # 其他使用者(也就是匿名使用者)映射到guest
328 guest ok = yes # 允許其他使用者(也就是匿名使用者)登陸
[root@client ~]# smbclient //172.25.254.227/sam # 匿名使用者成功登入
Enter root's password: # 匿名使用者沒有密碼,直接按回車即可
在服務端檢視:
[root@shareserver ~]# ll /smbfile/ # 檢視到匿名使用者上傳的檔案
(8)指定root使用者
[root@shareserver ~]# vim /etc/samba/smb.conf
330 admin users = tom # 指定tom使用者為超級使用者
[root@shareserver ~]# systemctl restart smb.service
[root@client ~]# smbclient //172.25.254.227/smb -U tom
[root@shareserver ~]# ll /smbfile/