Библиотека сайта rus-linux.net
Chapter 5. Unix Clients
In Chapter 3 we showed you how to configure Windows systems to access shared resources on both Windows and Samba servers. This has probably opened up a whole new world of computing for you—one in which you have to run to a Windows system every time you want to copy a file between Unix and Windows! In this chapter, we will show you the "other side"—how to access SMB shares from your favorite Unix system.
You can access SMB resources from Unix in three ways, depending on your version of Unix. A program included with the Samba distribution called smbclient can be used to connect with a share on the network in a manner similar to using ftp when transferring files to or from an FTP site.
If your system is running Linux, you can use the smbfs filesystem to mount SMB shares right onto your Linux filesystem, just as you would mount a disk partition or NFS filesystem. The SMB shares can then be accessed and manipulated by all programs running on the Linux system: command shells, desktop GUI interfaces, and application software.
On some BSD-based systems, including Mac OS X, a pair of utilities named smbutil and mount_smbfs can be used to query SMB servers and mount shares.
For other Unix variants, smbsh can be run to enable common shell commands such as cd, ls, mv, wc, and grep to access and manipulate files and directories on SMB shares. This effectively extends the reach of the Unix shell and utilities beyond the Unix filesystem and into the SMB network.
Sharing Files on Windows 95/98/Me
When sharing files on Windows 95/98/Me, you can authenticate users in two different ways. Share-level security is the default and is easy to use. However, it is not as secure and can require users to type in passwords when connecting to shares. User-level security offers a better security model and can be used if you have either a Samba or Windows NT/2000 server on your network performing user authentication.
To configure the type of access control for your system, open the Control Panel, double-click the Network icon, then click the Access Control tab. You should see the dialog box shown in Figure 5-1.
Figure 5-1. The Access Control tab of the Windows 98 Network Control Panel window
Figure 5-2 shows what the Sharing tab of the folder's Properties dialog box will look like when using share-level security. The security settings are very simple. You can select a radio button for read-only access or full (read/write) access, or have the user's permissions (either read-only or read/write) depend on which password they use. In accordance with which you select, you will be asked to assign either or both of the read-only and full-access passwords for the share.
Figure 5-2. The Sharing tab of the folder's Properties dialog, with share-level security
If your system is configured with user-level security, the Sharing
tab of the folder's Properties dialog box will look
like Figure 5-3. As you can see,
we've created a share named
"DATA", and used the Add . . .
button to create permissions that allow read-only access for all
domain users and read/write (full access) for jay
.
Figure 5-3. The Sharing tab of the folder Properties dialog, with user-level security
Sharing Files on Windows NT/2000/XP
To create a file share on Windows NT/2000/XP, you first must log in to the system as any member of the Administrators, Power Users, or Server Operators groups. Right-click the icon of a folder you wish to share, and click Sharing . . . in the pop-up menu. The Sharing tab of the folder's Properties dialog box will appear, as shown in Figure 5-4. Click the "Share this folder" radio button.
Figure 5-4. The Sharing tab of the folder's Properties dialog on Windows 2000
By clicking the Permissions button, you can set permissions for the share on a user-by-user basis. This is equivalent to the user-level security of Windows 95/98/Me file sharing. On Windows NT/2000/XP, Microsoft recommends that share permissions be set to allow full access by everyone, with the permissions controlled on a file-by-file basis using filesystem access control lists (ACLs). The actual permissions given to network clients are a combination of the share permissions and file access permissions. To edit the ACL for the folder, click the Security tab. For more information on ACLs, see Section 8.3 in Chapter 8.
If you want, you can limit the number of users who can concurrently connect to the share using the "User limit:" radio button. The New Share button allows you to create multiple file shares for the same folder, each having its own name, comment, user limit, and other parameters.
When you are done, click the OK button, and the folder will be accessible from clients on the network.
smbclient
The Samba Team supplies smbclient as a basic part of the Samba suite. At
first, it might seem to be a primitive interface to the SMB network,
but smbclient is actually a versatile tool. It
can be used for browsing shares on servers, testing configurations,
debugging, accessing shared printers, backing up shared data, and
automating administrative tasks in shell scripts. And unlike
smbfs
and smbsh,
smbclient works on all Unix variants that
support Samba.
In this chapter we'll focus mostly on running smbclient as an interactive shell, using its ftp-like commands to access shared directories on the network. Using smbclient to access printers and perform backups will be covered in Chapter 10.
A complete reference to smbclient is found in Appendix C.
Listing Services
$smbclient -L toltec
added interface ip=172.16.1.1 bcast=172.16.1.255 nmask=255.255.255.0 Password: Domain=[METRAN] OS=[Unix] Server=[Samba 2.2.5] Sharename Type Comment --------- ---- ------- test Disk For testing only, please IPC$ IPC IPC Service (Samba 2.2.5) ADMIN$ Disk IPC Service (Samba 2.2.5) Server Comment --------- ------- MAYA Windows 98 MIXTEC Samba 2.2.5 TOLTEC Samba 2.2.5 ZAPOTEC Workgroup Master --------- ------- METRAN TOLTEC
In the column labeled "Server",
maya
, mixtec
, and
zapotec
are shown along with toltec, the Samba
server. The services on toltec
are listed under
"Sharename". The IPC$ and ADMIN$
shares are standard Windows services that are used for network
communication and administrative purposes, and
test is the directory we added as a share in
Chapter 2.
Now that we know the names of computers in the domain, we can list
services on any of those computers. For example, here is how we would
list the services offered by maya
, a Windows 98
workstation:
$smbclient -L maya
added interface ip=172.16.1.1 bcast=172.16.1.255 nmask=255.255.255.0 Password: Sharename Type Comment --------- ---- ------- PRINTER$ Disk HP Printer HP 932C on Maya D Disk D: on Maya E Disk E: on Maya ADMIN$ Disk IPC$ IPC Remote Inter Process Communication Server Comment --------- ------- Workgroup Master --------- -------
A shared printer is attached to maya
, so we see
the PRINTER$ administrative service, along with the HP share for the
printer itself. Also on maya
are the D and E
shares, which allow access across the network to
maya
's D: and E: drives. It is
normal for the Server and Workgroup sections to be empty when listing
services on a Windows client.
Authenticating with smbclient
$smbclient //maya/e jayspassword
$smbclient //maya/e -U kelly%kellyspassword
smbcl( ) { smbclient $* -U jay%jayspassword }
Another method that can be used to supply both the username and password is to set the USER and PASSWD environment variables. Either set the USER environment variable using the username%password format, or set the USER environment variable to the username, and set PASSWD to the user's password.
username = jay password = jayspassword
Then, smbclient is run using the -A option to specify the name of the file:
$smbclient //maya/e -A ~/.smbpw
NOTE
Of the methods we described in this section, the only one that is really secure is the default method of allowing smbclient to prompt for the password and typing in the password without echoing.
An Interactive smbclient Session
$smbclient //maya/e
added interface ip=172.16.1.3 bcast=172.16.1.255 nmask=255.255.255.0 Password: smb: \>
Forward slashes are accepted by smbclient for
the share's UNC, which makes entering the UNC on the
command line easier. Backslashes can also be used, but they must be
quoted or escaped, and it is somewhat more difficult to type
'\\maya\e
' or \\\\maya\\e
.
After connecting to the share, smbclient
displays the smb: \>
prompt, waiting for a
command to be entered. Commands are similar to those with which you
might be familiar in ftp and are also somewhat
similar to Unix shell commands. To get a list of
smbclient commands, use the
help command:
smb: \>help
ls dir du lcd cd pwd get mget put mput rename more mask del open rm mkdir md rmdir rd prompt recurse translate lowercase print printmode queue cancel quit q exit newer archive tar blocksize tarmode setmode help ? history !
smb: \>? ls
HELP ls: <mask> list the contents of the current directory
smb: \>ls *doc
ms-ProfPol-wp.doc A 131 Tue Dec 18 09:12:34 2002 smbclient.doc A 33969 Mon Dec 10 20:22:24 2002 smbmount.doc A 7759 Mon Dec 10 20:20:00 2002 48590 blocks of size 524288. 40443 blocks available
smb: \>cd trans
smb: \trans\>
smb: \trans\>lcd /u/snd
the local directory is now /u/snd
smb: \trans\>! ls -l
total 16 drwxrwxr-x 2 jay jay 4096 Jan 10 14:46 dr220-fet drwxrwxr-x 2 jay jay 4096 Sep 22 12:16 dr220-tube -rw-rw-r-- 1 jay jay 131 Jan 10 02:22 readme.txt drwxrwxr-x 7 jay jay 4096 Jan 10 02:19 xl1
File transfer is performed using the get and put commands. The get command transfers a single file from the remote to the local system, and the put command copies a file from the local to the remote system. For example, the following command copies the file readme.txt to the SMB share:
smb: \trans\>put readme.txt
putting file readme.txt as \trans\readme.txt (127.9 kb/s) (average 10.7 kb/s)
NOTE
Unlike ftp, smbclient does not have ascii and binary commands to set the type of the file that is being transferred. Before transferring a text file from a Unix system to a Windows or Macintosh system, you might want to use the GNU unix2dos command to reformat newlines in the file to work with the carriage return linefeed (CRLF) standard:
$unix2dos text_file >text_file.txt
and then transfer the CRLF-formatted version. After transferring a text file from a Windows or Macintosh system to Unix, you can use the GNU dos2unix command to perform the inverse operation:
$dos2unix text_file.txt >text_file
To transfer more than one file with a single command, you can use the mget and mput commands, which accept a list of filenames in the command line. The list can be provided by typing in the filenames on the command line separated by spaces, or the group of files can be specified with a pattern as one would use in Unix shell commands. The command:
smb: \trans\>mget plain/*
smb: \trans\>mget plain/*
Get file tomm.wav? n Get file toml.wav? n Get file tomh.wav? n Get file snare.wav? n Get file rim.wav? n Get file handclap.wav? n Get file bassdrum.wav? n
smb: \trans\>prompt
prompting is now off
smb: \trans\>recurse
directory recursion is now on
After setting things up with the prompt and recurse commands, we can copy a directory like this:
smb: \trans\>mget acc
getting file tomm.wav of size 55494 as tomm.wav (2580.6 kb/s) (average 2087.3 kb/s) getting file toml.wav of size 57220 as toml.wav (2660.9 kb/s) (average 2167.6 kb/s) getting file tomh.wav of size 55936 as tomh.wav (2601.2 kb/s) (average 2220.8 kb/s) getting file snare.wav of size 22132 as snare.wav (1200.7 kb/s) (average 2123.7 kb/s) getting file rim.wav of size 8314 as rim.wav (1623.8 kb/s) (average 2110.8 kb/s) getting file handclap.wav of size 14180 as handclap.wav (1978.2 kb/s) (average 2106.2 kb/s) getting file bassdrum.wav of size 6950 as bassdrum.wav (2262.3 kb/s) (average 2108.5 kb/s)
smb: \trans\>recurse
directory recursion is now off smb: \trans\>prompt
prompting is now on
There are other smbclient commands that you might find useful. The mkdir command can be used to create a directory; rmdir removes a directory; rm deletes a file; and rename changes a file's name. These behave very similarly to their Unix shell counterparts. Appendix C contains a complete reference to smbclient and its command set.
To exit smbclient, use the exit or quit command:
smb: \trans\>quit
Programming with smbclient
$smbclient //maya/e -c "lcd /u/snd; cd trans; get readme.txt" -A ~/.smbpw
smbls( ) { share=`echo $1 | cut -d '/' -f '1-4'` dir=`echo $1 | cut -d '/' -f '5-'` smbclient $share -c "cd $dir; ls" -A ~/.smbpw | \ grep "^ " | cut -d ' ' -f 3 - | sort }
After defining this function, we can use smbls like this:
$smbls //maya/e
CD-images lectures ms-ProfPol-wp.doc profile-map readme.txt RECYCLED smbclient.doc smbmount.doc smbsh.txt trans $smbls //maya/e/lectures
. .. lecture1.mp3 lecture2.mp3 lecture3.mp3 lecture4.mp3 lecture5.mp3 lecture6.mp3 lecture7.mp3 lecture8.mp3 lecture9.mp3
Backups with smbclient
A major use of smbclient is to create and restore backups of SMB file shares. The backup files smbclient writes are in tar format, making them easy to work with and portable among all Unix versions. Using smbclient on a Unix server to run network backups can result in a more centralized and easily managed solution for providing data integrity because both SMB shares and NFS filesystems can be backed up on the same system.
#smbclient //maya/e -A samba-domain-pw -Tc >maya-e.tar
#smbclient //maya/e -A samba-domain-pw -D trans -Tc >maya-e.tar
#smbclient //maya/e
added interface ip=172.16.1.3 bcast=172.16.1.255 nmask=255.255.255.0 Password: smb: \>cd trans
smb: \trans\>tarmode full hidden system quiet
smb: \trans\>tar c maya-e-trans.tar
#smbclient //maya/e -A samba-domain-pw -c "cd trans; tarmode full hidden \
system quiet; tar >maya-e-trans.tar"
Using either the -T command-line option or smbclient's tar command, additional options can be supplied. It is necessary to specify either the c option to create a backup archive or the x option to extract (restore) one.[1]
The other options can be appended to the option string and are explained in the section on smbclient in Appendix C. They allow you to create incremental backups, specify which files to include or exclude from the backup, and specify a few other miscellaneous settings. For example, suppose we wish to create an incremental backup of a share and reset the archive bit on the files to set things up for the next incremental backup. Instead of using the interactive commands:
smb: \>tarmode inc reset quiet
smb: \>tar c backup.tar
we could either use the interactive command:
smb: \>tar cgaq backup.tar
or specify the -Tcgaq option on the smbclient command line.
Your best strategy for using smbclient for network backups depends on your local configuration. If you have only a few Windows systems sharing a small amount of data, you might create a script containing smbclient -Tc commands to back up each share to a separate tar file, placing the files in a directory that is included with regular backups of the Unix system. If you have huge SMB shares on your network, you might prefer to write the backup directly to a tape drive. You can do this with smbclient just as you would with a Unix tar command:
#smbclient //maya/d -A samba-domain-pw -Tc >/dev/tape
After you have become more familiar with smbclient and have an automated backup system in place, you might find that using Samba has dramatically decreased your anxiety regarding the integrity of your network's data. The authors of this book are experienced Unix system administrators, and we highly recommend having a backup strategy that has been carefully planned, implemented, and most importantly, tested and known to work as it is supposed to.
smbfs
On Linux, the smbfs filesystem can be used to mount SMB shares onto the Linux filesystem in a manner similar to mounting disk partitions on NFS filesystems. The result is so transparent that users on the Linux system might never be aware that they are accessing files through a Windows or Samba server. Files and directories appear as any other files or directories on the local Linux system, although there are a few differences in behavior relating to ownership and permissions.[2]
Although smbfs is based on the Samba code, it is not itself part of the Samba distribution. Instead, it is included with Linux as a standard part of the Linux filesystem support.
The smbmount and
smbmnt programs are part of the Samba
distribution and are needed on the client to mount smbfs filesystems.
Samba must be compiled with the --with-smbmount
configure option to make sure these programs are compiled. They refer
to smb.conf for information they need regarding
the local system and network configuration, so you will need a
working smb.conf
file on the system, even if it is not acting as a Samba server.
Mounting an smbfs Filesystem
The smbmount command is used to mount an smbfs filesystem into the Linux filesystem. The basic usage is:
#smbmount
Share-UNC mount-point-o
options
#smbmount //maya/e /smb/e \
-o "credentials=/home/jay/.smbpw,uid=jay,gid=jay,fmask=664,dmask=775"
NOTE
If your Linux kernel doesn't include smbfs support, you will get the error message:
ERROR: smbfs filesystem not supported by the kernel$cat /proc/filesystems
and see a line that looks like:
nodev smbfs
The mount point must exist before smbmount is run and can be created using the mkdir command:
#mkdir /smb/e
TIP
The fmask and dmask options specify bitmasks for permissions of files and directories, respectively. These bitmasks are logically ANDed with whatever permissions are granted by the server to create the effective permissions on the client Unix system. On the server side, the permissions granted depend on the server's operating system. For a Windows 95/98/Me server using share-mode security, the MS-DOS read-only attribute can be set on individual files and directories and combined with the Full Access or Read Only permissions on the share as a whole. In user-level security mode, Windows 95/98/Me can have ACL-like permissions applied to the entire share, as discussed in Chapter 4. Windows NT/2000/XP support ACLs on individual files and directories, with Full Control, Change, or Read permissions that can be applied to the entire share. If the server is a Samba server, the permissions are whatever is defined by the Samba share and the local Unix system for the individual files and directories. In every case, the permissions applied to the share act to further limit access, beyond what is specified for the individual files and directories.
TIP
After mounting the \\maya\d share to /smb/e, here is what the contents of /smb/e look like:
$cd /smb/e ; ls -l
total 47 drwxrwxr-x 1 jay jay 512 Jan 8 20:21 CD-images drwxrwxr-x 1 jay jay 512 Jan 6 21:50 lectures -rw-rw-r-- 1 jay jay 131 Dec 18 09:12 ms-ProfPol-wp.doc -rw-rw-r-- 1 jay jay 59 Dec 18 09:12 profile-map -rw-rw-r-- 1 jay jay 131 Jan 15 05:01 readme.txt drwxrwxr-x 1 jay jay 512 Feb 4 2002 RECYCLED -rw-rw-r-- 1 jay jay 33969 Dec 10 20:22 smbclient.doc -rw-rw-r-- 1 jay jay 7759 Dec 10 20:20 smbmount.doc -rw-rw-r-- 1 jay jay 1914 Dec 10 20:17 smbsh.txt drwxrwxr-x 1 jay jay 512 Jan 10 03:54 trans
#chown root lectures
chown: changing ownership of 'lectures': Operation not permitted
#chmod 777 readme.txt
#ls -l readme.txt
-rw-rw-r-- 1 jay jay 131 Jan 15 05:01 readme.txt
Mounting smbfs Filesystems Automatically
Share-UNC mount-point smbfs options 0 0
//maya/e /smb/e smbfs credentials=/home/jay/.smbpw,uid=jay,gid=jay,fmask=664,dmask=775 0 0 (Please note that this should all go on one line.)
WARNING
If you make a mistake in modifying /etc/fstab, your system might not reboot properly, and you might be forced to boot into single-user mode to fix the problem. Before you edit /etc/fstab, be sure to make a backup copy of it, and be prepared to recover your system if anything