How to enable Samba sharing in Ubuntu

Cover Image for How to enable Samba sharing in Ubuntu

How to enable Samba sharing in Ubuntu

How-to written and screenshots taken on 2020 March 67 min read

In Windows it's easy to get file sharing to work. At least I can make it work all the time. Part of it is the graphical interface, but file sharing in Linux, in my case Ubuntu has it's peculiarities. Even if you use the Ubuntu GUI tools, it doesn't always work. It has cost me countless hours over the years and every time I have to reconfigure Samba I'm in hell. So this is my guide on how to enable Samba sharing in Ubuntu.

In this example we have the server user joediapna, two actual people diana and joep and a shared (somewhat public) computer account in the living room discobal.

1. Prepare

  1. List the folders or volumes you want to share. I want to share two volumes, or to be more precise a single folder in each volume. The Workspace volume is where for example software installers are shared and it has private folders for the "real" users. The second volume Data is used to share data between all computers. I want the server to be the owner of all volumes/data, except the private folders.

    One more thing: the sharename cannot have the same name as the folder it represents! It is however case-sensitive so Data is not the same as data.

Folder / volumeSharenameOwner
  1. List groups and permissions. To make life easier, I'll group users together in two groups. The first group joediapna can read and write anywhere (except the private folders). The sambashare group can only read anywhere (except the private folders). We need the x to build the tree of subfolders.
joediapnarwx / 7rwx / 7
sambasharer-x / 5r-x / 5
other usersr-x / 5r-x / 5
  1. List the users. We have 4 users split into 2 groups.
dianaAdminjoediapna, sambashare
joediapnaOwnerjoediapna, sambashare
joepAdminjoediapna, sambashare

2. Permissions

  1. Mount the volumes you want to share. To mount volumes you should make mount points (folders) and mount the volume to that folder. I have ZFS volumes which should mount automatically after every reboot. If you are mounting other types of volumes (NFS), you have to add them to /etc/fstab. Please Google for that (guide from me is coming some day too).

    As said, I'm mounting two ZFS volumes: Data and Workspace. So I create two mount directories: /mnt/Data and /mnt/Workspace to which I mount the ZFS volumes.

    sudo mkdir /mnt/Data
    sudo mount Data /mnt/Data
    sudo mkdir /mnt/Workspace
    sudo mount Workspace /mnt/Workspace
  2. Check if everything is mounted. It should show the two mounted volumes.

    df -h
  3. Check with ls -l for current owner and permissions.

    cd /mnt/Data
    ls -l
    1. Data: The owner should be joediapna:joediapna and the permissions should be drwxrwxr-x (775). With these permissions the owner (user joediapna) can do everything, the group joediapna can do everything. All other users can only read. We are going to apply these permissions on all the files and folders.
    sudo chown -R joediapna:joediapna /mnt/Data/data
    sudo chmod -R 775 /mnt/Data/data

    Folder permissions of /mnt/Data/data

    1. Workspace: The owner should again be joediapna:joediapna and the permissions should also be drwxrwxr-x (775). We are not going to apply these permissions on all the files and folders. We'll start with the main folder only
    cd /mnt/Workspace
    ls -l
    sudo chown joediapna:joediapna /mnt/Workspace/workspace
    sudo chmod 775 /mnt/Workspace/workspace

    Folder permission of /mnt/Workspace/workspace

    1. Now go in one level deeper and check for each folder what the permissions should be. Apply them for each folder recursively.
    cd /mnt/Workspace/workspace
    ls -l
    sudo chown -R joediapna:sambashare /mnt/Workspace/workspace/download
    sudo chmod -R 775 /mnt/Workspace/workspace/download
    sudo chown -R joep:joep /mnt/Workspace/workspace/joep
    sudo chmod -R 700 /mnt/Workspace/workspace/joep

    Folder permissions of Workspace folders

3. Users and groups

  1. Create groups. The joediapna group already exist as it was made with the initial user during setup of Ubuntu. We only have to make the sambashare group.

    sudo groupadd sambashare
  2. Create users and add them to the groups. We do have to create 3 users: diana, discobal and joep. This is the example for the first.

    sudo useradd -m diana -p <PASSWORD>
    sudo usermod -L diana
    sudo usermod -a -G joediapna diana
    sudo usermod -a -G sambashare diana

4. Install and configure Samba

  1. Install Samba:

    sudo apt update
    sudo apt install samba
  2. Open the file Samba configuration file:

    sudo nano /etc/samba/smb.conf
  3. Change the workgroup to your Windows workgroup if you have one. I have called it BALZAAL.

    # Change this to the workgroup/NT-domain name your Samba server will part of
    workgroup = balzaal
  4. Scroll all the way to the bottom and add the shares.

    path = /mnt/Data/data
    browseable = yes
    writeable = yes
    create mask = 0775
    directory mask = 0775
    guest ok = no
    path = /mnt/Workspace/workspace
    browseable = yes
    writeable = yes
    create mask = 0775
    directory mask = 0775
    guest ok = no
  5. Save and close by pressing Ctrl-O to save and Ctrl-X to close. Now restart Samba.

    sudo service smbd restart
  6. Add a rule in the firewall to allow Samba traffic.

    sudo ufw allow samba

5. Now the peculiar part

  • The Samba user passwords aren't the same as the system users we just created. I always forget that. We have to add that password to Samba too. So for each user we enter the command below. This command will ask to enter the password for that user.

    sudo smbpasswd -a diana

6. Check installation

  1. If everything went right, which it never does in my experience, you should now be able to browse these shares. From a Unix machine (Mac or Linux) type smb:// or from Windows: \\\Data.

  2. If it doesn't work... First check if Samba is installed and running:

    smbd --version
    systemctl status smbd
    systemctl status nmbd
  3. If yes, try to restart the Samba service:

    sudo systemctl restart smbd
    sudo systemctl restart nmbd
  4. Then install a small Samba GUI. It's a GUI, so you can only run it through VNC. See if you see anything strange there.

    sudo apt install system-config-samba
    sudo system-config-samba
    sudo touch /etc/libuser.conf