Compile Samba From Source

Introduction

This guide helps you to compile a particular Samba version from source which includes smbclient. This may be necessary if the provided version of your OS can not connect to a file server, in particular to older non Microsoft file servers serving SMB only with NT1.

The guide has been tested, is at it is and comes without any warranty.

Prerequisites

It is highly recommended to stop your Web Server and if configured php-fpm services.

Uninstalling samba

If you have already installed smbclient provided by your OS and you installed the smbclient extension from PECL (to support smbclient in PHP) you need to uninstall them first. Follow the steps described below to do so.

Copy your smb.conf or other configurations files you may want to keep to reuse them later on. Purging deletes the config files and helps to make a clean basis.
sudo phpdismod smbclient
sudo pecl uninstall smbclient
sudo apt purge smbclient
sudo apt autoremove

Preparing the Installation Environment

To compile Samba, you need to install necessary packages. Copy the Bootstrap Dependencies Script from Samba for Ubuntu 20.04. You will find it in section Verified Package Dependencies. Post downloading, make the script executable and execute it, which will install all required packages for a successful samba compilation. For your convenience, you can directly download the bootstrap samba master shell script here.

sudo ./bootstrap.sh

Prepare Compiling Samba

Download Your Copy of Samba

The latest version of Samba, which had support for protocol NT1 as client was version 4.10.18. In all versions above, the client support was dropped and is not available anymore. If you need NT1 support as client, download this version. You can choose any version that fits your needs. A search on bugzilla may help finding a particular version that fixes the issue you are facing, which is not provided by the OS delivered version. Keep the downloaded (and later configured) version at a location for later reuse. This will be necessary if you would like to uninstall it properly. The example uses /opt.

cd /opt
sudo wget https://download.samba.org/pub/samba/stable/samba-4.10.18.tar.gz

Extract Samba Sources

sudo tar -xvf samba-4.10.18.tar.gz
cd samba-4.10.18

Compile Samba

To compile Samba, you need three steps which are described in detail at Build Samba from Source. The complete process may take some time.

  1. sudo ./configure <options>

  2. sudo make <options>

  3. sudo make install

Configuring the Installation

The settings for the configuration options are important, so that Samba will be located and setup for Ubuntu properly. If you are planning to use Samba in your installation where your server will act as domain controller and not only as a client, you can safely remove --without-ad-dc from the options below. Read more on details config options for Ubuntu. For your convenience, you can directly download a compile samba shell script here.

sudo ./configure \
	--prefix=/usr \
	--enable-fhs \
	--sysconfdir=/etc \
	--localstatedir=/var \
	--with-privatedir=/var/lib/samba/private \
	--with-smbpasswd-file=/etc/samba/smbpasswd \
	--with-piddir=/var/run/samba \
	--with-pammodulesdir=/lib/x86_64-linux-gnu/security \
	--libdir=/usr/lib/x86_64-linux-gnu \
	--with-modulesdir=/usr/lib/x86_64-linux-gnu/samba \
	--datadir=/usr/share \
	--with-lockdir=/var/run/samba \
	--with-statedir=/var/lib/samba \
	--with-cachedir=/var/cache/samba \
	--with-socketpath=/var/run/ctdb/ctdbd.socket \
	--with-logdir=/var/log/ctdb \
	--systemd-install-services \
	--without-ad-dc

Start the Compilation

Start the compilation with following command. Even not mandatory, you can set options to run multiple jobs in parallel by adding -j <n>. This optimizes the CPU utilisation and reduces the time needed. In the example below, four jobs are enabled to utilize the 4 available cores of the CPU.

sudo make -j 4

Install the Compiled Software

To install the compiled software run following command:

sudo make install -j 4

Create a Default smb.conf File

If you do not have an existing or already configured smb.conf file, you can create a default one. The following command creates a new smb.conf if it does not exist, but does not overwrite an existing one.

sudo cp -n examples/smb.conf.default /etc/samba/smb.conf

Uninstall the Compiled Software

Uninstalling can be necessary, if you want to have a clean base. This is useful if you want to compile a different version or the version provided by the OS. To uninstall the compiled software run following command:

sudo make uninstall -j 4

Testing

Connection Test to Foreign Host

When the installation has completed, test your result. If you have used a smb.conf file before, copy it back to its original location (/etc/samba/).

sudo smbclient --version
Version 4.10.18
sudo smbclient -L <file_server_name> -U <full_domain_name>/<user_name>

You now should get a proper response with a directory listing.

Connection Test as Standalone Fileserver

If you want that this server acts as simple standalone smb fileserver, e.g. for testing, you need to prepare and set some settings. Following tasks are necessary to start and stop smb as service via systemd. The smbd service is necessary that your server can act as simple smb file server.

First create a link to the smb service.

sudo ln -s /lib/systemd/system/smb.service /etc/systemd/system/smbd.service

Then, change some startup parameters. These will not be overwritten on the source file, but be added via a separate non-destructive process.

sudo systemctl edit smbd.service

Add the following content and save the result. The location and naming will be done automatically. Just say save.

[Unit]
After=
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=
Type=forking
ExecStart=
ExecStart=/usr/sbin/smbd --configfile=/etc/samba/smb.conf

When finished, reload the daemon to recognize the update:

sudo systemctl daemon-reload

Finally, you can start (stop, reload ect), the smbd service with:

sudo service smbd start

When this is done and the service has started successfully, adopt your smb.conf according your needs as stand-alone fileserver, test the content by invoking the command testparm on the command line and restart the smbd service. You should then be able to connect to this standalone samba server.

Reinstalling Pecl smbclient

If you had removed pecl smbcient before, you can reinstall it now with:

sudo pecl channel-update pecl.php.net
sudo pecl install smbclient
sudo phpenmod smbclient

Restart Services

Restart your Web Server and/or php-fpm when everything is finished.

Scripting the Compiling Procedure

If you want to automate the compiling procedure, you can perform the following steps. Prepare a directory structure like in the example below where opt/ and Samba version samba-4.10.18 are used:

opt/
  bootstrap.sh
  compile_samba.sh
  install_samba.sh
  samba-4.10.18

Download the scripts:

Make the scripts executable:

sudo chmod +x <script_name>

Change into the extracted samba directory you want to compile, e.g. samba-4.10.18, and run the following command:

sudo ../install_samba.sh

Creating a default smb.conf file, testing, uninstalling etc. remain a manual task as described in the above sections.