DEV Community

Hedy
Hedy

Posted on

How to install Samba on Raspberry Pi?

Installing Samba on a Raspberry Pi is a very common task to allow it to share files with Windows, macOS, and Linux computers on your local network.

Here is a detailed, step-by-step guide.

Prerequisites

  1. A Raspberry Pi (any model) running Raspberry Pi OS (formerly Raspbian) or another Debian-based OS.
  2. The Pi should be connected to your local network and have internet access.
  3. You should be comfortable using the command line.

Step 1: Update Your System
Always start by updating the package list and upgrading existing packages to their latest versions. Open a terminal on your Pi or connect via SSH and run:

bash

sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Samba
The Samba software is in the default repositories. Install it with this command:

bash

sudo apt install samba samba-common-bin -y
Enter fullscreen mode Exit fullscreen mode
  • samba: The main Samba suite.
  • samba-common-bin: Provides common Samba binaries and utilities, like the one we need to add users (smbpasswd).

Step 3: Configure Samba (The Most Important Step)
The main Samba configuration file is /etc/samba/smb.conf. It's crucial to back up the original file before making changes.

1. Create a Backup:

bash

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup
Enter fullscreen mode Exit fullscreen mode

2. Edit the Configuration File:
You can use the text editor you prefer, like nano or vim. I'll use nano:

bash

sudo nano /etc/samba/smb.conf
Enter fullscreen mode Exit fullscreen mode

3. Modify the Global Settings (Optional but Recommended):
Near the top of the file, look for the [global] section. Ensure it has at least these lines to ensure it works well on modern networks and to set a descriptive workgroup name (like WORKGROUP for Windows):

ini

[global]
workgroup = WORKGROUP
wins support = yes
security = user
passdb backend = tdbsam
obey pam restrictions = yes
unix password sync = yes
Enter fullscreen mode Exit fullscreen mode

security = user is the most common and secure setting, requiring a username and password.

4. Define Your Share:
Scroll to the bottom of the file and add a new section for the directory you want to share. The section name (e.g., [pi-share]) is what will appear as the name of the shared folder on other computers.

Here is a robust example configuration to share the home directory of the pi user:

ini

[pi-share]
comment = Raspberry Pi Share
path = /home/pi
browseable = yes
read only = no
writable = yes
create mask = 0777
directory mask = 0777
public = no
valid users = pi
Enter fullscreen mode Exit fullscreen mode
  • path: The absolute path to the directory you want to share (e.g., /home/pi/sharedfolder if you created a separate folder).
  • browseable: Allows the share to be visible in the network neighborhood.
  • read only / writable: Defines if users can write to the share.
  • create mask / directory mask: Defines the permissions for new files and directories.
  • public: Set to no to require a password.
  • valid users: The system user(s) allowed to access this share.

5. Save and Exit:
In nano, press Ctrl+X, then Y to confirm, then Enter to save the file.

Step 4: Create a Samba User and Password
Samba uses its own password database. You need to add a Linux system user (which you already have, pi) to Samba and set a password for them.

Important: This Samba password can be different from your Linux pi user password.

bash

sudo smbpasswd -a pi
Enter fullscreen mode Exit fullscreen mode
  • -a adds the user.
  • pi is the username. You will be prompted to enter and confirm a new password for Samba access.

Step 5: Restart the Samba Service
For all your changes to take effect, restart the Samba service:

bash

sudo systemctl restart smbd
Enter fullscreen mode Exit fullscreen mode

To ensure Samba starts automatically when the Pi boots, run:

bash

sudo systemctl enable smbd
Enter fullscreen mode Exit fullscreen mode

Step 6: Test Your Configuration
It's good practice to test your configuration file for syntax errors:

bash

testparm
Enter fullscreen mode Exit fullscreen mode

This command will load your smb.conf file and show you any errors. If it runs without critical errors, you're good to go.

Step 7: Connect from Another Computer
Now, try to access the share from another computer on your network.

On Windows:

  1. Open File Explorer.
  2. In the address bar, type \raspberrypi (or \).
  3. Press Enter. You will be prompted for the username (pi) and the Samba password you set in Step 4.
  4. You should see the pi-share folder and have access to it.

On macOS:

  1. In Finder, go to the Go menu and select Connect to Server... (or press Cmd+K).
  2. In the server address field, enter smb://raspberrypi (or smb://).
  3. Click Connect and enter the credentials when prompted.

On Linux:

Typically, you can use your file manager (Nautilus, Dolphin, etc.) and look for the Pi under "Network" or enter the address smb://raspberrypi in the location bar.

Troubleshooting Tips

  • Can't connect? Check if the Raspberry Pi's firewall (if enabled, like ufw) is blocking Samba ports. You can temporarily disable it for testing with sudo ufw disable.
  • Permission denied? Double-check the Linux file permissions on the directory you are sharing (/home/pi). The user pi must have read/write access to it. You can run ls -ld /home/pi to check.
  • Still having issues? Check the Samba logs for clues: /var/log/samba/log.smbd.

Top comments (0)