In "Connecting Linux Workstations to Windows 2000 and NT Servers," May 2000, I looked at smbmount and smbumount, two commands that typically ship with Linux distributions and let Linux boxes read from and write to file shares on Windows 2000 (Win2K) and Windows NT servers. In this issue, I look at a somewhat broader Linux-to-NT tool, the smbclient utility.
Why another tool? One reason is that Samba, the main Linux-Microsoft file-server interoperability project, doesn't support smbmount and smbumount. Samba, a set of programs that lets you make a Linux box look like a Microsoft file server, comes free with every Linux distribution I've seen. As a result, smbmount and smbumount could fade away. Another reason is that smbclient has more functionality than smbmount and smbumount. In particular, smbclient lets you send messages to Win2K, NT, and Windows workstations; display browse lists; and connect to Server Message Block (SMB) file shares (although in a somewhat less user-friendly manner than smbmount and smbumount do).
Sending Messages to Workstations
You might know the Net Send command, which lets you send realtime, pop-up messages to machines on a Microsoft network. The command
net send jane hi there!
causes a box containing the words "hi there!" to pop up on Jane's machine. You can direct the Net Send command to machine names as well as to usernames, so if Jane's machine is named \\PC004, you could as easily type
net send pc004 hi there!
Smbclient performs this task with the -M option. The command smbclient -M pc004 establishes contact with \\PC004 and waits for you to type your message. When you end the message (by pressing Ctrl+D), smbclient sends it. As with many UNIX and Linux commands, the option's case is significant—the -M option must be uppercase.
Displaying Browse Lists
Sometimes you might want to connect to a share on a server but can't remember the share's name. In that case, you can examine the server's list of shares in My Network Places (in Win2K) or Network Neighborhood (in NT), or you can use Win2K or NT's Net View command: net view \\servername. With smbclient, the -L option returns a list of share names. Of course, a server won't tell just anyone what shares it has—you need to prove that you're a recognized user. That's where smbclient's -U option comes in. Most Samba tools let you pass a user account name and password to Samba. You specify a username simply as -U username, or you can specify both a username and a password with -U username%password. For example, if I wanted to see the shares on an NT server named \\BIGSERVE and I had an account on that system named usermark with a password of swordfish, I could type
smbclient -L //bigserve -U usermark
and the system would prompt me for a password. Alternatively, I could type
smbclient -L //Bigserve -U Usermark%swordfish
If the system that you query is a domain controller, the system will also display the list of servers in the workgroup.
Notice a couple of things in the examples I've used so far. First, you can use uppercase, lowercase, or mixed case on user account and server names. Second, notice that the -L option requires two slashes (//) before the name of the server; in contrast, the -M option doesn't work if you precede the name with slashes—as I finally figured out after 45 minutes of experimentation. I also found that smbclient -L seems not to work on Windows 2000 Professional (Win2K Pro) and Windows 2000 Server (Win2K Server) systems.
Connecting to SMB File Shares
I've said that smbclient lets you access Win2K, NT, and Windows shares from a Linux box, but although that's true, smbclient doesn't let you access them in the way that smbmount does. Instead, you use smbclient to attach to a share and use FTP-like commands to copy files to and from Microsoft network shares.
You connect to a share with the command smbclient //servername/sharename -U username. (This command does work with Win2K systems.) So, if \\BIGSERVE contains a share named Plans and my usermark account has access to that share, I could connect with the command
smbclient //bigserve/plans -U\ usermark%swordfish
(The backslash is a continuation character; don't type it if you enter the command on one line.) I then get the prompt smb>, and I can carry out commands from that prompt.
To copy files to and from NT shares, the first commands you'll need are lcd and cd. These commands should look familiar if you're an old hand with FTP. Lcd specifies the default directory on your workstation—the directory in which smbclient looks for the files you want to send and puts the files you've received. Cd specifies the default directory for the server. So, if you're connected to a share that contains a directory named mail, from which you want to copy some files to a directory named /mymailfiles on your local Linux box, you can set those directories as the default directories by typing
cd mail lcd /mymailfiles
If you have trouble remembering things, as I seem to, you'll probably need to list the files in the directory on the remote machine so that you can find the ones you want. To list files on the remote system, you can use either ls or dir. However, to list files on your local system (the Linux system, in our case), you need to prefix the command with an exclamation point (e.g., !ls). Whichever command you use, you'll see the smbclient prompt followed by a directory (e.g., smb: \stuff>). Be aware that the directory the system displays is the directory on the Microsoft server, not the directory on your local Linux box.
To transfer files, you use the get and put commands. Typing
tells smbclient to get the specified file from the Microsoft network server and copy it to your local Linux box's disk. The put command does the reverse, moving files from the local workstation to the server. Unfortunately, get and put don't take wildcards—you can't get *.* or the like. To use wildcards to transfer multiple files, use mget (multiple get) and mput (multiple put).
Mget and mput seem straightforward until you try to use them. If you hook up to a share and try to retrieve files with mget *, smbclient ridiculously pauses before transferring each file to ask whether you really want to transfer it. To prevent the nagging, use the prompt command to toggle off the confirmation message.
Seasoned FTP users are probably waiting for me to introduce a binary command, a toggle that tells FTP whether the incoming files are ASCII or binary. Smbclient doesn't have such a command, however; smbclient treats every file as if it were binary. As with FTP, the rd and rmdir commands let you remove directories on the remote system.
Here's one final hint about using smbclient. Sometime you might need to use smbclient on someone else's Linux system. If the system's owner has never used smbclient and hasn't created the smb.conf file, Linux can't check smb.conf for the WINS server's location, and consequently smbclient often can't find the Win2K or NT server that you're trying to connect to. (For an explanation of the smb.conf file, see "Connecting Linux Workstations to Windows 2000 and NT Servers.")
You can, of course, create an smb.conf file. But if you don't want to do that much work, you can lead smbclient by the nose to your Win2K or NT server with the -W and -I options. The -W option lets you enter a domain's name, and the -I option lets you specify the server's IP address. So, using my earlier example, if I knew that \\BIGSERVE had an IP address of 184.108.40.206 and that my usermark account was in the domain GUYS, I could invoke smbclient with the command
smbclient //bigserve/plans -I 220.127.116.11 -W guys -U usermark%swordfish
and be pretty sure that my connection would be successful. As always, the -I and -W options must be uppercase.
Smbclient has many other options as well. To see all of them, use the man smbclient command.
A Useful Linux Command
With smbclient, you can sit at a Linux box and access a file share on a Win2K, NT, Win9x, or even a Windows for Workgroups (WFW) system. Smbclient doesn't need any setup if you use the -I and -W options, and you can use the command to find out which shares are on the network as well as to transfer files. With the -M option, you can even annoy—I mean chat with—your friends through pop-up messages.