文件共享之samba


一、samba基礎知識介紹

samba:

工作於會話層,不是真正應用層,是NAS的解決方案之一(nfs也是),爲局域網內的linux計算機與其他計算機之間提供文件及打印機等資源的共享服務。linux或unix安裝了samba後就可以基於CIFS協議進行文件交互。

smb: Service Message Block  服務信息塊,windows上文件共享的模式

CIFS: Common Internet File System 通用網絡文件系統,samba所遵循的協議,是公共的或開放的SMB協議版本。


windows的共享

NetBIOS:網絡基本輸入輸出系統,Windows基於主機實現互相通信的機制;最長15個字符

使用的模型有兩種:

    ① 工作組模型

    ② 域模型 : 將所有共享資源統合在一個編錄中輸出出來,編錄可隨着資源的變化而不斷改變,動態更新,形成一個活動目錄ad。

linux要加入到這個域中,就必須實現ldap協議,並結合cobbler功能才能成爲域中的一個成員。

活動目錄ad:統合了windows共享域中的所有資源,動態更新,直接訪問ad就能使用各共享


samba可以實現以下三種服務:

① nmbd: 實現netbios協議,提供netbios名稱解析,以便跟windows兼容

② smbd: 實現cifs協議,完成文件共享

③ winbindd: 讓linux能加入windows的域ad(活動目錄)中去,因爲windows不一定創建了域,所以samba默認只啓動前兩者,winbindd不自動啓動


CIFS監聽端口:   137/udp, 138/udp

  139/tcp, 445/tcp

137/udp, 138/udp: 用於實現NetBIOS的

samba現在有3版和4版

三版流行一些,4版功能更強大

我們一般使用只需要兩個程序包: 客戶端和服務器端

wKiom1VR7GHRypbjAAV3WJhy5c8411.jpg


服務腳本:

/etc/rc.d/init.d/nmb

/etc/rc.d/init.d/smb

主配置文件:

/etc/samba/smb.conf

最重要的應用程序:

/usr/sbin/nmbd

/usr/sbin/smbd

/var/spool/samba

/etc/pam.d/samba

samba用戶:

賬號:都是系統用戶, /etc/passwd中必須有這個用戶名

密碼:samba服務自有密碼文件, 


將系統用戶添加爲samba的命令:smbpasswd

samba用戶的創建過程:

先創建系統用戶,然後用smbpasswd命令將其添加爲samba用戶

# smbpasswd    [option]    USERNAME

-a Sys_User  :  添加系統用戶爲samba用戶

-d  : disable 禁用

-e    :  enable 啓用

-x    :  刪除


配置文件:

/etc/samba/smb.conf

全局設定

特定共享的設定

私有家目錄:其他用戶不能訪問

打印機共享

自定義共享:可以定義別的用戶也能訪問的目錄

wKioL1VR7n6zR01QAAOsK4UxXSw177.jpg

wKiom1VR7Q-j2xKbAAMWThu1KQI618.jpg

wKioL1VR7pewViCcAAInZJRGSV4210.jpg

二、作爲客戶端訪問windows上的共享資源

1、在windows上添加一個新賬戶,名字叫爲test,密碼爲123456

2、對要共享的文件夾配置好

wKiom1VR7VKQerhHAANGFz_C_ZA046.jpg

3、在linux虛擬機上訪問windows共享的文件夾

    3.1 先安裝samba客戶端

  

[root@kingdom etc]# yum install -y samba-client
  samba-client-3.6.23-12.el6.x86_64


wKioL1VR7wSRhp9ZAAKHXUpxZKU622.jpg

    3.2 訪問windows共享的文件夾

  訪問方式有兩種:

1、交互式數據訪問:

① 先獲取到共享信息

# smbclient -L HOST -U USERNAME

-L, --list=HOST   :    Get a list of shares available on a host

wKiom1VR7anDPJb7AALr57lzD8I523.jpg

② 連接到共享目錄訪問


# smbclint //SERVER/shared_name -U USERNAME

wKioL1VR713AMYqTAAZj7qHdVEE015.jpg

wKiom1VR7euCVSzPAAM96YIkK64959.jpg



2、基於掛載的方式訪問:

# mount -t cifs //SERVER/shared_name  /mount_point -o username=USERNAME,password=PASSWORD

以指定用戶USERNAME,密碼PASSWORD掛載//SERVER/shared_name

也可不寫密碼,只寫USERNAME,回車後再輸入密碼。

# mount -t cifs //SERVER/shared_name  /mount_point -o username=USERNAME

因爲需要輸入密碼才能掛載,所以不建議開機自動掛載。

wKioL1VR75rTPsnXAAOq9kXNaV4877.jpg


三、作爲服務器端共享本機上的文件目錄:


目標:

建立samba共享,共享目錄爲/data,要求:

1)共享名爲shared,工作組爲smbgroup;

2)添加組develop,添加用戶gentoo,centos和ubuntu,其中gentoo和centos以develop爲附加組,ubuntu不屬於develop組;密碼均爲用戶名;

3)添加samba用戶gentoo,centos和ubuntu,密碼均爲“123456”;

4)此samba共享shared僅允許develop組具有寫權限,其他用戶只能以只讀方式訪問;

5)此samba共享服務僅允許來自於172.16.0.0/16網絡的主機訪問;

(一)、安裝服務器端程序包並啓動服務:

[root@kingdom etc]# rpm -q samba
package samba is not installed
[root@kingdom etc]# yum install -y samba
Installed:
  samba.x86_64 0:3.6.23-12.el6                                                         
Complete!


samba安裝後可以直接啓動,不需要做什麼修改

[root@kingdom etc]# service smb start; service nmb start
Starting SMB services:                                     [  OK  ]
Starting NMB services:                                     [  OK  ]


wKiom1VR7kyx_jIzAAO28XrRsZ0552.jpg

(二)、驗證共享是否可行

[root@kingdom samba]# vim smb.conf
workgroup = WORKGROUP
netbios name = shared


        工作組設置爲WORKGROUP

添加smb登錄帳號smbuser1並設置登錄密碼123456

[root@kingdom nfs]# useradd smbuser1
[root@kingdom nfs]# smbpasswd -a smbuser1
New SMB password:
Retype new SMB password:
Added user smbuser1.
[root@kingdom samba]# service smb restart ; service nmb restart 
Shutting down SMB services:                                [  OK  ]
Starting SMB services:                                     [  OK  ]
Shutting down NMB services:                                [  OK  ]
Starting NMB services:                                     [  OK  ]

windows的訪問路徑是 UNC路徑 :\\ SERVER\shared_name

wKioL1VR8RiRocHiAAFcBYsoEUI967.jpg

(三)、設置共享文件目錄

1、目錄創建

        

[root@kingdom /]# mkdir /data


        2、 用戶創建及設置

添加組develop,添加用戶gentoo,centos和ubuntu,其中gentoo和centos以develop爲附加組,ubuntu不屬於develop組;密碼均爲用戶名;

[root@kingdom /]# groupadd develop
[root@kingdom /]# useradd -G develop gentoo
[root@kingdom /]# useradd -G develop centos
[root@kingdom /]# passwd gentoo
Changing password for user gentoo.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@kingdom /]# passwd centos
Changing password for user centos.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@kingdom /]# useradd ubuntu
[root@kingdom /]# passwd ubuntu
Changing password for user ubuntu.
New password: 
BAD PASSWORD: it does not contain enough DIFFERENT characters
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@kingdom /]# id gentoo ; id centos ; id ubuntu
uid=501(gentoo) gid=502(gentoo) groups=502(gentoo),501(develop)
uid=502(centos) gid=503(centos) groups=503(centos),501(develop)
uid=503(ubuntu) gid=504(ubuntu) groups=504(ubuntu)
添加samba用戶gentoo,centos和ubuntu,密碼均爲“123456”;
[root@kingdom /]# smbpasswd -a gentoo 
New SMB password:
Retype new SMB password:
Added user gentoo.
[root@kingdom /]# smbpasswd -a centos
New SMB password:
Retype new SMB password:
Added user centos.
[root@kingdom /]# smbpasswd -a ubuntu
New SMB password:
Retype new SMB password:
Added user ubuntu.


        3、自定義共享share:

[root@kingdom /]# vim /etc/samba/smb.conf

修改global配置:

workgroup = SMBGROUP
netbios name = SMBSERVER
hosts allow = 172.16.

在末尾添加如下行:

[share]
comment = smb shared test
path = /data
write list = +develop
read only = No
guest ok = Yes

測試是否有語法錯誤:

[root@kingdom /]# 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 "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
[global]
workgroup = SMBGROUP
netbios name = SMBSERVER
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
idmap config * : backend = tdb
hosts allow = 172.16.
cups options = raw
……
[share]
comment = smb shared test
path = /data
write list = +develop
read only = No
guest ok = Yes


        4、語法測試並重啓服務

        測試配置文件是否有語法錯誤,以及顯示最終生效的配置:

# testparm     (testparameter的簡寫)

        重啓服務,是配置生效:

[root@kingdom /]# service smb restart ; service nmb restart
Shutting down SMB services:                                [  OK  ]
Starting SMB services:                                     [  OK  ]
Shutting down NMB services:                                [  OK  ]
Starting NMB services:                                     [  OK  ]


5、配置/data目錄的文件系統權限

最終是否有寫權限還需要文件系統權限開放

共享權限 + 文件系統權限的交集 = 最終權限

[root@kingdom /]# ll -d /data
drwxr-xr-x 2 root root 4096 May 11 15:43 /data
[root@kingdom /]# chmod 755 /data

(從ll命令結果看,其實這裏可以不用使用chmod命令,但如果需要用,則必須在使用setfacl之前使用)

[root@kingdom /]# setfacl -m g:develop:rwx /data
[root@kingdom /]# getfacl /data
getfacl: Removing leading '/' from absolute path names
# file: data
# owner: root
# group: root
user::rwx
group::r-x
group:develop:rwx
mask::rwx
other::r-x

chmod和setfacl前後使用順序說明:

先setfacl再chmod,則直接起作用時是 #effective:r-x,develop也不具備寫權限。


[root@kingdom ~]# getfacl /etc/
getfacl: Removing leading '/' from absolute path names
# file: etc/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@kingdom ~]# chmod 755 /data/
[root@kingdom ~]# getfacl /data/
getfacl: Removing leading '/' from absolute path names
# file: data/
# owner: root
# group: root
user::rwx
group::r-x
group:develop:rwx       #effective:r-x
mask::r-x
other::r-x


6、最終效果測試:

三個用戶gentoo、centos、ubuntu都能查看到共享情況

wKioL1VR7_PBsUysAAJt6rBVShI784.jpg

通過另一臺虛擬機遠程登錄的情況:

        gentoo和centos都能登錄,有讀寫權限


交互式訪問:

[root@aunt-s ~]# smbclient //172.16.20.150/share -U gentoo
Enter gentoo's password: 
Domain=[SMBGROUP] OS=[Unix] Server=[Samba 3.6.23-12.el6]
smb: \> ls
  .                                   D        0  Mon May 11 16:16:23 2015
  ..                                 DR        0  Mon May 11 14:48:59 2015
  gentoo                              D        0  Mon May 11 16:16:23 2015
  fstab                                      966  Mon May 11 15:43:20 2015
  welcom.txt                                  27  Mon May 11 15:43:08 2015
40061 blocks of size 524288. 35549 blocks available
smb: \gentoo\> pwd
Current directory is \\172.16.20.150\share\gentoo\
smb: \> mkdir gentoo   能創建目錄
smb: \> put /tmp/smbsharetesting.txt   不能直接上傳帶路徑基名的文件
NT_STATUS_OBJECT_PATH_NOT_FOUND opening remote file \/tmp/smbsharetesting.txt
smb: \> lcd /tmp/
smb: \> put smbsharetesting.txt  能上傳不帶路徑基名的文件
putting file smbsharetesting.txt as \smbsharetesting.txt (10.7 kb/s) (average 10.7 kb/s)
smb: \> ls
  .                                   D        0  Mon May 11 16:26:17 2015
  ..                                 DR        0  Mon May 11 14:48:59 2015
  gentoo                              D        0  Mon May 11 16:16:23 2015
  fstab                                      966  Mon May 11 15:43:20 2015
  smbsharetesting.txt                 A       11  Mon May 11 16:26:17 2015
  welcom.txt                                  27  Mon May 11 15:43:08 2015
40061 blocks of size 524288. 35549 blocks available
smb: \> q
[root@aunt-s ~]#


掛載式訪問:

[root@aunt-s ~]# mount -t cifs //172.16.20.150/share /mnt -o username=gentoo
Password: 
[root@aunt-s ~]# mount | grep share
//172.16.20.150/share on /mnt type cifs (rw)
[root@aunt-s ~]# ls /mnt
fstab  gentoo  smbsharetesting.txt  welcom.txt
[root@aunt-s mnt]# touch /mnt/gentoo.txt
[root@aunt-s mnt]# ll
total 12
-rw-r--r-- 1 root   root 966 May 11 15:43 fstab
drwxr-xr-x 2 centos  502   0 May 11 16:16 gentoo
-rw-r--r-- 1 centos  502   0 May 11 18:24 gentoo.txt
-rwxr--r-- 1 centos  502  11 May 11 16:26 smbsharetesting.txt
-rw-r--r-- 1 root   root  27 May 11 15:43 welcom.txt

ubuntu也能交互式登錄和掛載式登錄,但沒有寫權限,只有讀權限

[root@aunt-s ~]# smbclient //172.16.20.150/share -U ubuntu
Enter ubuntu's password: 
Domain=[SMBGROUP] OS=[Unix] Server=[Samba 3.6.23-12.el6]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
smb: \> ls
  .                                   D        0  Mon May 11 16:26:17 2015
  ..                                 DR        0  Mon May 11 14:48:59 2015
  gentoo                              D        0  Mon May 11 16:16:23 2015
  fstab                                      966  Mon May 11 15:43:20 2015
  smbsharetesting.txt                 A       11  Mon May 11 16:26:17 2015
  welcom.txt                                  27  Mon May 11 15:43:08 2015
40061 blocks of size 524288. 35549 blocks available
smb: \> lcd /tmp/
smb: \> put ubuntu.txt 
NT_STATUS_ACCESS_DENIED opening remote file \ubuntu.txt
smb: \> mkdir ubuntu
NT_STATUS_ACCESS_DENIED making remote directory \ubuntu
smb: \> q
[root@aunt-s ~]#


[root@aunt-s ~]# mount -t cifs //172.16.20.150/share /mnt -o username=ubuntu
Password: 
[root@aunt-s ~]# mount | grep share
//172.16.20.150/share on /mnt type cifs (rw)
[root@aunt-s ~]# ls /mnt
fstab  gentoo  smbsharetesting.txt  welcom.txt
[root@aunt-s mnt]# touch ubuntu.txt
touch: cannot touch `ubuntu.txt': Permission denied
[root@aunt-s mnt]# mkdir ubuntu
mkdir: cannot create directory `ubuntu': Permission denied
[root@aunt-s mnt]# cp /etc/fstab /mnt/
cp: overwrite `/mnt/fstab'? y
cp: cannot create regular file `/mnt/fstab': Permission denied
[root@aunt-s mnt]#



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