SAMBA服務
SMB是由IBM服務公司發佈的,後來微軟將smb進行了增強改名爲cifs實現windows上的資源共享,1991年andrew Trigell將cifs協議進行了破解,模擬cifs協議,研發了SAMBA服務讓Linux和Windows之間可以實現資源共享。
SAMBA的功能
- 共享文件和打印
- 實現登錄SAMBA用戶的身份認證
- 可以進行NetBIOS名稱解析
- 外圍設備共享
計算機網路的管理模式
- 工作組(WORKGROUP)
計算機對等關係,帳號信息各自管理
計算機的工作組名可以隨意更改,加入工作組後才能看到samba - 域(DOMAIN)
域DOMAIN:C/S結構,帳號信息集中管理,DC,AD
域比工作組嚴謹,需要驗證後才能加入,加入後受到域的管理SAMBA安裝
SAMBA服務器端
服務器端需要安裝samba包
[root@samba ~]# yum install samba -y
SAMBA客戶端
samba客戶端需要安裝samba-client、samba-common、cifs-utils、samba-winbind包
[root@client ~]# yum install samba-client -y [root@client ~]# yum install cifs-utils -y [root@client ~]# yum install samba-winbind -y
samba-clinet:samba客戶端軟件
samba-common:通用軟件
cifs-utils:smb客戶端工具
samba-winbind:和AD相關SAMBA服務進程
samba服務啓動時會啓動兩個進程smbd和nmbd
smbd提供smb服務,使用的端口號爲TCP:139,445
nmbd提供NetBIOS名稱解析,使用端口號爲UDP:137,138SAMBA配置文件
CentOS7上samba配置有樣板文件可以參考修改
[root@samba ~]# ll /etc/samba/ -rw-r--r-- 1 root root 20 Oct 31 2018 lmhosts -rw-r--r-- 1 root root 706 Oct 31 2018 smb.conf #配置文件 -rw-r--r-- 1 root root 11327 Oct 31 2018 smb.conf.example #配置文件樣板可以用來參考
[root@samba ~]# vim /etc/samba/smb.conf
[global] #此爲全局配置
workgroup = SAMBA #工作組名
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
## SAMBA的使用
### samba用戶的創建
samba用戶的創建需要Linux上存在相應的用戶
1.創建linux用戶
```bash
[root@samba ~]# useradd -s /sbin/nologin smb1
[root@samba ~]# useradd -s /sbin/nologin smb2
[root@samba ~]# useradd -s /sbin/nologin smb3
2.把linux賬號加入samba賬號,輸入密碼
[root@samba ~]# smbpasswd -a smb1
New SMB password:
Retype new SMB password:
Added user smb1.
[root@samba ~]# smbpasswd -a smb2
New SMB password:
Retype new SMB password:
Added user smb2.
[root@samba ~]# smbpasswd -a smb3
New SMB password:
Retype new SMB password:
Added user smb3.
3.查看samba賬號
[root@samba ~]# pdbedit -L
smb1:1002:
smb3:1004:
smb2:1003:
samba賬號的存放位置爲/var/lib/samba/private
[root@samba ~]# ll /var/lib/samba/private/
total 832
drwx------ 2 root root 54 May 19 19:16 msg.sock
-rw------- 1 root root 421888 May 19 19:16 passdb.tdb #存放賬號的數據庫文件,此庫被刪除後再次創建用戶時會自動創建
-rw------- 1 root root 430080 May 19 19:15 secrets.tdb
4.samba賬號的刪除
先刪除smb賬號,再將Linux用戶刪除
[root@samba ~]# pdbedit -x smb3
[root@samba ~]# pdbedit -L
smb1:1002:
smb2:1003:
[root@samba ~]# userdel smb3
啓用samba
在samba服務端啓動服務
[root@samba ~]# systemctl start smb
[root@samba ~]# systemctl start nmb
在客戶端查看工作組
1.匿名登錄查看
[root@client ~]# smbclient -L 192.168.73.110
Enter SAMBA\root's password:
Anonymous login successful
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.8.3)
Reconnecting with SMB1 for workgroup listing.
Anonymous login successful
Server Comment
--------- -------
SAMBA Samba 4.8.3
Workgroup Master
--------- -------
SAMBA SAMBA
2.使用smb1登錄查看
[root@client ~]# smbclient -L 192.168.73.110 -Usmb1%111111
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.8.3)
smb1 Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
SAMBA Samba 4.8.3
Workgroup Master
--------- -------
SAMBA SAMBA
samba日誌
1.修改配置文件,重啓服務
[root@samba ~]# vim /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
security = user
netbios name=smbserver #服務器名,可以解析爲ip,需要啓用nmb服務
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
log file = /var/log/samba/log%I #添加日誌選項,%I以客戶端ip區分
log level = 2 #記錄日誌的級別
[root@samba ~]# systemctl restart smb
2.客戶端登錄
[root@client ~]# smbclient -L 192.168.73.110 -Usmb1%111111
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.8.3)
smb1 Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
SAMBA SAMBA
WORKGROUP OOOO
3.服務器端查看日誌
[root@samba ~]# ll /var/log/samba/
total 12
drwx------ 4 root root 30 May 19 19:27 cores
-rw-r--r-- 1 root root 902 May 19 21:06 log0.0.0.0
-rw-r--r-- 1 root root 0 May 19 20:03 log192.168.73.111 #帶ip的日誌
-rw-r--r-- 1 root root 710 May 19 21:06 log.nmbd
-rw-r--r-- 1 root root 168 May 19 19:27 log.smbd
drwx------ 2 root root 6 Oct 31 2018 old
samba訪問控制
可以使用hosts allow或者hosts deny實現
hosts allow = 127. 192.168.12. 192.168.13.
可以拒絕整個網段訪問,也可以拒絕單個地址的訪問
設置某個目錄的共享
1.創建需要共享的文件夾
[root@samba ~]# mkdir /data/smbshare1
[root@samba ~]# mkdir /data/smbshare2
[root@samba ~]# touch /data/smbshare1/smb1
[root@samba ~]# touch /data/smbshare2/smb2
2.修改配置文件,重啓服務
[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
[root@samba ~]# systemctl restart smb
客戶端查詢
[root@client ~]# smbclient -L 192.168.73.110
Enter SAMBA\root's password:
Anonymous login successful
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
smbshare1 Disk #已經能查看到共享的目錄
IPC$ IPC IPC Service (Samba 4.8.3)
Reconnecting with SMB1 for workgroup listing.
Anonymous login successful
Server Comment
--------- -------
Workgroup Master
--------- -------
WORKGROUP OOOO
共享目錄的讀寫權限
1.修改服務端配置文件
[smbshare1]
path = /data/smbshare1
readonly = no
2.給予目錄讀寫執行權限
[root@samba ~]# chmod 777 /data/smbshare1
[root@samba ~]# ll /data
drwxrwxrwx 2 root root 18 May 19 21:22 smbshare1
drwxr-xr-x 2 root root 18 May 19 21:22 smbshare2
客戶端掛載,寫入文件
[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111
[root@client ~]# touch /data/aa
允許特定的人訪問
1.修改配置文件
[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
readonly = no
valid users = smb1 #只允許smb1訪問
[root@samba ~]# systemctl restart smb
此時只有smb1用戶可以登錄,其餘用戶都不能訪問
目錄只讀但某些用戶可寫
需要將目錄設置爲只讀,添加可寫列表寫入可寫的用戶名單
修改配置
[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
readonly = yes #將目錄設置爲制度
write list = smb1 #添加可寫的用戶
#write list = @g1 +g2 #也可以添加可寫的組
匿名用戶無需驗證訪問
需要使用到public,將其設置爲yes
[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
public = yes #添加此項設置爲匿名用戶也能訪問
#readonly = no #如果需要給匿名用戶寫權限還需要開啓此項
匿名用戶所創建的文件,屬主屬組爲nobody用戶,所以匿名用戶在Linux上被映射成nobody用戶
隱藏共享目錄
blowsable選項,把共享目錄隱藏,但如果知道共享的名字依舊能使用
[root@samba ~]# vim /etc/samba/smb.conf
[smbshare1]
path = /data/smbshare1
public = yes
read only = no
browsable = no #yes爲不隱藏 no爲隱藏
讓不同的人看到不同的資源
需要在配置文件的全局中添加config file指定每個用戶的配置文件
修改配置文件
[root@samba ~]# vim /etc/samba/smb.conf
[global]
config file = /etc/samba/conf.d/%U #創建每個用戶獨立的配置文件
...省略...
[smbshare1]
comment = text
path = /data/smbshare2
read only = no
創建用戶個人的配置文件
[root@samba ~]# vim /etc/samba/conf.d/smb1
[smbshare1]
comment = text
path = /data/smbshare2
read only = no
#browsable = no #也可以設置對單個用戶隱藏目錄
此時smb1訪問smbshare1是訪問的是/data/smbshare2,而未指定的用戶訪問時依舊是主配置文件中的/data/smbshare1
查看samba服務器狀態
smbstatus命令可以查看samba服務器狀態
[root@samba ~]# smbstatus
Samba version 4.8.3
PID Username Group Machine Protocol Version Encryption Signing
----------------------------------------------------------------------------------------------------------------------------------------
7826 smb1 smb1 192.168.73.111 (ipv4:192.168.73.111:39708) SMB3_02 - partial(AES-128-CMAC)
Service pid Machine Connected at Encryption Signing
---------------------------------------------------------------------------------------------
IPC$ 7826 192.168.73.111 Sun May 19 09:42:18 PM 2019 CST - AES-128-CMAC
smbshare1 7826 192.168.73.111 Sun May 19 09:42:18 PM 2019 CST - -
No locked files
直接登錄samba目錄
可以使用smbclient直接登錄samba來使用
[root@client ~]# smbclient //192.168.73.110/smbshare1 -Usmb1%111111
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sun May 19 21:38:39 2019
.. D 0 Sun May 19 21:21:42 2019
smb1 N 0 Sun May 19 21:22:24 2019
aa A 0 Sun May 19 21:38:39 2019
52403200 blocks of size 1024. 52370124 blocks available
smb: \>
將samba文件夾掛載
掛載samba文件,掛載時需要制定用戶名密碼
[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111
在CentOS6上用戶的加密協議不同所以掛載時需要指定加密協議
[root@client ~]# mount //192.168.73.110/smbshare1 /data -o username=smb1,password=111111,sec=ntlmssp
設置開機自動掛載samba
開機自動掛載需要將配置寫入/etc/fstab中
[root@client ~]# vim /etc/fstab
//192.168.73.110/smbshare1 /data cifs credentials=/etc/smb.txt 0 0
定義掛載設備 掛載點 文件系統 掛載時的賬戶和密碼文件
創建存放賬戶和密碼的文件,並設置安全權限
[root@client ~]# vim /etc/smb.txt
username=smb1
password=111111
[root@client ~]# chmod 600 /etc/smb.txt
[root@client ~]# mount -a
[root@client data]# mount | grep 192.168.73
//192.168.73.110/smbshare1 on /data type cifs (rw,relatime,vers=default,cache=strict,username=smb1,domain=,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.73.110,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1)