Setup Shareable Drive with Samba in Linux

📆 · ⏳ 5 min read · ·

Introduction

In the previous article, I talked about setting up a shareable drive in Linux with NFS. Although while researching, I did check out setting up a shareable drive with SMB using Samba.

While I am sticking with NFS for my use case since I am communicating between Linux servers, It might be useful to know how to setup drive with SMB which can be accessed from a Windows system as well.

What is SMB?

SMB stands for Server Message Block. It is a network protocol that allows users to share files, printers, and other information between computers. It is also known as Common Internet File System (CIFS).

If you want to have a shareable drive which communicates with Windows systems, you will need to setup a shareable drive with SMB.

What is Samba?

Samba ↗️ is an open-source implementation of the SMB protocol. It allows Linux systems to communicate with Windows systems using SMB protocol.

We will learn how to set it up correctly and make a drive network shareable.

Let’s get started

Once again we need at least two machines to setup a shareable drive. One will be the server and the other will be the client.

Server

First, let’s setup the server machine. I am using Debian 12 but the commands should be similar for Ubuntu and other Debian based distros (or replace some installation commands with your distro’s package manager).

Let’s install Samba on the server machine.

Terminal window
sudo apt install samba

Once samba is installed, we will add ourself as a user to samba.

Terminal window
sudo smbpasswd -a <username>

Replace <username> with your username.

Now we will create a directory which we will share with the client machine.

Terminal window
mkdir /media/smb_share

Next, we will edit the samba configuration file.

Terminal window
sudo vi /etc/samba/smb.conf

Go to the very bottom of the file and add the following lines.

Terminal window
[smb_share]
path = /media/smb_share
available = yes
valid users = <username>
read only = no
browsable = yes
public = yes

What this is doing is creating a shareable directory called smb_share in /media directory. It is also allowing the user <username> to access it.

We have also marked this as read only = no, which means that the user can read and write to this directory which is most likely your use case as well but feel free to update any values as per your need.

You can read more about the configurations on the available man page ↗️

Once you are done, save and exit the file.

Next, we will restart the samba service.

Terminal window
sudo systemctl restart smbd

That’s it from the server configuration point of view, let’s move to the client setup now.

Client

Now, we would pretty much do the same thing as we did in the previous blog with few additional steps.

First, we will save our samba credentials in a file. This is required because when we try to connect to the server, it will ask for the password and we don’t want to enter it every time.

Terminal window
touch ~/.smbcredentials
echo "username=<username>" >> ~/.smbcredentials
echo "password=<password>" >> ~/.smbcredentials

Replace <username> and <password> with your username and password that you added during the samba setup on the server.

Next, we will create a directory where we will mount the shareable drive.

Terminal window
mkdir -p /mnt/smb_share

Now, we will mount the shareable drive.

Terminal window
sudo mount -t cifs -o credentials=~/.smbcredentials //192.168.0.100/smb_share /mnt/smb_share

Replace 192.168.0.100 with your samba server IP address.

This will mount the shareable drive on the client machine. You can now access it from the /mnt/smb_share directory.

Auto Mounting Drive on Boot

If you want to mount the drive automatically on boot, you can add the following line to your /etc/fstab file.

Terminal window
//192.168.0.100/smb_share /mnt/smb_share cifs credentials=~/.smbcredentials,defaults 0 0

Once again, replace 192.168.0.100 with your IP address.

Use the mount -a command to check if the drive is mounted correctly.

And you can again verify if it is mounted correctly or not by using mount and grep commands.

Terminal window
mount | grep smb_share

Few Gotchas

In this section, I will just list down few things that I encountered while setting up the shareable drive.

Firewall

If you are using a firewall, you will need to allow the samba port. The default port for samba is 445.

If you are using ufw firewall, you can directly use the samba rule and add it to the allow list.

Terminal window
sudo ufw allow samba

Debugging Mounting Issues

If for some reason you are not able to mount the drive, you can use the dmesg command to debug the issue.

Terminal window
dmesg | tail

I have found this very helpful in debugging the mounting issues. Looking for the messages related to the mount operation and in case of any errors it will most likely tell you what is wrong along with the error code.

Use that to find the relevant solution to your problem.

Conclusion

In this article, we learned how to setup a shareable drive with SMB using Samba. We also learned how to mount the drive on the client machine and how to auto mount it on boot.

While I am sticking with NFS for my use case, I hope this article was helpful to you in setting up a shareable drive with SMB.

I hope you found this article useful. If you have any questions or feedback, please feel free to reach out to me on X / Twitter ↗️.

Until next time đź‘‹.

You may also like

  • Setup Shareable Drive with NFS in Linux

    In this article we will learn how to setup a shareable drive with NFS in Linux. We will see the steps to setup NFS server and mount the drive on a client machine.

  • AdGuard Home — Network Wide Ad Blocking in your Homelab

    Let's talk about AdGuardHome, a network-wide ad blocking software that you can run in your homelab. It's a great way to block ads and trackers on your network without having to install ad blockers on every device.

  • Nginx — The reverse proxy in my Homelab

    Nginx is a powerful reverse proxy that I use in my homelab to expose services to the internet. In this post, I'll show you how I use it and how you can use it too.