Samba 服務共享

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
browseable = no
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
[global]
workgroup =WORKGROUP
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
security = share
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
browseable = no
guest ok = no
writable = no
printable = yes
[share]

      comment = share all
        path = /tmp/samba
        browseable = yes
        public = yes
        writable = no

mkdir /tmp/samba
chmod 777 /tmp/samba
touch /tmp/samba/sharefile
echo "111111" > /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

[global]
server string = Samba Server Version %v
security = SHARE
log file = /var/log/samba/log.%m
max log size = 50
client signing = required
idmap config * : backend = tdb
cups options = raw

[homes]
comment = Home Directories
read only = No
browseable = No

[printers]
comment = All Printers
path = /var/spool/samba
printable = Yes
print ok = Yes
browseable = No

[share]
comment = share all
path = /tmp/samba
guest ok = Yes

4.Samba實踐二
共享一個目錄,使用用戶名和密碼登錄後纔可以訪問,要求可以讀寫;
[root@samba-server samba]# cat /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
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

browseable = no

guest ok = no

writable = no

printable = yes

#[share]
comment = share all
path = /tmp/samba
browseable = yes
public = yes
writable = no
[myshare]
comment = share for users
path = /samba
browseable = yes
writable = yes
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
new password:
retype new password:
Passwords do not match!
[root@samba-server /]# pdbedit -a test01
new password:
retype new password:
Unix username: test01
NT username:
Account Flags: [U ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1000
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test01
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test01\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:07 CST
Password can change: Fri, 05 Aug 2016 17:33:07 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

5.Samba實踐三
samba服務端,先暫停用戶,賬號標記爲[DU],正常狀態爲[U];客戶端訪問,提示賬號不可用;

[root@samba-server samba]# pdbedit -c [D] -u test01
Unix username: test01
NT username:
Account Flags: [DU ]
User SID: S-1-5-21-3196454316-2411642794-2178690100-1000
Primary Group SID: S-1-5-21-3196454316-2411642794-2178690100-513
Full Name:
Home Directory: \samba-server\test01
HomeDir Drive:
Logon Script:
Profile Path: \samba-server\test01\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:55:14 CST
Password can change: Fri, 05 Aug 2016 17:55:14 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

[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
[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \>

smb: \> 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

    41119 blocks of size 131072. 13988 blocks available

[root@samba-server samba]# pdbedit -c [D] -u test02
Unix username: test02
NT username:
Account Flags: [DU ]
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 samba]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
session setup failed: NT_STATUS_ACCOUNT_DISABLED

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

[root@samba-server samba]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> 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

    41119 blocks of size 131072. 13988 blocks available

smb: \> q

[root@samba-server ~]# pdbedit -L
test01:504:
刪除用戶:
[root@samba-server ~]# pdbedit -x test01
[root@samba-server ~]# pdbedit -L
[root@samba-server samba]# pdbedit -x test02
[root@samba-server ~]#

[root@samba-server ~]# smbclient //192.168.56.17/myshare -Utest02
Enter test02's password:
session setup failed: NT_STATUS_LOGON_FAILURE

6.Linux訪問samba共享文件
參考文檔:
http://mofansheng.blog.51cto.com/8792265/1654871

[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
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> q

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 輸錯密碼報錯
session setup failed: NT_STATUS_LOGON_FAILURE

輸入正確密碼後進入,put上傳文件,get下載文件;上傳的文件smb服務器中查看所屬主、組爲user1;user1在共享文件夾可以新建目錄,刪除文件;help可以列出當前命令行可以執行的命令;

必須先在samba-client當前目錄下創建文件,才能上傳對應的文件到samba-server端的共享目錄下/share 下,而且通過test01 用戶登陸的上傳的文件到samba-server 服務端的share下,顯示該文件的用戶和用戶組爲test01.

[root@samba-client ~]# echo 123 >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

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \> put 1.sql
putting file 1.sql as \1.sql (0.8 kb/s) (average 0.8 kb/s)

[root@samba-server samba]# ll
total 8
-rwxr--r-- 1 test01 test01 4 Aug 5 18:51 1.sql
-rw-r--r-- 1 root root 10 Aug 5 17:38 contest

[root@samba-client ~]# smbclient //192.168.56.17/myshare -Utest01
Enter test01's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-35.el6_8]
smb: \>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實例共享文件訪問權限的控制
http://304076020.blog.51cto.com/7503470/1367740

nginx+lwa
http://lee90.blog.51cto.com/10414478/1833724

編輯samba的配置文件:
comment:註釋信息;
path:當前共享所映射的文件系統路徑;
browseable:是否可瀏覽,指是否可被用戶查看;
guest ok:是否允許來賓賬號訪問;
public:是否公開所有用戶;
writable:是否可寫;
write list:擁有寫權限的用戶列表;

vim /etc/samba/smb.conf

comment = wordpress 名稱
path = /data/application/web 要導出的路徑
browseable = yes
writable = yes
2.客戶端配置:
掛載:
mount -t cifs -o username=apachepassword=admin //172.16.35.1/wordpress /var/www/html

參考資料:
Samba服務共享的Linux和windows 參數介紹:
http://yuanbin.blog.51cto.com/363003/115761/
http://www.linuxidc.com/Linux/2015-05/117977.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章