Friday 15 July 2011

Networking Linux and Windows with SAMBA

Networking Linux and Windows with SAMBA

Coming from a Windows background myself, switching to a Linux environment is a daunting and seemingly impossible task at first glance. You might have finally gotten your distribution of choice installed only to find out that you have no idea how anything works and a simple task such as mounting and browsing a USB thumb drive is next to impossible! To the beginning Linux user, a simple problem like this is enough to act as a catalyst of failure causing them to revert back to the safety and comfort of Windows, where everything is done for you, (sometimes without your permission.) Throw networking a few machines into the mix and you got yourself a complete fiasco! or do you?

Purpose of this Tutorial

The purpose of this tutorial is not to scare you away from using Linux but to encourage you! I will go over the very basics of what you need to get your Linux box and your Windows machine talking seamlessly and cover what needs to be done to accomplish some very basic file sharing capabilities.

Note: All following examples have been performed in Ubuntu Linux 8.04. Depending on your distribution you may need to modify some of the directories used in this tutorial as they vary from distro to distro. This tutorial assumes that tcp/ip is installed and functioning on both the Linux and Windows machine and that all the hardware required for proper communication is configured correctly.

Part 1: What is Samba and Why do I Need It?

To answer this question we need to discuss briefly how Windows machines exchange information and share files. In a small Windows network the initial connection and session setup is handled by NetBios, (Network Basic Input Output System.) It is the job of NetBios to handle any names that need to be resolved and to make sure that all sessions are properly configured from the start and properly terminated when the connection is closed. Once a client has a NetBios session setup with the server he can than access the shared resources provided using a protocol called SMB (Server Message Block.) When a client requests a resource on the network a SMB packet is created with some of the following information.

1. Credentials. Basically your username and password. This lets the server know who you are and what your allowed to do with the resource. (Your permissions.)


2. The requested resource. How the heck do you expect to get anything in return if you dont let the server know what you want?

Although theres a lot more associated with the format of an SMB packet, these are two of the big ones you need to know. Now you may be wondering, well what the heck does this have to do with sharing files with linux!? This is where Samba comes in. You can think of Samba as a unix version of Microsofts smb protocol. It emulates the protocol in such a way that it allows Linux to share files with Windows and (horrors...!) vice versa. In fact it is so effective Windows computers have no idea they're even talking to a linux machine! So to wrap this part up, Samba is the Linux implementation of the smb protocol and allows Linux and windows to share files seamlessly.

Part 2: Installing Samba

To install Samba we need to open a terminal. You can open a terminal either by going to applications--->accessories---->terminal or by switching to one of virtual terminals (ctrl - alt - f5) I prefer a virtual terminal because I am easily distracted by the gui and all of what we will be working with will be done in the shell anyways. Alright, to begin we must install samba through the aptitude package manager. At the prompt type...

    sudo apt-get install samba

You will be prompted for the root password. Enter it and the shell will tell you the size of the package and ask you if you really want to install it. hit [Y] and allow the package to install.

Part 3: Configuring Samba

Once installed a number of directories that pertain to the Samba service and configuration will be created. I will explain the ones we will be using in this section. The first and probably most important is the actual service script itself located in /etc/init.d/samba you will use this script to start, stop and restart the service.

Note: anytime you change the samba configuration you must restart the service for the changes to take effect. the commands to start, stop and restart the service are as follows...

    /etc/init.d/samba stop (stops the service)
    /etc/init.d/samba start (starts the service)
    /etc/init.d/samba restart (restarts the service)

Like alot of applications in Linux all configuration changes that will be made to Samba will be done through its configuration file smb.conf located at /etc/samba/smb.conf So lets get right into it and configure this bad boy. you can use any text editor you feel comfortable with for this step. I prefer vi because its simple and you can expect it to be installed by default on all versions of Linux.

    sudo vi /etc/samba/smb.conf

You will see a lot of information but there are only a few lines that we need to concern ourselves with for basic file sharing. These entries are as follows.

Note: Lines that begin with "#" are considered comments and are ignored by Samba. Also, I like to delete everything from this configuration file and start from scratch so that the only entries in the file are entries I actually use. Doing this makes administration a little bit simpler.

workgroup = Enter your MS workgroup here

netbios name = The netbios name the samba machine will use

server string = The name you want your server to appear as under network neighborhood

#===== Share Definitions =====
# Configure your Shares under here. You Configure shares like this....

comment = My shares
path = /tmp
read only = No
guest ok = Yes

    # The above share will share the /tmp directory with everybody. No user #name or password required
    # To add some basic security you can restrict access to certain users like so...

comment = Veronicas Stuff
path = /home/Veronica/Documents
valid users = Veronica
public = no
writable = yes

    #The above share specifies user Veronica as the only valid user able to access #the specified share. On a windows machine you will see a share called #VeronicasDir and the share will require a valid username and password #(veronicas) to access.

Note: In order for user authentication to work properly the user needs to have a valid account on the Linux system and the user needs to have an SMB password configured to work with his or her account. configuring a user account and SMB password is done by using the adduser and smbpasswd commands:

    adduser Veronica

You will be prompted to enter a UNIX password for the user and enter some basic information. Once completed assign the user an smb password:

    smbpasswd -a Veronica

Enter a password. If the user will be accessing the share from a Windows machine it would be wise to use the same username and password so no confusion on the client side will take place. After this step is completed User Veronica can now access her home directory!

Once you have the above smb.conf configuration, save the file and restart the samba service. If you entered everything correctly in the configuration and the samba service is started, you should be able to see and navigate the Linux shares from a windows computer! Congratulations you have succesfully networked Linux and Windows using samba!

Part 4: Accessing a Windows share from Linux

Ok, so we configured Linux so Windows computers can access the shares we specified but what if a Linux machine wants to obtain a resource from a Windows computer? Dont worry, we dont have to configure another text file but we do need to become familiar with the smbclient command. This command uses the smb protocol to view and navigate shares on a windows computer. It is fairly straightforward and some basic commands are as follows.

    smbclient -L host

this will list the remote shares on a windows host and looks something like this:

Domain=[HOST] OS=[Windows 5.1] Server=[Windows 2000 LAN Manager]

Sharename Type Comment
--------- ---- -------
My Stuff Disk Put stuff here

Once you know whats on the machine you can now connect to the share:

    smbclient \\HOST\My Stuff

Enter the proper username and password if required and happy sharing! Once your in the directory, most of the basic Linux commands work. you can type help to get a list of commands you can use to manipulate the files.

Final Notes and Some Helpful Links

This is a very basic tutorial for configuring Samba on a windows network. There is a lot you can do with Samba that is not covered here and i encourage you to pursue the options! Whatever the case I hope you found this tutorial informative and most of all i hope the tutorial worked for you! Below you will find some suggested reading that really helped me out when i first started out. Thanks for reading.