Linux如何詳細搭建samba服務
阿新 • • 發佈:2018-08-07
信息服務 sys 防火墻 共享服務 window mkdir block 添加 smbd 1.samba簡介
samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,有服務器及客戶端程序構成
進過我們了解了NFS服務後,其實NFS和samba一樣的,就是在網絡中實現文件共享的一種實現,但二者有著略微不同,NFS服務其不支持windows平臺,而本章介紹的是samba服務是能夠在任何SMB協議的主機之間共享文件的一種實現,當然包括windows。
SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通信協議,它為局域網內的不同計算機之間提供文件及打印機等資源的共享服務。
SMB協議是客戶機/服務器型協議,客戶機通過該協議可以訪問服務器上的共享文件系統、打印機及其他資源。通過設置“NetBIOS over TCP/IP”使得Samba不但能與局域網絡主機分享資源,還能與全世界的電腦分享資源。
samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,有服務器及客戶端程序構成
進過我們了解了NFS服務後,其實NFS和samba一樣的,就是在網絡中實現文件共享的一種實現,但二者有著略微不同,NFS服務其不支持windows平臺,而本章介紹的是samba服務是能夠在任何SMB協議的主機之間共享文件的一種實現,當然包括windows。
SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通信協議,它為局域網內的不同計算機之間提供文件及打印機等資源的共享服務。
SMB協議是客戶機/服務器型協議,客戶機通過該協議可以訪問服務器上的共享文件系統、打印機及其他資源。通過設置“NetBIOS over TCP/IP”使得Samba不但能與局域網絡主機分享資源,還能與全世界的電腦分享資源。
2.samba監聽端口有:
TCP | UDP |
---|---|
139 | 137 |
445 | 138 |
TCP端口相對應的服務是samba服務,其作用是提供對服務器中文件、打印資源的共享訪問。
UDP端口相對應的服務是nmbad服務,其作用是提供基於NetBIOS主機名稱的解析
3.samba進程:
進程 | 對應的協議 |
---|---|
nmbd | 對應netbios協議 |
smbd | 對應cifs協議 |
winbindd + ldap | 對應Windows AD 活動目錄 |
4.samba用戶:
賬號 | 密碼 |
---|---|
都是系統用戶 | samba服務自有密碼文件 |
/etc/passwd | 通過smbpasswd -a USERNAME命令設置 |
5.samba安全級別
samba服務器的安全級別有三個,分別是user, server, domain
安全級別 | 作用 |
---|---|
user | 基於本地的驗證 |
server | 由另一臺指定的服務器對用戶身份進行認證 |
domain | 由域控進行身份驗證 |
以前的samba版本支持的安全級別有四個,分別是share,user,server,domain
share是用來設置匿名訪問的,但現在的版本已經不支持share了,但是還是可以實現匿名訪問的,只是配置方式變了。
6.samba 配置文件
/etc/samba/smb.conf (主配置文件)
samba三大組成 | 作用 |
---|---|
【global】 | 全局配置,此處的設置項對整個samba服務器都有效 |
【homes】 | 宿主目錄共享設置,此處用來設置Linux用戶的默認共享,對應用戶的宿主目錄。當用戶訪問服務器中與自己用戶名同名的共享目錄時,通過驗證後將會自動映射到該用戶的宿主目錄中 |
【printers】 | 打印機共享設置 |
常用配置文件參數:
參數 | 作用 |
---|---|
workgroup | 表示設置工作組名稱 |
server string | 表示描述samba服務器 |
security | 表示設置安全級別,其值可為share、user、server、domain |
passdbackend | 表示設置共享賬戶文件的類型,其值可為tdbsam (tbd數據庫文件)、ldapsam(LDAP目錄認證)、smbpasswd(兼容舊版本samba密碼文件) |
comment | 表示設置對應共享目錄的註釋,說明信息,即文件共享名 |
browseable | 表示設置共享是否可見 |
writable | 表示設置目錄是否可寫 |
path | 表示共享目錄的路徑 |
guest ok | 表示設置是否所有人均可訪問共享目錄 |
public | 表示設置是否允許匿名用戶訪問 |
write list | 表示設置允許寫的用戶和組,組要用@表示,例如 write = root,@root |
valid users | 設置可以訪問的用戶和組,例如 valid users = root,@root |
hosts deny | 設置拒絕哪臺主機訪問,例如 hosts deny = 192.168.209.12 |
hosts allow | 設置允許哪臺主機訪問,例如 hosts deny = 192.168.209.13 |
printable | 表示設置是否為打印機 |
//測試配置文件是否有語法錯誤,以及顯示最終生效的配置:使用testparm命令
[root@lanzhiyong ~]# 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 "[lan]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
7.samba訪問
環境說明:
服務器IP | 客戶機IP |
---|---|
192.168.209.12 | 192.168.209.13 |
//交互式數據訪問 (有兩種訪問模式),查看samba服務器有哪些共享資源
smbclient -L HOST -U USERNAME
例如:[root@lan ~]# smbclient -L 192.168.209.12 -U lan
Enter SAMBA\lan‘s password:
Domain=[LANZHIYONG] OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
lan Disk lan
IPC$ IPC IPC Service (Samba 4.6.2)
Domain=[LANZHIYONG] OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
smbclient //SERVER/share_name -U USARENAME
例如:[root@lan ~]# smbclient //192.168.209.12/lan -U lan
Enter SAMBA\lan‘s password:
Domain=[LANZHIYONG] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> ls
. D 0 Mon Aug 6 16:10:26 2018
.. D 0 Mon Aug 6 15:53:39 2018
9999 D 0 Mon Aug 6 16:10:22 2018
8888 D 0 Mon Aug 6 16:10:26 2018
17811456 blocks of size 1024. 8041628 blocks available
smb: \> quit //quit退出
-----
8.實例 (配置用戶認證共享,就是指定用戶登入密碼才能訪問共享資源)
前提是在服務器端和客戶端在關閉防火墻的情況下才能實現共享目錄。服務器端IP:192.168.209.12 客戶端IP:192.168.209.13
在服務器端做以下操作:
//使用yum命令安裝samba-*
[root@lanzhiyong ~]# yum install -y samba-*
//映射共享目錄
//創建用lan
[root@lanzhiyong ~]# useradd -M lan
//為lan用戶創建smb共享密碼:
[root@lanzhiyong ~]# smbpasswd -a lan
New SMB password: //設置密碼
Retype new SMB password:
```
//假設這裏映射lan 用戶為share 用戶,那麽就要在/etc/samba/smbusers文件中添加如下內容:
```
[root@lanzhiyong ~]# echo ‘lan = share‘ > /etc/samba/smbusers
//在全局配置中添加如下內容:
[root@lanzhiyong ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
username map = /etc/samba/smbusers //添加此行內容
passdb backend = tdbsam
//創建一個共享目錄lanzhiyong
[root@lanzhiyong ~]# mkdir /opt/lanzhiyong
[root@lanzhiyong ~]# chown -R lan.lan /opt/lanzhiyong/
[root@lanzhiyong ~]# ls -ld /opt/lanzhiyong/
drwxr-xr-x. 4 lan lan 30 8月 6 16:10 /opt/lanzhiyong/
```
//配置共享
```
[root@lanzhiyong ~]# cat >> /etc/samba/smb.conf <<EOF
[lan]
comment = lan
path = /opt/lanzhiyong
browseable = yes
guest ok = yes
writable = yes
write list = share
public = yes
[root@lanzhiyong ~]# tail -8 /etc/samba/smb.conf
[lan]
comment = lan
path = /opt/lanzhiyong
browseable = yes
guest ok = yes
writable = yes
write list = share
public = yes
```
```
//啟動smb服務
[root@lanzhiyong ~]# systemctl start smb
//重啟smb服務
[root@lanzhiyong ~]# systemctl restart smb
//重新加載smb服務
[root@lanzhiyong ~]# systemctl reload smb
//設置smb服務隨系統啟動而啟動
[root@lanzhiyong ~]# systemctl enable smbc
```
*** //在客戶端先安裝samba-smbclient
[root@lan ~]# yum install -y samba-client
//在客戶端查看samba服務器有哪些共享資源
[root@lan ~]# smbclient -L 192.168.209.12 -U share
Enter SAMBA\share‘s password:
```
Domain=[LANZHIYONG] OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
lan Disk lan
IPC$ IPC IPC Service (Samba 4.6.2)
Domain=[LANZHIYONG] OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
//創建需要在客戶端掛載到哪個目錄下
[root@lan ~]# mkdir /opt/smb
//將samba服務器的共享資源lan掛載到客戶機本地
[root@lan ~]# mount -t cifs //192.168.209.12/lan /opt/smb/ -o username=share,password=redhat
[root@lan ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 9.4G 7.7G 55% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 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.209.12/lan 17G 9.4G 7.7G 55% /opt/smb
//在客戶機上進行共享目錄創建新文件
[root@lan ~]# cd /opt/smb/
[root@lan smb]# touch 8888
[root@lan smb]# mkdir 9999
[root@lan smb]# ls
8888 9999
//最後到服務器端上驗證:
[root@lanzhiyong lan]# cd /opt/lanzhiyong/
[root@lanzhiyong lanzhiyong]# ls
8888 9999
9.實例 (配置匿名共享,就是不需要密碼都可以訪問共享資源)
前提是在服務器端和客戶端在關閉防火墻的情況下才能實現共享目錄。服務器端IP:192.168.209.12 客戶端IP:192.168.209.13
先在服務器端:
//使用yum命令安裝samba服務器
[root@lanzhiyong ~]# yum install -y samba-*
//在全局配置中添加如下內容:
[root@lanzhiyong ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
map to guest = Bad User //添加此行內容
//創建一個共享目錄lanzhiyong
[root@lanzhiyong ~]# mkdir /opt/lanzhiyong
[root@lanzhiyong ~]# chmod -R 777 /opt/lanzhiyong/
[root@lanzhiyong ~]# ll -ld /opt/lanzhiyong/
drwxrwxrwx. 4 lan lan 30 8月 6 16:10 /opt/lanzhiyong/
//配置共享
[root@lanzhiyong ~]# cat >> /etc/samba/smb.conf <<EOF
[lanzhiyong]
comment = lanzhiyong
path = /opt/lanzhiyong
browseable = yes
guest ok = yes
writable = yes
public = yes
EOF
[root@lanzhiyong ~]# tail -7 /etc/samba/smb.conf
[lanzhiyong]
comment = lanzhiyong
path = /opt/lanzhiyong
browseable = yes
guest ok = yes
writable = yes
public = yes
//啟動smb服務
[root@lanzhiyong ~]# systemctl start smb
在客戶端做以下操作:
//在客戶端先安裝samba-smbclient
[root@lanzhiyong ~]#yum install -y samba-client
//在客戶端查看samba服務器有哪些共享資源
[root@lanzhiyong ~]#smbclient -L 192.168.209.12 -U ‘Bad User‘
Enter SAMBA\Bad User‘s password:
Anonymous login successful
OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
lan Disk lan
IPC$ IPC IPC Service (Samba 4.6.2)
Anonymous login successful
OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
//將samba服務器的共享資源lan掛載到客戶機本地
[root@lan ~]# mount -t cifs //192.168.209.12/lanzhiyong /opt/smb/ -o username=share,password=redhat
[root@lan ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 17G 9.4G 7.7G 55% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 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.209.12/lanzhiyong 17G 9.4G 7.7G 55% /opt/smb
//在客戶機上進行共享目錄創建新文件
[root@lan ~]# cd /opt/smb/
[root@lan smb]# touch 123
[root@lan smb]# mkdir 123123123
[root@lan smb]# ls
123 123123123
//到服務器端上驗證:
[root@lanzhiyong ~]# cd /opt/lanzhiyong/
[root@lanzhiyong lanzhiyong]# ls
123 123123123
Linux如何詳細搭建samba服務