Samba file share in linux

Firstly, use apt to install samba in our linux system

sudo apt install samba -y

Samba hosts all it’s configuration under the directory /etc/samba/
In this tutorial we will be making a fresh configuration file, before we do so we’ll take a backup of the default configuration.

sudo mv /etc/samba/smb.conf /etc/smb/smb.conf.old

Now we can go ahead and build our configuration file. Here I will be using nano as the text editor.

sudo nano /etc/samba/smb.conf

And copy paste the below configuration.

[global]
server string = Media Server
workgroup = WORKGROUP
map to guest = Bad User
usershare allow guests = yes
hosts allow = 192.168.8.0/24
hosts deny = 0.0.0.0/0
name resolve order = bcast host
include = /etc/samba/shares.conf

Press Ctrl + O to save and Ctrl + X to exit.

Now lets dig in and see what each line is for.

[global] - Parameters in this section apply to the server as a whole.
server string
- It can be any string that you wish to show to your users.
workgroup - Name of your windows workgroup in your network. Default is “WORKGROUP” in caps.
map to guest - Tells samba to what to do with login requests.
Bad User - Means if the username and password is invalid, then treat the user as guest.
usershare allow guest - Allows guest users to access the share
hosts allow - Specify which hosts to allow access. Here I have allow everything in my internal network.
hosts deny - Specify which hosts to deny access. Here I have denied everything other than my internal network.
name resolve order - Tells samba what order to resolve host names to IP addresses. In my example, we do a broadcast “bcast” first, followed by “host” specified in /etc/hosts

Lets now create another file called shares.conf and tell what all folders we need to share.
This is not strictly necessary, you can include the below configuration in /etc/samba/smb.conf and it will work the same.

sudo nano /etc/samba/shares.conf

Copy paste the below configuration and edit it to your needs.

[Movies]
path = /media/pi/HDD/Movies
read only = no
guest ok = yes
force user = pi
force group = pi

[Anime]
path = /media/pi/HDD/Anime
read only = no
guest ok = yes
force user = pi
force group = pi

Press Ctrl + O to save and Ctrl + X to exit.

Now I will go through what each line is for.
[Movies] - Your share folder name
path - Path of the folder you want to share.
read only - If this parameter is no, then users can read and write to the directory.
guest ok - Allows guest users.
force user - Any file or folder created in the share folder will have the user rights specified under this parameter, regardless of the username and password clients uses to access the share folder.
force group - Any file or folder created in the share folder will have the group rights specified under this parameter.

Before we access the share folders. lets restart the samba service.

sudo service smbd restart
or
sudo systemctl restart smbd.service


Leave a Reply

Your email address will not be published. Required fields are marked *