Choose a different version or distribution
Introduction
Before we begin talking about how to install Samba and configure it on Ubuntu 18.04, let's understand - What is Samba?
Samba enables cross-platform file sharing. Connect Windows, Linux, and macOS systems effortlessly. Improve collaboration with easy access to shared files and printers.
Samba simplifies file sharing by providing a common language for computers of various types. With Samba, users can effortlessly access files and printers across networks, enhancing collaboration and efficiency. Learn how Samba can streamline your networking experience and foster seamless interoperability between diverse systems.
In this tutorial, you will install Samba on Ubuntu 18.04 and configure it in the form of a standalone server to enable sharing of documents and files across multiple OSs across a network.
Advantages of Samba
- Cross-platform Compatibility: Samba enables seamless file and print sharing between Windows, Linux, and macOS systems.
- Easy Integration: It simplifies network integration by allowing different operating systems to communicate effortlessly.
- Cost-effective Solution: Samba is free and open source, eliminating the need for expensive proprietary solutions.
- Enhanced Collaboration: Users can easily access shared files and printers across networks, fostering collaborative workflows.
- Interoperability: Samba promotes the interoperability of diverse systems, making it an ideal solution for heterogeneous network environments.
To start off, you need to generate these Samba shares and users.
Users:
- sadmin - An administrative user who has read and write access to everything which is shared.
- josh - A regular user that has its own private documents and files.
Shares:
- users - This share can be accessed by all users and every user has read and write permission.
- josh - This share can be accessed by only sadmin and josh and only they possess the permission to read or write.
The shared documents and files could be accessed by each and every device on the network. In the second part of this tutorial, we shall go through the procedure to connect to the Samba server on Linux, Windows, and macOS clients.
Prerequisites
Prior to moving further, do ensure that you are logged on to your Ubuntu 18.04 system with sudo privileges.
Step 1 - Installing Samba on Ubuntu
Samba can be found at official Ubuntu repositories. In order to install it on the Ubuntu system, carefully follow the following instructions:
1) Initiate the process by updating the apt packages index:
sudo apt update
2) Install Samba package using this:
sudo apt install samba
3) After installation, the Samba service will be operative. To verify if the Samba server is running or not, use the below command:
sudo systemctl status smbd
This output will appear to show that the Samba service is activated:
output
● 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
...
Now, Samba is installed and can be configured.
Step 2 - Configuring firewall
In case, there is a firewall running on the Ubuntu system, grant permission to incoming UDP connections on ports 137
and 138
and TCP connections on ports 139
and 445
.
1) Presuming that UFW
is being used to manage the firewall, open the ports by allowing the ‘Samba’ profile:
sudo ufw allow 'Samba'
Step 3 - Configuring Global Samba Options
1) Prior to effecting any changes to the Samba configuration file, make a backup for reference purposes in the future:
sudo cp /etc/samba/smb.conf{,.backup}
2) At this point, the default configuration file which comes alongside the Samba package has been configured for a standalone Samba server. Open the file and ensure that server role
is set to standalone server
by using this:
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
...
3) By default, Samba operates on each and every interface. In case you desire to restrict access to the Samba server only on the internal network then uncomment the following two 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
...
4) After completion, run the testparm
utility to verify the Samba configuration file and ensure that it's error-free. In case there are no syntax errors Loaded services file OK.
will appear.
Lastly, restart the Samba services with:
sudo systemctl restart smbd
Step 4 - Creating Samba Users and Directory Structure
1) To manage easily instead of utilizing the standard home directories (/home/user
) each and every Samba directories and data will be located in the /samba
directory.
In order to create the /samba
directory use the following command:
sudo mkdir /samba
2) Then make sure that group ownership is set to sambashare
. While the Samba installation, this group is created, and later you will add all Samba users here.
sudo chgrp sambashare /samba
3) Samba utilizes Linux users and a group permission system. Although it has an authentication mechanism of its own as well. One may create users through the standard Linux useradd
tool and later on set the password of the user via smbpasswd
utility.
4) Following step is to create a regular user that can access private file share and a single administrative account with read/write permission to all shares on the Samba server.
Step 5 - Creating Samba Users
1) For creating a new user named josh
, run this:
sudo useradd -M -d /samba/josh -s /usr/sbin/nologin -G sambashare josh
2) The useradd
options mean the following:
-M
- do not create the user’s home directory, you will manually create this directory.-d /samba/josh
- set the user’s home directory to/samba/josh
.-s /usr/sbin/nologin
- disable shell access to this user.-G sambashare
- add the user to thesambashare
group.
Following this, create a user’s home directory and set the directory ownership to user josh
and group sambashare
using this command:
sudo mkdir /samba/josh
3) This next command would add the setgid
bit to the /samba/josh
directory and hence, the files created recently in the directory will inherit the group of the parent directory. This ensures that if a user creates a new file the file will have a group-owner of sambashare
.
sudo chmod 2770 /samba/josh
4) Then, josh
user account is to be added to the Samba database and can be done by setting the user password:
sudo smbpasswd -a josh
Once the command is run you would be prompted to put the user password.
Output
New SMB password:
Retype new SMB password:
Added user josh.
5) After setting the password, to activate the Samba account run:
sudo smbpasswd -e josh
output
Enabled user josh.
6) For creating another user, carry out the same process which you would for creating the user josh
.
Now, follow these steps to create a user and group sadmin
. Each and every member of this group has administrative permissions. In case, you desire to grant administrative permissions to any other user, just add that user to the sadmin
group.
7) For creating the administrative user, use this:
sudo useradd -M -d /samba/users -s /usr/sbin/nologin -G sambashare sadmin
8) The aforementioned command shall also create a group sadmin
and add the user to both sadmin
and sambashare
groups.
9) The next step is to set the password and allow the user:
sudo smbpasswd -a sadmin
10) After this, you need to create the Users
share directory:
sudo mkdir /samba/users
11) Then you have to set directory ownership to the user sadmin
and group sambashare
by using this command:
sudo chown sadmin:sambashare /samba/users
12) This directory can be accessed by each and every authenticated user. The next chmod
command provides write/read access to members of the sambashare
group in the /samba/users
directory:
sudo chmod 2770 /samba/users
Step 6 - Configuring Samba Shares
1) For this, the Samba configuration file is to be opened first, and then you must 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
These options have the meanings given below:
[users]
and[josh]
- The names of the shares to use when logging in.path
- The path to the share.browseable
- If the share is to be listed in the list of the available shares. By setting tono
, other users won't be able to see the share.read only
- If the users specified in thevalid users
list can write to this share.force create mode
- Sets the permissions for the files which are newly created.force directory mode
- Sets the permissions for the newly created directories in this share.valid users
- A list of users and groups that are allowed to access the share.
2) At this point, restart the Samba services using:
sudo systemctl restart smbd
sudo systemctl restart nmbd
In the next sections, we have discussed how to connect to a Samba share from Linux, macOS, and Windows clients.
Step 7 - Connecting to a Samba Share from Linux
Linux users can access the samba share from the command line, using the file manager, or mount the Samba share.
Step 8 - Using smbclient
smbclient
is a tool that enables one to gain access to Samba from a command line. The smbclient
package is not default on most Linux distros and thus, it is needed to be installed alongside the distribution package manager.
1) For installing smbclient
on Ubuntu and Debian, use this command:
sudo apt install smbclient
2) For installing smbclient
on CentOS and Fedora, use this:
sudo yum install samba-client
3) For accessing Samba share, use this:
mbclient //samba_hostname_or_server_ip/share_name -U username
4) For instance, in order to connect to a share named josh
on a Samba server with an IP address 192.168.121.118
as user josh
, you need to use the command:
smbclient //192.168.121.118/josh -U josh
5) Then, you have to enter the user password.
Output
Enter WORKGROUP\josh's password:
6) After entering the password, you are into the Samba command-line interface.
Output
Try "help" to get a list of possible commands.
smb: \>
Step 9 - Mounting the Samba share
For mounting Samba share to Linux, you must install the cifs-utils
package.
1) For Ubuntu and Debian, use:
sudo apt install cifs-utils
2) For CentOS and Fedora, use:
sudo yum install cifs-utils
3) Then, create the mount point using:
sudo mkdir /mnt/smbmount
4) After that, mount the share using:
sudo mount -t cifs -o username=username //samba_hostname_or_server_ip/sharename /mnt/smbmount
5) For instance, for mounting a share titled josh
on a Samba server along with the IP address 192.168.121.118
as user josh
to the /mnt/smbmount
mount point, use the following command:
sudo mount -t cifs -o username=josh //192.168.121.118/josh /mnt/smbmount
6) Then, this will appear and you will enter the user password.
Output
Password for josh@//192.168.121.118/josh: ********
Step 10 - Using GUI
The default file manager in Gnome has a built-in option to access Samba shares.
- Open Files and click on “Other Locations” in the sidebar.
- In “Connect to Server”, put the address of the Samba share in this format
smb://samba_hostname_or_server_ip/sharename
. - Then, click on “Connect”.
- The next step is to select “Registered User”, put the Samba username and password, and hit “Connect”.
- The files on the Samba server will appear.
Step 11 - Connecting to a Samba Share from macOS
For macOS, users could gain access to the Samba Shares through the command line or by resorting to the default macOS file manager which is "Finder". These instructions can be followed to access the share using Finder.
- Open “Finder”, choose the option “Go” and then click on “Connect To”.
- Under “Connect To”, you must put the address of the Samba share in this format:
smb://samba_hostname_or_server_ip/sharename
. - Then hit “Connect”.
- Choose the “Registered User” option and then put the Samba username and password and hit “Connect”.
- The files on the Samba server will appear.
Step 12 - Connecting to a Samba Share from Windows
Windows users can also connect to the Samba share from, both, the command line and GUI. These are the steps to access the share utilizing the Windows File Explorer.
- Open the "File Explorer" and on the left-panel right-click on “This PC”.
- Find and select the option which says “Choose a custom network location” and then hit “Next”.
- In “Internet or network address”, put the address of the Samba share in this format:
\\samba_hostname_or_server_ip\sharename
. - After hitting the option “Next” and you need to enter the login credentials.
- Then, in the following window, one may type a custom name associated with the network location. The Samba server will choose the default one.
- Hit “Next” for moving the last screen on the connection setup wizard.
- Finally, click on “Finish” then Samba server filed will appear.
FAQs to Install and Configure Samba on Ubuntu 18.04
Where can I find the Samba configuration file on Ubuntu 18.04?
The Samba configuration file can be found at /etc/samba/smb.conf
How do I create a shared folder using Samba?
First, create a directory to be shared. Then, edit the Samba configuration file, adding the necessary share settings. Finally, restart the Samba service with sudo service smbd restart
How can I add Samba users on Ubuntu 18.04?
You can add Samba users using the smbpasswd
command. For instance, to add a new user named "john," run: sudo smbpasswd -a john
How do I secure my Samba shares?
You can secure Samba shares by configuring access permissions in the Samba configuration file (smb.conf
). Set appropriate permissions, enable encryption, and restrict access to specific users or groups.
How can I access Samba shares from Windows?
Open File Explorer in Windows, then enter the Samba share path in the address bar using the format: \\<Ubuntu-IP>\<share-name>
. Alternatively, you can map the share as a network drive.
How do I troubleshoot Samba issues on Ubuntu 18.04?
Check the Samba logs located in /var/log/samba/
for any error messages. You can also use the testparm
command to validate the configuration file for errors.
How do I restart the Samba service on Ubuntu 18.04?
To restart the Samba service, use the command: sudo service smbd restart
. Make sure to reload the configuration file after making any changes: sudo service smbd reload
Conclusion
We hope this detailed guide helped you understand how to install and configure Samba. To learn more about Samba installation, check out the official Samba documentation.
If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.