Samba
Samba
Samba是在Linux和UNIX系統上實作SMB協定的一個免費軟體,由伺服器及用戶端程式構成
SMB(Server Messages Block,資訊服務塊)
區域網路上共享檔案和列印機的一種通信協定,它為區域網路内的不同計算機之間提供檔案及列印機等資源的共享服務
SMB協定是客戶機/伺服器型協定,客戶機通過該協定可以通路伺服器上的共享檔案系統、列印機及其他資源
Samba不但能與區域網路絡主機分享資源,還能與全世界的電腦分享資源
伺服器端
安裝 smb 用戶端、主程式、smb文法檢測等
yum install samba-client.x86_64 samba-common.x86_64 samba.x86_64 -y
開啟 smb
systemctl start smb
yum install cifs-utlis -y
netstat -antlupe | grep smb
檢視 smb 端口
smb 端口為 445 和 139
開啟防火牆
systemctl start firewalld
添加 samba 服務
firewall-cmd --permanent --add-service=samba
添加 samba-client 服務
firewall-cmd --permanent --add-service=samba-client
firewall-cmd --reload
檢視服務已開啟
firewall-cmd --list-all
伺服器端
useradd zhang
建立本地使用者 zhang 和 westos
useradd westos
smbpasswd -a westos
建立 westos 和 zhang 的 smb 使用者密碼
smbpasswd -a zhang
pdbedit -L
檢視以建立 smb 使用者
注意
smb 使用者必須是伺服器端本地使用者
測試端
安裝 smb 用戶端
yum install samba-client -y
yum install cifs-utlis -y
smbclient -L //172.25.254.130 -U zhang
檢視伺服器端 smb 使用者 zhang 所共享的資源,需要 smb 使用者 zhang 的密碼
Enter zhang's password:
smbclient //172.25.254.130/zhang -U zhang
進入 smb 使用者 zhang 共享的目錄内
Enter zhang's password:
!ls
檢視進入 smb 用戶端前, shell 目前目錄
檢視 smb 使用者 zhang 目錄
ls
put f1
目前 shell 所在目錄内有 f1 檔案
上傳至 smb 使用者 zhang 目錄内
伺服器端
切換到使用者 zhang 家目錄下
cd /home/zhang
ls
測試端
删除 f1 檔案
rm f1
ls
伺服器端
ls
f1 檔案已被删除
測試端
建立挂載點
mkdir /mnt/zhang
挂載伺服器端 smb 使用者 zhang 到 /mnt/zhang ,需要提供 smb 使用者 zhang 賬号密碼
mount //172.25.254.130/zhang /mnt/zhang -o username=zhang,password=123
切換到 /mnt/zhang/
cd /mnt/zhang/
ls
建立f1–f5檔案
touch f{1..5}
ls
伺服器端
切換到 /home/zhang/
cd /home/zhang
可檢視到用戶端操作所建立的 f1–f5 檔案
ls
測試端
smbclient -L //172.25.254.130/zhang -U zhang
檢視到域名為 [MYGROUP]
伺服器端
vim /etc/samba/smb.conf
域名修改為 WESTOS
workgroup = WESTOS
systemctl restart smb
測試端
smbclient -L //172.25.254.130/zhang -U zhang
域名已改變為 WESTOS
伺服器端
修改smb配置檔案
vim /etc/samba/smb.conf
隻允許 172.25.254.131 通路
hosts allow = 172.25.254.131
systemctl restart smb
測試端
smbclient -L //172.25.254.130/zhang -U zhang
172.25.254.130
禁止通路
smbclient -L //172.25.254.130/zhang -U zhang
vim /etc/samba/smb.conf
拒絕 172.25.254.131 通路
host deny =172.25.254.131
systemctl restart smb
vim /etc/samba/smb.conf
允許廣播域 172.25.254.0 – 172.25.254.255 通路
hosts allow = 172.25.254.
smb寫權限
檢視SELinux
getenforce
關閉
Disabled
vim /etc/sysconfig/selinux
修改為強制級别
SELINUX=enforcing
重新開機生效
reboot
getenforce
Enforing
方法一:
修改目錄及目錄内容安全上下文
建立目錄
mkdir /westos_smb
打開目錄及目錄内容安全上下文
semanage fcontext -a -t samba_share_t '/westos_smb(/.*?)?'
編輯 samba 配置檔案
vim /etc/samba/smb.conf
[WESTOS]
備注(自定義)
comment = Share Dirctory
路徑
path = /westos_smb
開啟寫權限
writable = yes
systemctl restart smb
使用者 westos 設定 acl 權限 對于 /westos_smb 目錄
setfacl -m u:westos:rwx /westos_smb/
使用者 zhang 設定 acl 權限 對于 /westos_smb 目錄
setfacl -m u:zhang:rwx /westos_smb/
檢視 acl 權限
getfacl /westos_smb
挂載
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
cd /mnt
touch f1
rm -rf W*
方法二:
修改 SEBool
vim /etc/samba/smb.conf
[mnt]
備注(自定義)
comment = Share System mnt Dirctory
路徑
path = /mnt
開啟寫權限
writable = yes
systemctl restart smb
systemctl restart smb
挂載
mount //172.25.254.130/mnt /mnt -o username=westos,password=123
cd /mnt
權限不夠
touch f1
檢視SEBool
getsebool -a | grep samba
打開權限
setsebool -P samba_export_all_rw=on
檢視 samba SEBool 權限
getsebool -a | grep samba
cd /mnt
touch f1
ls
隐藏共享檔案目錄
vim /etc/samba/smb.conf
[WESTOS]
備注(自定義)
comment = Share Dirctory
路徑
path = /westos_smb
關閉隐藏
browseable = yes
systemctl restart smb
WESTOS 目錄可見
smbclient -L //172.25.254.130/WESTOS -U zhang
vim /etc/samba/smb.conf
[WESTOS]
備注(自定義)
comment = Share Dirctory
路徑
path = /westos_smb
開啟隐藏
browseable =no
systemctl restart smb
WESTOS 目錄隐藏
smbclient -L //172.25.254.130/WESTOS -U zhang
使用者以 root 身份登陸
vim /etc/samba/smb.conf
[WESTOS]
備注(自定義)
comment = Share Dirctory
路徑
path = /westos_smb
root 身份
admin users = westos
systemctl restart smb
挂載 測試
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
cd /mnt
touch f1
ll
匿名使用者
vim /etc/samba/smb.conf
map to guest = bad user
[WESTOS]
comment = Share Dirctory
path = /westos_smb
writable = yes
guest ok = yes
systemctl restart smb
getenforce
當 SELinux 為 Enforing 時需要修改 SEBool 權限
Enforing
getsebool -a | grep samba
setsebool -P samba_export_all_rw=on
getsebool -a | grep samba
匿名使用者無密碼,回車即可
smbclient //172.25.254.130/WESTOS
檢視 /WESTOS 目錄
ls
檢視未開啟 smbclient 前,使用者目前目錄
!ls
挂載
mount //172.25.254.130/WESTOS /mnt -o username=guest
touch f2
ll
寫權限清單
服務端
vim /etc/smaba/smb.conf
write list = +leon
在寫權限清單裡添加 leon 使用者
leon 使用者 擁有寫權限
systemctl restart smb
測試端
mount //172.25.254.130/WESTOS /mnt -o username=leon,password=123
cd /mnt
權限不夠
touch f1
服務端
chmod 777 /westos_smb
測試端
touch f1
ll
umount /mnt
更換 westos 使用者
mount //172.25.254.130/WESTOS /mnt -o username=westos,password=123
cd /mnt
touch f2
權限不夠
因為沒有開啟 writable (寫)權限
在 write list 權限裡隻有 leon 使用者
服務端
vim /etc/smaba/smb.conf
在 leon 使用者組裡的所有使用者都擁有寫權限
valid users = @leon
systemctl restart smb
usermod -G leon westos
添加 westos 使用者到 leon 使用者組裡
id westos
測試端
touch f2
再次建立 f2 檔案
生成