Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Saturday, 9 May 2015

Introduction to Samba - Share Files and Directories between Linux, Windows and Mac

   Samba is the most popular and efficient way with which you can share your files and directories between Linux, Windows and Mac. You just have to create a Samba user, Decide which file/directory you wish to share and Set the permissions, in order to create a Samba share. Well, initially, this might seem to be difficult- especially for beginners, but when you understand all the configuration options, it will seem to be as simple as anything.


This tutorial will explain how to share files and directories on Linux with Windows and Mac clients.

Step 1: Installing Samba

For Red Hat Linux and derivatives, Samba can be installed using yum as follows:

$ yum install samba smbfs
For Debian and derivatives, you can use apt-get as follows (Know more about apt-get here):

$ sudo apt-get install samba smbfs

Step 2 : Samba Configuration

    The best part in samba is, there is a single configuration file - /etc/samba/smb.conf. So, before making any configurations, it's always a better practice to have this file backed up, so that, if anything goes wrong, we can revert the changes back to original ones pretty easily.

$ cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
Now that, we can move forward to edit this file. Let's do it.

$ vi /etc/samba/smb.conf
    When you open the smb.conf file, you'll typically observe three sections here - [global], [share], [homes] and [printers]. Let's now see the significance of each of them:
  • [global]: As the name denotes, setting defined here will be common to all the subsequent sections.
  • [share]: Settings related to the shares are defined here.
  • [homes]: If this section is present and when a user tries to connect to share_name which is not configured in smb.conf, then it is assumed that share_name is a Linux user and it is trying to connect to his home directory.
  • [printers]: Settings for shared printers appear here.
Here is an example of /etc/samba/smb.conf file:

[global]
    workgroup = MYGROUP
    security = user
    passdb backend = tdbsam
    cups options = raw
    load printers = yes
    log file = /var/log/samba.log.%m

[homes]
    comment = Home Directories
    browseable = no
    writable = yes

[share_name]
    comment = SOME_COMMENT_HERE
    path = /path/to/share
    writable = yes
    browseable = yes
    public = no
    ...
    SOME MORE PERMISSIONS HERE
    ...

[printers]
    comment = All Printers
    path = /var/spool/samba
    guest ok = no
    printable = yes
    browseable = no
Above is just an example of /etc/samba/smb.conf configuration. It might vary from system-to-system, but the syntax would be the same. The values in the colored format can be adjusted to meet your requirements. Let's now understand the configuration part in more detail:

  • security: Here, we will specify the mode of authentication Samba should use to authenticate a user. It can be any of the following:
    • user: It needs to create a Samba user and this samba user must also be the Linux user.
    • domain: A user account present in a centralized account repository shared between domain controllers.
    • ads: Active directory security mode.
  • writable: To set Write permission for the share.
  • read only: To set Read only permission for the share.
  • browsable: To set browsing permissions for the share.
  • read list: Users to be given read only permissions to the share.
  • write list: Users to be given write permissions to the share.
  • valid users: Users allowed to access the share.
  • invalid users: Users to be prevented from accessing the shares.
  • hosts allow: IP addresses allowed to access the share.
  • create mask: Permissions to be given when users create anything inside the share.

Step 3. Creating a Samba User

The most important thing to be remembered is - A Samba user must also be the actual system user. So, in order to create a new Samba user, you must ensure that, that user must be a system user. If it is not, then add that user to the system first.

So, in case the user is the system user and we wish to create a samba user, use the smbpasswd command as follows:

$ smbpasswd -a USERNAME
You will be asked for a Samba Password, when you're done, it will create a Samba user for you.

Example:

Here, I need to create a share with name myshare and it must be accessed by the user mandar only. For that, I must create a system user first.

$ useradd mandar
$ passwd mandar
Then, I must create a Samba user.

$ smbpasswd -a mandar
Time to make some configurations.

$ vi /etc/samba/smb.conf

[global]
    ...
    ...
    ...

[myshare]
    comment = This is Mandar's Share
    path = /smbshare/mandar
    valid users = mandar
    hosts allow = 192.168.100.3
    writable = yes
    public = no

[printers]
    ...
    ...
When we make any configuration changes, we must restart the services.

$ /etc/init.d/smb restart

OR

$ service smb restart
And we're done. If we try to access this share from the system 192.168.100.3 with the user mandar, it should work. In order to access this share from my windows machine, I would execute in the 'RUN' box - \\[samba-server-ip]\myshare.

3 comments:

  1. Thanks for taking the time to share this helpful tutorial Mandar.

    ReplyDelete
  2. Thank you. Keep ��

    ReplyDelete
  3. Mac support for Samba is horrible, slow and buggy. Not recommended!

    ReplyDelete