Networking

How to set up Samba shares for groups

Samba is an incredibly powerful tool for sharing directories. Here's how to use Samba, in conjunction with the built-in user and group tools, to make Samba easier to administer.

sambahero.jpg
Image: Jack Wallen

If you make use of a Linux server in your company, and do so for sharing files, then you know Samba. Samba is a very powerful and flexible interoperability suite of programs for Linux and Unix that allows you to share out directories to UNIX and non-UNIX platforms. Of course, Samba can do much more than that; but for now we're going to focus on sharing. In particular, sharing for groups.

Adding groups into the Samba mix means you can gain a more efficient means of controlling what users can see what.

I'm going to walk you through the steps of creating a new Samba share, give a group access to that share, and then assign users to said group. I will assume you already have your Linux server up and running. I'll be demonstrating on the Ubuntu Server platform.

Install Samba

In case you already haven't done so, we must first install Samba. To do this, open up a terminal window and issue the command:

sudo apt-get install samba samba-client samba-common

Once the installation completes, you'll want to enable Samba to start at boot with the following commands:

sudo systemctl enable smbd.service
​sudo systemctl enable nmbd.service
​sudo systemctl restart smbd.service
​sudo systemctl restart nmbd.service

Create the necessary directory and group

Before we configure Samba, let's create the necessary directory and group. We'll then add users to the group.

I'll be creating a new share called editorial. Create a new directory with the command:

sudo mkdir -p /opt/editorial

Now let's create the group editorial with the command:

sudo groupadd editorial

Now we change the group ownership and permissions of the directory with the commands:

sudo chgrp editorial /opt/editorial
​sudo chmod -R 770 /opt/editorial

Now we add users to the new group with the command:

sudo usermod -a -G editorial USER

Where USER is the username to add to the group.

If you ever need to remove a user from a group, this can be done with the command:

sudo deluser USER GROUP

Where USER is the username and GROUP is the group name.

Finally, we must add the users to Samba. This is done with the smbpasswd command like so:

sudo smbpasswd -a USER
​sudo smbpasswd -e USER

Where USER is the username to be added. The first command adds the user and the second command enables the user. When issuing the first of the above commands, you will be prompted to create a new Samba password for the user.

Configure Samba

Now we come to the actual Samba configuration. The first thing we're going to do is make a backup copy of our Samba configuration file. Issue the command:

sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.BAK

Now create a new configuration file with the following command:

sudo nano /etc/samba/smb.conf

In this new file, we'll add the following contents to share out our editorial directory to the group editorial (customize as needed):

[global}
​workgroup = WORKGROUP
​server string = Editorial Server
​netbios name = Ubuntu
​security = user
​map to guest = bad user
​dns proxy = no

#### SHARES ####

[editorial]
​path = /opt/editorial
​browsable = yes
​writable = yes
​guest ok = yes
​read only = no
​valid users = @editorial

Save and close that file. Restart Samba with the commands:

sudo systemctl restart smbd.service
​sudo systemctl restart nmbd.service

You can now point one of your machines to the newly configured Samba share. So long as the user is a member of the editorial group, they'll be able to log on with their username and samba password.

Even more flexibility

By working with groups in Samba, you can make your admin life slightly easier, while making Samba more flexible. With this way you can add and remove users to the group with ease (which, in turn, would revoke their access to the Samba share).

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks