Samba is an open-source software suite that runs on Unix/Linux based platforms but is able to communicate with Windows clients like a native application. So Samba is able to provide this service by employing the Common Internet File System (CIFS). At the heart of this CIFS is the Server Message Block (SMB) protocol.
In this context, we shall look into how to install and use Samba to share files on Ubuntu Linux distribution.
How to install Samba on Ubuntu 20.04 ?
Here, on your Ubuntu Server, log into your server as a root user with sudo privileges.
To install Samba on your system, follow the steps provided below.
i. It is recommended to update your system apt packages repository before installing any new package or software.
So, run the below-mentioned command to update the apt repository:
$ sudo apt update
ii. Now, by executing the following command install Samba:
$ sudo apt install samba smbclient
To further proceed with the installation process press 'y' and then hit 'Enter'.
In a while, the installation of Samba will complete on your system.
iii. You can check the Samba running service status by typing the following command:
$ sudo systemctl status smbd
iv. Now, we will enable the 'Samba' profile by changing the firewall access rules.
$ sudo ufw allow 'Samba'
How to Share folders using Samba ?
Once the installation of Samba is completed, now we can share folders or directories graphically by using the default Ubuntu file manager tool 'Nautilus'.
Let's play a demo on how to share directories through the Nautilus file manager.
i. Go to the file manager and choose a folder that you want to share through your local network.
ii. Right-click on the required folder and then choose the 'Local Network Share' option from the displaying list items.
iii. The following folder sharing dialog will be displayed on the system. You need to mark the following checkboxes to share the directory.
iv. Check the 'Share this folder' checkbox and then, type the share name and you can also add a comment but, this is optional.
Only the owner of the share directory in the default Linux configuration can read or write to the files and directories in the Samba share.
v. Click on the 'Create Share' button.
vi. After that, you will see dialog box on your system. Click on the 'Add the permissions automatically'.
You will see that share is enabled now and ready to use. Now, close the folder sharing dialog by clicking on the cancel option.
Once you share a folder, the folder icon should be changed.
How to Create Samba users in Linux ?
If you are a guest user then, you don't need any user account authentication to access Samba shares directories. By using guest shares, everyone can access the sharing folder on the network. But, you need a Samba user account to allow access to Samba shares as an authorized person.
Note: you should know that local Linux server accounts are linked with the Samba user accounts. So, you can keep the same name of Samba users that matched with the local Linux server user's accounts.
For example, we are creating a Samba user named 'linuxapt' by running the following command:
$ sudo smbpasswd -a linuxapt
In the above command, the name 'linuxapt' is the name of my login user on Ubuntu.
Enter the new password for a user and press 'Enter'. Again, retype the new password and hit 'Enter'.
You will see on the terminal that the new Samba user is created successfully and a new password has been assigned to a newly created user.
How to Access Samba shares on Linux system ?
To access the Samba shares the Samba client program should be installed on your Ubuntu system.
i. Click on the 'other Locations' and enter the address of Samba shares in connect to server field.
ii. You will see a dialog on your system:
Where you have both choices either you want to connect as an 'Anonymous' user or as a 'Registered user'. Type Samba user credentials and then click on the 'Connect' button.
iii. Click on your server or hostname.
iv. Here, you will see that all Samba shares directories.
v. Now, just click on a particular directory and enter credentials.
vi. You can then access the Samba shares folder's content on your system.
How to Access Samba shares on Windows ?
Windows user can also connect to the Samba shares by using the following steps:
i. Open the file explorer and right-click on 'This PC' option. The following dropdown list will open. Select 'Add a Network Location'.
ii. You will see an setup wizard on the system. Then, Click on Next.
iii. Now, again click on Next to choose a custom network location.
iv. Enter your server IP and Samba shares User name.
v. Enter the Samba account credentials now you can see the shares directories.
How to Access Samba Shares using Command Line ?
i. Create a directory that you want to share through Samba using the following syntax and assign privileges using the chown and chmod 2770 command:
$ mkdir /home/<folder-name>/<user-name>
$ mkdir /home/samba/linuxapt
ii. Now, enable user using the following command:
$ sudo smbpasswd -e linuxapt
iii. Make the copy of the smb.conf file for safety using the following command:
$ sudo cp /etc/samba/smb.conf ~
iv. Edit the configuration file in your favorite text editor by running the following command:
$ sudo nano /etc/samba/smb.conf
v. Once the configuration file is loaded, add the following code at the end of the file:
path = /home/<user_name>/<folder_name>
valid users = <user_name>
read only = no
vi. Replace the user_name and folder_name with your own details.
Now, restart the samba services by using the command as mentioned below:
$ sudo service smbd restart
vii. To test any errors in the smb.conf file, run the below-given command:
Now, using the following command you can access samba share folders:
$ sudo apt install smbclient
The following command will list all samba shares folders:
$ smbclient -L //<HOST_IP_OR_NAME>/<folder_name> -U <user>
Type the following command to connect with samba share:
$ smbclient //<HOST_IP_OR_NAME>/<folder_name> -U <user>