Easily Install and Configure Samba File Server on Ubuntu 20.04

|
Last Updated:
|
|

In this tutorial, you will learn how to easily install and configure Samba file server on Ubuntu 20.04. Samba is an opensource suite that implements the Server Message Block (SMB) protocol. Microsoft Windows operating systems and the OS/2 operating system use SMB to perform client-server networking for file and printer sharing and associated operations. Running on a Unix system, it allows Windows to share files and printers on the Unix host, and it also allows Unix users to access resources shared by Windows systems. It is therefore a very useful networking tool for anyone who has both Windows and Unix systems on their network.

Installing and configure Samba File Server on Ubuntu 20.04

Samba can be run as:

  • An Active Directory (AD) or NT4 domain member
  • A standalone server
  • An NT4 Primary Domain Controller (PDC) or Backup Domain Controller (BDC)

In this setup, we will run Samba as a standalone file server on Ubuntu 20.04.

Step through the following steps to learn how to easily install and configure Samba file server on Ubuntu.

Install Samba on Ubuntu 20.04

To easily install and configure Samba File Server, you first need to install Samba packages by running the commands below;

apt update
apt install samba smbclient cifs-utils

Configure Samba File Server on Ubuntu 20.04

Once the Samba package is installed, proceed to configure Samba file server on Ubuntu 20.04. As already stated, in this setup, we will run Samba as a standalone file server on Ubuntu 20.04. This means that Samba is not configured as a member of any directory service and thus, local system database will be used for authenticating users to access shared files.

Create a Shared Directory/Folder

Create a directory where you will place files to be shared. You can create a public or a private shared folders.

For example, we use /smb-public and /smb-private as our shared directories in this setup.

mkdir /smb-public
mkdir /smb-private

We will allow public access to the public folder and a few users to access the private folder.

Configure Samba File Server on Ubuntu 20.04

Open the Samba configuration file for editing;

vim /etc/samba/smb.conf
Global Samba configuration options

Below is our global Samba configuration, with comment lines removed.


[global]
   unix charset = UTF-8
   workgroup = WORKGROUP
   server string = %h server (Samba, Ubuntu)
   log file = /var/log/samba/log.%m
   log level = 1
   max log size = 1000
   logging = file
   panic action = /usr/share/samba/panic-action %d
   server role = standalone server
   obey pam restrictions = yes
   unix password sync = yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
   pam password change = yes
   map to guest = bad user
   usershare allow guests = yes

Consult man smb.conf for a description of the configuration options used.

Configure file or printer sharing

For now, we will only be configuring file sharing. Hence, at the end of the Samba configuration file, add the share name and the configuration options.

In above, we created two directories, a public and a private one. We will set the public share as publicly accessible and private one will require authentication to access it.

Public Share Configuration

Example public share configurations.


[publicshare]
   path = /smb-public
   writable = yes
   guest ok = yes
   guest only = yes
   force create mode = 775
   force directory mode = 775
Private Share Configuration

Example Private share configuration.


[privateshare]
   path = /smb-private
   writable = yes
   guest ok = no
   valid users = @smbinternal
   force create mode = 770
   force directory mode = 770
   inherit permissions = yes
Create Samba Share User Group

The above Private share will only allow users of the smbinternal group to access the share.

Therefore, let us create an smbinternal group and add specific users to this group to allow access to the private share.

groupadd smbinternal
Update the permissions of the Shares

Change the group of the shared private directory;

chgrp -R smbinternal /smb-private/
chgrp -R smbinternal /smb-public

Set the permissions of the directory;

chmod 2770 /smb-private/
chmod 2775 /smb-public

The value 2 above represents SGID bit. This makes the new files/folders created to inherit the group of the parent directory instead setting it to the users primary group.

Next, create local accounts for the users you would like to give access to the private share. The users doesnt need to have the shell.

useradd -M -s /sbin/nologin demouser

Add the user to the smbinternal group;

usermod -aG smbinternal demouser

Create SMB password for the user;

smbpasswd -a demouser

Enable the Samba account:

smbpasswd -e demouser

Verifying the Samba configuration

It is  recommended that you verify the Samba configuration each time you update the /etc/samba/smb.conf file using the  testparm utility 

You can simply execute it as follows:

testparm

Sample output;


Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
	log file = /var/log/samba/log.%m
	logging = file
	map to guest = Bad User
	max log size = 1000
	obey pam restrictions = Yes
	pam password change = Yes
	panic action = /usr/share/samba/panic-action %d
	passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
	passwd program = /usr/bin/passwd %u
	server role = standalone server
	server string = %h server (Samba, Ubuntu)
	unix password sync = Yes
	usershare allow guests = Yes
	idmap config * : backend = tdb


[publicshare]
	force create mode = 0775
	force directory mode = 0775
	guest ok = Yes
	guest only = Yes
	path = /smb-public
	read only = No


[privateshare]
	force create mode = 0770
	force directory mode = 0770
	inherit permissions = Yes
	path = /smb-private
	read only = No
	valid users = @smbinternal

In case of any error, fix it before you can proceed.

Restart Samba;

systemctl restart smbd

Create test files/folders on the shares;

mkdir /smb-private/demofolder-priv /smb-public/demofolder-pub
touch /smb-private/demofile-priv /smb-public/demofile-pub

Allow Remote Access to Samba

To allow remote access to Samba from a specific network;

ufw allow from 192.168.59.0/24 to any app Samba

Accessing SMB Shares from Clients

To learn how to access SMB share from various clients, follow the guide below;

How to Access Samba Share on Linux and Windows Systems

Other Tutorials

Quick Way to Setup Samba File Server on Debian 10

Install and Setup GlusterFS Storage Cluster on CentOS 8

How to Configure NFS Server on Ubuntu 18.04 Server

How to Install and Configure NFS Server on RHEL/CentOS 7

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment