Skip to main content

Setting Up a File Sharing Server Using Samba

··3 mins·
Linux Samba
Makoto Morinaga
Author
Makoto Morinaga
A personal notebook for tech notes, coding, and system experiments.
Table of Contents

Storing various data such as photos and videos on a personal computer can gradually consume disk space. Additionally, sharing data within a household often requires manual file transfers using USB drives, which can be cumbersome.

Setting up a dedicated file-sharing server helps address these issues. A central file server allows storing data in one location, freeing up space on personal computers, and enabling seamless data sharing across multiple devices.

Samba is used as the file-sharing solution, facilitating interoperability among Linux, macOS, and Windows systems.

Installing Samba
#

On the server, install Samba using the following command:

Terminal
# Arch Linux
sudo pacman -S samba

Creating Samba Access Users
#

Samba requires a corresponding Linux user to manage access. If necessary, create users using useradd. After that, register the users with Samba using the pdbedit command.

Example of registering existing Linux users (mako, sato, taka) for Samba access:

Terminal
sudo pdbedit -a mako
sudo pdbedit -a sato
sudo pdbedit -a taka

Usernames are shared with the Linux system, but passwords are managed separately.

Creating Directories for Samba
#

A directory structure is set up to distinguish between personal directories and a shared directory for multiple users.

Creating the Samba Root Directory
#

Terminal
sudo mkdir -p /data/share

Creating Personal Directories
#

Directories for individual users are created with appropriate ownership:

Terminal
sudo mkdir /data/share/personal
sudo mkdir /data/share/personal/mako
sudo mkdir /data/share/personal/sato
sudo mkdir /data/share/personal/taka
sudo chown mako:mako /data/share/personal/mako
sudo chown sato:sato /data/share/personal/sato
sudo chown taka:taka /data/share/personal/taka

Creating a Shared Directory
#

A user group is created for shared access, and users are added to it:

Terminal
sudo groupadd -g 2000 common
sudo gpasswd -a mako common
sudo gpasswd -a sato common
sudo gpasswd -a taka common

The shared directory is created and assigned appropriate group ownership:

Terminal
sudo mkdir /data/share/common
sudo chown :common /data/share/common

To ensure all files inherit the correct group ownership, the setgid bit is set:

Terminal
sudo chmod g+s /data/share/common

Configuring Samba
#

Create or modify the smb.conf configuration file at /etc/samba/smb.conf as follows:

/etc/samba/smb.conf
[global]
   server min protocol = SMB2
   dos charset = CP932
   unix charset = UTF-8
   load printers = no
   workgroup = WORKGROUP
   server string = Samba Server
   server role = standalone server
   hosts allow = 192.168.10. 127.
   log file = /var/log/samba/%m.log
   max log size = 500
   dns proxy = no

[homes]
   comment = Home Directories
   path = /data/share/personal/%U
   browseable = no
   writable = yes

[common]
   path = /data/share/common
   valid users = mako sato taka
   writable = yes
   create mode = 0770
   directory mode = 0770
   printable = no

Starting and Enabling Samba Service
#

Start the Samba service:

Terminal
sudo systemctl start smb.service

Enable the service to start at boot:

Terminal
sudo systemctl enable smb.service

Configuring iptables
#

To allow access to the file-sharing server from the LAN, permit incoming connections on TCP port 445.

For further details, refer to: iptables Configuration.

Example rule:

Terminal
iptables -A INPUT -p tcp -s 192.168.10.0/24 --dport 445 -j ACCEPT

Verifying Samba Functionality
#

A Mac system can be used to verify connectivity using Finder’s Connect to Server feature. Navigate to Go > Connect to Server, enter the server’s IP address, and provide the configured username and password. If the shared directories appear, the setup is successful.

Related

Setting Up an Internal DNS Server Using NSD and Unbound
··2 mins
Unbound Nsd Linux
iptables Configuration
··4 mins
Linux Firewall Iptables
Setting Up an NTP Server Using ntpd
··2 mins
Linux Ntp