實驗環境:一台samba伺服器,一台用戶端(均為centos7作業系統)
一.samba伺服器設定匿名使用者,使用用戶端進行通路
首先要讓兩台虛拟機網絡ping通,這裡采用自動擷取的方式
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 編輯samba伺服器的網卡

[[email protected] ~]# systemctl restart network 重新開機網絡服務
[[email protected] ~]# ifconfig ens33 檢視ens33網卡IP
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.22.132 netmask 255.255.255.0 broadcast 192.168.22.255
inet6 fe80::d80:6234:3fc8:976c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a3:8b:28 txqueuelen 1000 (Ethernet)
RX packets 32 bytes 3888 (3.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43 bytes 4363 (4.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
切換到用戶端同上開啟網絡服務,是同一網段
網絡配置好之後,來安裝samba服務,用yum來安裝,可忽略依賴關系(這邊我們提前搭建好的yum)
[[email protected] ~]# yum -y install samba 用yum安裝samba服務
安裝好samba服務之後,先建立一個目錄/share
[[email protected] ~]# mkdir /share
之後先賦予/share目錄最高權限777
[[email protected] /]# chmod 777 share
[[email protected] /]# ll -d share
drwxrwxrwx. 2 root root 6 Jun 18 03:24 share
現在進行編輯samba主配置檔案/etc/samba/smb.conf
[[email protected] /]# vim /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = SAMBA
security = user
map to guest = Bad 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 = root
create mask = 0664
directory mask = 0775
[share1] 自定義名稱
comment = share txt 對共享目錄的注釋、說明資訊
path = /share 共享目錄在伺服器中對應的實際路徑
public = yes 允許匿名通路
read only = no 拒絕讀的權限
writable = yes 允許寫入
啟動smb服務
[[email protected] /]# systemctl start smb
[[email protected] /]# systemctl start nmb
[[email protected] /]# systemctl stop firewalld 關閉防火牆
[[email protected] /]# setenforce 0 降低沙盒級别
[[email protected] /]# cd share 切換到/share路徑
[[email protected] share]# touch 1.txt 建立檔案1.txt,以驗證用戶端匿名通路可下載下傳檔案
[[email protected] share]# ls 檢視
1.txt
現在用用戶端進行匿名通路
在目前路徑下建立2.txt,以便上傳
[[email protected] ~]# smbclient //192.168.22.132/share1 匿名通路samba方式
Enter root's password: 無密碼
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls 檢視内容
. D 0 Fri Jun 18 03:37:45 2021
.. DR 0 Fri Jun 18 03:24:52 2021
1.txt N 0 Fri Jun 18 03:37:45 2021
40137576 blocks of size 1024. 35518264 blocks available
smb: \> put 2.txt 上傳目前用戶端路徑下建立的檔案2.txt成功
putting file 2.txt as \2.txt (0.0 kb/s) (average 0.0 kb/s)
smb: \> ls
. D 0 Fri Jun 18 03:43:52 2021
.. DR 0 Fri Jun 18 03:24:52 2021
1.txt N 0 Fri Jun 18 03:37:45 2021
2.txt A 0 Fri Jun 18 03:43:52 2021
40137576 blocks of size 1024. 35518208 blocks available
smb: \> get 1.txt 下載下傳samba伺服器的檔案1.txt成功
getting file \1.txt of size 0 as 1.txt (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec
smb: \> q 退出
samba服務匿名使用者通路成功
二、samba服務指定使用者通路
同上環境一樣
[[email protected] ~]# mkdir /xk 建立目錄
[[email protected] ~]# chmod 777 /xk 給該目錄的權限
[[email protected] ~]# vim /etc/samba/smb.conf 編輯smb主配置檔案
添加以下内容
[samba]
comment = samba.txt
path = /xk
public = no
read only = yes
valid users = xkxk,jljl 本地使用者的名稱
write list = xkxk 擁有讀寫權限的本地使用者
[[email protected] ~]# useradd xkxk 添加xkxk使用者
[[email protected] ~]# useradd jljl 添加jljl使用者
[[email protected] ~]# passwd xkxk 激活使用者
Changing password for user xkxk.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[[email protected] ~]# passwd jljl 激活使用者
Changing password for user jljl.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
将兩個本地使用者添加為共享使用者
[[email protected] ~]# pdbedit -a -u xkxk
[[email protected] ~]# pdbedit -a -u jljl
然後開啟smb服務
[[email protected] ~]# systemctl start smb
[[email protected] ~]# systemctl start nmb
切換到用戶端驗證
[[email protected] ~]# smbclient --user=xkxk //192.168.3.129/samba
現在用jljl使用者來驗證,隻能下載下傳不能上傳
[[email protected] ~]# touch 3.txt 在目前路徑建立3.txt
[[email protected] ~]# smbclient --user=jljl //192.168.3.129/samba
ok 實驗完成。