Samba is an open-source software suite providing file and print-sharing services between Windows and Linux-based systems. Its increasing popularity is for being interoperable and a versatile solution for integrating Linux and Windows. This tutorial will provide you with a step-by-step walkthrough on how to install and configure the Samba server in Ubuntu. Besides file sharing, Samba supports features such as authentication and access control, integrating with cloud storage services and streaming media files. Samba relies on smb or Server Message Block protocol, which is the core network sharing protocol for interprocess communication.
Key Takeaways
- Learning how to install and configure Samba in Ubuntu 22.04.
- Knowing about web-based Samba configuration.
- Knowing how to remove and back up Samba files.
Requirements
- The user must have sudo/root privilege.
- The user must have a text editor.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.02 LTS
Step-by-Step Process to Install and Configure Samba Server
Samba is a popular choice for communication over a network due to its cross-platform compatibility, resource management, and fast sharing services. To avail these benefits, the foremost task is to install Samba and configure it according to your requirements. Follow the steps below and you will be done with the installation process in no time.
Step 1: Install Samba Server on Ubuntu
First, you need to install Samba on your Linux distribution, in this regard, I am using Ubuntu 22.04. This step ensures updates of all existing packages and verifies Samba installation.
Steps to Follow >
➊ Open your Ubuntu terminal.
➋ Type the following command to update all packages in your Linux system.
sudo apt update
- sudo: Permits a user with administrative privileges to execute commands.
- apt update: Updates all outdated packages and dependencies on your system.
sudo apt-get install samba -y
- sudo: Permits a user with administrative privileges to execute commands.
- apt install: Specifies that the install task needs to be performed.
- samba: Name of an open-source software package, Samba for file sharing.
- -y: Automatically answers ‘yes’ to any confirmation message during installation.
whereis samba
- whereis: Looks for the location of the installed package.
➎ Next type the following command to instruct UFW firewall to open necessary ports or allow network traffic for the Samba service.
sudo ufw allow samba
- ufw: Uncomplicated Firewall (ufw) is a command line tool for managing firewall configurations on Ubuntu.
- allow: Command option that allows incoming network traffic for Samba service.
- samba: Name of an open-source software package, Samba for file sharing.
Step 2: Create a Directory for Sharing Files
Now you have to create a shared directory in the Linux system that can be easily accessible from both Linux and Windows.
Steps to follow >
➊ Type the following command to create a directory for Samba sharing. I will create a folder, Sambashare in Desktop for file sharing.
mkdir /home/ayesha/Desktop/Sambashare
- mkdir: Creates a new directory or folder
- /home/ayesha/Desktop/Sambashare: Absolute path of the location of the folder, Sambashare.
Step 3: Add User to Samba Server on Ubuntu
You have to add a user who will have access to the shared folder.
Steps to follow >
➊ To add a new Samba user and assign a password to that user, run the following command.
sudo smbpasswd -a ayesha
- smbpasswd: Sets password for Samba users.
- -a: Allows the command to add a new Samba user.
- ayesha: User name.
This step involves customizing settings and permissions of the shared folder, Sambashare in the Samba configuration file.
Steps to follow >
➊ Now run the command below to open and edit the configuration file.
sudo nano /etc/samba/smb.conf
- nano: Text editor
- /etc/samba/smb.conf: Configuration file of Samba to customize settings such as user authentication, shares, and security.
- conf: Configuration file. smb stands for Sever Message Block Configuration.
Provide your password and this command will open an editor file for modifying configuration settings.
➋ Insert these lines at the end of the configuration file. Remember to write the name of your shared folder inside the braces. I wrote Sambashare in this regard.
[Sambashare] path = /home/ayesha/Desktop/Sambashare available = yes valid users = ayesha read only = no writeable=yes browseable=yes public = yes
- sambashare: Name of the folder whose contents you want to share.
- path: Absolute path of the folder you want to share.
- available: Specifies whether the file is available for sharing or not.
- valid user: User name.
- read-only: Specifies whether the file is available for reading or not.
- writeable: Specifies whether the folder can be written to or not.
- browseable: Specifies whether the shared file is visible while browsing the network or not.
- public: Specifies whether the shared file is accessible to all users or not.
➌ Press CTRL+O and CTRL+X to save the changes and exit the editor respectively.➍ Restart Samba and enter the password of your currently logged-in user.
sudo service smbd restart
- service: Controls services in the Linux system.
- smbd: Samba daemon which provides file-sharing services to Windows clients.
- restart: Stop running the service and start again.
systemctl status smbd
- systemctl: Manages system services, along with starting, stopping, and checking the status of services.
- status: Displays the current status of a service.
- smbd: Samba daemon which provides file-sharing services to Windows clients.
Complementary Information
You have now successfully installed and configured the Samba server on your Ubuntu system. However, the file-sharing process, managing Samba from a remote device, or tackling security concerns are still unclear. Let’s explore the instructions below to have a smooth sharing experience.
A. Sharing Files using Samba Server on Ubuntu
To finally serve the purpose of Samba i.e. to facilitate file sharing, you need to have the flexibility to create files in either Linux or Windows and access them seamlessly from both systems.
Step 1: Establish a Connection between Linux and Windows
To help Linux and Windows to communicate with each other, you need to install the required file-sharing packages and protocols.
Steps to follow >
➊ Run the command below to install client and utils package on Samba.
sudo apt install cifs-utils samba client -y
- cifs-utils: Package that supports mounting CIFS/SMB file system. CIFS is the core file-sharing protocol used by Samba.
- samba client: Package containing necessary client tools for Samba shares and other Samba-related operations.
- -y: Automatically answers ‘yes’ to any confirmation message during installation.
hostname -I
- hostname -I: Obtain the IP address of the current Linux system.
Step 2: Create files in Ubuntu and Access them from Windows
In this step, I will save an image called Photo.jpeg in Linux and attempt to access it from Windows.
Steps to follow >
➊ In Linux, navigate to the shared folder, Sambashare by following the path Files>Home>Desktop>Sambashare and save the image file there.➋ Then go to Windows and type the IP address of Linux system and the name of shared folder, Sambashare in the address bar.
\\192.168.153.128\Sambashare
- 192.168.153.128: IP address of my Linux system.
- Webshare: Name of the folder used for sharing files.
➍ You have now successfully accessed Sambashare folder from Windows and can view its contents. You can see that the image file is now accessible from the Windows system.
Step 3: Create Files in Windows and Access them from Ubuntu
I will demonstrate to you whether I can share any file from Windows. In this case, I will create a folder, Windows_files in Sambashare in Windows and try to access it from Linux.
Steps to follow >
➊ Go to Windows and create a folder, Windows_files.➋ Then open Sambashare folder in Linux.
The image above shows that the Windows_files folder is now accessible from Linux.
B. Web-based Samba Server Configuration on Ubuntu
You can opt for a web-based Samba configuration if you prefer a graphical user interface(GUI) or want to manage Samba remotely from any device using a web browser. Webmin is a powerful web-based configuration tool for managing all aspects of the Linux system. You can easily configure Samba settings with Webmin instead of a command-line interface (CLI).
Step 1: Install Webmin on Ubuntu
The steps below will guide you on how to set up the required environment for installing Webmin in Ubuntu.
Steps to Follow >
➊ Open your Ubuntu terminal.
➋ Type the following command to set up the necessary repositories for installing Webmin on a Linux system by downloading setup-repos.sh script from the Webmin repository on GitHub.
sudo curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
- curl: Retrieves data from or sends data to web or application server by specifying URL.
- -o setup-repos.sh: Option -o determines that output of the URL should be saved to a file named ‘setup-repos.sh’.
- https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh: URL of ‘setup-repos.sh’ in webmin/webmin repository of Gthub from where data will be fetched.
➌ Afterwards, run the following command.
sudo sh setup-repos.sh
- sh: Command for ‘shell’ and executes shell scripts.
- setup-repos.sh: Name of the shell script you want to execute.
➍ Finally your Linux system is ready to install Webmin with the following command.
sudo apt-get install webmin
- sudo: Permits a user with administrative privileges to execute commands.
- apt-get install: Specifies that the install task needs to be performed.
- webmin: Name of Webmin package.
Step 2: Log into Webmin
Now you have to enter your Samba user credentials i.e. username and password to log into Webmin and make necessary changes.
Steps to follow >
➊ Open your internet browser and type the URL of the Webmin into the browser address field. Enter your username and password or your currently logged-in user.
https://localhost:10000
- https: Stands for Hypertext Transfer Protocol and encrypt data for secure communication.
- localhost: Hostname referring to the current machine where the web browser is running.
- 10000: Port number associated with Webmin.
➋ Click on Remember me if you want your user credentials saved.
➌ Hit on Sign in.
➍ Webmin interface will look like the image below.
➎ Then browse through Webmin > Servers > Samba Windows File Sharing.
The preceding image displays the location of Samba Windows File Sharing.
You will see that my previously created folder, Sambashare is already enlisted in the sharing folders. What if I want to create a completely new folder and configure it? Let’s see how to do that.
Steps to follow >
➊ Open your Ubuntu terminal.
➋ I created a folder called Webshare on Desktop of my Linux system using the command below.
mkdir /home/ayesha/Desktop/Webshare
- mkdir: Creates a new folder or directory.
- /home/ayesha/Desktop/Webshare: Absolute path of the newly created Webshare folder.
➌ Go back to Webmin and click on Create a new file share.➍ The browser will direct you to the page, Create File Share. You can now enter the necessary details for a new folder.
➎ Now click on the Directory to share icon.
➏ A list of available directories will appear on the screen.➐ Navigate to the previously created Webshare folder in Desktop and press Select.
The image displays the entire path of the folder, Webshare.
➑ You will be directed back to Create File Share page. Type the shared directory name.
➒ Then click on Create button. You can see in the picture above that the shared directory is enlisted here.
Step 4: Giving Security and Access Control
Now you have to decide if your folder will be accessible or not or who can have control over files. To do that you need to bring modifications in Security and Access Control.
Steps to follow >
➊ Go to the main interface of Samba Windows File Sharing and click on the newly added folder, Webshare.➋ Then click on Security and Access Control.
➌ Select Yes to Writeable and add your user name who will have access. Press on Save afterward.
Step 5: Giving File Permissions
Whether the contents of a shared folder will be readable, writable, or executable depends on the permission granted to users. You can perform this in the File Permissions section.
Steps to follow >
➊ Go back to Edit File Share page and click on File Permissions.➋ Set the Unix file mode to 755 as in the image below and save the changes.
The image shows that the file permission is 755. Here, 7 indicates read-write-execute permission for the owner and 5 denotes read-and-execute permission for both groups and others.
Step 6: Accessing Files
Now to create files and access them from either Windows or Ubuntu operating system, carry out the instructions below.
Steps to follow >
➊ Run the command below to install all cifs and utils protocols which are mandatory parts of the file-sharing process.
sudo apt install cifs-utils samba client -y
- cifs-utils: Package that supports mounting CIFS/SMB file system. CIFS is the core file sharing protocol used by Samba.
- samba client: Package containing necessary client tools for Samba shares and other Samba-related operations.
- -y: Automatically answers ‘yes’ to any confirmation message during installation.
hostname -I
- hostname -I: Display IP address of the current Ubuntu system.
➌ Then navigate to Files >Home> Desktop>Webshare. I have already created a text file in Webshare folder.➍ Afterwards go to the Windows system. Go to This PC and type the IP address you obtained and the folder name, Webshare in your address bar.
\\192.168.153.128\Webshare
- 192.168.153.128: IP address of my Ubuntu system.
- Webshare: Name of the folder used for sharing files.
C. Scheduled Backup of Samba Files on Ubuntu
It is crucial to have a backup of your Samba shared files since easy and flexible access can often pose potential security risks. Having an automated backup ensures recovery of data in case of accidental deletion or data loss incidents and you can continue your sharing operations without any interruption. In this section, I will create a scheduled backup system for my Samba files that will back up my Samba files on Ubuntu every 2 minutes. To do so follow the instructions given below.
Steps to follow >
➊ First create a backup directory where you will save your shared folder. I have chosen to create a directory called Backup in Desktop and viewed its contents with the following commands. You can choose your directory location according to your preference.
mkdir /home/ayesha/Desktop/Backup
ls Desktop/Backup
- mkdir: Creates a folder or directory.
- /home/ayesha/Desktop/Backup: Absolute path of the Backup Directory.
- ls Desktop/Backup: Lists the contents of the Backup directory located in Desktop.
crontab -e
- crontab -e: Edits cron table or crontab. Crontab contains commands or scripts that are executed at specified time intervals.
➌ Now type the command below in the crontab editor.
*/2 * * * rsync -avz /home/ayesha/Desktop/Sambashare /home/ayesha/Desktop/Backup
- */2 * * *: Specifies that the cron job will run every two minutes. This means the copying of files will be executed every two minutes.
- rsync: Copies and synchronizes files with different locations.
- -a: Stands for ‘archive’ and preserves file permissions, ownership, timestamps, and symbolic links. Copied files will retain their original attributes.
- -v: Stands for ‘verbose’ and displays information about files being transferred.
- -z: Compresses data during file transfer.
- /home/ayesha/Desktop/Sambashare: Source directory of the files that you want to backup.
- /home/ayesha/Desktop/Backup: Destination directory where files will be sent for backup.
➍ Press CTRL+O and CTRL+X to save the editor file and exit respectively.➎ Now go to Files > Home > Desktop and click on the Backup folder within Desktop.
➏ You will find the entire Sambashare folder copied to the Backup folder.
➐ Proceed to Sambashare by clicking on it.
The image shows that the contents of Sambashare are automatically copied and synchronized to the Backup folder.
D. How to Remove Samba Server from Ubuntu
If you no longer require Samba or want to simplify system configuration, the removal of Samba would be a wise decision. But before Samba deletion, make sure that it will not adversely affect any other application or service in the Linux system. Remember to have a backup of important files before proceeding with the removal.
Steps to follow >
➊ At first, you need to uninstall the Samba and Samba-client package from your system with the command below.
sudo apt remove samba samba-client -y
- remove: Removes or uninstalls specified packages from the system.
- samba: Name of open-source software package, Samba for file sharing services.
- samba-client: Package containing tools and utilities for interacting with Samba servers.
➋ Run the command below to remove any unnecessary or unused packages that are no longer needed.
sudo apt auto-remove -y
- auto-remove: Automatically removes any unused packages that were initially installed but are no longer needed.
Conclusion
Samba is a powerful tool to facilitate smooth file sharing between Linux and Windows systems. In this article, I have demonstrated a step-by-step guide on how to install and configure Samba using both a command line interface and a web-based tool. Additionally, the removal of Samba and backup of Samba files enhance data integrity and easy data restoration when needed. Furthermore, this guide will provide you optimal file-sharing experience. Comment below and let us know if you have found this article helpful.
People Also Ask
There is a limitation for files mounted using smbmount which is it does not support files larger than 2 GB.
Does Samba have GUI?
Yes, Samba has Graphical User Interface or GUI. It offers a user-friendly way or visual interface to configure Samba, manage user access, and create shares. Some popular GUI for Samba are Webmin, SWAT, KDE control module, and Gnome Samba configuration tool.
How do I access Webmin remotely?
To remotely access Webmin, open your web browser and type https://your-server-ip-address:10000 in the address bar. <your-server-ip-address> is the IP of your Linux system.
How do I stop Samba?
To stop Samba service run the command: sudo service smbd stop or the command: sudo /etc/init.d/smbd stop.
How do I disable Samba on Linux?
To disable Samba service from starting automatically, execute the command: sudo systemctl disable smbd. Moreover, you can disable Samba client service with the command sudo systemctl disable nmbd.