Samba是Linux中模拟Windows 文件/打印共享的服务器软件,在Linux / Windows 混合网络环境下是非常有用的。

安装Samba
devadm@devserver:~$ sudo tasksel

选择 [*]Samba file server,按[tab]键跳到,敲回车。很简单,也可以 $ sudo apt-get install samba,再Ubuntu里没试过,应该也是一样的。

一. 创建共享目录,任何人可读、写、执行
devadm@devserver:~$ sudo mkdir /opt/smbshare
devadm@devserver:~$ sudo chmod 777 /opt/smbshare
devadm@devserver:~$

二. 编辑配置文件
devadm@devserver:~$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup
devadm@devserver:~$ sudo vi /etc/samba/smb.conf

1. 找到[global]
workgroup = WORKGROUP
display charset = UTF-8
unix charset = UTF-8
dos charset = cp936

2. 搜寻这一行文字代码:
#   security = user
用下面这两行取代
security = user
username map = /etc/samba/smbusers

3. 在最后一行添加:
[devshare]
comment = Shared Folder with username and password
path = /opt/smbshare
public = yes
writable = yes
valid users = dev
create mask = 0777
directory mask = 0777
force user = nobody
force group = nogroup
available = yes
browseable = yes
退出保存。

三. samba用户
1. 创建用户dev
devadm@devserver:~$ sudo useradd dev

2. 给dev分配smb密码,注意,这个不是本机登录密码,本机登录密码没有设定。
devadm@devserver:~$ sudo smbpasswd -a dev
New SMB password:
Retype new SMB password:
devadm@devserver:~$

3. 编辑/etc/samba/smbusers
devadm@devserver:~$ sudo vi /etc/samba/smbusers
添加下行:
dev = "Samba user."

退出保存。

如果防火墙UFW enable 了,则需要允许samba端口。
devadm@devserver:~$ sudo ufw allow 139/tcp
Rules updated
Rules updated (v6)
devadm@devserver:~$ sudo ufw allow 445/tcp
Rules updated
Rules updated (v6)

四. 测试samba服务器
1. 重启smbd
devadm@devserver:~$ sudo service smbd restart
smbd start/running, process 9873
devadm@devserver:~$

2. 服务器端测试:
devadm@devserver:~$ sudo testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[printers]"
Processing section "[print$]"
Processing section "[devshare]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions 【enter】
[global]
    dos charset = cp936
    display charset = UTF-8
    server string = %h server (Samba, Ubuntu)
    map to guest = Bad User
    obey pam restrictions = Yes
    pam password change = Yes
    passwd program = /usr/bin/passwd %u
    passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
    username map = /etc/samba/smbusers
    unix password sync = Yes
    syslog = 0
    log file = /var/log/samba/log.%m
    max log size = 1000
    dns proxy = No
    usershare allow guests = Yes
    panic action = /usr/share/samba/panic-action %d

[printers]
    comment = All Printers
    path = /var/spool/samba
    create mask = 0700
    printable = Yes
    browseable = No

[print$]
    comment = Printer Drivers
    path = /var/lib/samba/printers
    browseable = No

[devshare]
    comment = Shared Folder for DEV
    path = /opt/smbshare
    valid users = dev
    force user = nobody
    force group = nogroup
    read only = No
    create mask = 0777
    directory mask = 0777
    guest ok = Yes
devadm@devserver:~$

查看samba服务器状态
devadm@devserver:~$ smbstatus
Samba version 3.5.11
PID     Username      Group         Machine                      
-------------------------------------------------------------------
Service      pid     machine       Connected at
-------------------------------------------------------
No locked files

3. 从另一台电脑,用smbclient(类似ftp客户端)打开samba共享:
gt@gt-Dell:~/Documents$ smbclient //devserver/devshare -U=dev
Enter dev's password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.5.11]
smb: \>
smb: \> help
?              allinfo        altname        archive        blocksize    
cancel         case_sensitive cd             chmod          chown        
close          del            dir            du             echo         
exit           get            getfacl        hardlink       help         
history        iosize         lcd            link           lock         
lowercase      ls             l              mask           md           
mget           mkdir          more           mput           newer        
open           posix          posix_encrypt  posix_open     posix_mkdir  
posix_rmdir    posix_unlink   print          prompt         put          
pwd            q              queue          quit           readlink     
rd             recurse        reget          rename         reput        
rm             rmdir          showacls       setmode        stat         
symlink        tar            tarmode        translate      unlock       
volume         vuid           wdel           logon          listconnect  
showconnect    ..             !            
smb: \>
smb: \> ls
  .                                   D        0  Sun Jan 29 16:12:45 2012
  ..                                  D        0  Sun Jan 29 12:34:37 2012

        33888 blocks of size 4194304. 31870 blocks available
smb: \>
smb: \> put bookmarks.html
putting file bookmarks.html as \bookmarks.html (5480.1 kb/s) (average 5480.2 kb/s)
smb: \> ls
  .                                   D        0  Sun Jan 29 16:13:38 2012
  ..                                  D        0  Sun Jan 29 12:34:37 2012
  bookmarks.html                      A    22447  Sun Jan 29 16:13:38 2012

        33888 blocks of size 4194304. 31870 blocks available
smb: \>
smb: \> q
gt@gt-Dell:~/Documents$

4. 回到服务器,看看刚才上传的文件:
devadm@devserver:~$ sudo ls -l /opt/smbshare
total 24
-rwxrw-rw- 1 nobody nogroup 22447 2012-01-29 16:13 bookmarks.html
devadm@devserver:~$

如果希望网络用户以“只读/可执行”方式访问,则:
devadm@devserver:~$ sudo chmod 755 /opt/nfsshare -R

另外修改/etc/samba/smb.conf
在[devshare]段内
writable = yes 改为no

这时候在客户端上传文件就会提示错误:
smb: \> put firefox_addons.txt
NT_STATUS_ACCESS_DENIED opening remote file \firefox_addons.txt
smb: \>

但是下载文件是可以的:
smb: \> get bookmarks.html
getting file \bookmarks.html of size 22447 as bookmarks.html (1217.8 KiloBytes/sec) (average 1217.8 KiloBytes/sec)
smb: \>

接着测试从Windows客户端和 Ubuntu客户端打开samba共享,成功了,但是测试从Ubuntu客户端mount 共享目录出错了:

gt@gt-Dell:~$ sudo mount -t cifs //devserver/devshare /mnt/smb -o username=dev
mount: 块设备 //devserver/devshare 写保护,已只读方式挂载
mount: 无法将 块设备 //devserver/devshare 以只读方式挂载

暂时没找到原因。


参考:

http://wiki.ubuntu.org.cn/Samba