Chuck Carroll


How to Set Up a Samba Share on a Linux Server

Published: 2024-08-21

One of the neat things about taking computer networking courses is learning how to do things I more or less already know how to do, but learning how to do them better and more simply. I've been considering replacing my Thinkcentre M920Q running OpenMediaVault and CasaOS, and instead using my Raspberry Pi 5 running an Arch Linux distro and just set up a samba server.

There's certainly advantages to using OMV - it's got a user-friendly GUI that makes setting up a NAS easier. However, I wonder if an Intel Core i7 is a bit overkill for what I use a home server for. The RPI5 is more power efficient, physically smaller, and great for these kinds of simple tasks. Of course, I could simply install OMV on an SD card, pop it into the RPI5, and manage my NAS that way, but 1) I love doing things myself and 2) I don't like having multiple machines doing tasks that could easily be done by one.

Right now, the RPI5 is running the Arch-based distribution EndeavourOS and it's only job at the moment is to be a torrent seed box. But now I'd like to give it more responsibilities like providing NAS services on my home network. Below are the steps I used to set up a Samba share, inspired by one of the lab assignments in my Linux server course.

1. Install Samba

  1. The first step is to actually install Samba. I'm using an Arch based distribution so I run sudo pacman -Sy samba, but any package manager will have samba.
  2. Enable the service with the command
    sudo systemctl enable smb nmb.
  3. If you're running a firewall, you can allow Samba to run at ports 139 and 255 by default with sudo firewall-cmd --add-service=samba --permanent. Then run sudo firewall-cmd --reload to reload the firewall.

2. Setting up User Accounts

  1. If running Windows, create a user account by running
    net user [username] [password] /add.
  2. Back on the Linux machine, create the server user with
    sudo useradd -M -s /sbin/nologin [username].
  3. Set a password for the new user account by running
    sudo passwd [username].
  4. Create a group for the user with
    sudo groupadd [group share name] and add the user account to this group with
    sudo usermod -aG [group share name] [username].
  5. Create a new Samba File Server user with
    sudo smbpasswd -a [username] and enter a password.

3. Create/Choose Directories to Share

  1. You first need to create a folder or choose an existing one to share, for example /home/[user]/share.
  2. Set the correct ownership for the folder with
    sudo chown root:[group share name] /home/[user]/share -R.
  3. Set the permissive file permissions for the new path with
    sudo chmod 777 /home/[user]/share -R.

3a. Configure SE Linux if necessary

  1. If you're running SE Linux, you need to allow Samba File Server to read and write to the path by running sudo semanage fcontext -a -t samba_share_t "/home/[user]/share(/.*)?".
  2. Then run restorecon -R -v /home/[user]/share.

4. Create the Samba File Share

  1. Edit the Samba File Server configuration with
    sudo vim /etc/samba/smb.conf.
  2. Scroll down to the Global Section which begins with "[global]" and add the following:
    workgroup = WORKGROUP
    netbios name = myserver
    security = user
    map to guest = bad user

  3. Scroll to the bottom of the configuration file and add:
    [share]
    comment = restricted access read/write share
    path = /home/[user]/share
    read only = No
    valid users = @[group share name]
  4. Write and quit vim (:wq).
  5. Now start the file server by running sudo systemctl start smb nmb.

Connect to the server

Now connect with your machine. I'm using GNOME, so I'll connect to the new server by opening my file manager (Nautilus), click on "Other Locations" in the sidebar, and in the server address field enter smb://[ip address of server]/share/. From there, I am prompted with the username and password created in step 2. If all went well, you should be able to access the shared folder from your Linux server.

Thanks for reading. Feel free to send comments, questions, or recommendations to hey@chuck.is.