Linux下资源共享服务之samba 的讲解!

简介:

一、Samba 简介:

  Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。

samba是一个工具套件,在Unix上实现SMB(Server Message Block)协议,或者称之为NETBIOS/LanManager协议。SMB协议通常是被windows系列用来实现磁盘和打印机共享。需要注意的是,NetBIOS是基于以太网广播机制的,没有透明网桥是不能跨越网段的,也许用WINS和LMHOSTS可以,但我没试过。我感觉samba是把SMB绑定到TCP/IP上实现的,samba只在IP子网内广播(很多时候我不得不指定IP地址:-()。所以在win95上与samba通讯既要装NetBEUI协议,也要装tcp/ip协议

SMB应用功能:

由上面说明的 SAMBA 发展缘由,可以看出, SAMBA 最初发展的主要目就是要用来沟通Windows 与 Unix Like 这两个不同的作业平台。最大的好处就是不必让同样的一份数据放置在不同的地方,搞到后来都不晓得哪一份资料是最新的!而且也可以透过这样的一个档案系统上Linux 与 Windows 的档案传输变得更为简单!也就是说,可以透过『网络上的芳邻』来进行Linux 与 Windows 档案的传输。那么 SAMBA 可以进行哪些动作呢?

①分享档案与打印机服务;

②提供使用者登入 SAMBA 主机时的身份认证,以提供不同身份者的个别数据;

③进行 Windows 网络上的主机名称解析 (NetBIOS name)

④进行装置的分享 ( 例如 Zip, CDROM... )

两个守护程序:smbd 和 nmbd(对客户端提供NetBIOS名服务)

二、内容讲解:

1.挂载光盘,安装samba服务:

[root@gjp99 ~]# mkdir /mnt/cdrom 
[root@gjp99 ~]# mount /dev/cdrom /mnt/cdrom 
mount: block device /dev/cdrom is write-protected, mounting read-only 
[root@gjp99 ~]# cd /mnt/cdrom/Server/ 
[root@gjp99 Server]# ll samba* 
-r--r--r-- 86 root root 17012467 Jul 23  2009 samba-3.0.33-3.14.el5.i386.rpm 
-r--r--r-- 86 root root  5930356 Jul 23  2009 samba-client-3.0.33-3.14.el5.i386.rpm 
-r--r--r-- 99 root root  9094678 Jul 23  2009 samba-common-3.0.33-3.14.el5.i386.rpm 
-r--r--r-- 86 root root  8628593 Jul 23  2009 samba-swat-3.0.33-3.14.el5.i386.rpm

服务器端:samba-3.0.33-3.14.el5.i386.rpm  samba-common-3.0.33-3.14.el5.i386.rpm 
客户端:samba-client-3.0.33-3.14.el5.i386.rpm 
通过web页面管理 :samba-swat-3.0.33-3.14.el5.i386.rpm

[root@gjp99 Server]# rpm -qa |grep samba  查询已安装过的软件包 
samba-common-3.0.33-3.14.el5 
samba-client-3.0.33-3.14.el5

由于samba依赖perl-Convert-ASN1-0.20-1.1.noarch.rpm 
[root@gjp99 Server]# rpm -ivh perl-Convert-ASN1-0.20-1.1.noarch.rpm

warning: perl-Convert-ASN1-0.20-1.1.noarch.rpm: Header V3 DSA signature: NOKEY, key ID 37017186 
Preparing...                ########################################### [100%] 
   1:perl-Convert-ASN1      ########################################### [100%] 
[root@gjp99 Server]# rpm -ivh samba-3.0.33-3.14.el5.i386.rpm 
warning: samba-3.0.33-3.14.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186 
Preparing...                ########################################### [100%] 
   1:samba                  ########################################### [100%]

[root@gjp99 Server]# rpm -ql samba |less

/etc/pam.d/samba    支持pam验证 
/etc/rc.d/init.d/smb   控制脚本

samba的账号库:  /usr/bin/smbpasswd   与本地账号库不同

主配置文档    /etc/samba/smb.conf 

image

[root@gjp99 Server]# netstat -tupln |grep smb 
tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN      19547/smbd          
tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      19547/smbd          
[root@gjp99 Server]# netstat -tupln |grep nmb 
udp        0      0 192.168.10.99:137           0.0.0.0:*                               19550/nmbd          
udp        0      0 0.0.0.0:137                 0.0.0.0:*                               19550/nmbd          
udp        0      0 192.168.10.99:138           0.0.0.0:*                               19550/nmbd          
udp        0      0 0.0.0.0:138                 0.0.0.0:*                               19550/nmbd

2. windows 客户机访问samba服务器

windows xp 配置:

image

image

访问方式:

<1.【网上邻居】---搜索计算机

image

<2. 网上邻居

<3. unc路径   \\对方地址或名字\共享名字

image image

<4. 映射网络驱动器

命令行下  net use       查看 net share

3.编辑主配置文档

以防配置文件误改或者不小心删除,则首先备份一份!

[root@gjp99 Server]# cp /etc/samba/smb.conf  /etc/samba/smb.conf.bak

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

image 0

意思:访问eth0的任意一个地址,都可以访问到eth0

测试:配辅助地址

[root@gjp99 Server]# ifconfig eth0:0 192.168.10.98

image

出于安全考虑,这样是不安全的

增加此行,限制仅能用192.168.10.99/24的访问

80         interfaces = lo  192.168.10.99/24

image 

测试结果发现:192.168.10.98  和 192.168.10.98  都能访问,说明设置未生效

[root@gjp99 Server]# man 5 smb.conf   //查看配置文档

image

image

image

image

[root@gjp99 Server]# service smb restart 
Shutting down SMB services:                                [  OK  ] 
Shutting down NMB services:                                [  OK  ] 
Starting SMB services:                                     [  OK  ] 
Starting NMB services:                                     [  OK  ]

image 

发现访问192.168.10.98 已访问不到,达到我们期望的效果!

image

目前达到只要能ping通192.168.10.99 ,就能够访问其samba

用windows 7 访问:

image

image

测试结果证明

192.168.10.31 已无法访问!仅能192.168.10.1 能够访问!

image

image

刚才不能访问的ip,已可以成功访问!

  image

[root@gjp99 ~]# ll /var/log/samba/    发现没有访问日志 
total 12 
drwx------ 4 root root 4096 Aug  3 14:09 cores 
-rw-r--r-- 1 root root 1450 Aug  3 15:09 nmbd.log 
-rw-r--r-- 1 root root 3454 Aug  3 15:38 smbd.log 
[root@gjp99 ~]# vim /etc/samba/smb.conf

image

[root@gjp99 ~]# service smb restart 
Shutting down SMB services:                                [  OK  ] 
Shutting down NMB services:                                [  OK  ] 
Starting SMB services:                                     [  OK  ] 
Starting NMB services:                                     [  OK  ] 
[root@gjp99 ~]# ll /var/log/samba/    还没有日志文件 
total 12 
drwx------ 4 root root 4096 Aug  3 14:09 cores 
-rw-r--r-- 1 root root 1688 Aug  3 16:27 nmbd.log 
-rw-r--r-- 1 root root 3600 Aug  3 16:27 smbd.log

说明需要访问下,才能够产生!

image

虽然没进去,说明已经访问了!

[root@gjp99 ~]# ll /var/log/samba/ 
total 16 
-rw-r--r-- 1 root root    0 Aug  3 16:29 192.168.10.1.log   访问时以客户端的ip为名字进行记录 
drwx------ 4 root root 4096 Aug  3 14:09 cores 
-rw-r--r-- 1 root root 1688 Aug  3 16:27 nmbd.log 
-rw-r--r-- 1 root root 3600 Aug  3 16:27 smbd.log 
-rw-r--r-- 1 root root  151 Aug  3 16:29 xz-20120711gzjd.log   和以客户端的计算机名称进行记录

[root@gjp99 ~]# tail -f /var/log/samba/smbd.log 
  Denied connection from  (192.168.10.31) 
[2012/08/03 15:38:14, 1] smbd/process.c:process_smb(1062) 
  Connection denied from 192.168.10.31 
[2012/08/03 15:38:14, 0] lib/access.c:check_access(327) 
  Denied connection from  (192.168.10.31) 
[2012/08/03 15:38:14, 1] smbd/process.c:process_smb(1062) 
  Connection denied from 192.168.10.31 
[2012/08/03 16:27:32, 0] smbd/server.c:main(944) 
  smbd version 3.0.33-3.14.el5 started. 
  Copyright Andrew Tridgell and the Samba Team 1992-2008

samba服务器的安全级别 
share  可匿名访问 
user   身份验证  samba帐号库 【本地】 
server  身份验证  samba帐号库 【其他服务器】   
domain 【ads活动目录数据库】  身份验证   dc

image

[root@gjp99 ~]# service smb restart 
Shutting down SMB services:                                [  OK  ] 
Shutting down NMB services:                                [  OK  ] 
Starting SMB services:                                     [  OK  ] 
Starting NMB services:                                     [  OK  ]

测试:

image

image

image

由于开启了这行,所以才会一旦进去,就能够看到打印机和传真!

三、案例: 

1.要求:

共享名       物理路径            权限 
public        /pub                任何人 ro 
smbgjp      /gjp                 gjp    rw 
smblzw      /lzw                  lzw     rw

2.准备知识:
本地权限    网络权限 
相同      相同 
不同      最小 
ro         ro   ---》ro 
rw         ro  ---》ro

3.具体配置:

[root@gjp99 ~]# mkdir /pub /gjp /lzw 
[root@gjp99 ~]# ll -d /pub /gjp /lzw 
drwxr-xr-x 2 root root 4096 Aug  3 17:22 /gjp 
drwxr-xr-x 2 root root 4096 Aug  3 17:22 /lzw 
drwxr-xr-x 2 root root 4096 Aug  3 17:22 /pub

创建账号,可以不设置密码,因为访问samba使用的是samba账号! 
[root@gjp99 ~]# useradd gjp 
[root@gjp99 ~]# useradd lzw

必须有本地账号,才会有samba账号!

[root@gjp99 ~]# chown gjp.gjp /gjp 
[root@gjp99 ~]# chown lzw.lzw /lzw 
[root@gjp99 ~]# ll -d /pub /gjp /lzw 
drwxr-xr-x 2 gjp  gjp  4096 Aug  3 17:22 /gjp 
drwxr-xr-x 2 lzw  lzw  4096 Aug  3 17:22 /lzw 
drwxr-xr-x 2 root root 4096 Aug  3 17:22 /pub

为了访问时有提示信息,所以在每个目录中设置了相应的文件

[root@gjp99 ~]# cd /gjp 
[root@gjp99 gjp]# touch f1.gjp 
[root@gjp99 gjp]# cd /lzw 
[root@gjp99 lzw]# touch f1.lzw 
[root@gjp99 lzw]# cd /pub 
[root@gjp99 pub]# touch f1.public

samba 账号的创建:

[root@gjp99 ~]# vim /etc/samba/smb.conf 
265 [public] 
266         comment = public dir

267         path = /pub 
268         browseable = yes 
269         guest ok = yes 
270         writable = no 
271         printable = yes 
272 [smbgjp]273         comment = gjp share  dir 
274         path = /gjp 
275         browseable = yes 
276         guest ok = no 
277         valid users = gjp 
278         writable = yes 
280 [smblzw]281         comment = lzw share  dir 
282         path = /lzw 
283         browseable = yes 
284         guest ok = no 
285         valid users = lzw 
286         writable = yes

image

必须事先创建好本地账号,才能创建smb账号!

[root@gjp99 pub]# smbpasswd -a gjp  
New SMB password:输入密码:123 
Retype new SMB password: 
Added user gjp. 
[root@gjp99 pub]# smbpasswd -a lzw 
New SMB password: 
Retype new SMB password: 
Added user lzw. 
[root@gjp99 pub]# service smb restart 
Shutting down SMB services:                                [  OK  ] 
Shutting down NMB services:                                [  OK  ] 
Starting SMB services:                                     [  OK  ] 
Starting NMB services:                                     [  OK  ]

用一个账号访问过之后,再次访问时,最好注销下!

image

image

image 

用自己的账号登录进去,rw权限可以实现!属于自己的私有空间!可以进行任意操作!

用lzw账号登录:

image image

image

把smb.conf 文件中public中的打印机那行去掉!

[root@gjp99 pub]# service smb restart

在用lzw账号登录:(查看public有什么不同)

image

image

 4.用linux系统作为samba客户端进行访问:

[root@mail ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F9:1C:6F  
          inet addr:192.168.10.2  Bcast:192.168.10.255  Mask:255.255.255.0

[root@mail ~]# ping 192.168.10.99 
PING 192.168.10.99 (192.168.10.99) 56(84) bytes of data. 
64 bytes from 192.168.10.99: icmp_seq=1 ttl=64 time=2.34 ms 
64 bytes from 192.168.10.99: icmp_seq=2 ttl=64 time=0.394 ms

查看是否安装了smb客户端:

[root@mail ~]# rpm -qa |grep samba 
samba-common-3.0.33-3.14.el5 
samba-client-3.0.33-3.14.el5

用账号gjp登录测试: 
[root@mail ~]# smbclient -L \\192.168.10.99 -U gjp 
Password: 
Domain=[GJP99] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

    Sharename       Type      Comment 
    ---------       ----      ------- 
    public          Disk      public dir 
    smbgjp          Disk      gjp share  dir 
    smblzw          Disk      lzw share  dir 
    IPC$            IPC       IPC Service (Samba Server Version 3.0.33-3.14.el5) 
    gjp             Disk      Home Directories 
Domain=[GJP99] OS=[Unix] Server=[Samba 3.0.33-3.14.el5]

    Server               Comment 
    ---------            -------

    Workgroup            Master 
    ---------            ------- 
    MYGROUP              GJP99 
    WORKGROUP            PC-201203120919

[root@mail ~]# smbclient //192.168.10.99/smbgjp -U gjp 
Password: 
Domain=[GJP99] OS=[Unix] Server=[Samba 3.0.33-3.14.el5] 
smb: \> dir 
  .                                   D        0  Fri Aug  3 17:58:40 2012 
  ..                                  D        0  Fri Aug  3 17:22:35 2012 
  f1.gjp                                       0  Fri Aug  3 17:31:46 2012

        38752 blocks of size 262144. 27325 blocks available 
smb: \> ?      //下面都是ftp指令 
?              altname        archive        blocksize      cancel         
case_sensitive cd             chmod          chown          close          
del            dir            du             exit           get            
getfacl        hardlink       help           history        lcd            
link           lock           lowercase      ls             mask           
md             mget           mkdir          more           mput           
newer          open           posix          posix_open     posix_mkdir    
posix_rmdir    posix_unlink   print          prompt         put            
pwd            q              queue          quit           rd             
recurse        reget          rename         reput          rm             
rmdir          showacls       setmode        stat           symlink        
tar            tarmode        translate      unlock         volume         
vuid           wdel           logon          listconnect    showconnect    
!             

smb: \> dir   显示远程 
  .                                   D        0  Fri Aug  3 17:58:40 2012 
  ..                                  D        0  Fri Aug  3 17:22:35 2012 
  f1.gjp                                       0  Fri Aug  3 17:31:46 2012

        38752 blocks of size 262144. 27325 blocks available 
smb: \> !dir   显示本地 
Desktop                     extmail-1.2.tar.gz 
Maildir                     extman-1.1.tar.gz 
Unix-Syslog-1.1                 install.log 
Unix-Syslog-1.1.tar.gz             install.log.syslog 
anaconda-ks.cfg                 mbox 
courier-authlib-0.63.1.20111230.tar.bz2  postfix-2.8.2.tar.gz 
smb: \> put mbox    上传文件 
putting file mbox as \mbox (28.5 kb/s) (average 28.5 kb/s) 
smb: \> dir 
  .                                   D        0  Fri Aug  3 18:44:04 2012 
  ..                                  D        0  Fri Aug  3 17:22:35 2012 
  f1.gjp                                       0  Fri Aug  3 17:31:46 2012 
  mbox                                A     1399  Fri Aug  3 18:44:04 2012

        38752 blocks of size 262144. 27325 blocks available

smb: \> mget f1*   批量下载文件 
Get file f1.gjp? y 
getting file \f1.gjp of size 0 as f1.gjp (0.0 kb/s) (average 0.0 kb/s) 
smb: \> !dir 
Desktop                     extman-1.1.tar.gz 
Maildir                     f1.gjp 
Unix-Syslog-1.1                 install.log 
Unix-Syslog-1.1.tar.gz             install.log.syslog 
anaconda-ks.cfg                 mbox 
courier-authlib-0.63.1.20111230.tar.bz2  postfix-2.8.2.tar.gz 
extmail-1.2.tar.gz 
smb: \> quit   退出

[root@mail ~]# mount -t cifs //192.168.10.99/smbgjp /mnt/smb -o username=gjp%123 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
//192.168.10.99/smbgjp on /mnt/smb type cifs (rw,mand)挂载成功

[root@mail ~]# umount /mnt/smb 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
[root@mail ~]# vim /etc/fstab

增加此行:

//192.168.10.99/smbgjp  /mnt/smb                cifs    defaults,username=gjp%123 0 0

[root@mail ~]# mount -a 
[root@mail ~]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
//192.168.10.99/smbgjp on /mnt/smb type cifs (rw,mand)

可以实现,但是这样不安全,如果有人查看/etc/fstab,则能够看到用户名和密码!

把密码放到一个文件中:

//192.168.10.99/smbgjp  /mnt/smb             cifs    defaults,credentials=/etc/samba/f1 0 0

[root@mail samba]# vim f1

username=gjp 
passwd=123

[root@mail samba]# mount -a 
[root@mail samba]# mount 
/dev/sda2 on / type ext3 (rw) 
proc on /proc type proc (rw) 
sysfs on /sys type sysfs (rw) 
devpts on /dev/pts type devpts (rw,gid=5,mode=620) 
/dev/sda1 on /boot type ext3 (rw) 
tmpfs on /dev/shm type tmpfs (rw) 
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 
//192.168.10.99/smbgjp on /mnt/smb type cifs (rw,mand)



本文转自 gjp0731 51CTO博客,原文链接:http://blog.51cto.com/guojiping/1022373

相关文章
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
监控 Shell Linux
【Shell 命令集合 网络通讯 】Linux 共享文件和打印机 Samba 命令 使用指南
【Shell 命令集合 网络通讯 】Linux 共享文件和打印机 Samba 命令 使用指南
40 0
|
7天前
|
存储 关系型数据库 MySQL
Linux服务详解
Linux服务详解
20 0
|
8天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
15天前
|
监控 Java Linux
linux下监控java进程 实现自动重启服务
linux下监控java进程 实现自动重启服务
|
18天前
|
关系型数据库 MySQL Linux
linux特定服务日志
Linux系统的服务日志在`/var/log`目录下,如系统日志(`/var/log/syslog`或`/var/log/messages`)、认证日志(`/var/log/auth.log`)、SSH日志(`/var/log/auth.log`或`/var/log/secure`)。Web服务器(Apache和Nginx)的访问和错误日志、MySQL错误日志、Postfix及Dovecot邮件服务器日志也在此处。日志位置可能因发行版和服务配置而异,不确定时可查服务配置或用`grep`搜索。使用`logrotate`可管理日志文件大小。
19 6
|
22天前
|
Ubuntu Linux 虚拟化
【Linux】ubuntu安装samba服务器
【Linux】ubuntu安装samba服务器
|
25天前
|
Linux
linux中服务管理
在Linux服务管理中,从传统的System V init到Upstart,再到广泛采用的systemd,管理方式不断发展。systemd以其强大的功能和依赖解决成为现代Linux的核心。`systemctl`是管理服务的关键命令,如启动(`start`)、停止(`stop`)、重启(`restart`)服务及设置开机启动(`enable`)或取消(`disable`)。了解和服务管理,特别是systemd和`systemctl`的使用,对系统管理员至关重要。其他如xinetd用于控制网络服务,而特定环境下有OpenRC等工具。
17 2
|
1月前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 管理系统服务 ntsysv命令 使用指南
【Shell 命令集合 系统设置 】Linux 管理系统服务 ntsysv命令 使用指南
28 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
29 0