一栈拖、簡介
samba是在Linux和Unix系統(tǒng)上實現(xiàn)SMB協(xié)議的一個免費軟件,由服務器及客戶端程序構(gòu)成没陡。SMB協(xié)議是一種在局域網(wǎng)上共享文件和打印機的通信協(xié)議辱魁,它為局域網(wǎng)內(nèi)不同的計算機之間提供文件及打印機等服務。Samba服務監(jiān)聽著137/udp,138/udp139/tcp,445/tcp端口诗鸭,通過結(jié)合NetBIOS廣播協(xié)議及SMB協(xié)議,使得samba服務能夠?qū)崿F(xiàn)局域網(wǎng)內(nèi)的windowns参滴、Linux主機的文件共享强岸。
二、samba服務器的搭建
1砾赔、samba服務的安裝
首先安裝samba服務:
[root@samba ~]# yum install -y samba
.......
Installed:
samba.x86_64 0:4.6.2-12.el7_4
Dependency Installed:
avahi-libs.x86_64 0:0.6.31-17.el7 cups-libs.x86_64 1:1.6.3-29.el7 libldb.x86_64 0:1.1.29-1.el7
libtalloc.x86_64 0:2.1.9-1.el7 libtdb.x86_64 0:1.3.12-2.el7 libtevent.x86_64 0:0.9.31-2.el7_4
libwbclient.x86_64 0:4.6.2-12.el7_4 pytalloc.x86_64 0:2.1.9-1.el7 samba-client-libs.x86_64 0:4.6.2-12.el7_4
samba-common.noarch 0:4.6.2-12.el7_4 samba-common-libs.x86_64 0:4.6.2-12.el7_4 samba-common-tools.x86_64 0:4.6.2-12.el7_4
samba-libs.x86_64 0:4.6.2-12.el7_4
Complete!
關(guān)閉firewalld及設置selinux為permissive蝌箍,避免它們對測試效果造成影響:
[root@samba ~]# systemctl stop firewalld
[root@samba ~]# setenforce 0
2、samba服務的程序及配置文件
安裝完成后暴心,samba服務生產(chǎn)的相關(guān)的程序及配置文件如下:
主配置文件:/etc/samba/smb.conf
主程序:nmbd:NetBIOS name server 及smbd:SMB/CIFS services
Unit文件:smb.service和nmb.service
其主配置文件/etc/samba/smb.conf由samba-common程序包提供妓盲,主要包括以下幾個部分:
Global Settings
NetworkRelated Options
Logging Options
Standalone Server Options
Domain Members Options
Domain Controller Options
Browser Control Options
Printing Options
File System Options
Share Definitions
對于/etc/samba/smb.conf文件常見參數(shù)及分類如下:
- 全局配置:
[global]
workgroup = MYGROUP #工作組名
server string = Samba Server Version %v #服務器信息介紹
netbios name = MYSERVER #用netbios名來指定服務
interfaces = [interface1 interface2...|address1 address2...] #用于讓samba服務監(jiān)聽多個網(wǎng)絡接口或IP
hosts allow = [address1 address2...] #指定允許訪問的主機IP
log file = /var/log/samba/log.%m #指定日志存放路徑,%m為來訪的主機名
max log size = 50 #定義日志文件最大容量為50K
security=user #設置samba服務的安全認證方式為user
passdb backend=tdbsam #定義用戶后臺的類型為tdbsam专普,其他類型還有smbpasswd悯衬、ldapsam
load prints = yes # #設置是否共享打印機
cups options = raw #打印機選項
- 共享文件系統(tǒng)配置的配置大體有三類:
[homes]:為每個samba用戶定義其是否能夠通過samba服務訪問自己的家目錄;
[printers]:定義打印服務檀夹;
[shared_fs]:定義共享的文件系統(tǒng)筋粗;
其常用指令有:
comment=STRING:注釋;
path=/PATH/TO/FILENAME:當前共享所映射的文件系統(tǒng)路徑炸渡;
browseable=YES:是否可瀏覽娜亿,指是否可被用戶查看;
guest ok=YES:是否允許來賓賬號訪問蚌堵;
browseable = No:是否公開目錄
writable=YES:是否可寫买决;
read only = no|yes :是否為只讀
write list=/PATH/TO/user_list:擁有寫權(quán)限的用戶列表;
其它可用參數(shù)吼畏,可通過/etc/samba/smb.conf.example文件或命令man smb.conf
來查看督赤。
此處對/etc/samba/smb.conf配置文件添加如下配置:
[root@samba ~]# vim /etc/samba/smb.conf
[shared_dir]
comment=This is a new share directory
path=/samba_dir
writable=yes
[root@samba ~]# mkdir /samba_dir #創(chuàng)建共享文件
配置完成后可使用testparm命令來校驗/etc/samba/smb.conf文件的配置是否正確:
[root@samba ~]# 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 "[samba_dir]"
Processing section "[data]"
Loaded services file OK.
WARNING: 'workgroup' and 'netbios name' must differ.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
workgroup = SAMBA
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw
[homes]
comment = Home Directories
browseable = No
inherit acls = Yes
read only = No
valid users = %S %D%w%S
[printers]
comment = All Printers
path = /var/tmp
browseable = No
printable = Yes
create mask = 0600
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
create mask = 0664
directory mask = 0775
write list = root
[samba_dir]
comment = This is a new share directory
path = /samba_dir
guest ok = Yes
read only = No
samba服務默認的驗證模式為user,因此我們還需要創(chuàng)建用戶的數(shù)據(jù)庫宫仗。
3够挂、創(chuàng)建samba用戶數(shù)據(jù)庫
samba帳號必須要存在于Linux系統(tǒng)中(/etc/passwd),但其密碼的卻是單獨維護的藕夫。創(chuàng)建samba用戶數(shù)據(jù)庫需要使用命令pdbedit孽糖,pdbedit命令是用于管理smb服務的賬號信息數(shù)據(jù)庫其用法格式為:
pdbedit [options] account
常用選項有:
-a USERNAME:創(chuàng)建samba用戶枯冈;
-x:刪除samba用戶;
-L:列出samba用戶列表办悟;
-Lv:列出用戶詳細信息列表尘奏;
除了pdbedit命令之外,我們也可以使用smbpasswd命令來管理創(chuàng)建samba用戶病蛉,其語法格式如下:
smbpasswd [options] USERNAME
常用選項有:
-a:添加賬號
-x:刪除賬號
-d:禁用賬號
-e:啟用賬號
接著我們就來創(chuàng)建samba用戶:
[root@samba ~]# useradd samba
[root@samba ~]# pdbedit -a samba
new password:
retype new password:
Unix username: samba
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2841389940-495581649-2996202120-1000
Primary Group SID: S-1-5-21-2841389940-495581649-2996202120-513
Full Name:
Home Directory: \\samba\samba
HomeDir Drive:
Logon Script:
Profile Path: \\samba\samba\profile
Domain: SAMBA
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 10:06:39 EST
Kickoff time: Wed, 06 Feb 2036 10:06:39 EST
Password last set: Mon, 07 May 2018 07:34:53 EDT
Password can change: Mon, 07 May 2018 07:34:53 EDT
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@samba ~]# useradd magedu
[root@samba ~]# smbpasswd -a magedu
New SMB password:
Retype new SMB password:
Added user magedu.
4炫加、啟動smb服務,測試訪問
[root@samba ~]# systemctl start smb
由上圖所示铺然,samba用戶登錄后無法在samba_dir共享文件夾下創(chuàng)建目錄俗孝,這是因samba_dir共享目錄的屬主和屬組均為root,其他用戶沒有寫權(quán)限
[root@samba ~]# ll /samba_dir/ -d
drwxr-xr-x. 2 root root 6 May 7 07:19 /samba_dir/
我們將目錄的屬組改為samba魄健,并添加寫權(quán)限:
[root@samba ~]# usermod -G samba magedu #將samba添加為magedu賬號的附加組
[root@samba ~]# chown :samba /samba_dir/ #修改共享目錄的屬組為samba
[root@samba ~]# chmod g+w /samba_dir/ #給samba增加寫權(quán)限
[root@samba ~]# ll -d /samba_dir/
drwxrwxr-x. 2 root samba 6 May 7 07:19 /samba_dir/
[root@samba ~]# systemctl restart smb
此時再次測試:
在Linux系統(tǒng)上掛載samba共享目錄:
[root@test ~]# mount -t cifs //192.168.0.84/samba_dir /mnt -o username=samba,password=123456 #掛載samba共享目錄
[root@test ~]# mount
......
//192.168.0.84/samba_dir on /mnt type cifs (rw,relatime,vers=1.0,cache=striorcegid,addr=192.168.0.84,unix,posixpaths,serverino,acl,rsize=1048576,wsiz
[root@test ~]# cd /mnt/
[root@test mnt]# ll
總用量 0
drwxr-xr-x. 2 1502 1502 0 5月 7 2018 magedu_test
[root@test mnt]# touch samba_files #創(chuàng)建文件成功
[root@test mnt]# rm magedu_test/
rm: 無法刪除"magedu_test/": 是一個目錄
[root@test mnt]# rmdir magedu_test/ #刪除文件成功赋铝,
[root@test mnt]# ll
總用量 0
-rw-r--r--. 1 1501 1501 0 4月 24 15:16 samba_files
另外,在linux系統(tǒng)中訪問samba文件系統(tǒng)還可以使用smbclient命令工具:
[root@test ~]# yum install samba-client -y #安裝smbclient命令工具
[root@test ~]# smbclient -L 192.168.0.84 -U samba #查看samba服務器的共享情況
Enter SAMBA\samba's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
samba_dir Disk This is a new share directory
IPC$ IPC IPC Service (Samba 4.6.2)
samba Disk Home Directories
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@test ~]# smbclient //192.168.0.84/samba_dir -U samba #交互式訪問samba共享目錄
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> lcd /etc/
smb: \> put fstab
putting file fstab as \fstab (75.7 kb/s) (average 75.7 kb/s) #成功上傳文件
smb: \> ls
. D 0 Mon May 7 20:28:07 2018
.. DR 0 Mon May 7 19:19:53 2018
samba_files N 0 Tue Apr 24 15:16:13 2018
fstab A 465 Mon May 7 20:28:07 2018
hello.txt A 0 Mon May 7 20:28:49 2018
17811456 blocks of size 1024. 16554288 blocks available
smb: \> rm hello.txt #成功刪除文件
smb: \> ls
. D 0 Mon May 7 20:29:00 2018
.. DR 0 Mon May 7 19:19:53 2018
samba_files N 0 Tue Apr 24 15:16:13 2018
fstab A 465 Mon May 7 20:28:07 2018
17811456 blocks of size 1024. 16554264 blocks available
smb: \>
三沽瘦、綜合案例
1革骨、創(chuàng)建一個共享data,路徑為/var/ftp/data,要求僅centos和gentoo用戶能上傳析恋,此路徑對其他用戶不可見良哲;
[root@samba ~]# mkdir -pv /var/ftp/data
mkdir: created directory ‘/var/ftp’
mkdir: created directory ‘/var/ftp/data’
[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
[data]
comment=ftp
path = /var/ftp/data
browseable = No #不公開data目錄
writable=yes
directory mask = 0775
create mask = 0664
write list=centos,gentoo #設置只允許centos、gentoo訪問data共享目錄
[root@samba ~]# testparm #運行testparm命令檢查/etc/samba/smb.conf配置文件是否有錯誤
[root@samba ~]# useradd centos -s /sbin/nologin
[root@samba ~]# useradd gentoo -s /sbin/nologin
[root@samba ~]# smbpasswd -a centos
New SMB password:
Retype new SMB password:
Added user centos.
[root@samba ~]# smbpasswd -a gentoo
New SMB password:
Retype new SMB password:
Added user gentoo.
[root@samba ~]# setfacl -m u:centos:rwx /var/ftp/data/ #設置用戶centos對/var/ftp/data的目錄權(quán)限為rwx
[root@samba ~]# setfacl -m u:gentoo:rwx /var/ftp/data/ #設置用戶gentoo對/var/ftp/data的目錄權(quán)限為rwx
[root@samba ~]# getfacl /var/ftp/data/
getfacl: Removing leading '/' from absolute path names
# file: var/ftp/data/
# owner: root
# group: root
user::rwx
user:centos:rwx
user:gentoo:rwx
group::r-x
mask::rwx
other::r-x
[root@samba ~]# systemctl restart smb