Setting up a Samba server on Linux


Setting up a Samba server on Linux allows you to share files and printers with Windows, macOS, and other Linux machines on your network. I wanted to be able to share files from my higher storage linux laptop to my lower storage mac mini.

1. Installation

First, install the Samba package on your Linux system.
For debian systems

sudo apt update
sudo apt install samba

For Fedora and redhat based systems. This also sets the firewall to allow access to Samba from other computers.

sudo dnf install samba
sudo systemctl enable smb --now
firewall-cmd --get-active-zones
sudo firewall-cmd --permanent --zone=FedoraWorkstation --add-service=samba
sudo firewall-cmd --reload

2. Verify Samba Service Status

After installation, check if the Samba service is running.

Bash

systemctl status smbd

The output should indicate that the service is active and running.

3. Configuration

The main Samba configuration file is smb.conf, located in /etc/samba/. It’s a good practice to back up the original configuration and create a new one.

Bash

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

Here’s a template for the essential global settings in /etc/samba/smb.conf:

Ini, TOML

[global]
   server string = Samba Server Description %v
   workgroup = WORKGROUP
   security = user
   guest account = baduser
   name resolve order = broadcast host
   include = /etc/samba/shares.conf
  • server string: A descriptive name for your Samba server.
  • workgroup: Set this to the workgroup or domain name of your network.
  • security: Set to user for username and password authentication.
  • guest account: Specifies the user used for guest access (if enabled).
  • name resolve order: Defines the order in which Samba resolves hostnames.
  • include: This line includes a separate file for defining your shared directories, keeping the main configuration clean.

4. Configure Shared Directories

Create the file specified in the include directive (e.g., /etc/samba/shares.conf) to define your shares.

Bash

sudo nano /etc/samba/shares.conf

Here’s an example of a share definition in /etc/samba/shares.conf:

Ini, TOML

[public files]
   path = /srv/samba/public
   force user = smbuser
   force group = smbgroup
   create mask = 0664
   force create mode = 0664
   directory mask = 0775
   force directory mode = 0775
   public = yes
   writable = yes
  • [public files]: The name of the share as it will appear on the network.
  • path: The absolute path to the directory you want to share.
  • force user and force group: Ensures that files and directories created within the share are owned by the specified user and group.
  • create mask, force create mode, directory mask, and force directory mode: Control the permissions of newly created files and directories.
  • public: Set to yes to allow guest access to the share.
  • writable: Set to yes to allow users to write to the share.

5. Set Up Directories, Users, and Permissions

Create the directory you specified in the share configuration and set appropriate permissions and ownership.

Bash

sudo mkdir -p /srv/samba/public
sudo groupadd --system smbgroup
sudo useradd -M -s /sbin/nologin -g smbgroup smbuser
sudo chown -R smbuser:smbgroup /srv/samba/public
sudo chmod -R g+w /srv/samba/public
  • sudo mkdir -p /srv/samba/public: Creates the shared directory.
  • sudo groupadd --system smbgroup: Creates a new system group for Samba users.
  • sudo useradd -M -s /sbin/nologin -g smbgroup smbuser: Creates a new system user for Samba access without a home directory and with no login shell.
  • sudo chown -R smbuser:smbgroup /srv/samba/public: Changes the ownership of the shared directory to the Samba user and group.
  • sudo chmod -R g+w /srv/samba/public: Grants write permissions to the Samba group for the shared directory.

If you require password-protected shares, you will need to create Samba users and set their passwords using smbpasswd.

Bash

sudo smbpasswd -a smbuser

Follow the prompts to set the password for the Samba user.

6. Restart and Test Samba

After making configuration changes, restart the Samba service.

Bash

sudo systemctl restart smbd

Check the service status again to ensure it restarted without errors.

Bash

sudo systemctl status smbd

SELinux Issue

I spent alot of time trying to debug the share only to realise se linux was blocking my access to the public directory. The following command fixed it for me

sudo setsebool -P samba_export_all_rw 1

7. Accessing Shares

You can now access the Samba shares from other machines on your network:

  • Windows: Open File Explorer and navigate to “Network”. You should see your Samba server listed.
  • macOS: In Finder, go to “Go” > “Connect to Server…” and enter smb://your_server_ip or smb://your_server_name.
  • Linux: Use a file manager and connect to smb://your_server_ip/share_name.

This generalized report provides the core steps and configurations for setting up a basic Samba server on Linux, based on the information from the video. Remember to adjust paths, workgroup names, and user/group names to match your specific requirements.

References:


Leave a Reply

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