1.SMB文件共享
通用Internet文件系統(CIFS)也稱爲服務器信息塊(SMB),是適用於Microsoft Windows服務器和客戶端的標準文件和打印機共享系統。
Samba服務可用於將Linux文件系統作爲CIFS/SMB網絡文件共享進行共享,並將Linux打印機作爲CIFS/SMB打印機共享進行共享。
Samba作用:提供cifs協議實現共享文件
2.Samba服務的組成部分
軟件包:
Samba-common – Samba的支持文件
Samba-client – 客戶端應用程序
Samba – 服務器應用程序
服務名稱:
smb nmb
服務端口:
通常使用TCP/445進行所有連接。還使用UDP137、UDP138和TCP/139進行向後兼容
主配置文件:
/etc/samba/smb.conf
3.安裝
yum install samba samba-common samba-client -y
systemctl start smb nmb ##開啓服務
systemctl enable smb nmb ##自啓動
4.添加smb用戶
smb用戶必須是本地用戶
smbpasswd -a student ##設定samba用戶密碼
New SMB password: ##輸入smb當前用戶密碼
Retype new SMB password: ##確認密碼
Added user student. ##添加成功
pdbedit -L ##查看smb用戶信息
pdbedit -x smb用戶 ##刪除smb用戶
setsebool -P samba_enable_home_dirs on ##在selinux中設定smb用戶可以訪問自己的家目錄
測試
smbclient //172.25.254.231/student -U student
5.共享目錄的基本設定
mkdir /smbshare ##建立目錄
touch /smbshare/westos1 ##建立文件
vim /etc/samba/smb.conf
[westos] ##共享名稱
comment = local directory ##對共享目錄的描述
path = /smbshare ##共享目錄的絕對路徑
workgroup = WESTOS
systemctl restart smb.service ##重啓服務
semanage fcontext -a -t samba_share_t '/smbshare(/.*)?' ##安全上下文
restorecon -RvvF /smbshare ##刷新
setsebool -P samba_export_all_ro on ##只讀共享
setsebool -P samba_export_all_rw on ##讀寫共享
測試
smbclient //172.25.254.231/westos -U student
ls
6.samba的配置參數
##匿名用戶訪問##
125 map to guest = bad user
324 guest ok = yes
測試
smbclient //172.25.254.231/westos
##訪問控制##
hosts allow = ip ##僅允許
hosts deny = ip ##僅拒絕
valid users = 用戶名 ##當前共享的有效用戶
valid users = westos ##當前共享的有效用戶爲westos
valid users = @westos ##當前共享的有效用戶爲westos組
valid users = +westos ##當前共享的有效用戶爲westos組
##讀寫控制##
測試
mount -o username=student,password=123 //172.25.254.231/westos /mnt/
所有用戶均可寫
chmod o+w /共享目錄
setsebool -P samba_export_all_rw on ##讀寫共享
vim /etc/samba/smb.conf
writable = yes
設定指定用戶可寫
write list = student ##可寫用戶
write list = +student ##可寫用戶組
write list = @student
admin users = westos ##共享的超級用戶指定
7.smb多用戶掛載
在client上
vim /root/smbuser
username=student
password=123
chmod 600 /root/smbuser
yum install cifs-utils -y
mount -o credentials=/root/smbuser,multiuser,sec=ntlmssp //172.25.254.231/westos /mnt/
#credentials=/root/smbuser 指定掛載時所用到的用戶文件
#multiuser 支持多用戶認證
#sec=ntlmssp 認證方式爲標準smb認證方式
su - student
ls /mnt
ls: cannot access /mnt: Permission denied ##因爲沒有做smb的認證所以無法訪問smb共享
cifscreds add -u student 172.25.254.231
Password: ##smb用戶student的密碼
ls /mnt