How to build a file server with Linux

Table of contents

No heading

No headings in the article.

If you're learning about Linux, it's great to get hands on. In this article, I'm going to demonstrate how to set up a file server on Debian 11. If you'd like to learn more about Debian, please click here.


Overview
We will install Samba to run our file server. Samba is a linux package which allows you to make SMB shares.

Server Message Block (SMB)
If you know about SMB, you can skip this section. If you're new, I'd like you to read this, especially if you're unsure if whether SMB or FTP is right for you.

Server Message Block (SMB) allows you to share files, folders, and printers over a network. It operates on the client-server model. So, when a computer connects to a server with SMB shares, they will be able to see those shares from their file explorer (as long as they have appropriate permissions).

In a practical scenario, let's say a company wants their employees to have access to a centralized location of important document templates. What's the best thing to do here? Make SMB shares with those templates and have your employees connect to that server from their file explorer.

SMB and FTP are commonly used interchangeably, and sometimes, can be confused with each other. If you ever hear someone say, "l need to set up an FTP server" - they are most likely talking about building a file server with SMB shares.

What makes SMB and FTP different is that, SMB, again, operates on the client-server model, while FTP, operates on the peer-to-peer model. FTP is simply used to transfer files between two computers, while SMB is used to share files, folders, and printers, out to your network.

That being said, if you are trying to transfer a bash script, or some file, to another computer, you are better off using FTP. If you want to build a server that shares folders/files to clients on your network, then SMB shares are the way to go.

Installing Required Packages
Let's begin with installing the required packages. We'll begin with running this command to install Samba.

sudo apt-get install samba -y

Once Samba is done installing, you can verify it installed properly by checking that the daemon is running. Use the following command below to do so.

systemctl status smbd

You should get the following output below. Look to find "active (running)" in your output.

smbd.service - Samba SMB Daemon
Loaded: (lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-10-15 19:55:55 EDT; 27s ago
Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)


Initial Setup
Now that Samba is installed, we have to create an SMB user, so we can log into the server to access the SMB share(s). With Samba, you can either create individual user accounts, or you can integrate with Active Directory or LDAP (beyond the scope of this article).

Let's go ahead and create a user now. Run the following commands below. Replace the username and passwords with your preference.

The command below will create a user with the name "demo".

adduser demo

This command will set a password for "demo".

passwd demo
New password: somepassword
Retype new password: somepassword
passwd: password updated successfully

This command will add an SMB user and set its password. Users are not automatically added when created in linux, so you have to run this command.

smbpasswd -a demo
New SMB password: somepassword
Retype new SMB password: somepassword
Added user demo.


Creating your first share
Now that you've done some initial set up, let's go ahead and create our first share. First, you must choose a location in the file system to create a share. For simplicity, I will go ahead and create a subdirectory in the root directory called "samba", then I will make a subdirectory in samba called "share", which will be the share.

Let's start with setting up our directories. The commands below will navigate you to the root directory and create the samba directory, and a share subdirectory in the samba directory.

cd /
mkdir samba
mkdir samba/share

Now, in order to make an SMB share, we have to edit a config file called the smb.conf file. The config file is stored in the /etc/ directory. Let's go ahead and edit the file now. Use your favourite text editor to edit the file. I'll use vim.

sudo vim /etc/samba/smb.conf

Make your way down to the bottom of the file and add the following text. Note that [share] will be the name of the share when you connect to the server.

[share]
path = /samba/share
public = yes
browseable = yes
writeable = yes

Now, the text you see above consists of a very basic configuration. To learn more about using smb.conf, I strongly encourage reading the man page for it. (yes, there's a man page for smb.conf!)

After saving changes, you'll need to restart the SMB daemon. Run the following command below to do that.

systemctl restart smbd


Connecting to the SMB Share
It is now time to connect to the SMB share. I'll show you how to connect to your share from macOS and Windows.

macOS
First, locate your finder app and open it.

Screen Shot 2022-10-15 at 8.00.05 PM.png
In the menu bar, click "go", then click "connect to server".

Screen Shot 2022-10-15 at 8.18.41 PM.png
Type in smb://x.x.x.x/share replacing x with the IP address of your server. The share value represents that [share] decorator from the smb.conf file.

When prompted, put the username and password, and voila. You should be connected now.

Screen Shot 2022-10-15 at 8.03.58 PM.png Screen Shot 2022-10-15 at 8.19.25 PM.png
Windows
Let's connect to the SMB share from Windows. Start by navigating to your file explorer, then in the path bar, type \\x.x.x.x\share replacing x with the IP address of the server. Again, share represents the [share] decorator from the smb.conf file.

Screen Shot 2022-10-15 at 8.23.15 PM.png
You'll be prompted for a username and password. Add the account credentials that were created earlier.
After that, you're in!

Screen Shot 2022-10-15 at 8.25.40 PM.png