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