How to Install and Configure Samba File Sharing on Ubuntu 20.04

Samba is a suite of open source applications that implements the Server Message Block (SMB) protocol. Many operating systems, including Microsoft Windows, use the SMB protocol for client-server networking. Samba enables Linux / Unix machines to communicate with Windows machines in a network. Samba provides file and print services for various Microsoft Windows clients and can integrate with a Microsoft Windows Server domain, either as a Domain Controller (DC) or as a domain member.

Sharing files is an essential part of server administration. It allows sharing of resources across the network which are needed by users to carry out their tasks. Samba server is usually installed on central Linux server where information is stored in variety of media and accessed using windows client or Linux using Samba client.

In this article, we will be learning how to install and configure samba file sharing on Ubuntu 20.04. Ubuntu 20.04 will be configured as the Samba server then we can access the files with either Windows or Linux clients.

Related Content:

# Ensuring that the server is up to date

Before proceeding, let us ensure that our server is up to date and all the packages are the latest version. Use these commands to achieve this:

1
2
sudo apt update
sudo apt upgrade -y

If there are packages to upgrade, the above command may take a couple of minutes.

# Install Samba packages

The samba packages are available in the default Ubuntu repositories. Install them with this command:

1
sudo apt install samba

When prompted to confirm the installation process, typeYthen enter to proceed.

We can check if the installation was successful by running:

1
2
3
$ whereis samba

samba: /usr/sbin/samba /usr/lib/x86_64-linux-gnu/samba /etc/samba /usr/share/samba /usr/share/man/man8/samba.8.gz /usr/share/man/man7/samba.7.gz

Confirm the installed software package version with this command:

1
2
3
4
5
6
7
8
9
$ apt-cache policy samba
samba:
  Installed: 2:4.13.14+dfsg-0ubuntu0.20.04.4
  Candidate: 2:4.13.14+dfsg-0ubuntu0.20.04.4
  Version table:
 *** 2:4.13.14+dfsg-0ubuntu0.20.04.4 500
        500 http://eu-west-3.ec2.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
        100 /var/lib/dpkg/status

# Configuring Samba shares

The samba configuration file is located in this path /etc/samba/smb.conf. Before doing any modification, let us create a backup of it so if we mess we have a copy. Use this command in the terminal to create a backup file:

1
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bk

Next, let us create a directory to be shared.

1
sudo mkdir -p /home/sambapublicshare

Let us now edit the samba config to define the export for the directory we created.

Open the config file:

1
sudo vim /etc/samba/smb.conf

Update the configuration in the file to reflect the following:

1
2
3
4
5
6
7
[Public]
    path = /home/sambapublicshare
    comment = Citizix Samba Shares
    browsable =yes
    writable = yes
    guest ok = yes
    read only = no

Save and close the file.

Verify the configuration using this command:

1
sudo testparm

You should see something similar to this if all is well.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ sudo testparm

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

Press enter to see a dump of your service definitions

# Global parameters
...

# Enable Samba service through Firewall

If you have ufw firewall installed and enabled, you need to enable the samba service through firewall. Use this command to achieve that:

1
sudo ufw allow samba

# Start and enable the Samba service

The service will not be started by default. Start the service using this command:

1
sudo systemctl start smbd

Confirm the service status using this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ sudo systemctl status smbd
● smbd.service - Samba SMB Daemon
     Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-01-21 13:27:31 UTC; 5s ago
       Docs: man:smbd(8)
             man:samba(7)
             man:smb.conf(5)
    Process: 617106 ExecStartPre=/usr/share/samba/update-apparmor-samba-profile (code=exited, status=0/SUCCESS)
   Main PID: 617118 (smbd)
     Status: "smbd: ready to serve connections..."
      Tasks: 4 (limit: 4624)
     Memory: 7.0M
     CGroup: /system.slice/smbd.service
             ├─617118 /usr/sbin/smbd --foreground --no-process-group
             ├─617123 /usr/sbin/smbd --foreground --no-process-group
             ├─617124 /usr/sbin/smbd --foreground --no-process-group
             └─617125 /usr/sbin/smbd --foreground --no-process-group

Jan 21 13:27:30 ubuntusrv.citizix.com systemd[1]: Starting Samba SMB Daemon...
Jan 21 13:27:31 ubuntusrv.citizix.com update-apparmor-samba-profile[617106]: Reloading updated AppArmor profile for Samba...
Jan 21 13:27:31 ubuntusrv.citizix.com systemd[1]: Started Samba SMB Daemon.

The above output shows that the service has been started successfully. To enable the service on boot, use this command:

1
sudo systemctl enable smb

# Access the Samba shares with Linux client

To access the file system from a linux client, we need to have the Samba client software installed.

On a Ubuntu Linux, install the software using this command:

1
sudo apt install -y samba-client

To access the share, this is the format:

1
smbclient //server-IP/Public

In our client:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ smbclient //10.2.11.10/Public

Enter WORKGROUP\ubuntu's password:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Fri Jan 21 13:23:25 2022
  ..                                  D        0  Fri Jan 21 13:23:25 2022

		50758604 blocks of size 1024. 41160968 blocks available
smb: \>

Since we have no password set, just press enter when prompted.

# Access Samba shares with Windows Client

In Windows Launch run dialogue in windows by pressing Windows Key + R.

Enter hostname or IP-Address of the server then press Enter Key.

1
2
3
\\hostname
      or
\\server-IP-address

In my case

1
\\35.180.123.111

This will open the shares. You can add a file or directory either from samba server or windows client machine to the repository.

# Secure Samba share Configuration

Samba allows public access without verification and also private share where one has to be authenticated to access shared resources. In this section, we are going to configure a private directory to shared with samba and accessed using username and password.

Let’s create a Samba user group in our server.

1
sudo groupadd sambagrp

Create a new user and add it to samba group you just created.

1
sudo useradd -g sambagrp sambausr

Create a secure folder then grant it relative permissions and ownership .

1
sudo mkdir -p /home/sambaprivateshare

Create password for Samba user we just created that will be used for authentication when accessing the directory.

1
sudo smbpasswd -a sambausr

Type and retype the password:

1
2
3
4
$ sudo smbpasswd -a sambausr
New SMB password:
Retype new SMB password:
Added user sambausr.

Edit samba configuration file to add secure share configurations.

1
sudo vim /etc/samba/smb.conf

Add lines below the command and edit to match your configurations.

1
2
3
4
5
6
[Private]
    path = /home/sambaprivateshare
    valid users = @private
    guest ok = no
    writable = yes
    browsable = yes

Restart or reload samba services to apply configurations

1
sudo systemctl restart smbd

# Accessing Samba Secure files using Windows Systems

Press Windows Key + R, enter the server hostname or IP address to access the shared folders.

When you open a private folder will prompt for username and password to grant access right to the files.

# Access Samba Secure Directory with Linux

To access secure folder, You need to provide a user to the command using this format:

1
smbclient //server-IP/Private -U user

Enter Samba user password when prompted.

1
2
3
4
5
6
7
8
9
$ smbclient //10.2.11.10/Private -U sambausr
Enter SAMBA\sambausr's password:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Sat Jan 15 09:02:33 2022
  ..                                  D        0  Sat Jan 15 09:02:33 2022

		52417516 blocks of size 1024. 48489312 blocks available
smb: \>

That is it! We have managed to install and set up Samba for Anonymous and Secure file sharing between our Ubuntu 20.04 Server and other Linux or Desktop Windows clients.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy