How to Set up a Samba Server in Ubuntu 20.04

Samba is a free software re-implementation of the SMB networking protocol. It let's you share files and print services on a network. It makes it possible for both Windows, MacOS and GNU/Linux clients to use the same services.

This tutorial will walk you through how to set up a Samba server with a public share and a private share. No shared printers or other (extra) features. I run my Samba Server on a Raspberry Pi with Ubuntu Server 20.04 LTS and it works great for my home network.

Let's get going!

First off, install Samba:

sudo apt update
sudo apt install samba

Check if the Samba daemon is running:

sudo systemctl status smbd

It should show that Samba is active and running.

Create the directory for the public Samba share and set the permissions:

sudo mkdir -p /srv/samba/public
sudo chmod -R 777 /srv/samba/public

The public share is open for all on your local network and has no authentication required. It's nice for sharing files with others on your home network.

Set up the private Samba share (name it how you want) by creating the directory for it, setting the correct ownership and permissions, adding a user on the server, and at last adding and enabling your Samba user.

sudo mkdir /srv/samba/nouptime
sudo chown nouptime:nouptime /srv/samba/nouptime/
sudo chmod 700 /srv/samba/nouptime
sudo adduser --home /srv/samba/nouptime --no-create-home --shell /usr/sbin/nologin nouptime
sudo smbpasswd -a nouptime
sudo smbpasswd -e nouptime

Backup your Samba configuration file (smb.conf):

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.original

Edit smb.conf (I use GNU nano to edit it, but you can use your favorite text editor):

sudo nano /etc/samba/smb.conf

Delete all the content of the file and paste in the following (edit it to your needs):

#======================= Global Settings =======================

[global]
   workgroup = WORKGROUP
   security = user
   map to guest = bad user
   dns proxy = no
   load printers = no
   printing = bsd
   printcap name = /dev/null
   disable spoolss = yes
   show add printer wizard = no

#======================= Share Definitions =======================

[public]
   comment = Shared directory
   path = /srv/samba/public
   read only = no
   writeable = yes
   browseable = yes
   guest ok = yes

[nouptime]
   comment = Private directory
   path = /srv/samba/nouptime
   read only = no
   writeable = yes
   browseable = yes
   guest ok = no
   valid users = nouptime
   smb encrypt = required

I set up encryption for the private share for added security.

Check smb.conf for correctness:

testparm

Restart the Samba daemon so the changes we made take effect.

sudo systemctl restart smbd

Allow Samba through your firewall. I use ufw for this.

sudo ufw allow samba

Optional: Automatically Backup Your Data to an External Device

Backup is a must, so I figured out I'd write up a little guide to show you how to configure an easy solution. You just need some kind of external device, like an external hard drive or an USB flash drive.

Format a device and make a partition with ext4 using GParted or similar. This will wipe all the data on that device. You could use a device you already have without formatting it, but I like to keep things organized and use it only for this purpose, which is to backup the data on the Samba server.

Make a directory where you can mount your external drive and fix the ownership of the directory:

sudo mkdir /media/backup
sudo chown ubuntu:ubuntu /media/backup

"ubuntu" is the default user on the Ubuntu Server 20.04 LTS image for the newest Raspberry Pi.

Find the name of your external drive:

lsblk

Mount the external drive (use your own output from the command above):

sudo mount -t ext4 /dev/sda1 /media/backup

Let's get it to mount automatically when you start your server.

Find information about your mounted device:

df -T

You'll get an output similar to this:

/dev/sda1      ext4      29438948   44992  27875468   1% /media/backup

Let's edit the fstab file so that your backup device gets mounted during the boot up:

sudo nano /etc/fstab

In my case it was as simple as adding this:

#device      mountpoint        fstype     options      dump    fsck
/dev/sda1    /media/backup     ext4       defaults     0       0

Test your configuration by mounting all the fstab entries:

sudo mount -a

If you get no errors, you should be fine, and your fstab file is most likely correctly edited.

Reboot your server to see if the device gets mounted when you boot up:

sudo reboot now

Check your mounted file systems/devices with "df -T" again. You should be all set now to get the automatic backup job going.

Now set up a cron job for the automatic backup to take place.

As root:

crontab -e

This will let you edit the crontab file. To run a backup of your Samba server every night at 03:00, add the following line:

0 3 * * * rsync -a --delete /srv /etc/samba/smb.conf /media/backup

This makes an exact copy of your data in /srv and of your smb.conf file and stores it in /media/backup.

That's it! Have fun and enjoy your new network storage solution.

No Uptime

No Uptime

I'm the only author on this blog and I do all the administration stuff. I guess that makes me the administrator.
The Internet