Samba是一個能讓Linux系統應用Microsoft網絡通信協議的軟件,而SMB是Server Message Block的縮寫,即爲服務器消息塊 ,SMB主要是做爲Microsoft的網絡通信協議,後來Samba將SMB通訊協議應用到了Linux系統上,就造成了如今的Samba軟件。後來微軟又把 SMB 更名爲 CIFS(Common Internet File System),即公共 Internet 文件系統,而且加入了許多新的功能,這樣一來,使得Samba具備了更強大的功能。
Samba最大的功能就是能夠用於Linux與windows系統直接的文件共享和打印共享,Samba既能夠用於windows與Linux之間的文件共享,也能夠用於Linux與Linux之間的資源共享,因爲NFS(網絡文件系統)能夠很好的完成Linux與Linux之間的數據共享,於是 Samba較多的用在了Linux與windows之間的數據共享上面。
SMB是基於客戶機/服務器型的協議,於是一臺Samba服務器既能夠充當文件共享服務器,也能夠充當一個Samba的客戶端,例如,一臺在Linux 下已經架設好的Samba服務器,windows客戶端就能夠經過SMB協議共享Samba服務器上的資源文件,同時,Samba服務器也能夠訪問網絡中 其它windows系統或者Linux系統共享出來的文件。
Samba在windows下使用的是NetBIOS協議,若是你要使用Linux下共享出來的文件,請確認你的windows系統下是否安裝了NetBIOS協議。
組成Samba運行的有兩個服務,一個是SMB,另外一個是NMB;SMB是Samba 的核心啓動服務,主要負責創建 Linux Samba服務器與Samba客戶機之間的對話, 驗證用戶身份並提供對文件和打印系統的訪問,只有SMB服務啓動,才能實現文件的共享,監聽139 TCP端口;而NMB服務是負責解析用的,相似與DNS實現的功能,NMB能夠把Linux系統共享的工做組名稱與其IP對應起來,若是NMB服務沒有啓動,就只能經過IP來訪問共享文件,監聽137和138 UDP端口。shell
兩臺主機:數據庫
[root@centos7 ~]# cat /etc/redhat-release # 查看系統版本 CentOS Linux release 7.2.1511 (Core) [root@centos7 ~]# uname -r # 查看系統內核版本 3.10.0-327.el7.x86_64 [root@centos7 ~]# getenforce # 查看SELinux是否關閉 Disabled [root@centos7 ~]# systemctl status firewalld.service # 查看防火牆是否關閉 ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead)
[root@centos7 ~]# yum -y install samba
[root@centos7 ~]# rpm -qa | grep samba samba-common-libs-4.6.2-11.el7_4.x86_64 samba-common-4.6.2-11.el7_4.noarch samba-client-libs-4.6.2-11.el7_4.x86_64 samba-common-tools-4.6.2-11.el7_4.x86_64 samba-4.6.2-11.el7_4.x86_64 samba-libs-4.6.2-11.el7_4.x86_64
[root@centos7 ~]# systemctl start smb [root@centos7 ~]# systemctl status smb ● smb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2017-11-11 19:07:55 CST; 2s ago Main PID: 1661 (smbd) Status: "smbd: ready to serve connections..." CGroup: /system.slice/smb.service ├─1661 /usr/sbin/smbd ├─1662 /usr/sbin/smbd ├─1663 /usr/sbin/smbd └─1664 /usr/sbin/smbd Nov 11 19:07:55 centos7 systemd[1]: Starting Samba SMB Daemon... Nov 11 19:07:55 centos7 smbd[1661]: [2017/11/11 19:07:55.299475, 0] ../lib/util/...y) Nov 11 19:07:55 centos7 smbd[1661]: STATUS=daemon 'smbd' finished starting up a...ns Nov 11 19:07:55 centos7 systemd[1]: Started Samba SMB Daemon. Hint: Some lines were ellipsized, use -l to show in full.
Samba的主配置文件爲/etc/samba/smb.conf
主配置文件由兩部分構成:windows
[root@centos7 ~]# cat /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 說明:設定 Samba Server 所要加入的工做組或者域。 security = user 說明:設置用戶訪問Samba Server的驗證方式,一共有四種驗證方式 * share:用戶訪問Samba Server不須要提供用戶名和口令, 安全性能較低 * user:Samba Server共享目錄只能被受權的用戶訪問,由Samba Server負責檢查帳號和密碼的正確性。帳 號和密碼要在本Samba Server中創建 * server:依靠其餘Windows NT/2000或Samba Server來驗證用戶的帳號和密碼,是一種代理驗證 * domain:域安全級別,使用主域控制器(PDC)來完成認證 passdb backend = tdbsam 說明:passdb backend就是用戶後臺的意思。目前有三種後臺:smbpasswd、tdbsam和ldapsam。sam應該是security account manager(安全帳戶管理)的簡寫。 * smbpasswd:該方式是使用smb本身的工具smbpasswd來給系統用戶(真實用戶或者虛擬用戶)設置一個Samba密碼,客戶端就用這個密碼來訪問Samba的資源。smbpasswd文件默認在/etc/samba目錄下,不過有時候要手工創建該文件。 * tdbsam: 該方式則是使用一個數據庫文件來創建用戶數據庫 * ldapsam:該方式則是基於LDAP的帳戶管理方式來驗證用戶 printing = cups 說明:設置Samba共享打印機的類型。如今支持的打印系統有:bsd, sysv, plp, lprng, aix, hpux, qnx printcap name = cups 說明:設置共享打印機的配置文件 load printers = yes 說明:設置是否在啓動Samba時就共享打印機 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 = i0775
共享參數centos
[共享名稱] comment = 任意字符串 說明:對該共享的描述 path = 共享路徑 說明:指定共享目錄的路徑 browseable = yes/no 說明:該共享是否能夠瀏覽 writable = yes/no 說明:該共享是否可寫 available = yes/no 說明:該共享資源是否可用 admin user = 該共享管理者 說明:指定該共享的管理用戶 valid users = 容許訪問該共享的用戶 說明:多個用戶之間用逗號隔開,用戶組用@組名 write list = 容許寫入該共享的用戶 說明:相似上面 public = yes/no 說明:public用來指定該共享目錄是否容許guest帳戶訪問 guest ok = yes/no 說明:同public
匿名訪問安全
[root@centos7 ~]# mkdir -p /data [root@centos7 ~]# touch /data/txt [root@centos7 ~]# chown -R nobody.nobody /data/ 說明:由於要全部用戶都有權限訪問,因此給予nobody權限
[root@centos7 ~]# cp /etc/samba/smb.conf{,.bak} [root@centos7 ~]# egrep -v "^#|^$" /etc/samba/smb.conf [global] workgroup = WORKGROUP security = user map to guest = Bad User #說明:共享級別,不須要用戶名和密碼 [data] comment = this is data path = /data public = yes browseable = yes
[root@centos7 ~]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[data]" Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions # Global parameters [global] map to guest = Bad User security = USER idmap config * : backend = tdb [data] comment = this is data path = /data guest ok = Yes
[root@centos7 ~]# systemctl start smb [root@centos7 ~]# systemctl status smb ● smb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2017-11-12 05:16:33 CST; 27min ago Main PID: 1680 (smbd) Status: "smbd: ready to serve connections..." CGroup: /system.slice/smb.service ├─1680 /usr/sbin/smbd ├─1681 /usr/sbin/smbd ├─1682 /usr/sbin/smbd └─1683 /usr/sbin/smbd Nov 12 05:16:32 centos7 systemd[1]: Starting Samba SMB Daemon... Nov 12 05:16:33 centos7 smbd[1680]: [2017/11/12 05:16:33.072669, 0] ../lib/util/...y) Nov 12 05:16:33 centos7 smbd[1680]: STATUS=daemon 'smbd' finished starting up a...ns Nov 12 05:16:33 centos7 systemd[1]: Started Samba SMB Daemon. Hint: Some lines were ellipsized, use -l to show in full.
[root@centos7 ~]# smbclient //10.0.0.200/data Enter WORKGROUP\root's password: OS=[Windows 6.1] Server=[Samba 4.6.2] smb: \> ls . D 0 Sun Nov 12 05:03:42 2017 .. DR 0 Sun Nov 12 05:03:35 2017 txt N 0 Sun Nov 12 05:03:42 2017 102706180 blocks of size 1024. 101218464 blocks available smb: \>
Windows測試
按win+r彈出運行界面服務器
用戶及密碼訪問網絡
[root@centos7 ~]# groupadd samba [root@centos7 ~]# useradd user -g samba [root@centos7 ~]# smbpasswd -a user New SMB password: Retype new SMB password: Added user user.
[root@centos7 ~]# egrep -v "^#|^$" /etc/samba/smb.conf [global] workgroup = WORKGROUP security = user [data] comment = this is data path = /data public = yes browseable = yes admin user = user valid users = @samba
[root@centos7 ~]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[data]" Unknown parameter encountered: "admin user" Ignoring unknown parameter "admin user" Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions # Global parameters [global] security = USER idmap config * : backend = tdb [data] comment = this is data path = /data guest ok = Yes valid users = @samba
[root@centos7 ~]# systemctl restart smb
測試
Linux測試dom
[root@centos7 ~]# smbclient //10.0.0.200/data -U user Enter WORKGROUP\user's password: Domain=[CENTOS7] OS=[Windows 6.1] Server=[Samba 4.6.2] smb: \> ls . D 0 Sun Nov 12 05:03:42 2017 .. DR 0 Sun Nov 12 05:03:35 2017 txt N 0 Sun Nov 12 05:03:42 2017 102706180 blocks of size 1024. 101218400 blocks available smb: \>
Windows測試
win+r彈出運行界面ide