Atlantic.Net Blog

How to Create Samba Share on Ubuntu

Samba, also known as “Server Message Block,” is a Common Internet File System protocol used for sharing files, directories, and print services across a network on Linux. It allows you to grant read, write, and anonymous access permissions on a shared directory. It is extremely useful for those who use both Windows and Linux systems on their network.

In this post, we will explain how to install and use Samba to share files and directories on Ubuntu. This procedure is compatible with Ubuntu 20.04 and Ubuntu 22.04.

In This Article

Step 1 – Install Samba Server

By default, the Samba package is included in the Ubuntu repository. You can install it using the following command:

apt-get install samba samba-common-bin acl -y

Once installed, start the Samba services and enable them to start at system reboot:

systemctl start smbd nmbd
systemctl enable smbd nmbd

You can check the installed version of Samba with the following command:

smbd --version

Output:

Version 4.11.6-Ubuntu

Step 2 – Create a Private Share with Samba

In this section, we will show you how to create a private share using Samba so that only authenticated users can access the share.

To do so, edit the Samba main configuration file:

nano /etc/samba/smb.conf

Next, add the following lines at the end of the file:

[Private]
comment = private share
path = /data/private/
browseable = yes
guest ok = no
writable = yes
valid users = @samba

Save and close the file, then create a new user with the following command:

adduser user1

You should see the following output:

Adding user `user1' ...
Adding new group `user1' (1000) ...
Adding new user `user1' (1000) with group `user1' ...
Creating home directory `/home/user1' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for user1
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] Y

Next, set a Samba password with the following command:

smbpasswd -a user1

You should see the following output:

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

Next, create a Samba group with the following command:

groupadd samba

Next, add user1 to the samba group:

gpasswd -a user1 samba

Next, create a shared folder that you have specified in the smb.conf file:

mkdir -p /data/private
touch /data/private/file1

Next, provide read and write permissions to the Samba share:

setfacl -R -m "g:samba:rwx" /data/private

Next, check the Samba configuration file for any errors with the following command:

testparm

Next, restart the Samba service to apply the changes:

systemctl restart smbd nmbd

Step 3 – Create a Public Share with Samba

In this section, we will show you how to create a public share with Samba so everyone can access the public share without providing a username and password.

To create a public share, edit the Samba main configuration file:

nano /etc/samba/smb.conf

Add the following lines at the end of the file:

[Public]
comment = public share
path = /data/public/
browseable = yes
writable = yes
guest ok = yes

Save and close the file, then create a public directory:

mkdir -p /data/public/
touch /data/public/file2

Next, set proper permissions on the public directory:

setfacl -R -m "u:nobody:rwx" /data/public

Next, restart the Samba service to apply the changes:

systemctl restart smbd nmbd

Step 4 – Access Samba Share from Linux

In order to access the Samba share, you will need to install the Samba client on the Linux system. You can install it using the following command:

apt-get install smbclient cifs-utils -y

Next, run the following command to access the private share from the Samba server:

smbclient //samba-ip-address/private -U user1

You will be asked to provide a password for user1:

Enter WORKGROUP\user1's password: 
Try "help" to get a list of possible commands.

Once you are connected, run the following command to list Samba share:

smb: \> list
0:	server=69.87.221.84, share=private

Next, list all the files in the private share directory:

smb: \> ls

You should see the following output:

  .                                   D        0  Sun Jun 27 08:17:58 2021
  ..                                  D        0  Sun Jun 27 08:18:13 2021
  file1                               N        0  Sun Jun 27 08:17:58 2021

		51538400 blocks of size 1024. 47348972 blocks available

Next, exit from the Samba shell with the following command:

smb: \> exit

If you want to connect to the Public share, run the following command:

smbclient //samba-ip-address/public

Just press Enter without providing any password:

Enter WORKGROUP\root's password: 

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

Next, run the following command to list all files in the Public share:

smb: \> ls

Output:

  .                                   D        0  Sun Jun 27 08:18:17 2021
  ..                                  D        0  Sun Jun 27 08:18:13 2021
  file2                               N        0  Sun Jun 27 08:18:17 2021

		51538400 blocks of size 1024. 47348972 blocks available

Step 5 – Mount Samba Share on Linux

Samba also allows you to mount a shared directory to the client system so you can access and use it.

First, create a directory on the client system where you want to mount the Samba share:

mkdir /mount

Next, run the following command to mount the Private share directory to the client system:

mount -t cifs -o username=user1 //samba-ip-address/private /mount

You will be asked to provide a password of user1 to mount the directory:

Password for user1@//69.87.221.84/private:  ********* 

Next, verify the mounted directory with the following command:

df -h

You should see your Private shared directory mounted on the /mount directory:

Filesystem              Size  Used Avail Use% Mounted on
udev                    981M     0  981M   0% /dev
tmpfs                   199M  2.2M  197M   2% /run
/dev/sda1                50G  1.9G   46G   4% /
tmpfs                   994M     0  994M   0% /dev/shm
tmpfs                   5.0M     0  5.0M   0% /run/lock
tmpfs                   994M     0  994M   0% /sys/fs/cgroup
tmpfs                   199M     0  199M   0% /run/user/0
//69.87.221.84/private   50G  4.0G   46G   9% /mount

Now, access the Samba share locally using the following command:

ls /mount/

You should see the following output:

file1

Conclusion

In the above guide, you learned how to install Samba and use it to share files and directories between Linux systems. Give it a try on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year