Difficulty: ★★★★☆
The exercise is to setup Samba to share different content for different audiences using an Arch Linux based OS.
The bar is set high as the configuration is done from scratch using a SSH terminal session to the target system.
It will also require understanding of the Access Control List in a GNU/Linux system as well as networking concepts.
The final setup will provide the following features
Users and groups
Shares
A minmal Arch Linux based server - with a valid network configuration accessible using SSH.
The Samba documentation at Arch Wiki.
On your workstation add a hosts entry to simplify connection - I use a Raspberry Pi named rpi so throughout this document uses rpi as a reference to the Samba server.
If you don't know the IP of your device - you can find it using nmap to scan network for devices exposing an open port 22
Depending on your network and your device IP and name - adapt the following to your usecase
Create a new ssh session to your server system.
Run a full system update and install the following packages samba, firewalld and micro
Enable firewalld and configure the service for the home zone
Restart the system and reopen the ssh connection
On the server we will store data in a designated tree starting from our system root (/)
Visualised using tree
The office folder's permission should be editable by group with no access for other - including guests.
The content of /data/media/music is stored on an external USB device, so initially there is no data. The device may or may not be attached, we will handle that later as this pose a challenge.
The media folder has suitable permissions - no need to change.
The public folder is writable by group and readable for other
The office group doesn't exist - so let's create it and assign the group to the documents folder.
For the public folder we decided that authenticated users are allowed to write - guests are not. The default users group is perfect for this.
If you list the content of the data folder - you.
To ensure we do not accidental enable folder traversel outside the shares we will use bind mounts.
Mounting using bind is a simple and efficient method of making data from one folder available from another folder on the same system. The function is similar to symlink but you cannot create folder traversal using dots.
We will create some corresponding folders in /srv and bind our data there.
Open the file /etc/fstab with micro
Append the following lines
Now execute a reload and mount everything
When we list the /srv/samba we see how our changes to the data folders is reflected in share tree
And the tree
In your shell session switch to root session - so we don't have to prepend everything with sudo.
Locate the samba configuration folder on your pi
Samba is always configured from scratch - there is no default config, it must be created before you can start the service.
The micro editor is terminal editor with support for copy/paste and other well known editor shortcuts.
In the samba configuration folder start micro and create the required configuration smb.conf
We start by defining two sections global configuration and public share configuration
Run the command testparm
to ensure your smb.conf is free from spelling errors then start the service.
If you are curios about smb.conf - try this
Verify the public share by opening the file manager on your workstation.
Use Ctrl+L and input smb://ip.x.y.z/public
then press Enter
Login using the anonymous method. Try creating new file - you should expect access denied.
Then go back to your shell and create an empty text file in public folder.
Go back to your file manager press F5 to reveal the new file.
All good - now unmount the public share from the file manager and reopen the share - this time login using the username and credentials from your ssh session - this time edit the file and save it.
Did you get access denied once more? Good - this is expected behavior as there is no users in the samba trust database. Unmount the public share using the eject button in the file manager.
It is very important to remember
If you change your user password you either remember two passwords or change using smbpasswd
for your username.
To enable passwd sync maintenance from samba to system - add this to the global section of smb.conf
Any changes to smb.conf requires the service to be restarted and as a precaution always test the config before restarting.
Using this command will prevent samba from restarting if the testparm command fails
Anonymous users is a security risk. You have no control over the content on your share. This is bad - very, very bad - think ransomware - you don't wannacry :sob: If you - despite all warnings - must create a public share with no restrictions refer to Samba Usage on Arch Wiki.
Remember the leading sentence for this section - a system user is not a samba user. Since our service should allow authenticated users to create content in the public folder - let us start easy with your ssh username.
In your file manager mount the share again - this time using your ssh user and the samba password. Open the empty file - enter some text and save it. Try creating some new content on the public share.
This time you are authenticated and thus allowed to make changes.
Remove your test content from the share and unmount.
Our server is a file server only and as such - besides the admin - users has no use for home folder and they don't need shell access.
Our challenge includes and administrator and two distinct authenticated users with access to the documents folder.
As we configured samba using unix password sync = yes samba will sync the password back to the system.
A designated administrator of shared content. The user has system login permissions and a home folder
A user named hans in group office with no shell, no home and no personal group
Let's create the office user - name grete with no shell, no home, no personal group but a supplementary group office
When you list the content of the /data
folder, root
is listed as owner - but we have our dedicated admin.
Let's change the group for /data root to admin - allowing admin to create new folders
Next we change the owner of content inside /data/ - you did remove the test files - right? Otherwise the owner will change for those as well.
Edit your samba configuration and add the following section
Test the config
restart the service
On your workstation open a terminal and run
The media share resides on a removable device and we want the device to be mounted all times. The problem with removable devices is - they are removable - which is a challenge to be solved.
We will face this challenge using systemd units.
We need the UUID of the partition we want to mount - so attach the device to the Raspberry Pi and list the available block devices
The partition is /dev/sda1 - let's get the UUID and pipe it to the unit file.
systemd mount unit naming convention is to use the mountpoint as filename replacing / with -.
In our case we have placed the music files in the folder /data/media/music so this is where we mount the partition.
Open the unit file using micro
Alter the content to match below - arrange for you UUID which is unique for my system to end the line What=/dev/disk/by-uuid/. (do not add spaces before or after the equal sign = and observe the casing of properties)
Now we create unit which takes care of mount the partition when it is needed. The same rule on filename applies - but the extension is .automount
The content of the automount is as follow
All that is left now is to enable the automount unit
To share the music folder on a common sharepoint - edit your smb.conf and add the following section
Test your configuration
Restart the service
Posted at