Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1116666
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2010-08-25 15:30:00

RHCE学习笔记

 

关于linux下面配置samba服务的讨论,

 

Samba介绍

Samba的全名为Server Message Block(服务信息块)

Samba服务通常用来做文件服务器,它可以实现windowslinux之间的互相通信,实现不同操作系统之间的资源共享等等。samba底层也是CIFS协议(通用的Internet文件系统)

 

下面是关于samba这个服务的属性

Samba的相关软件包

Samba                         samba的软件包   

System-config-samba   samba的图形界面软件包

Samba-swat                 sambaweb界面管理软件包

Samba的守护进程

/usr/sbin/nmbd    /usr/sbin/smbd

Samba的脚本

/etc/init.d/smb

Samba的端口

Nmbd:137  138     smbd:  139  445

Samba的配置文件

/etc/smb*

 

关于samba的配置方法

Samba的配置方法有三种,

  配置samba的配置文件

/etc/samba.conf

  图形界面配置

System-config-samba

  web界面配置

 

下面主要讨论通过samba的配置文件来配置samba服务

第一步,安装软件包

[root@localhost ~]#

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

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Install Process

Resolving Dependencies

There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.

The program yum-complete-transaction is found in the yum-utils package.

--> Running transaction check

---> Package samba.i386 0:3.0.33-3.14.el5 set to be updated

--> Finished Dependency Resolution

 

Dependencies Resolved

 

================================================================================

 Package        Arch          Version                     Repository       Size

================================================================================

Installing:

 samba          i386          3.0.33-3.14.el5             Server           16 M

 

Transaction Summary

================================================================================

Install           1 Package(s)        

Update         0 Package(s)        

Remove        0 Package(s)         

 

Total download size: 16 M

Downloading Packages:

samba-3.0.33-3.14.el5.i386.rpm                           |  16 MB     00:00    

Running rpm_check_debug

Running Transaction Test

Finished Transaction Test

Transaction Test Succeeded

Running Transaction

  Installing     : samba                                                    1/1

 

Installed:

  samba.i386 0:3.0.33-3.14.el5                                                 

 

Complete!

[root@localhost ~]#

OKsamba服务就安装成功了,

 

第二步,了解samba的配置文件

/etc/samba.smb.conf

关于samba service的全局配置

[global]

Workgroup = MYGROUP

工作组的名字

Server  string = Samba Server Version %v

samba的描述信息

下面还有三个被注释的条目

;       netbios name = MYSERVER

 

;       interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24

;       hosts allow = 127. 192.168.12. 192.168.13.

注意,关于samba服务的IP的表示方法有点特殊

127.0.0.0写成127.

192.168.12.0写成192.168.12.

只需要写出网络位,主机位省略,然后以点介绍。

Samba的安全模式

        security = user

        passdb backend = tdbsam

在这里可以定义samba的安全模式

关于samba的五种安全模式

Share      允许匿名访问

User        需要用户名和密码认证,认证信息来自于samba服务本身

Domain   认证来自于网络中另一天计算机

Server     domain一样

Ads         使用微软的AD来认证

(Active  Dirctory  Server)

Samba里面的默认共享

#==========================ShareDefinitions======================

[homes]

        comment = Home Directories

        browseable = no

        writable = yes

;       valid users = %S

;       valid users = MYDOMAIN\%S

 

[printers]

        comment = All Printers

        path = /var/spool/samba

        browseable = no

        guest ok = no

        writable = no

        printable = yes

可以看到,我们的samba默认共享了两个文件夹,一个是home目录,一个是打印机。

关于samba服务参数的解释

[public]

共享目录共享出去的名字

Comment = Public

共享目录的描述信息

Path = /home/samba

共享目录的真正路径

Writable = no

默认情况下,所有用户对该共享目录都没有写的权限。

Write list = user1user2

只允许user1user2对该目录有写入的权限。

Browseable = yes

是否允许所有人浏览

Public = yes

允许匿名用户访问共享目录

Guest ok = yes

public = yes是一样的

Hosts allow = 192.168.0.

只允许192.168.0.这个网段的主机可以访问samba服务

Hosts deny = 192.168.0.10

拒绝192.168.0.10这台主机可以访问samba服务

Valid users = user1

仅允许user1对该目录有操作的权限,默认也只有r的权限。其他用户没有任何权限。

 

第三步,配置samba服务

案例:

1.      共享系统中/data目录,共享名为shared

2.      仅允许user1user2对该目录有写的权限,其他用户均为只读。

3.      允许所有用户可以浏览,拒绝匿名用户访问。

4.      仅允许192.168.0.0/24的网络可以访问该共享目录。

首先我们共享/data目录,共享名为shared

编辑samba的配置文件/etc/samba/smb.conf

[shared]

comment = shared

path = /data

重启下samba服务,

[root@localhost ~]#

[root@localhost ~]# service smb restart

Shutting down SMB services:                                [  OK  ]

Shutting down NMB services:                               [  OK  ]

Starting SMB services:                                          [  OK  ]

Starting NMB services:                                         [  OK  ]

[root@localhost ~]#

服务启动成功,测试下

[root@localhost ~]#

[root@localhost ~]# smbclient -L //192.168.0.254

Password:

Anonymous login successful

Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type       Comment

        ---------          ----      -------

        shared             Disk         shared

        IPC$               IPC         IPC Service (Samba Server Version 3.0.33-3.14.el5

Anonymous login successful

我们可以看到,的确有个共享名是shared的。

然后是仅允许user1user2对该目录有写的权限,其他人为只读。

编辑samba的配置文件/etc/samba/smb.conf

writable = no

write list = user1, user2

重启下samba服务,

[root@localhost ~]#

[root@localhost ~]# service smb restart

Shutting down SMB services:                                [  OK  ]

Shutting down NMB services:                               [  OK  ]

Starting SMB services:                                          [  OK  ]

Starting NMB services:                                         [  OK  ]

[root@localhost ~]#

服务启动成功,测试下

在测试之前,我们必须解析下selinux的问题,否则我们挂载不起来,user1也是写不进去的。

[root@localhost ~]#

[root@localhost ~]# chcon -R -t samba_share_t /data

[root@localhost ~]#

[root@localhost ~]# ll -ldZ /data/

drwxrwxrwx  root root root:object_r:samba_share_t      /data/

[root@localhost ~]#

我们必须将data目录的context值改成samba_share_t

[root@station10 ~]#

[root@station10 ~]# mount -t cifs //192.168.0.254/shared /mnt/ -o username=user1

Password:

[root@station10 ~]# df -h

Filesystem             Size      Used      Avail      Use%    Mounted on

/dev/sda3              3.9G      2.5G     1.2G      68%        /

/dev/sda6              494M    11M      458M    3%          /home

/dev/sda2              3.9G      135M    3.6G     4%          /var

/dev/sda1              99M      14M      81M     15%         /boot

tmpfs                    188M     0          188M     0%          /dev/shm

//192.168.0.254/shared

                            3.9G      2.7G      1.1G     72%        /mnt

[root@station10 ~]#

[root@localhost ~]#

[root@localhost ~]# cd /mnt/

[root@localhost mnt]# ls

[root@localhost mnt]# mkdir user1

mkdir: cannot create directory `user1': Permission denied

[root@localhost mnt]#

可以看到,user1还是不可以写入,权限拒绝。

现在我们还没有开启/data目录的权限。

[root@localhost ~]#

[root@localhost ~]# chmod 777 /data

[root@localhost ~]# ll -ld /data/

drwxrwxrwx 2 root root 4096 Mar 21 21:00    /data/

[root@localhost ~]#

现在我们再来测试下,

[root@localhost ~]#

[root@localhost ~]# cd /mnt/

[root@localhost mnt]# ls

[root@localhost mnt]# mkdir user1

[root@localhost mnt]# ls

user1

[root@localhost mnt]#

OK,现在user1就可以往/data里面写入东西了。

总结一下,如果想开放用户对目录有写入的权限,

1 开放目录本身的权限

2 开放samba的本身的控制权限

3 解决selinux问题,改变共享目录的context值。

然后是允许所有用户浏览,拒绝匿名用户访问,

编辑samba的配置文件/etc/samba/smb/conf

browseable = yes

public = no

重启下samba服务,

[root@localhost ~]#

[root@localhost ~]# service smb restart

Shutting down SMB services:                                 [  OK  ]

Shutting down NMB services:                                [  OK  ]

Starting SMB services:                                           [  OK  ]

Starting NMB services:                                          [  OK  ]

[root@localhost ~]#

服务启动成功,测试下

[root@localhost ~]#

[root@localhost ~]# smbclient -L //192.168.0.254

Password:

Anonymous login successful

Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type      Comment

        ---------          ----          -------

        IPC$                IPC          IPC Service (Samba Server Version 3.0.33-3.14.el5)

        shared              Disk         shared

Anonymous login successful

[root@localhost ~]# smbclient -L //192.168.0.254 -U user1

Password:

Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type      Comment

        ---------          ----      -------

        IPC$               IPC        IPC Service (Samba Server Version 3.0.33-3.14.el5)

        shared             Disk        shared

        user1              Disk         Home Directories

OK,可以看到,我们的匿名用户和user1都是可以浏览samba的共享目录的。

现在我们再来看下匿名用户是否可以访问呢?

[root@localhost ~]#

[root@localhost ~]# smbclient //192.168.0.254/shared

Password:

Anonymous login successful

Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

tree connect failed: NT_STATUS_ACCESS_DENIED

[root@localhost ~]#

[root@localhost ~]# smbclient //192.168.0.254/shared -U user1

Password:

Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

smb: \> ls

  .                                      D        0    Sun Mar 21 21:17:46 2010

  ..                                     D        0    Sun Mar 21 21:00:00 2010

  user1                               D          Sun Mar 21 21:17:46 2010

 

                63462 blocks of size 65536. 18276 blocks available

smb: \>

OK,可以看到,我们的匿名用户是不可以访问的,但是我们的user1是可以访问的。

默认情况下,匿名用户是不能够访问samba的共享目录的。

最后一个是仅允许192.168.0.0/24的网络访问该共享目录,

编辑samba的配置文件/etc/samba/smb/conf

hosts allow = 192.168.0.

也可以写成192.168.0.0/255.255.255.0

这样就只允许192.168.0.0/24位的网络来访问该共享目录,

其他网络的主机就不可以访问该共享目录的。

OKsamba案例配置就完成了。

 

关于samba用户的概念

.手动添加用户到samba数据库里

添加一个用户到samba数据库

#smbpasswd  -a  user1

[root@localhost ~]#

[root@localhost ~]# smbpasswd -a user1

New SMB password:

Retype new SMB password:

Added user user1.

[root@localhost ~]#

注意,samba用户必须是本地用户,必须将本地用户加入到samba数据库里面。

修改samba

#smbpasswd user1用户的密码

[root@localhost ~]#

[root@localhost ~]# smbpasswd user1

New SMB password:

Retype new SMB password:

[root@localhost ~]#

注意,一定是samba用户才可以使用smbpasswd命令来修改密码。

我们的samba用户密码存放在/etc/samba/passdb.tdb这个文件里面。

但是/etc/samba/passdb.tdb这个文件里面是乱码,

如何查看系统中有那些samba用户呢?

#tdbdump  passwd.tdb

[root@localhost ~]#

[root@localhost ~]# cd /etc/samba/

[root@localhost samba]# ls

lmhosts  passdb.tdb  secrets.tdb  smb.conf  smbusers

[root@localhost samba]# tdbdump passdb.tdb

{

key(13) = "RID_000007d0\00"

data(6) = "user1\00"

}

{

key(11) = "USER_user1\00"

data(203) = "\00\00\00\00\FF\FF\FF\7F\FF\FF\FF\7F\00\00\00\002w\A4K\00\00\00\00\FF\FF\FF\7F\06\00\00\00user1\00\0A\00\00\00LOCALHOST\00\01\00\00\00\00\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\00\01\00\00\00\00\00\00\00\00\01\00\00\00\00\D0\07\00\00\01\02\00\00\10\00\00\00W\0C\E3\99\DA\14\12\AB\AA\D3\B45\B5\14\04\EE\10\00\00\00\B9\D2\D4\95[3\0BP<\C7\92\EBjU\BB\1F\00\00\00\00\10\00\00\00\A8\00\15\00\00\00 \00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\EC\04\00\00"

}

{

key(13) = "INFO/version\00"

data(4) = "\03\00\00\00"

}

[root@localhost samba]#

虽然有点乱,但是也只能够这样看。

 

.使用虚拟用户

使用虚拟用户实际上就是给samba用户启一个别名,

但是这个用户一定要是samba数据库里面的用户,

虚拟用户是通过/etc/samba/smbusers这个文件来定义的,

[root@localhost ~]#

[root@localhost ~]# cd /etc/samba/

[root@localhost samba]# ls

lmhosts  passdb.tdb  secrets.tdb  smb.conf  smbusers

[root@localhost samba]# vim smbusers

# Unix_name = SMB_name1 SMB_name2 ...

root = administrator admin

nobody = guest pcguest smbguest

user1 = admins1

现在我们就给user1这个samba用户创建了一个别名admins1。现在我们可以通过admins1这个用户来访问samba了。

这样也可以保证samba服务的安全性。

 

.连接winbindd

我们的samba用户可以通过连接微软的活动目录来取得账号。

 

关于samba的语法检查工具

#testparm

[root@localhost ~]#

[root@localhost ~]# testparm

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

Processing section "[homes]"

Processing section "[printers]"

Loaded services file OK.

Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

 

[global]

        workgroup = MYGROUP

        server string = Samba Server Version %v

        passdb backend = tdbsam

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

        max log size = 50

        cups options = raw

 

[homes]

        comment = Home Directories

        read only =    No

        browseable = No

 

[printers]

        comment = All Printers

        path = /var/spool/samba

        printable = Yes

        browseable = No

[root@localhost ~]#

这个里面可以检查到samba的那些语法是生效了的。

还可以检查允许和拒绝的参数是否正确,

#testparm /etc/samba/smb.conf  station10.example.com 192.168.0.10

[root@localhost ~]#

[root@localhost~]#testparm/etc/samba/smb.conf station10.example.com 192.168.0.10

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

Processing section "[homes]"

Processing section "[printers]"

Loaded services file OK.

Server role: ROLE_STANDALONE

Allow connection from station10.example.com (192.168.0.10) to homes

Allow connection from station10.example.com (192.168.0.10) to printers

[root@localhost ~]#

可以看到,两个共享目录192.168.0.10都是允许访问的。

 

关于samba的客户端工具

浏览samba共享

#smbclient  -L  //ip address 

[root@localhost ~]#

[root@localhost ~]# smbclient -L //192.168.0.254

Password:

Anonymous login successful

Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type      Comment

        ---------         ----      -------

        IPC$              IPC        IPC Service (Samba Server Version 3.0.33-3.14.el5)

        shared            Disk        shared

Anonymous login successful

这样可以浏览到samba共享了那些目录。

这里也可以使用-U的参数来指定用户浏览。

访问samba共享

[root@localhost ~]#

[root@localhost ~]# smbclient //192.168.0.254/shared -U user1

Password:

Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

smb: \> ls

  .                                      D        0  Sun Mar 21 21:17:46 2010

  ..                                     D        0  Sun Mar 21 21:00:00 2010

  user1                               D        0  Sun Mar 21 21:17:46 2010

 

                63462 blocks of size 65536. 19116 blocks available

smb: \>

这样我们就是使用user1的身份来访问samba的共享目录,

在里面我们可以上传和下载,

Samba的挂载

#mount –t cifs //192.168.0.254/shared /mnt/ -o username=user1

[root@localhost ~]#

[root@localhost ~]# mount -t cifs //192.168.0.254/shared /mnt/ -o username=user1

Password:

[root@localhost ~]# df -h

Filesystem            Size       Used     Avail     Use%    Mounted on

/dev/sda3             3.9G      2.6G     1.2G      69%       /

/dev/sda6             494M    11M      458M     3%        /home

/dev/sda2             3.9G      116M    3.6G      4%         /var

/dev/sda1             99M      14M      81M      15%       /boot

tmpfs                   188M      0         188M     0%        /dev/shm

//192.168.0.254/shared

                           3.9G      2.6G      1.2G      69%       /mnt

[root@localhost ~]#

OK,这样就可以将samba的共享目录给挂载过来,很方便,

 

关于/etc/fstab文件中的samba挂载

Samba的挂载有点特别,是需要输入用户名和密码的,

所以系统也给了我们个解决的办法。

我们可以在/etc/samba/下面建立一个文件,

[root@localhost ~]#

[root@localhost ~]# cd /etc/samba/

[root@localhost samba]# vim cred.txt

username=user1

password=redhat

[root@localhost samba]# ls | grep cred.txt

cred.txt

[root@localhost samba]#

[root@localhost samba]# chmod 400 cred.txt

[root@localhost samba]#

然后将这个文件的权限改为400,只允许root用户可以看到,这个也是为了保证samba的安全。

最后在/etc/fstab里面添加这样的一行,

[root@localhost ~]# vim /etc/fstab

//192.168.0.254/shared    /mnt    cifs   credentials=/etc/samba/cred.txt  0  0

这样下次启动的时候就可以自动挂载了。

 

关于samba的子配置文件

现在我们有这样的一个需求,要求user1可以浏览shared这个共享目录,其他用户不可以浏览这个共享目录。

我们知道browseable=yes这个参数是针对所有的用户,它可以做到允许所有人浏览或者是不允许用户浏览,却不可以指定那些用户可以浏览,那些用户不可以了浏览,如果想实现这个功能,就必须使用samba的子配置文件了。

可以在samba的配置文件/etc/samba/smb.conf的全局配置中加入,

Config  file  =  /etc/samba/%U.conf

samba的配置文件中的全局部分加入这样的一行就意味着用户就不会读取/etc/samba/smb.conf中下面的内容了,而直接读取/etc/samba/%U.conf这个文件。

现在我们来定义/etc/samba.%U.conf这个文件。

[root@localhost ~]#

[root@localhost ~]# cp smb.conf user1.conf

首先我们复制一下模板文件,然后在/etc/samba/user1.conf中定义browseable=yes

重启下samba服务,

[root@localhost ~]#

[root@localhost ~]# service smb restart

Shutting down SMB services:                                [  OK  ]

Shutting down NMB services:                               [  OK  ]

Starting SMB services:                                          [  OK  ]

Starting NMB services:                                         [  OK  ]

[root@localhost ~]#

服务启动成功,测试下

[root@localhost ~]#

[root@localhost ~]# smbclient -L //192.168.0.254 -U user2

Password:

Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type      Comment

        ---------          ----      -------

        IPC$               IPC        IPC Service (Samba Server Version 3.0.33-3.14.el5)

        user2               Disk        Home Directories

[root@localhost ~]#

可以看到,user2是不可以浏览到的shared共享目录的。

[root@localhost ~]# smbclient -L //192.168.0.254 -U user1

Password:

Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

 

        Sharename       Type      Comment

        ---------         ----         -------

        IPC$             IPC          IPC Service (Samba Server Version 3.0.33-3.14.el5)

        shared           Disk         shared

        user1             Disk         Home Directories

[root@localhost ~]#

OK,可以看到,user1可以浏览到shared共享目录的。

这个就是samba的子配置文件的作用,

user1读取到/etc/samba/smb.conf中定义的config file=%U.conf,那么usre1就会去读取/etc/samba/user1.conf这个文件,而不会在读取下面的内容。在/etc/samba/user1.conf中定义了browseable=yes,所有我们的user1就可以浏览到共享目录了。

Samba的子配置文件功能非常的强大,不光可以对用户做限制,还可以对组做限制,我们还可以针对每一个用户做一个子配置文件,如果这样的话,那么功能就太强大了。Samba的子配置文件不仅仅可以放在全局部分,也可以针对每个共享目录来做设置的。

Samba的子配置文件可以非常灵活的运用。

 

关于linux下面samba的基本配置就是这么多了。

阅读(532) | 评论(0) | 转发(1) |
0

上一篇:RHEL5.4管理SELinux

下一篇:RHEL5.4配置FTP服务

给主人留下些什么吧!~~