samba_ubuntu
Linux/Unix

How to install and configure Samba on Ubuntu 22.04

Samba is a free software that allows to share files and printers between computers running Linux and Windows. It is very useful for home networks and small businesses. In this article, I will show you how to install and configure Samba on Ubuntu 22.04.

samba
samba

Installing samba on Ubuntu

Before we get started, make sure your system is up to date :

root@Linux:~# apt-get update

Now that our system is up to date, use the following command to install Smb :

root@Linux:~# apt install samba

You can check samba’s version installed with this command :

root@Linux:~# samba -V
Version 4.15.13-Ubuntu

Configuring Samba

Step 1 : Open the Samba configuration file using the following command :

# nano /etc/samba/smb.conf

Step 2: Find the line workgroup = WORKGROUP and replace « WORKGROUP » with the name of your workgroup.

samba-workgroup
samba-workgroup

Step 3: Indicate the directory you want to share by adding sections like this one at the end of the file :

[ShareName]
   comment = Comment
   path = /path/to/your/directory
   read only = no
   writable = yes
   browseable = yes
   guest ok = no
   valid users = user1

-Path : This is the path to your share directory.

-valid users : Users who have the right to access the share.

read only : No, if you want users to have the right to add and modify files. Yes, if you want the users to have only the right to read.

writable : users have the right or not to add and modify files.

browseable : Indicates whether the share should be visible in the list of available shares.

Step 4: To check if there is a syntax errors in the configuration file, use testparm command :

root@Linux:~# testparm  /etc/samba/smb.conf
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Weak crypto is allowed

As you can see, the result of the command shows that everything is OK.

Step 5: Then restart the smbd service by running the command bellow :

# systemctl restart smbd
# systemctl status smbd
● smbd.service - Samba SMB Daemon
     Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2023-01-29 13:13:59 +01; 1h 2min ago
       Docs: man:smbd(8)
             man:samba(7)
             man:smb.conf(5)
    Process: 45822 ExecStartPre=/usr/share/samba/update-apparmor-samba-profile (code=exited, status=0/SUCCESS)
   Main PID: 45831 (smbd)
     Status: "smbd: ready to serve connections..."
      Tasks: 4 (limit: 5695)
     Memory: 8.8M
        CPU: 254ms
     CGroup: /system.slice/smbd.service
             ├─45831 /usr/sbin/smbd --foreground --no-process-group
             ├─45833 /usr/sbin/smbd --foreground --no-process-group
             ├─45834 /usr/sbin/smbd --foreground --no-process-group
             └─45835 /usr/lib/x86_64-linux-gnu/samba/samba-bgqd --ready-signal-fd=45 --parent-watch-fd=11 --debuglevel=0 -F

Samba user configuration

Create a user who will have the right to access your share using the following command:

# smbpasswd -a user1
New SMB password:
Retype new SMB password:
Added user user1.

You must then grant this user the right to read, modify and execute on the shared folder using ACLs:

# setfacl -R -m "u:user1:rwx" /data

Firewall configuration

root@Linux:~# ufw allow samba
Rules updated
Rules updated (v6)

Connection au partage Samba

Now that our samba is ready, we will test the access to the share from a Linux and Windows machine.

On Linux to access a Samba share we will use smbclient.

smbclient is a utility that allows you to access an SMB share from the command line. It is not installed on most of the distributions. Here is the command to install it on Centos/Redhat and Ubuntu/Debian :

On Centos/Redhat :

# yum install samba-client

Or use dnf.

On Ubuntu / Debian :

# apt install smbclient

Here is the syntax of the smbclient command to access the share :

# smbclient //ip_address_or_hostname/shareName -U user_name

For example in my case, here is my command :

[root@smb-client ~]# smbclient //192.168.139.150/DATA -U user1
Enter SAMBA\user1's password:
Try "help" to get a list of possible commands.
smb: \>

For example, I have created a file named « example.txt » on the samba share and I want to download it, just use the get command :

[root@smb-client ~]# smbclient //192.168.139.150/DATA -U user1
Enter SAMBA\user1's password:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Sun Jan 29 15:03:10 2023
  ..                                  D        0  Sun Jan 29 13:11:17 2023
  exemple.txt                             N        0  Sun Jan 29 15:03:10 2023


                19946096 blocks of size 1024. 4265416 blocks available
smb: \> get exemple
getting file \exemple of size 0 as exemple (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec)
smb: \> exit
[root@smb-client ~]# ls
anaconda-ks.cfg  exemple.txt
[root@smb-client ~]#

Mounting the Samba share

This time, instead of interacting with the share from the command line, we will make it go up to the samba client as if it existed locally.

To do this we need to install a package called cifs :

CentOS/Redhat :

[root@smb-client ~]# yum install cifs-utils

Ubuntu/Debian :

[root@smb-client ~]# apt install cifs-utils

Then create the mount point through which the network share can be accessed :

[root@smb-client ~]# mkdir /applis

Then mount the share with the command below indicating the samba user :

[root@smb-client ~]# mount -t cifs -o username=user1 //192.168.139.150/DATA /applis
Password for user1@//192.168.139.150/DATA:  ********

You will be prompted to type the samba user password.

Then run the command df -h :

[root@smb-client ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  898M     0  898M   0% /dev
tmpfs                   tmpfs     910M     0  910M   0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M   2% /run
tmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  1.6G   16G  10% /
/dev/sda1               xfs      1014M  226M  789M  23% /boot
tmpfs                   tmpfs     182M     0  182M   0% /run/user/0
//192.168.139.150/DATA  cifs       20G   15G  4.1G  79% /applis

And here is our share well mounted. As you can see below, I have the « example.txt » file that I created earlier:

[root@smb-client ~]# ls
exemple.txt

On Windows :

For Windows follow the steps below :

1- Open Windows Explorer and click on « This PC ».

2- Right click and choose « Add Network Location ».

3- Click on « Choose a custom network location ».

4- Specify the location of the samba share.

5- Give a name to your share.

6-Finally the smb share will be visible once authenticated with the smb user.

Congratulations, you are now able to configure a network share with SMB and how to access it from a Linux and Windows machine.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *