Samba client

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

Contents

[edit] Introduction

There are a few ways to connect to a remote Samba server. The way you connect depends on what you need to do.

[edit] smbclient

If you just need to grab a file, or put a file on the server, but don't need a constant connection, it is best to either browse to the share using Gnome/KDE's built in Samba browser, or to use the command line client smbclient. This assumes you have the samba-client package installed provided by your distribution.

Using smbclient is very similar to using a command-line FTP client. Here are a few useful commands:

smbclient -L //hostname

Where hostname is the NetBIOS name of the machine you wish to connect to. After being prompted for a password (enter no password for anonymous login) this command will give you a list of available shares on the machine, plus some other information.

To connect to a share, you must specify the share name, in the format //hostname/sharename where sharename could be the name of a file share, or a printer, or anything else Samba can handle.

smbclient //joescomp/joe

After entering joe's password, you are given an smb: \> prompt where you may enter commands much like an FTP client. Some commands include ls, get, put, rm, help and quit. The help command will list all the commands, and help command will give help on a certain command.

[edit] Mounting

If you need a permanent connection to your Samba server, it is best to mount the share on some local directory. This is done much like mounting any other volume, but there are some syntactical differences.

Note: It is not a good idea to mount Samba shares on a computer which might be changing networks a lot, like a laptop. If you forget to unmount the share before disconnecting from the network, you could experience some problems later if your computer tries to connect with the share, even if just to unmount it. It may think that the share is busy or take a long time to disconnect.

[edit] Module notes

In order to do this you must have cifs or smbfs modules built into your kernel or available as a module. To check, execute:

# modinfo cifs

or

# modinfo smbfs

If you are mapping a share with many non-ASCII (ie: non-US or American) based file names, the legacy smbfs module can create problems accessing those files. The newer cifs (Common Internet File System) module has greater support for correctly recognizing and translating those characters.

Note: Use the cifs module as demonstrated above when connecting to a Samba or Windows 2000 and above server. smbfs is the old module used to connect to Windows 98 style shares. If one doesn't work, try the other. A noteworthy mention is Netware's CIFS implementation, which is not compatible with the cifs module. Use smbfs instead.

[edit] Manual mounting

To mount a share one time, use the mount command as root. First you will need to create a directory to mount the share on.

# mkdir -p /mnt/joescomp/joe
# mount -t cifs //joescomp/joe -o username=joe /mnt/joescomp/joe

You will then be prompted for a password, and if connection is successful your share will now be mounted. To unmount a share, use the umount command as root.

# umount /mnt/joescomp/joe

This command will unmount the share. To unmount all mounted Samba shares, do this:

# umount -t cifs -a

[edit] Automatic mounting

Linux can automatically mount your network shares. First it is a good idea to make sure that manual mounting works. To set your computer up to automatically mount a share whenever it boots, or so that a non-privileged user can mount the shares, you can add them to your /etc/fstab file. You must be root to edit this file.

# nano -w /etc/fstab

Now add a line to the end of the file like this:

//joescomp/joe  /mnt/joescomp/joe  cifs  username=joe,password=joespass,user

The user option means that any user (not just root) can mount and unmount the share. For this to work, however, the mount.cifs utility must be setuid root, and the user who will do the mounting must own the mount point directory.

# chmod u+s /sbin/mount.cifs
# chown joe:joe /mnt/joescomp/joe

Note: You can leave out the password option if you do not want your password stored in this file as clear text. This way, every time you mount this share it will ask you for the password.

With newer smbfs and cifs modules, it is better to place the username/password combination in a file, and reference it with the credentials option. Create the file /etc/samba/joe.cred, make it only visible to root, and then update your /etc/fstab entry.

# nano -w /etc/samba/joe.cred
username=joe
password=joespass
# chmod 600 /etc/samba/joe.cred
# nano -w /etc/fstab
//joescomp/joe  /mnt/joescomp/joe  cifs  credentials=/etc/samba/joe.cred,user

Once you've added this to your /etc/fstab you can mount the share any time:

mount /mnt/joescomp/joe

If you have many shares defined you can mount them all thus:

mount -t cifs -a

Unmounting is the same as above.

umount -t cifs -a

[edit] Anonymous Automounting

If you have a share setup that requires no credentials, you can remove the credentials from /etc/fstab, and put guest in it's place.

//joescomp/joe /mnt/joescomp/joe cifs guest

Personal tools