Install Samba on Ubuntu 20.04

Step 1: Identify Windows Workgroup

To find out which Workgroup Windows machine belongs, open the command prompts and type the commands below

net config workstation

When you run the commands above, you should see your current Workstation domain name for the computer, usually called WORKGROUP.

windows computer workgroup

Step 2: Add Ubuntu to Windows Host File.

If you don’t have a DNS system in place and you want to reference each system by their names, you’ll want to add their names in the local host file on each machine..

For Windows system, open the commands prompt as administrator and run the commands below.

notepad C:\\Windows\System32\drivers\etc\hosts

Then add the local entry for the Ubuntu machine to be referenced by the named ubuntu16.04

192.168.1.3            ubuntu2004.localhost           ubuntu2004

Save your changes and you’re done.

To add Windows system name to Ubuntu host file, press Ctrl + Alt + T on your keyboard to open the command terminal.

Then run the commands below:

sudo nano /etc/hosts

Next, type the IP with hostname for Windows machine, save the file and exit.

STEP 3: Enable File Sharing

To make file sharing possible, that feature must be enabled on Windows systems. To enable it, run the commands prompt as administrator and run the commands below

Then run the commands below to enable filesharing and network discovery.

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes
netsh advfirewall firewall set rule group="Network Discovery" new enable=Yes

File sharing should be enabled on Windows machine after running the commands above.

Step 4: Install Samba on Ubuntu

At this point, Windows and Ubuntu systems should be member of the same workgroup and both systems have entries in their local host file to reference the other by name.

Next, logon on to the Ubuntu machine to install Samba. To install Samba, run the commands below.

sudo apt update
sudo apt install samba

After installing Samba, go and config Samba.

To verify if Samba services are running, run the commands below:

sudo systemctl status smbd

It should output similar lines as below:

mbd.service - Samba SMB Daemon
     Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: >
     Active: active (running) since Wed 2020-05-06 18:23:55 CDT; 4min 27s ago
       Docs: man:smbd(8)
             man:samba(7)
             man:smb.conf(5)
    Process: 2026 ExecStartPre=/usr/share/samba/update-apparmor-samba-profile (>
   Main PID: 2039 (smbd)
     Status: "smbd: ready to serve connections..."
      Tasks: 4 (limit: 4657)
     Memory: 14.9M
     CGroup: /system.slice/smbd.service
             ├─2039 /usr/sbin/smbd --foreground --no-process-group

Step 5: Configure Samba Public share

Now that Samba is installed, run the commands below to backup its default configuration file.

sudo cp /etc/samba/smb.conf{,.backup}

Next, open Samba configuration file by running the commands below.

sudo nano /etc/samba/smb.conf

Then make sure you setup the highlighted lines to match the ones below.

======================= Global Settings =======================
[global]
## Browsing/Identification ###
# Change this to the workgroup/NT-domain name your Samba server will part of
   workgroup = WORKGROUP
   netbios name = ubuntu2004
   security = user
   proxy = no
   map to guest = bad user
#### Networking ####
# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
   interfaces = 127.0.0.0/8 eth0

# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
bind interfaces only = yes

#Most people will want "standalone server" or "member server".
#Running as "active directory domain controller" will require first

server role = standalone server
obey pam restrictions = yes

#This boolean parameter controls whether Samba attempts to sync the Unix
[public]
   path = /media/hdd4tb.ntfs/share
   browseable = yes
   guest ok = yes
   guest only = yes
   read only = no
   force user = nobody
   force create mode = 0777
   force directory mode = 0777

Once done, save your changes. Then run the testparm utility to check the Samba configuration file for errors.

Restart Samba services.

sudo systemctl restart smbd

Step 6: Create the public folder

Next, create the public folder where everyone should have access to as defined in Samba configuration above…

sudo mkdir -p /media/hdd4tb.ntfs/share

Set the permissions so that everyone can read and write to it.

sudo chown -R nobody:nogroup /media/hdd4tb.ntfs/share
sudo chmod -R 0775 /media/hdd4tb.ntfs/share
sudo chgrp sambashare /media/hdd4tb.ntfs/share

Restart Samba and open Windows File Explorer to view the shared location on Ubuntu

sudo service smbd restart

Now go to your Windows machine and you should see the shared public folder on Ubuntu from when you browse File Manager as shown below.

ubuntu samba sharing

Everyone should have access there.

Step 6: Configure Samba Private Share

Now you know how to create Samba public shares, let’s go and create private and protected shares. Only users that are member of the approved group will be able to access the secure location with passwords.

First create a samba group called smbgroup for the share.. only members will have access. To create a groups in Ubuntu, run the commands below.

sudo addgroup smbgroup

Then add a user to the group by running the commands below

sudo usermod -aG smbgroup pi

Finally, all users who need to access a protected samba share will need to type a password. To add a user to samba password database, run the commands below for each user.

sudo smbpasswd -a pi
sudo smbpasswd -e pi

The user will be prompted to enter and confirm a password. This password will be used to access the protected samba shares.

Next, go and create a protected share in the /samba directory.

sudo mkdir -p /samba/protected

Then give only root and members group access to this share.

cd /samba/
sudo chown -R root:smbgroup protected
sudo chmod -R 0770 protected

When you’re done creating the protected share, go and share it in the smb.conf file.

sudo nano /etc/samba/smb.conf

Then add configuration block below into smb.conf file just below the one above

[Protected]
  path = /samba/protected
  valid users = @smbgroup
  guest ok = no
  read only = no
  browseable = yes
  force create mode = 0660
  force directory mode = 2770

Save your changes and you’re done.

Restart Samba and test your changes.

sudo service smbd restart

You should now see two folders… one is protected

Many more shares can be defined using the format above.

Only member of the smbgroup will be able to access the Protected area…

You could map the drive in Windows for easy access….

Enjoy!

That’s it!

Conclusion:

This post showed you how to install Samba on Ubuntu to allow file sharing. If you find any error above, please use the comment form below to report.

Thanks,

Leave a Reply

Your email address will not be published. Required fields are marked *