天天看點

Samba 服務共享comment = Home Directoriesbrowseable = nowritable = yescomment = All Printerspath = /var/spool/sambabrowseable = noguest ok = nowritable = noprintable = yesvim /etc/samba/smb.conf

linux下配置samba伺服器

1、 什麼是samba

Samba服務類似于windows上的共享功能,可以實作在Linux上共享檔案,windows上通路,當然在Linux上也可以通路到。

是一種在區域網路上共享檔案和列印機的一種通信協定,它為區域網路内的不同計算機之間提供檔案及列印機等資源的共享服務。

2、安裝配置samba

安裝指令:yum install -y samba samba-client

[root@samba-server ~]# egrep -v "^$|#|;" /etc/samba/smb.conf

[global]

workgroup = MYGROUP

server string = Samba Server Version %v

log file = /var/log/samba/log.%m

max log size = 50

security = user

passdb backend = tdbsam

load printers = yes

cups options = raw

[homes]

comment = Home Directories

browseable = no

writable = yes

[printers]

comment = All Printers

path = /var/spool/samba

guest ok = no

writable = no

printable = yes

1.配置檔案解釋:

[global] 定義全局的配置,workgroup用來定義工作組,相信如果您安裝過windows的系統,你會對這個workgroup不陌生。一般情況下,需要我們把這裡的MYGROUP改成WORKGROUP(windows預設的工作組名字)。

security = user #這裡指定samba的安全等級。

關于安全等級有四種:

share:使用者不需要賬戶及密碼即可登入samba伺服器

user:由提供服務的samba伺服器負責檢查賬戶及密碼(預設)

server:檢查賬戶及密碼的工作由另一台windows或samba伺服器負責

domain:指定windows域控制伺服器來驗證使用者的賬戶及密碼。

passdb backend = tdbsam #passdb backend(使用者背景)

samba有三種使用者背景:smbpasswd, tdbsam和ldapsam。

load printers:加載列印機

cups options = raw 列印機相關設定;

2.samba配置檔案參數介紹

smbpasswd:該方式是使用smb工具smbpasswd給系統使用者(真實使用者或者虛拟使用者)設定一個Samba密碼,用戶端就用此密碼通路Samba資源。smbpasswd在/etc/samba中,有時需要手工建立該檔案。

tdbsam: 使用資料庫檔案建立使用者資料庫。資料庫檔案叫passdb.tdb,在/etc/samba中。passdb.tdb使用者資料庫可使用 smbpasswd -a 建立Samba使用者,要建立的Samba使用者必須先是系統使用者。也可使用pdbedit建立Samba賬戶。

pdbedit參數很多,列出幾個主要的:

pdbedit -a username:建立Samba賬戶。

pdbedit -x username:删除Samba賬戶。

pdbedit -L:列出Samba使用者清單,讀取passdb.tdb資料庫檔案。

pdbedit -Lv:列出Samba使用者清單詳細資訊。

pdbedit -c “[D]” -u username:暫停該Samba使用者賬号。

pdbedit -c “[]” -u username:恢複該Samba使用者賬号。

ldapsam:基于LDAP賬戶管理方式驗證使用者。首先要建立LDAP服務,設定 “passdb backend = ldapsam:ldap://LDAP Server”

load printers 和 cups options 兩個參數用來設定列印機相關。

除了這些參數外,還有幾個參數需要你了解:

netbios name = MYSERVER # 設定出現在網路上的芳鄰中的主機名

hosts allow = 127. 192.168.12. 192.168.13. 172.16.# 用來設定允許的主機,如果在前面加 ”;” 則表示允許所有主機

log file = /var/log/samba/%m.log #定義samba的日志,這裡的%m是上面的netbios name

max log size = 50 # 指定日志的最大容量,機關是K

[homes] 該部分内容共享使用者自己的家目錄,也就是說,當使用者登入到samba伺服器上時實際上是進入到了該使用者的家目錄,使用者登陸後,共享名不是homes而是使用者自己的辨別符,對于單純的檔案共享的環境來說,這部分可以注視掉。

[printers] 該部分内容設定列印機共享。

3.samba實踐一

要求共享一個目錄,任何人都可以通路,即不用輸入密碼即可通路,要求隻讀;

[root@samba-server samba]# cp /etc/samba/smb.conf /etc/samba/smb.conf.ori

[root@samba-server samba]# egrep -v "^$|#|;" /etc/samba/smb.conf.ori >smb.conf

在配置檔案/etc/samba/smb.conf中做如下修改(紅顔色字型為修改的内容)

[root@samba-server samba]# cat /etc/samba/smb.conf

workgroup =<code>WORKGROUP</code>

security = share

[share]

mkdir /tmp/samba

chmod 777 /tmp/samba

touch /tmp/samba/sharefile

echo "111111" &gt; /tmp/samba/sharefile

testparm 測試配置檔案

/etc/init.d/smb start

netstat -lntup|grep smb

[root@samba-server samba]# netstat -lntup|grep smb

tcp 0 0 0.0.0.0:139 0.0.0.0: LISTEN 3338/smbd 

tcp 0 0 0.0.0.0:445 0.0.0.0: LISTEN 3338/smbd 

tcp 0 0 :::139 ::: LISTEN 3338/smbd 

tcp 0 0 :::445 ::: LISTEN 3338/smbd

啟動:/etc/init.d/smb start 

檢查配置的smb.conf是否正确 testparm

測試:windows機器浏覽器輸入 file://192.168.56.17/share

或者運作欄輸入: \192.168.56.17 或 \192.168.56.17 \share

浏覽器顯示:

root@samba-server samba]# testparm

Load smb config files from /etc/samba/smb.conf

Processing section "[homes]"

Processing section "[printers]"

Processing section "[share]"

WARNING: The security=share option is deprecated 有警告,但是不影響測試

Loaded services file OK.

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

security = SHARE

client signing = required

idmap config * : backend = tdb

read only = No

browseable = No

printable = Yes

print ok = Yes

comment = share all

path = /tmp/samba

guest ok = Yes

4.Samba實踐二

共享一個目錄,使用使用者名和密碼登入後才可以通路,要求可以讀寫;

workgroup = WORKGROUP

#[homes]

#[printers]

#[share]

browseable = yes

public = yes

[myshare]

comment = share for users

path = /samba

public = no

[root@samba-server /]# mkdir /samba

[root@samba-server /]# chmod 777 /samba/

[root@samba-server /]# useradd test01

[root@samba-server /]# useradd test02

[root@samba-server /]# pdbedit -a test02

new password:

retype new password:

Unix username: test02

NT username: 

Account Flags: [U ]

User SID: S-1-5-21-3196454316-2411642794-2178690100-1001

Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513

Full Name: 

Home Directory: \samba-server\test02

HomeDir Drive: 

Logon Script: 

Profile Path: \samba-server\test02\profile

Domain: SAMBA-SERVER

Account desc: 

Workstations: 

Munged dial: 

Logon time: 0

Logoff time: Wed, 06 Feb 2036 23:06:39 CST

Kickoff time: Wed, 06 Feb 2036 23:06:39 CST

Password last set: Fri, 05 Aug 2016 17:33:23 CST

Password can change: Fri, 05 Aug 2016 17:33:23 CST

Password must change: never

Last bad password : 0

Bad password count : 0

Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

[root@samba-server /]# pdbedit -a test01

Passwords do not match!

Unix username: test01

User SID: S-1-5-21-3196454316-2411642794-2178690100-1000

Home Directory: \samba-server\test01

Profile Path: \samba-server\test01\profile

Password last set: Fri, 05 Aug 2016 17:33:07 CST

Password can change: Fri, 05 Aug 2016 17:33:07 CST

5.Samba實踐三

samba服務端,先暫停使用者,賬号标記為[DU],正常狀态為[U];用戶端通路,提示賬号不可用;

[root@samba-server samba]# pdbedit -c [D] -u test01

Account Flags: [DU ]

Password last set: Fri, 05 Aug 2016 17:55:14 CST

Password can change: Fri, 05 Aug 2016 17:55:14 CST

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest01

Enter test01's password: 

session setup failed: NT_STATUS_ACCOUNT_DISABLED

samba服務端恢複使用者,賬号标記為[U];用戶端通路,恢複正常;

[root@samba-server samba]# pdbedit -c [] -u test01

Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]

smb: \&gt;

smb: \&gt; ls

. D 0 Fri Aug 5 17:38:07 2016

.. DR 0 Fri Aug 5 17:31:37 2016

contest 10 Fri Aug 5 17:38:07 2016

[root@samba-server samba]# pdbedit -c [D] -u test02

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest02

Enter test02's password: 

[root@samba-server samba]# pdbedit -c [] -u test02|grep -i Account

Account desc:

smb: \&gt; q

[root@samba-server ~]# pdbedit -L

test01:504:

删除使用者:

[root@samba-server ~]# pdbedit -x test01

[root@samba-server samba]# pdbedit -x test02

[root@samba-server ~]#

[root@samba-server ~]# smbclient //192.168.56.17/myshare -Utest02

session setup failed: NT_STATUS_LOGON_FAILURE

6.Linux通路samba共享檔案

參考文檔:

<a href="http://mofansheng.blog.51cto.com/8792265/1654871">http://mofansheng.blog.51cto.com/8792265/1654871</a>

[root@samba-client ~]# yum install -y samba-client

安裝用戶端軟體 yum install -y samba-client

指令格式:smbclient //IP/共享名 -U 使用者名

如:smbclient //127.0.0.1/share 如果是匿名通路可以省略掉-U

[root@samba-client ~]# mount -t cifs //192.168.56.17/myshare /mnt -o username=test01,password=123456

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01 

Enter test01's password: 123456

mount -t cifs //192.168.0.22/myshare /mnt 匿名通路不需要-o參數;

如執行指令提示參數有錯,還需要安裝cifs-utils包;

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01

Enter test01's password: 1234 輸錯密碼報錯 

輸入正确密碼後進入,put上傳檔案,get下載下傳檔案;上傳的檔案smb伺服器中檢視所屬主、組為user1;user1在共享檔案夾可以建立目錄,删除檔案;help可以列出目前指令行可以執行的指令;

必須先在samba-client目前目錄下建立檔案,才能上傳對應的檔案到samba-server端的共享目錄下/share 下,而且通過test01 使用者登陸的上傳的檔案到samba-server 服務端的share下,顯示該檔案的使用者和使用者組為test01.

[root@samba-client ~]# echo 123 &gt;1.sql

[root@samba-client ~]# ls -ls

total 8

4 -rw-r--r-- 1 root root 4 Aug 5 18:51 1.sql

4 -rw-r--r-- 1 root root 10 Aug 5 18:48 contest

smb: \&gt; put 1.sql

putting file 1.sql as \1.sql (0.8 kb/s) (average 0.8 kb/s)

[root@samba-server samba]# ll

-rwxr--r-- 1 test01 test01 4 Aug 5 18:51 1.sql

-rw-r--r-- 1 root root 10 Aug 5 17:38 contest

smb: \&gt;get contest

getting file \contest of size 10 as contest (1.2 KiloBytes/sec) (average 1.2 KiloBytes/sec)

[root@samba-client ~]# ls

contest

[root@samba-client ~]# cat contest 

Wujianwei

用戶端mount挂載samba共享檔案到本地

mount -t cifs //192.168.56.17/myshare /mnt -o username=test01,password=123456

[root@samba-client ~]# mount

/dev/sda5 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/sda2 on /data type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

//192.168.56.17/myshare on /mnt type cifs (rw,username=test01,password=123456)

7執行個體共享檔案通路權限的控制

<a href="http://304076020.blog.51cto.com/7503470/1367740">http://304076020.blog.51cto.com/7503470/1367740</a>

nginx+lwa

<a href="http://lee90.blog.51cto.com/10414478/1833724">http://lee90.blog.51cto.com/10414478/1833724</a>

編輯samba的配置檔案:

comment:注釋資訊;

path:目前共享所映射的檔案系統路徑;

browseable:是否可浏覽,指是否可被使用者檢視;

guest ok:是否允許來賓賬号通路;

public:是否公開所有使用者;

writable:是否可寫;

write list:擁有寫權限的使用者清單;

comment = wordpress 名稱

path = /data/application/web 要導出的路徑

browseable = yes 

2.用戶端配置:

挂載:

mount -t cifs -o username=apachepassword=admin //172.16.35.1/wordpress /var/www/html

參考資料:

Samba服務共享的Linux和windows 參數介紹:

<a href="http://yuanbin.blog.51cto.com/363003/115761/">http://yuanbin.blog.51cto.com/363003/115761/</a>

<a href="http://www.linuxidc.com/Linux/2015-05/117977.htm">http://www.linuxidc.com/Linux/2015-05/117977.htm</a>

 本文轉自 wjw555 51CTO部落格,原文連結:http://blog.51cto.com/wujianwei/2057172

繼續閱讀