samba簡介
Samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,由服務器及客戶端程序構成。SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通信協議,它爲局域網內的不同計算機之間提供文件及打印機等資源的共享服務。SMB協議是客戶機/服務器型協議,客戶機通過該協議可以訪問服務器上的共享文件系統、打印機及其他資源。通過設置“NetBIOS over TCP/IP”使得Samba不但能與局域網絡主機分享資源。
samba監聽端口:
TCP | UDP |
---|---|
139 | 137 |
445 | 138 |
tcp端口對應的服務是smbd服務,其作用是提供對服務器中文件,打印資源的共享訪問。
udp端口對應的服務是nmbd服務,其作用是提供基於NetBOIS主機名稱的解析。
進程 | 對應 |
---|---|
nmbd | 對應netbois |
smbd | 對應cifs協議 |
Samba 服務配置
服務端ip | 客戶端ip |
---|---|
192.168.47.11 | 192.168.47.12 |
關閉防火牆
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# setenforce 0安裝Samba服務程序
[root@yanyinglai ~]# yum install samba*
已加載插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
啓動Samba服務程序
[root@yanyinglai ~]# systemctl start smb
設置Samba服務隨系統啓動而啓動
[root@yanyinglai ~]# systemctl enable smb
創建映射共享目錄
創建用戶dd
[root@yanyinglai ~]# useradd -M dd
爲dd用戶創建smb共享密碼
[root@yanyinglai ~]# smbpasswd -a dd
New SMB password:
Retype new SMB password:
Added user dd.
假設這裏映射dd用戶爲share用戶,那麼就要在/etc/samba/smbusers文件中添加內容
[root@yanyinglai ~]# echo 'dd = share' > /etc/samba/smbusers
[root@yanyinglai ~]# 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
username map = /etc/samba/smbusers // 添加此行內容
passdb backend = tdbsam
創建一個共享目錄yan
[root@yanyinglai ~]# mkdir /opt/yan
[root@yanyinglai ~]# chown -R dd.dd /opt/yan/
[root@yanyinglai ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 dd dd 6 8月 7 19:30 yan
drwxr-xr-x. 9 root root 254 7月 16 15:51 yanyinglai
配置共享
[root@yanyinglai ~]# cat >> /etc/samba/smb.conf <<EOF
[yan] 共享名
comment = this is 描述信息,任意字符串
path = /opt/yan/ 共享目錄路徑browseable = yes 指定該共享是否可以瀏覽
guest ok = yes 表示設置是否所有人均可訪問共享目錄
writable = yes 指定該共享路徑是否可寫
write list = share 表示設置允許寫的用戶和組public = yes 表示設置是否允許匿名用戶訪問
EOF
[root@yanyinglai ~]# tail -8 /etc/samba/smb.conf
[yan]
comment = this is
path = /opt/yan/
browseable = yes
guest ok = yes
writable = yes
write list = share
public = yes
testparm
測試配置文件是否有語法錯誤,以及顯示最終生效的配置
[root@yanyinglai ~]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[yan]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
重新啓動smb服務
重新啓動smb服務
[root@yanyinglai ~]# systemctl restart smb
重新加載smb服務
[root@yanyinglai ~]# systemctl reload smb
在客戶機查看samba服務器有哪些共享資源
yum查找smbclient軟件包的絕對路徑
[root@yanyinglai ~]# yum provides *bin/smbclient
[root@yanyinglai ~]# yum install /usr/bin/smbclient -y
在客戶端查看samba服務器有哪些共享資源
[root@yanyinglai3 ~]# smbclient -L 192.168.47.11 -U share
Enter SAMBA\share's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
yan Disk this is
IPC$ IPC IPC Service (Samba 4.6.2)
dd Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
將samba服務器的共享資源yan掛載到客戶機本地
[root@yanyinglai3 ~]# mkdir /opt/smb
[root@yanyinglai3 ~]# mount -t cifs //192.168.47.11/yan /opt/smb -o username=share,password=123
[root@yanyinglai3 ~]# df-h
-bash: df-h: 未找到命令
[root@yanyinglai3 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.6M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.47.11/yan 47G 5.0G 42G 11% /opt/smb
驗證
在客戶端上進入共享目錄創建新文件
[root@yanyinglai3 ~]# cd /opt/smb
[root@yanyinglai3 smb]# touch abc
[root@yanyinglai3 smb]# mkdir dfc
[root@yanyinglai3 smb]# ls
abc dfc
在服務端查看共享的目錄裏面是否存在客戶端創建的文件和目錄
[root@yanyinglai ~]# cd /opt/yan/
[root@yanyinglai yan]# ls
abc dfc
匿名共享
服務器ip | 客戶端ip |
---|---|
192.168.47.11 | 192.168.47.12 |
配置匿名共享時,還是需要關閉防火牆
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# setenforce 0
使用yum命令安裝samba服務器
[root@yanyinglai ~]# yum install samba*
[root@yanyinglai ~]# 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
創建一個共享目錄,創建目錄名爲yan
[root@yanyinglai ~]# mkdir /opt/yan
[root@yanyinglai ~]# chmod 777 /opt/yan/
[root@yanyinglai ~]# ll /opt/
總用量 0
drwxrwxrwx. 2 root root 6 8月 7 20:28 yan
drwxr-xr-x. 9 root root 254 7月 16 15:51 yanyinglai
配置共享
[root@yanyinglai ~]# cat >> /etc/samba/smb.conf <<EOF
[yan]
comment = yan
path = /opt/yan
browseable = yes
guest ok = yes
writable = yes
public = yes
EOF
啓動smb服務:
[root@yanyinglai ~]# systemctl start smb
[root@yanyinglai ~]# systemctl restart smb
在客戶端查看samba服務器有哪些共享資源
[root@yanyinglai3 ~]# smbclient -L192.168.47.11 -U 'Bad User'
Enter SAMBA\Bad User's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
yan Disk yan
IPC$ IPC IPC Service (Samba 4.6.2)
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
將samba服務器的共享資源yan掛載到客戶機本地
[root@yanyinglai3 ~]# mkdir /opt/smb
[root@yanyinglai3 ~]# mount -t cifs //192.168.47.11/yan /opt/smb -o username='Bad User'
[root@yanyinglai3 ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 1.4G 16G 8% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
//192.168.47.11/yan 47G 5.0G 42G 11% /opt/smb
在客戶端上進入共享目錄創建文件或目錄驗證一下,並在服務器上查看客戶端創建的文件
客戶端
[root@yanyinglai3 ~]# cd /opt/smb
[root@yanyinglai3 smb]# touch qwer
[root@yanyinglai3 smb]# mkdir qazs
服務器
[root@yanyinglai ~]# cd /opt/yan/
[root@yanyinglai yan]# ls
qazs qwer