文章目錄
- 1. samba簡介
-
- 1.1 Samba監聽端口有
- 1.2 samba程序
- 1.3 samba使用者
- 1.4 samba配置檔案
- 1.5 Samba安全級别
- 1.6 常用配置檔案參數
- 2. samba通路
- 3. 配置使用者認證共享
- 4. 配置匿名共享
1. samba簡介
1.1 Samba監聽端口有
TCP | UDP |
---|---|
139 445 | 137 138 |
安裝samba服務
[[email protected] ~]# yum -y install samba* 服務端安裝所有的
[[email protected] ~]# yum -y install samba-client 用戶端隻需安裝一個
[[email protected] ~]# systemctl enable smb nmb
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nmb.service → /usr/lib/systemd/system/nmb.service.
[[email protected] ~]# systemctl start smb nmb
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 0.0.0.0:139 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 50 0.0.0.0:445 0.0.0.0:*
LISTEN 0 50 [::]:139 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 50 [::]:445 [::]:*
[[email protected] ~]# ss -anul|grep -E '137|138'
UNCONN 0 0 192.168.50.255:137 0.0.0.0:*
UNCONN 0 0 192.168.50.131:137 0.0.0.0:*
UNCONN 0 0 0.0.0.0:137 0.0.0.0:*
UNCONN 0 0 192.168.50.255:138 0.0.0.0:*
UNCONN 0 0 192.168.50.131:138 0.0.0.0:*
UNCONN 0 0 0.0.0.0:138 0.0.0.0:*
tcp端口相對應的服務是smbd服務,其作用是提供對伺服器中檔案、列印資源的共享通路。
udp端口相對應的服務是nmbd服務,其作用是提供基于NetBIOS主機名稱的解析。
1.2 samba程序
程序 | 對應 |
---|---|
nmbd | 對應netbios |
smbd | 對應cifs協定 |
winbindd + ldap | 對應Windows AD活動目錄 |
1.3 samba使用者
帳号 | 密碼 |
---|---|
都是系統使用者 /etc/passwd | Samba服務自有密碼檔案 通過smbpasswd -a USERNAME指令設定 |
smbpasswd指令:
- -a Sys_User : 添加系統使用者為samba使用者并為其設定密碼
- -d : 禁用使用者帳号
- -e : 啟用使用者帳号
- -x : 删除使用者帳号
[[email protected] ~]# useradd -r -M -s /sbin/nologin tom 建立一個系統不能登陸的使用者tom
[[email protected] ~]# su - tom 切換不了使用者說明登陸不了
su: warning: cannot change directory to /home/tom: No such file or directory
This account is currently not available.
[[email protected] ~]# smbpasswd -a tom 添加系統使用者為samba使用者并為其設定密碼
New SMB password:
Retype new SMB password:
Added user tom.
tom登陸,密碼123456
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN2XjlGcjAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL9QzVkFHZtJGcGNDWwx2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL3YDNzAjNwcTMzEDMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
通路不了是因為在建立tom使用者時沒有家目錄
1.4 samba配置檔案
- /etc/samba/smb.conf(主配置檔案)
samba三大組成 | 作用 |
---|---|
[global] | 全局配置,此處的設定項對整個samba伺服器都有效 |
[homes] | 宿主目錄共享設定,此處用來設定Linux使用者的預設共享,對應使用者的宿主目錄。當使用者通路伺服器中與自己使用者名同名的共享目錄時,通過驗證後将會自動映射到該使用者的宿主目錄中 |
[printers] | 列印機共享設定 |
//測試配置檔案是否有文法錯誤,以及顯示最終生效的配置:使用testparm指令
[[email protected] samba]# testparm
Load smb config files from /etc/samba/smb.conf
Loaded services file OK. 有ok就說明沒有文法錯誤
Weak crypto is allowed
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global] 表示全局,寫在其下面表示對全伺服器生效
printcap name = cups
security = USER
workgroup = SAMBA
idmap config * : backend = tdb
cups options = raw
[homes] 寫在其下面隻對使用者的家目錄生效
browseable = No
comment = Home Directories
inherit acls = Yes
read only = No
valid users = %S %D%w%S
[share] 隻對該目錄生效或隻對該共享生效
path = /opt/smb
1.5 Samba安全級别
- Samba伺服器的安全級别有三個,分别是user,server,domain
安全級别 | 作用 |
---|---|
user | 基于本地的驗證 |
server | 由另一台指定的伺服器對使用者身份進行認證 |
domain | 由域控進行身份驗證 |
以前的samba版本支援的安全級别有四個,分别是share,user,server,domain
share是用來設定匿名通路的,但現在的版本已經不支援share了,但是還是可以實作匿名通路的
隻是配置方式變了
1.6 常用配置檔案參數
參數 | 作用 |
---|---|
workgroup | 表示設定工作組名稱 |
server string | 表示描述samba伺服器 |
security | 表示設定安全級别,其值可為share、user、server、domain |
passdb backend | 表示設定共享帳戶檔案的類型,其值可為tdbsam(tdb資料庫檔案)、ldapsam(LDAP目錄認證)、smbpasswd(相容舊版本samba密碼檔案) |
comment | 表示設定對應共享目錄的注釋,說明資訊,即檔案共享名 |
browseable | 表示設定共享是否可見 |
writable | 表示設定目錄是否可寫 |
path | 表示共享目錄的路徑 |
guest ok | 表示設定是否所有人均可通路共享目錄 |
public | 表示設定是否允許匿名使用者通路 |
write list | 表示設定允許寫的使用者群組,組要用@表示,例如 write list = root,@root |
valid users | 設定可以通路的使用者群組,例如 valid users = root,@root |
hosts deny | 設定拒絕哪台主機通路,例如 hosts deny = 192.168.72.1 |
hosts allow | 設定允許哪台主機通路,例如 hosts allow = 192.168.72.2 |
printable | 表示設定是否為列印機 |
- server string
[[email protected] samba]# vim smb.conf.example
server string = Samba Server Version %v 描述伺服器版本号
- passdb backend
[[email protected] samba]# vim smb.conf
passdb backend = tdbsam
[[email protected] samba]# find / -name tdbsam*
/usr/lib64/samba/pdb/tdbsam.so
- comment
服務端的smb.conf檔案裡添加comment,表示說明
[[email protected] samba]# vim smb.conf
[share]
path = /opt/anon
comment = jjyy 添加此内容
browseable = yes
guest ok = yes
writable = yes
public = yes
[[email protected] samba]# systemctl restart smb nmb
用戶端檢視
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
mq Disk
share Disk jjyy 此時就能看到jjyy的注釋
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
- valid users;write list
服務機添加valid users和write list
[[email protected] samba]# vim smb.conf
[share]
path = /opt/anon 可共享目錄
browseable = yes
writable = no 都不能寫;此步驟需要寫到write list前面,不然效果出不來
valid users = tom,jerry 可以登陸的賬戶
write list = jerry 可以寫的賬戶
客戶機檢視效果
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry 可檢視jerry賬戶
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
mq Disk
share Disk jjyy
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
[[email protected] ~]# smbclient -L 192.168.50.131 -U tom 可檢視tom賬戶
Enter SAMBA\tom's password:
Sharename Type Comment
--------- ---- -------
share Disk
IPC$ IPC IPC Service (Samba 4.11.2)
SMB1 disabled -- no workgroup available
[[email protected] ~]# smbclient -L 192.168.50.131 -U mq 可以檢視mq使用者
Enter SAMBA\mq's password:
Sharename Type Comment
--------- ---- -------
share Disk
IPC$ IPC IPC Service (Samba 4.11.2)
SMB1 disabled -- no workgroup available
[[email protected] ~]# smbclient //192.168.50.131/share -U tom 可登陸tom賬戶
Enter SAMBA\tom's password:
Try "help" to get a list of possible commands.
smb: \> ls 可以檢視
. D 0 Tue Oct 13 20:58:35 2020
.. D 0 Tue Oct 13 20:36:34 2020
abc A 0 Tue Oct 13 20:58:35 2020
17811456 blocks of size 1024. 16172024 blocks available
[[email protected] ~]# smbclient //192.168.50.131/share -U mq mq使用者不能登陸
Enter SAMBA\mq's password:
tree connect failed: NT_STATUS_ACCESS_DENIED
[[email protected] ~]# smbclient //192.168.50.131/share -U jerry jerry使用者可以登陸
Enter SAMBA\jerry's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Tue Oct 13 20:58:35 2020
.. D 0 Tue Oct 13 20:36:34 2020
abc A 0 Tue Oct 13 20:58:35 2020
17811456 blocks of size 1024. 16171400 blocks available
smb: \> mkdir 456 可以建立說明可寫
smb: \> ls
. D 0 Wed Oct 14 15:04:25 2020
.. D 0 Tue Oct 13 20:36:34 2020
abc A 0 Tue Oct 13 20:58:35 2020
456 D 0 Wed Oct 14 15:04:25 2020
17811456 blocks of size 1024. 16171400 blocks available
服務端檢視jerry建立的456效果
[[email protected] samba]# ll /opt/anon/
total 0
drwxr-xr-x 2 jerry jerry 6 Oct 14 15:04 456
-rwxr--r--. 1 nobody nobody 0 Oct 13 20:58 abc
- hosts deny(hosts allow與之相反)
服務機配置檔案裡添加不讓通路的主機ip
[[email protected] samba]# vim smb.conf
[share]
path = /opt/anon
browseable = yes
writable = no
valid users = tom,jerry
write list = jerry
hosts deny = 192.168.50.135 添加此行内容說明該台主機不能通路,其他主機可以通路
[[email protected] samba]# systemctl restart smb nmb
用戶端檢視
[[email protected] ~]# smbclient //192.168.50.131/share -U jerry
Enter SAMBA\jerry's password:
tree connect failed: NT_STATUS_ACCESS_DENIED 權限被拒絕
此時用真機可以通路,登陸。
2. samba通路
環境說明:
伺服器IP | 客戶機IP |
---|---|
192.168.50.131 | 192.168.50.135 |
- 互動式資料通路
服務機建立一個使用者
[[email protected] samba]# smbpasswd -a jerry
New SMB password:
Retype new SMB password:
Added user jerry.
客戶機上檢視
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
在服務機的smb.conf檔案裡添加一個共享
[[email protected] samba]# vim smb.conf
......
[share]
path = /opt
[[email protected] samba]# systemctl restart smb nmb 重新開機服務
檢視客戶機
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
share Disk 此時可以檢視到共享
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
在上一步的基礎上添加以下内容
[[email protected] samba]# vim smb.conf
[share]
path = /opt
browseable = No 為No時客戶機上檢視不到共享内容,但是能用,相當于一個隐藏共享
[[email protected] samba]# systemctl restart smb nmb
檢視客戶機
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
客戶機
[[email protected] ~]# smbclient -L 192.168.50.131 -U jerry 跟服務機ip
Enter SAMBA\jerry's password:
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba 4.11.2)
jerry Disk Home Directories
SMB1 disabled -- no workgroup available
[[email protected] ~]#
[[email protected] ~]# smbclient //192.168.50.131/jerry -U jerry
Enter SAMBA\jerry's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Tue Oct 13 17:51:28 2020
.. D 0 Tue Oct 13 17:51:28 2020
.bash_logout H 18 Fri Aug 30 13:30:21 2019
.bash_profile H 141 Fri Aug 30 13:30:21 2019
.bashrc H 312 Fri Aug 30 13:30:21 2019
17811456 blocks of size 1024. 16140572 blocks available
smb: \> mkdir abc 建立一個abc
smb: \> ls
. D 0 Tue Oct 13 19:09:22 2020
.. D 0 Tue Oct 13 17:51:28 2020
.bash_logout H 18 Fri Aug 30 13:30:21 2019
.bash_profile H 141 Fri Aug 30 13:30:21 2019
.bashrc H 312 Fri Aug 30 13:30:21 2019
abc D 0 Tue Oct 13 19:09:22 2020
17811456 blocks of size 1024. 16140572 blocks available
smb: \>
服務機上檢視是否有abc
[email protected] samba]# ls /home/jerry/
abc
[[email protected] samba]# ll /home/jerry/
total 0
drwxr-xr-x. 2 jerry jerry 6 Oct 13 19:09 abc
退出
smb: \> exit
[[email protected] ~]#
- 基于挂載的方式通路(推薦使用)
1. 臨時挂載
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 980M 0 980M 0% /dev/shm
tmpfs 980M 8.7M 972M 1% /run
tmpfs 980M 0 980M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 160M 855M 16% /boot
tmpfs 196M 0 196M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /mnt
[[email protected] ~]# mount -t cifs //192.168.50.131/jerry /media/ -o username=jerry,password=123456 将服務機上的jerry挂載到media上去
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 980M 0 980M 0% /dev/shm
tmpfs 980M 8.7M 972M 1% /run
tmpfs 980M 0 980M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 160M 855M 16% /boot
tmpfs 196M 0 196M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /mnt
//192.168.50.131/jerry 17G 1.6G 16G 10% /media
[[email protected] ~]# cd /media/
[[email protected] media]# ls
[[email protected] media]# touch abc 直接建立檔案
[[email protected] media]# ll
total 0
-rwxr-xr-x. 1 root root 0 Oct 13 19:21 abc
服務機上檢視
[[email protected] samba]# ll /home/jerry
total 0
-rwxr--r--. 1 jerry jerry 0 Oct 13 19:21 abc
2.永久挂載
客戶機上添加以下内容
[[email protected] ~]# vim /etc/fstab
......
//192.168.50.131/jerry /media cifs defaults,username=jerry,password=123456 0 0
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 980M 0 980M 0% /dev/shm
tmpfs 980M 8.7M 972M 1% /run
tmpfs 980M 0 980M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 160M 855M 16% /boot
tmpfs 196M 0 196M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /mnt
//192.168.50.131/jerry 17G 1.6G 16G 10% /media
3. 配置使用者認證共享
安裝samba伺服器:
[[email protected] ~]# yum -y install samba-*
建立一個系統使用者(不允許登陸)
[[email protected] ~]# useradd -r -M -s /sbin/nologin mq
[[email protected] ~]# smbpasswd -a mq
New SMB password:
Retype new SMB password:
Added user mq.
在/etc/samba/smbusers檔案中添加如下内容
[[email protected] ~]# cd /etc/samba/
[[email protected] samba]# ls
lmhosts smb.conf smb.conf.example
[[email protected] samba]# vim smbusers
mq = share share使用者是個假賬戶,檢視不到
[[email protected] samba]# id share
id: ‘share’: no such user
[[email protected] samba]# id mq
uid=993(mq) gid=989(mq) groups=989(mq)
在配置檔案裡添加映射
[[email protected] samba]# vim smb.conf
[global]
workgroup = SAMBA
security = user
username map = /etc/samba/smbusers 添加此行内容
建立目錄,修改所有者
[[email protected] samba]# mkdir /opt/smb
[[email protected] samba]# chown -R mq.mq /opt/smb
配置共享
[[email protected] samba]# vim smb.conf
[mq]
path = /opt/smb 路徑,表示共享哪個目錄
browseable = yes 匿名通路
guest ok = yes
writable = yes 可讀可寫
write list = share
public = yes 公開的,所有人都可以看到
重新開機服務
[[email protected] samba]# systemctl restart smb nmb
在客戶機檢視samba伺服器有哪些共享資源
[[email protected] ~]# smbclient -L 192.168.50.131 -U share
Enter SAMBA\share's password:
Sharename Type Comment
--------- ---- -------
mq Disk
IPC$ IPC IPC Service (Samba 4.11.2)
SMB1 disabled -- no workgroup available
将samba伺服器的共享資源mq挂載到客戶機本地
[[email protected] ~]# vim /etc/fstab
//192.168.50.131/mq /media cifs defaults,username=share,password=123456 0 0 添加此内容
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 980M 0 980M 0% /dev/shm
tmpfs 980M 8.7M 972M 1% /run
tmpfs 980M 0 980M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 160M 855M 16% /boot
tmpfs 196M 0 196M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /mnt
//192.168.50.131/mq 17G 1.7G 16G 10% /media
在客戶機上進入共享目錄建立新檔案
[[email protected] ~]# cd /media/
[[email protected] media]# ls
[[email protected] media]# touch abc
[[email protected] media]# ll
total 0
-rwxr-xr-x. 1 root root 0 Oct 13 20:30 abc
到服務機上驗證
[[email protected] samba]# ll /opt/smb/
total 0
-rwxr--r--. 1 mq mq 0 Oct 13 20:30 abc
4. 配置匿名共享
安裝samba伺服器:
[[email protected] ~]# yum -y install samba-*
建立一個共享目錄anon
[[email protected] samba]# mkdir /opt/anon
[[email protected] samba]# chmod 777 /opt/anon/
[[email protected] samba]# ll /opt/
total 0
drwxrwxrwx. 2 root root 6 Oct 13 20:35 anon
drwxr-xr-x. 2 mq mq 17 Oct 13 20:30 smb
在配置檔案中添加映射
[[email protected] samba]# vim smb.conf
[global]
workgroup = SAMBA
security = user
map to guest = Bad User 添加此行内容
配置共享
[[email protected] samba]# vim smb.conf
[share]
path = /opt/anon
browseable = yes
guest ok = yes
writable = yes
public = yes
重新開機服務
[[email protected] samba]# systemctl restart smb nmb
在客戶機檢視samba伺服器有哪些共享資源
[[email protected] ~]# smbclient -L 192.168.50.131 -U 'Bad User'
Enter SAMBA\Bad User's password: 此處不需要輸密碼
Sharename Type Comment
--------- ---- -------
mq Disk
share Disk
IPC$ IPC IPC Service (Samba 4.11.2)
SMB1 disabled -- no workgroup available
将samba伺服器的共享資源mq挂載到客戶機本地
[[email protected] ~]# vim /etc/fstab
//192.168.50.131/share /media cifs defaults,username=BadUser 0 0 添加此行,不需要輸密碼
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 963M 0 963M 0% /dev
tmpfs 980M 0 980M 0% /dev/shm
tmpfs 980M 8.7M 972M 1% /run
tmpfs 980M 0 980M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 1.8G 16G 11% /
/dev/nvme0n1p1 1014M 160M 855M 16% /boot
tmpfs 196M 0 196M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /mnt
//192.168.50.131/share 17G 1.7G 16G 10% /media
在客戶機上進入共享目錄建立新檔案
[[email protected] media]# touch abc
[[email protected] media]# ll
total 0
-rwxr-xr-x. 1 root root 0 Oct 13 20:58 abc
到服務機上驗證
[[email protected] samba]# ll /opt/anon/
total 0
-rwxr--r--. 1 nobody nobody 0 Oct 13 20:58 abc 匿名使用者通路,是以是nobody