文件共享之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]#



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