YouTube Icon

Code Playground.

How to Install and Configure Samba on Ubuntu 18.04f

CFG

How to Install and Configure Samba on Ubuntu 18.04f

Samba is a free and open-source re-implementation of the SMB/CIFS network document sharing protocol that lets in give up users to get entry to files, printers, and different shared resources.

This academic explains the way to installation Samba on Ubuntu 18.04 and configure it as a standalone server to offer report sharing throughout one-of-a-kind running systems over a community.
We’ll create the subsequent Samba stocks and users.

Users:

  • sadmin - An administrative user with study and write get entry to to all stocks.
  • Josh - A normal consumer with its own personal file percentage.

Shares:

  • customers - This proportion might be accessible with study/write permissions through all customers.
  • Josh - This percentage will be handy with study/write permissions simplest by way of users josh and sadmin.

The report shares might be on hand from all gadgets on your network. Later in this academic, we can also provide precise commands on how to connect with the Samba server from Linux, Windows and macOS customers.

Prerequisites
Before persevering with, make sure you are logged in for your Ubuntu 18.04 device as a user with sudo privileges .
Installing Samba on Ubuntu
Samba is to be had from the legit Ubuntu repositories. To install it in your Ubuntu machine follow the steps underneath:

Start by means of updating the apt packages index:

sudo apt update


Install the Samba package deal with the subsequent command:

sudo apt install samba


Once the installation is finished, the Samba carrier will begin automatically. To take a look at whether the Samba server is walking, type:

sudo systemctl status smbd


The output must look some thing like underneath indicating that Samba service is active and strolling:

? smbd.service - Samba SMB Daemon
   Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-11-27 09:25:38 UTC; 2min 12s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 15142 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 4 (limit: 1152)
   CGroup: /system.slice/smbd.service
...


At this point, Samba has been set up and ready to be configured.

Configuring firewall
If you have a firewall walking for your Ubuntu machine you’ll want to allow incoming UDP connections on ports 137 and 138 and TCP connections on ports 139 and 445.

Assuming you are using UFW to control your firewall, you may open the ports with the aid of permitting the ‘Samba’ profile:

sudo ufw allow 'Samba'


Configuring Global Samba Options
Before making modifications to the Samba configuration file, create a backup for destiny reference functions:

sudo cp /etc/samba/smb.conf{,.backup}


The default configuration file that ships with the Samba package deal is configured for standalone Samba server. Open the file and make certain server role is about to standalone server

sudo nano /etc/samba/smb.conf

/etc/samba/smb.conf

...
# Most people will want "standalone sever" or "member server".
# Running as "active directory domain controller" will require first
# running "samba-tool domain provision" to wipe databases and create a
# new domain.
   server role = standalone server
...


By default, Samba listens on all interfaces. If you want to restrict get entry to to the Samba server only out of your inner network uncomment the subsequent  lines and specify the interfaces to bind to:

/etc/samba/smb.conf

...
# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
interfaces = 127.0.0.0/8 eth0

# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
# It is recommended that you enable this feature if your Samba machine is
# not protected by a firewall or is a firewall itself.  However, this
# option cannot handle dynamic or non-broadcast interfaces correctly.
bind interfaces only = yes
...

Once finished run the testparm application to test the Samba configuration record for mistakes. If there are not any syntax mistakes you'll see Loaded services document OK.

Finally, restart the Samba services with:

sudo systemctl restart smbd
sudo systemctl restart nmbd


Creating Samba Users and Directory Structure
For less complicated maintainability and versatility instead of using the usual home directories (/home/consumer) all Samba directories and facts might be placed inside the /samba listing.

To create the /samba listing type:

sudo mkdir /samba


Set the organization possession to sambashare. This group is created for the duration of the Samba set up, later we can add all Samba customers to this organization.

sudo chgrp sambashare /samba


Samba uses Linux users and organization permission device but is has its personal authentication mechanism cut loose the standard Linux authentication. We will create the users using the same old Linux useradd device after which set the consumer password with the smbpasswd application.

As we stated within the advent, we’ll create a regular user so that it will have get admission to to its non-public record percentage and one administrative account with read and write get admission to to all stocks at the Samba server.
Creating Samba Users
To create a new person named josh use the following command:

sudo useradd -M -d /samba/josh -s /usr/sbin/nologin -G sambashare josh

The useradd options have the subsequent meanings:

-M -do no longer create the user’s home directory. We’ll manually create this listing.
-d /samba/josh - set the person’s domestic directory to /samba/josh.
-s /usr/sbin/nologin - disable shell get admission to for this consumer.
-G sambashare - upload the user to the sambashare group.
Create the consumer’s domestic directory and set the directory ownership to person josh and institution sambashare:

sudo mkdir /samba/josh
sudo chown josh:sambashare /samba/josh


The following command will add the setgid bit to the /samba/josh listing so the newly created files in this directory will inherit the organization of the figure listing. This manner, irrespective of which consumer creates a brand new report, the record could have organization-proprietor of sambashare. For instance, in case you don’t set the directory’s permissions to 2770 and the sadmin person creates a brand new file the person josh will not be able to study/write to this record.

sudo chmod 2770 /samba/josh


Add the josh person account to the Samba database by way of putting the person password:

sudo smbpasswd -a josh


You might be triggered to enter and verify the user password.

New SMB password:
Retype new SMB password:
Added user josh.


Once the password is set to allow the Samba account run:

sudo smbpasswd -e josh
Enabled user josh.


To create another person repeat the equal system as while creating the user josh.

Next, permit’s create a person and organization sadmin. All individuals of this organization can have administrative permissions. Later if you want to grant administrative permissions to every other person in reality upload that person to the sadmin group .

Create the administrative consumer by way of typing:

sudo useradd -M -d /samba/users -s /usr/sbin/nologin -G sambashare sadmin

The command above will even create a collection sadmin and upload the person to each sadmin and sambashare agencies.

Set a password and enable the consumer:

sudo smbpasswd -a sadmin
sudo smbpasswd -e sadmin


Next, create the Users percentage directory:

sudo mkdir /samba/users


Set the listing ownership to consumer sadmin and institution sambashare:

sudo chown sadmin:sambashare /samba/users


This listing may be handy by means of all authenticated users. The following chmod command offers write/examine get right of entry to to contributors of the sambashare institution in the /samba/users directory:

sudo chmod 2770 /samba/users

Configuring Samba Shares
Open the Samba configuration document and append the sections:

sudo nano /etc/samba/smb.conf

/etc/samba/smb.conf

[users]
    path = /samba/users
    browseable = yes
    read only = no
    force create mode = 0660
    force directory mode = 2770
    valid users = @sambashare @sadmin

[josh]
    path = /samba/josh
    browseable = no
    read only = no
    force create mode = 0660
    force directory mode = 2770
    valid users = josh @sadmin

The options have the subsequent meanings:

  • [users] and [josh] - The names of the stocks that you may use whilst logging in.
  • Path - The path to the percentage.
  • Browseable - Whether the share need to be indexed in the to be had stocks listing. By placing to no different customers will now not be able to see the proportion.
  • Study handiest - Whether the users particular within the valid customers listing are able to write to this percentage.
  • Pressure create mode - Sets the permissions for the newly created files in this proportion.
  • Force directory mode - Sets the permissions for the newly created directories in this proportion.
  • Valid users - A list of customers and groups which might be allowed to get entry to the share. Groups are prefixed with the @ image.

For more facts approximately to be had alternatives see the Samba configuration file documentation page.

Once completed, restart the Samba offerings with:

sudo systemctl restart smbd
sudo systemctl restart nmbd


In the following sections, we are able to display you ways to connect to a Samba percentage from Linux, macOS and Windows customers.

Connecting to a Samba Share from Linux
Linux users can access the samba proportion from the command line, the use of the record manager or mount the Samba proportion.

Using the smbclient client
smbclient is a tool that allows you to get admission to Samba from the command line. The smbclient package deal is not pre-installed on most Linux distros so you will want to install it with your distribution package manager.

To installation smbclient on Ubuntu and Debian run:

sudo apt install smbclient


To set up smbclient on CentOS and Fedora run:

sudo yum install samba-client


The syntax to get entry to a Samba percentage is as follows:

mbclient //samba_hostname_or_server_ip/share_name -U username


For instance to hook up with a proportion named josh on a Samba server with IP cope with 192.168.121.118 as consumer josh you will run:

smbclient //192.168.121.118/josh -U josh


You will be brought on to enter the person password.

Enter WORKGROUP\josh's password: 


Once you enter the password you'll be logged into the Samba command line interface.

Try "help" to get a list of possible commands.
smb: \>


Mounting the Samba share
To mount a Samba share on Linux first you want to instal the cifs-utils package deal.

On Ubuntu and Debian run:

sudo apt install cifs-utils


On CentOS and Fedora run:

sudo yum install cifs-utils


Next, create a mount factor:

sudo mkdir /mnt/smbmount
Mount the share the usage of the subsequent command:

sudo mount -t cifs -o username=username //samba_hostname_or_server_ip/sharename /mnt/smbmount

For instance to mount a share named josh on a Samba server with IP address 192.168.121.118 as user josh to the /mnt/smbmount mount point you'll run:

sudo mount -t cifs -o username=josh //192.168.121.118/josh /mnt/smbmount

You could be induced to go into the consumer password.

Password for josh@//192.168.121.118/josh:  ********

Using GUI
Files, the default report supervisor in Gnome has integrated option to get entry to Samba shares.

Open Files and click on on “Other Locations” inside the sidebar.
In “Connect to Server”, enter the deal with of the Samba share inside the following format smb://samba_hostname_or_server_ip/sharename.
Click “Connect” and the subsequent screen will appear:
Select “Registered User”, input the Samba username and password and click “Connect”.
The files on the Samba server will be proven.
Connecting to a Samba Share from macOS
In macOS you may get entry to the Samba Shares either from the command line or the use of the default macOS document manager Finder. The following steps display how to get entry to the proportion the usage of Finder.

Open “Finder”, pick out “Go” and click on “Connect To”.
In “Connect To”, input the address of the Samba proportion inside the following layout smb://samba_hostname_or_server_ip/sharename.
Click “Connect” and the following screen will seem:
Select “Registered User”, input the Samba username and password and click on “Connect”.
The files on the Samba server will be proven.
Connecting to a Samba Share from Windows
Windows customers additionally have an choice to hook up with the Samba percentage from both command line and GUI. The steps below display the way to get admission to the percentage using the Windows File Explorer.

Open up File Explorer and inside the left pane proper-click on on “This PC”.
Select “Choose a custom network location” and then click “Next”.
In “Internet or network deal with”, enter the cope with of the Samba percentage in the following layout samba_hostname_or_server_ipsharename.

Click “Next” and you'll be brought about to enter the login credentials as shown below:
In the next window you can type a custom name for the community place. The default one could be picked up by using the Samba server.
Click “Next” to move to the remaining screen of the relationship setup wizard.
Click “Finish” and the files at the Samba server will be proven.


Conclusion
In this tutorial, you've got discovered how to set up Samba server on Ubuntu 18.04 and create distinctive types of shared and customers. We have also shown you the way to hook up with the Samba server from Linux, macOS and Windows gadgets.




CFG