Samba服務(wù)簡述

Samba

Samba是在Linux和UNIX系統(tǒng)上實現(xiàn)SMB協(xié)議的一個免費軟件,由服務(wù)器及客戶端程序構(gòu)成。SMB(Server Messages Block,信息服務(wù)塊)是一種在局域網(wǎng)上共享文件和打印機的一種通信協(xié)議,它為局域網(wǎng)內(nèi)的不同計算機之間提供文件及打印機等資源的共享服務(wù)。SMB協(xié)議是客戶機/服務(wù)器型協(xié)議,客戶機通過該協(xié)議可以訪問服務(wù)器上的共享文件系統(tǒng)、打印機及其他資源。通過設(shè)置“NetBIOS over TCP/IP”使得Samba不但能與局域網(wǎng)絡(luò)主機分享資源,還能與全世界的電腦分享資源。

CIFS: Common Internet File System通用網(wǎng)絡(luò)文件系統(tǒng),是windows主機之間共享的協(xié)議,samba實現(xiàn)了這個協(xié)議,所以可以實現(xiàn)wondows與linux之間的文件共享服務(wù)。

監(jiān)聽的端口有:tcp/137 udp/137 tcp/139 udp/139
137:是實現(xiàn)NetBIOS協(xié)議,實現(xiàn)解析windows之間主機名的解析,實現(xiàn)在windows的網(wǎng)上鄰居可以看到Linux的主機名。
139:是實現(xiàn)cifs協(xié)議

Samba服務(wù)器的搭建

安裝samba服務(wù)

[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!

samba服務(wù)的程序及配置文件

主配置文件:/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

全局配置
[global]
    workgroup = MYGROUP  #工作組名
    server string = Samba Server Version %v  #服務(wù)器信息介紹
    netbios name = MYSERVER  #用netbios名來指定服務(wù)
    interfaces = [interface1 interface2...|address1 address2...]  #用于讓samba服務(wù)監(jiān)聽多個網(wǎng)絡(luò)接口或IP
    hosts allow = [address1 address2...]  #指定允許訪問的主機IP
    log file = /var/log/samba/log.%m  #指定日志存放路徑,%m為來訪的主機名
    max log size = 50  #定義日志文件最大容量為50K
    security=user  #設(shè)置samba服務(wù)的安全認(rèn)證方式為user
    passdb backend=tdbsam  #定義用戶后臺的類型為tdbsam,其他類型還有smbpasswd、ldapsam
    load prints = yes  #  #設(shè)置是否共享打印機
    cups options = raw  #打印機選項

共享文件系統(tǒng)配置

[homes]:為每個samba用戶定義其是否能夠通過samba服務(wù)訪問自己的家目錄;
[printers]:定義打印服務(wù);
[shared_fs]:定義共享的文件系統(tǒng);
常用指令

comment=STRING:注釋;
path=/PATH/TO/FILENAME:當(dāng)前共享所映射的文件系統(tǒng)路徑;
browseable=YES:是否可瀏覽,指是否可被用戶查看;
guest ok=YES:是否允許來賓賬號訪問;
browseable = No:是否公開目錄
writable=YES:是否可寫;
read only = no|yes :是否為只讀
write list=/PATH/TO/user_list:擁有寫權(quán)限的用戶列表;

此處對/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服務(wù)默認(rèn)的驗證模式為user,因此我們還需要創(chuàng)建用戶的數(shù)據(jù)庫。

創(chuàng)建samba用戶數(shù)據(jù)庫

samba帳號必須要存在于Linux系統(tǒng)中(/etc/passwd),但其密碼的卻是單獨維護的。創(chuàng)建samba用戶數(shù)據(jù)庫需要使用命令pdbedit,pdbedit命令是用于管理smb服務(wù)的賬號信息數(shù)據(jù)庫

格式:pdbedit [options] account

常用選項有:
-a USERNAME:創(chuàng)建samba用戶;
-x:刪除samba用戶;
-L:列出samba用戶列表;
-Lv:列出用戶詳細(xì)信息列表;
-t,--password-from-stdin:從標(biāo)準(zhǔn)輸出接收字符串作為用戶密碼;
使用空提示符,然后將密碼輸入兩次

除了pdbedit命令之外,我們也可以使用smbpasswd命令來管理創(chuàng)建samba用戶

格式:smbpasswd [options] USERNAME

常用選項有:
-a:添加賬號
-x:刪除賬號
-d:禁用賬號
-e:啟用賬號

smbstatus命令

顯示samba服務(wù)的相關(guān)共享的訪問狀態(tài)信息
-b:顯示簡要格式信息
-v:顯示詳細(xì)格式信息

查看服務(wù)器端共享:smbclient -t SMB_SERVER [-U USERNAME]
交互式文件訪問:smbclient //SMB_SERVER/SHARE_NAME [-U USERNAME]
掛載訪問:mount -t clfs //MB_SERVER/SHARE_NAME -o username=USERNAME,password=PASSWORD

注意:掛載操作用戶,與-o選項中指定用戶直接產(chǎn)生映射關(guān)系;此時,訪問掛載點,是以-o選項中的username指定的用戶身份進行;本地用戶對指定的路徑訪問,首先得擁有對應(yīng)的本地文件系統(tǒng)權(quán)限;

創(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.

啟動smb服務(wù),測試訪問

[root@samba ~]# systemctl start smb
輸入賬號密碼后成功登陸
無法在samba_dir目錄下創(chuàng)建文件

由上圖所示,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 samba  #將samba添加為samba賬號的附加組
[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
成功創(chuàng)建目錄

在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服務(wù)器的共享情況
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: \>

練習(xí)

創(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  #設(shè)置只允許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/  #設(shè)置用戶centos對/var/ftp/data的目錄權(quán)限為rwx
[root@samba ~]# setfacl -m u:gentoo:rwx /var/ftp/data/  #設(shè)置用戶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

輸入\\192.168.0.106\data

手動添加data路徑訪問

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Samba簡介 Samba是在Linux和UNIX系統(tǒng)上實現(xiàn)SMB協(xié)議的一個免費軟件,由服務(wù)器及客戶端程序構(gòu)...
    garyond閱讀 14,631評論 2 29
  • 什么是samba samba是實現(xiàn)windows、linux、unix的系列主機之間文件共享存儲的一種解決方案。s...
    華龍007閱讀 1,120評論 0 0
  • samba是一個實現(xiàn)類UNIX操作系統(tǒng)與WINDOWS操作系統(tǒng)之間共享的一種SMB協(xié)議的軟件。包括的文件共享和打印...
    4a873e424089閱讀 1,370評論 0 0
  • 1.SAMBA服務(wù)簡介 (1)SAMBA的功能: ? 共享文件和打印,實現(xiàn)在線編輯? 實現(xiàn)登錄SAMBA用戶的身份...
    尛尛大尹閱讀 1,247評論 0 1
  • SAMBA簡介 Samba是linux和unix系統(tǒng)上實現(xiàn)SMB/CIFS協(xié)議的一個免費軟件,由服務(wù)器和客戶端程序...
    geekdeedy閱讀 8,706評論 1 4

友情鏈接更多精彩內(nèi)容