Install Samba on Ubuntu 20.04 | 18.04

installing ssl nginx

This brief tutorial shows students and new users how to install and configure Samba on Ubuntu 20.04 | 18.04.

Samba is an open source implementation of SMB/CIFS protocol that allows users to access shared files printers and other network resources.

Samba enables Linux systems, including Ubuntu to share files with Windows systems, including Windows 10 and other operating systems.

For this tutorial to work, you may want to configure both the Windows and Ubuntu machines on the same subnet. This means they should be able to communicate over the network.

For this tutorial, we’re going to be using a 192.168.1.0/24 network.

The Windows machine will have IP address 192.168.1.2 and the Linux machine 192.168.1.3

Both machine will also be in the same local workgroup. You can name the workgroup whatever you want, but for this post, our workgroup will be the default Windows workgroup called WORKGROUP.

Windows IP address =========================> 192.168.1.2
Ubuntu IP address ==========================> 192.168.1.3
Workgroup Name ===========================> WORKGROUP

Both the Windows and Ubuntu machines will be member of the local domain or workgroup called WORKGROUP.

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 = /samba/public
   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 /samba/public

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

sudo chown -R nobody:nogroup /samba/public
sudo chmod -R 0775 /samba/public
sudo chgrp sambashare /samba/public

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 richard

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 richard
sudo smbpasswd -e richard

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
  writable = yes
  browsable = yes

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

ubuntu sharing windows 10

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….

ubuntu samba shares

Enjoy!

ubuntu samba windows

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,

You may also like the post below:

6 thoughts on “Install Samba on Ubuntu 20.04 | 18.04

  1. Who is ‘richard’?
    I mean, I’ve created a group, ‘mygroup’, and want to add users. Do I first need to create ubuntu users and then samba users (with the same name)?
    Is ‘richard’ a previously created samba- or ubuntu user?

  2. That line `sudo chgrp sambashare /samba/public`
    is breaking ability to saving files/folders in public area

  3. If not showing on network browser in Windows –

    Find the ip address of the ubuntu computer (type ip addr in a terminal to find it)
    Then in the address bar type \\your_ip_address\ in Windows file explorer (eg. \\10.168.4.34\ where 10.168.4.34 is the ubuntu’s address)

    If still not showing or windows cannot find the share and shows an error, check the last characters of interfaces line ( interfaces = 127.0.0.0/8 eth0 ) in smb.conf. Is it the same as the the interface (network card name) when ip addr was used in the terminal? (eg. 2: ens18: ).
    Ignore the 1: lo: <LOOPBACK, line.
    If not change eth0 to the network interface name (eg. in above, ens18) save and exit.
    Restart Samba. sudo service smbd restart

    To be able to write to the public folder reverse the sudo chgrp sambashare /samba/public command that stops this, use sudo chgrp nogroup /samba/public.
    Restart Samba. sudo service smbd restart

  4. 1. I type my Toshiba Windows Laptop IP address into the “connect to server bar” in nautilus

    2. I can see my shared folder on the Toshiba Windows Laptop

    3. However, when I try to access the folder Nautilus throws up a window
    Saying ” Password required for share [Shared folder name].
    Asking to connect as Anonymous or registered user
    Also asking for a user name, Domain, and Password

    For the life of me, I can’t get through
    I have not set a password share in windows
    it does not accept my SMB user name and Password

    Help !!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.