Nếu bạn không muốn file cấu hình của bạn ở /etc, hãy đặt nó ở đâu cũng được, rồi cho symlink vào /etc: ln -s /path/to/smb.conf /etc/smb.conf 3.. Cấu hình tổng quát /etc/smb.conf Cấu hìn
Trang 1Tài liệu này mô tả cách dùng giao thức Server Message Block, còn gọi là giao thức Session Message Block, NetBIOS hay giao thức LanManager, với Linux sử dụng Samba.
MỤC LỤC
1 Giới thiệu
2 Cài đặt
3 Cách vận hành Daemons
4 Cấu hình tổng quát (/etc/smb.conf)
5 Chia xẻ Linux Drive với Windows Machines
6 Truy cập SMB Share với Linux Machines
7 Chia xẻ Linux Printer với Windows Machines
8 Chia xẻ Windows Printer với Linux Machines
9 Backing Up Windows Machines vào một máy chủ Linux
10 Sử dụng Samba Across Routed Networks
11 Tài liệu tham khảo
Trang 21 Giới thiệu:
Tài liệu này mô tả cách dùng giao thức Server Message Block, còn gọi là giao thức
Session Message Block, NetBIOS hay giao thức LanManager, với Linux sử dụng
Samba mặc dù tài liệu có trọng tâm là Linux, nhưng Samba có thể chạy trong hầu
hết các hệ điều hành tương tự Unix
Samba chỉ làm việc trong mạng cục bộ Ethernet dùng giao thức TCP/IP
<! [if !supportLists] >1. <! [endif] >Chia xẻ drive Linux với Windows machines
<! [if !supportLists] >2. <! [endif] >Truy cập SMB share với Linux machines
<! [if !supportLists] >3. <! [endif] >Chia xẻ máy in Linux với Windows machines
<! [if !supportLists] >4. <! [endif] >Chia xẻ máy in Windows với Linux machines
2 Cài đặt:
Để cài đặt Windows 95/98 dùng TCP/IP, chọn Control Panel | Network, rồi add and configure Microsoft TCP/IP
trong /usr/sbin and run hoặc được boot từ systems startup scripts hay từ inetd
smbclient ( SMB client cho UNIX machines)
smbprint ( script de in toi 1 may in trong may chu SMB )
smbprint.sysv (SBN client cho SVR4 UNIX machines) smbstatus (danh sach ket noi hien hanh SMB cho local host)
Trang 3smbrun (Mot 'glue' script tao thuan tien cho viec chay cac ung dung tren may chu SMB)
Cài đặt file cấu hình, smb.conf Vào thư mục chứa Samba Tìm thư mục con examples/simple và đọc file README Copy file Smb.conf đã tìm thấy ở thư mục này vào to /etc
Nếu bạn không muốn file cấu hình của bạn ở /etc, hãy đặt nó ở đâu cũng được, rồi cho symlink vào /etc:
ln -s /path/to/smb.conf /etc/smb.conf
3 Cách vận hành Daemons:
Hai SMB daemons là /usr/sbin/smbd and /usr/sbin/nmbd Dưới phần lớn sự phân phối của Linux , các daemon này là started, stoped và restarted theo startup script đặt trong /etc/rc.d/init.d/smb và symlinked tới một runlevel thích hợp
Nếu không dùng startup script tiêu chuẩn, có thể chạy Samba daemon từ inetd hay tiến trình stand-alone.Samba sẽ đáp ứng nhanh hơn một chút đối với Stand-alone daemon hơn là inetd
Mặt khác, bạn nên kiểm tra file /etc/services for lines trông giống như:
Trang 4Phải chắc rằng mọi file đều uncommented Tuỳ thuộc vào sự phân phối của bạn, bạn có lẽ còn phải thêm chúng vào Samba sẽ không thể nối kết với cổng thích hợp, trừ khi nó có các đầu vào này /etc/services
Để chạy daemons từ inetd, hãy đặt những dòng theo sau vào inetd configuration file, /etc/inetd.conf:
# SAMBA NetBIOS services (for PC file and print sharing)
netbios-ssn stream tcp nowait root /usr/sbin/smbd smbd
netbios-ns dgram udp wait root /usr/sbin/nmbd nmbdRồi restart inetd daemon bằng lịnh:
kill -HUP `cat /var/run/inetd.pid`
Để chạy daemons từ system startup scripts, hãy để các script sau vào file /etc/rc.d/init.d/smb và symbolically link nó vào files specified bằng lịnh:
Trang 5[ ${NETWORKING} = "no" ] && exit 0
# See how we were called
Nếu khi khởi động Samba bạn nhận được báo lỗi rằng says something about the
daemon failing to bind to port 139, thì có thể bạn có một Samba process khác
đang chạy rồi mà chưa shut down Kiểm tra process list (với 'ps auxww | grep mbd') để xem lại
4 Cấu hình tổng quát (/etc/smb.conf)
Cấu hình Samba trong một Linux được điều khiển bởi một file đơn, /etc/smb.conf File này xác định tài nguyên hệ thống mà bạn muốn chia xẻ với bên ngoài và giới hạn nào mà bạn muốn đưa vào đó
Phần [global] định nghĩa vài biến mà Samba dùng để vạch rõ việc chia xẻ mọi tài nguyên
Phần [homes] cho phép những user biệt lập truy cập thư mục home của họ trên local (Linux machine) Nghĩa là, người dùng kết nối vào share này từ Windows machines, sẽ được kết nối với thư mục cá nhân của họ bằng một tài khoản trên Linux box
Trang 6File smb.conf mẫu dưới đây cho phép người dùng đơn lẻ vào home directories của
họ trong local machine và để viết vào một thư mục tạm Đối với một Windows user để thấy được những sự chia xẻ này, Linux box phải hiện hữu trong mạng local Rồi người dùng kết nối vào một network drive từ Windows File Manager hay Windows Explorer
; /etc/smb.conf
;
; Make sure and restart the server after making changes
to this file, ex:
; /etc/rc.d/init.d/smb stop
; /etc/rc.d/init.d/smb start
[global]
; Uncomment this if you want a guest account
; guest account = nobody
log file = /var/log/samba-log.%m
lock directory = /var/lock/samba
share modes = yes
Để kiểm tra sự chính xác của smb.conf file , dùng tiện ích 'testparm'
Nếu Samba server của bạn có nhiều giao diện ethernet, bạn có thể thêm vào phần [global] /etc/smb.conf: như duới đây, để sửa lỗi cho một Samba daemon nào đó.interfaces = 192.168.1.1/24
5 Chia xẻ một Linux Drive với Windows Machines
Để chia xẻ một directory với public, tạo một dòng ở phần [tmp] nêu trên để thêm vào smb.conf:
Trang 7write list = @staff
Để sử dụng Samba cho sản phẩm của Microsoft thì hơi khó vì các Window hiên hành dùng password được mã hoá một cách mặc định mà Samba thì không mà hóa Do đó bạn phải cấu hình Samba dùng password mã hoá hoặc ngược lại
Làm cho Windows hiểu được encrypted SMB passwords hãy vào registry editor:Windows 95/98 =============
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\VNETSU
P Add a new DWORD value: Value Name: EnablePlainTextPassword Data: 0x01 Windows NT ==========
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Rdr\Parameters Add a new DWORD value: Value Name: EnablePlainTextPassword Data: 0x01 Windows 2000 ============
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkStation\Parameters Add a new DWORD value: Value Name:
EnablePlainTextPassword Data: 0x01
Để cấu hình Samba dùng encrypted passwords:
Trong phần [global] của /etc/smb.conf, thêm dòng:
Trang 8encrypt passwords = yes
smb passwd file = /etc/smbpasswd
Nếu clients và server của bạn dùng encrypted passwords, bạn phải tạo kết nối ban đầu bằng cách, điền tên phần chia vào Windows File Manager hay Explorer dialog box, theo form '\\<hostname>\<sharename>' rồi Logon
Nếu bạn nghi ngờ NetBIOS name service không đúng cấu hình, hãy dùng địa chỉ
IP address của server: '\\<host ip address>\<sharename>'
Để filenames thể hiện đúng trong Windows 95/98/NT, bạn còn phải thiết lập vài tuỳ chọn trong phần share tương ứng:
; Mangle case = yes seems to give the correct filenames
; for Win95/98/NT
mangle case = yes
; If samba is case sensitive when looking for files case sensitive = no
; Default case of files that are created
default case = lower
; Preserve case for all filenames
preserve case = yes
; Preserve case for dos (8.3) filenames
short preserve case = no
6 Truy cập một SMB Share với Linux Machines
Linux (UNIX) machines cũng có thể browse và mount SMB shares, bất kể server
là Windows machine hay Samba server!
Samba có trong chương trình SMB client của UNIX machines Nó cung cấp một giao diện ftp-like trong command line Bạn có thể dùng tiện ích này để chuyển files qua lại giữa Windows 'server' và một Linux client
Phần lớn Linux distributions hiện nay cũng bao gồm smbfs package, cho phép người ta mount và umount SMB shares
Để thấy shares nào có hiệu lực trên host mà bạn muốn xem:
Trang 9/usr/bin/smbclient -L host
Tùy theo server, có thể đòi password, và lịnh này xuất ra chẳng hạn như:
Server time is Sat Aug 10 15:58:27 1996
ADMIN$ Disk Remote Admin
public Disk Public
C$ Disk Default share
IPC$ IPC Remote IPC
OReilly Printer OReilly
print$ Disk Printer Drivers
Machine này có browse list:
/usr/bin/smbclient service <password>
Ở đây, 'service' là tên một machine và share Thí dụ, bạn muốn tìm một thu mục được share như 'public' trên máy zimmerman, hãy làm như sau:
/usr/bin/smbclient \\\\zimmerman\\public mypasswd
Trang 10bạn sẽ nhận được:
Server time is Sat Aug 10 15:58:44 1996
Timezone is UTC+10.0
Domain=[WORKGROUP] OS=[Windows NT 3.51] Server=[NT LAN Manager 3.51]
smb: \>
Đánh 'h' để gọi help của smbclient:
smb: \> h
ls dir lcd
cd pwd
get mget put
mput rename
more mask del
rm mkdir
md rmdir rd
prompt recurse
translate lowercase print
printmode queue
cancel stat quit
q exit
newer archive tar
blocksize tarmode
setmode help ?
!
smb: \>
smbclient chỉ dùng để kiểm tra, nếu bạn muốn làm việc thực sự thì phải dùng đến package với hai tiện ích smbmount và smbumount Với điều kiện smbfs có hỗ trợ biên dịch vào kernel
Sau đây là một kiểu mẫu sử dụng smbmount để mount 1 SMB share là
"customers" từ máy "samba1":
[root@postel]# smbmount "\\\\samba1\\customers" -U
rtg2t -c 'mount /customers -u 500 -g 100'
Added interface ip=192.168.35.84 bcast=192.168.255.255 nmask=255.255.0.0
Got a positive name query response from 192.168.168.158 ( 192.168.168.158 )
Server time is Tue Oct 5 10:27:36 1999
Trang 11/dev/hda2 on / type ext2 (rw)
none on /proc type proc (rw)
none on /dev/pts type devpts (rw,mode=622)
//SAMBA1/CUSTOMERS on /customers type smbfs (0)
7 Chia xẻ một Linux Printer với Windows Machines
Thêm cấu hình printing vào smb.conf:
[global]
printing = bsd
printcap name = /etc/printcap
load printers = yes
log file = /var/log/samba-log.%m
lock directory = /var/lock/samba
Trang 12print command = lpr -r -h -P %p %s
8 Chia sẽ một Windows Printer với Linux Machines
<! [if !supportLists] >1. <! [endif] >Phải có cổng váo phù hợp trong /etc/printcap và chúng phải tương xứng với cấu trúc thư mục local
<! [if !supportLists] >2. <! [endif] >Phải có script /usr/bin/smbprint trong Samba source, nhưng không phải mọi Samba binary distributions đều có (có thể bổ sung)
<! [if !supportLists] >3. <! [endif] >Nếu bạn muốn biến đổi file ASCII sang Postscript, phải có nenscript, hay tương đuơng nenscript là Postscript converter và thường được cài đặt trong /usr/bin
<! [if !supportLists] >4. <! [endif] >Sử dụng easy-to-use front end công việc
in ấn của Samba xẻ dễ dàng hơn Một perl script đơn giản để vận dụng bảng mã ASCII, Postscript hay Postscript được tạo ra như dưới đây
<! [if !supportLists] >5. <! [endif] >Bạn cũng có thể dùng MagicFilter để làm như trên Nó có thể convert tự động nhiều dạng format của file Chi tiết thiết lập MagicFilter được cho ở dưới perl script
Cổng vào /etc/printcap dưới đây của máy in HP 5MP trên máy chủ Windows NT :
cm - comment
lp - device name to open for output
sd - the printer's spool directory (on the
local machine)
af - the accounting file
mx - the maximum file size (zero is unlimited)
if - name of the input filter (script)
Trang 13# server and service.
# For example you could have a printcap entry like this
#
#
smb:lp=/dev/null:sd=/usr/spool/smb:sh:if=/usr/local/samba/smbprint
Trang 14# In order for this to work the /etc/printcap entry must include an
# accounting file (af= ):
Trang 15eval `cat $config_file`
$password -U $user -N -P >> $logfile
Hầu hết các Linux distributions dùng nenscript để chuyển đổi từ văn bản ASCII sang Postscript Perl script makes sau đây cung cấp một giao diện đơn giản công việc in của Linux qua smbprint
Usage: print [-a|c|p] <filename>
-a prints <filename> as ASCII
-c prints <filename> formatted as source code
-p prints <filename> as Postscript
If no switch is given, print attempts to
guess the file type and print appropriately
Dùng smbprint để in ASCII files nhằm rút ngắn những hàng dài Script này ngắt dòng bằng khoảng trắng
Việc định dạng source code được thực hiện bởi nenscript Nó lấy một file ASCII
và foramts file này thành 2 cột có gán tiêu đề (date, filename, etc), nó cũng đánh
Trang 16# Plugged In Communications
# Date: 960808
#
# Script de in toi mot may in Postscript theo Samba
# Purpose: Takes files of various types as arguments and
# processes them appropriately for piping to a Samba print script
#
# Currently supported file types:
#
# ASCII - ensures that lines longer than
$line_length characters wrap on
# whitespace
# Postscript - Takes no action
# Code - Formats in Postscript (using nenscript)
Trang 17# Pipe the array to smbprint
open(PRINTER, "|$print_prog") || die "Can't open
$print_prog: $!\n";
foreach $line (@newlines) {
print PRINTER $line;
}
# Send an extra linefeed in case a file has an
incomplete last line
Trang 18Usage: print [-a|c|p] <filename>
-a prints <filename> as ASCII
-c prints <filename> formatted as source code -p prints <filename> as Postscript
Trang 19If no switch is given, print attempts to
guess the file type and print appropriately.\n
#print "Entering subroutine wrap\n";
#print "The line length limit is $limit\n";
# Read in the file, parse and put into an
#print "The line is:\n$line\n";
# Wrap the line if it is over the
# DBG
Trang 20#print "The partial line is:\n
$part\n";
# Check to see if the last
character is a space
$last_char = substr($part,-1, 1);
# If it is not, find the last space in the
# sub-line and print up to there
it easy to find
# the last space
$revpart = reverse($part); $index = index($revpart,"
Trang 21} else {
# There was no space in the line, so
# print it up to $limit substr($line,0,$limit) =
die "Can't open nenscript: $!\n";
foreach $line (@newlines) {
print FILE $line;
Trang 22open(FILE, "<$tmpfile") || die "Can't open
# Create the array for postscript
open(FILE, "<$file") || die "Can't open $file:
Cò bây giờ là cách dùng MagicFilter
1) Cài đặt MagicFilter với filter cho máy in trong /usr/bin/local
2) Viết /etc/printcap như sau: (máy in LaserJet 4L):
lp|ljet4l:\ :cm=HP LaserJet 4L:\ :lp=/dev/null:\ #
or /dev/lp1 :sd=/var/spool/lpd/ljet4l:\ :af=/var/spool/lpd/ljet4l/acct:\ :sh:mx#0:\ :if=/usr/local/bin/main-filter:
3) Viết bộ lọc /usr/local/bin/main-filter giống như bạn đề nghị dùng bộ lọc filter thay vì dùng cat
ljet4l-Here's mine
#! /bin/sh logfile=/var/log/smb-print.log spool_dir=/var/spool/lpd/ljet4l ( echo
"print -" /usr/local/bin/ljet4l-filter ) | /usr/bin/smbclient "\\\\SHIR\\HPLJ4" -N -P