ag@linux:~$

history 33 of 36

Installing Samba on Linux

The basic steps to install Samba with yum, edit its config files, add a Samba user, set folder ownership, test the config and restart the service.

Written

From my Linux study notes, 2018. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.

Samba lets a Linux machine share folders with Windows (and macOS and other Linux) computers using the SMB protocol, the same protocol Windows file sharing uses. These are my short notes for setting it up on a Red Hat-family system using yum.

Before you start

  • A Red Hat-family server (CentOS, Rocky Linux, AlmaLinux) where you can run commands as root or with sudo. My commands below are as I ran them, as root.
  • Placeholders: wherever you see <username> or <dirname>, replace the whole thing, including the angle brackets, with your own value:
    • <username> is the Linux account that will connect to the share, for example amar.
    • <dirname> is the full path of the folder you want to share, for example /srv/samba/share.

Steps

  1. Install Samba and the Samba client tools:

    Terminal window
    yum install samba
    yum install samba-client

    samba is the server. samba-client gives you tools such as smbclient for testing.

  2. Go to the Samba config folder and edit the files:

    Terminal window
    cd /etc/samba
    vi /etc/samba/lmhosts
    vi /etc/samba/smb.conf
    vi /etc/samba/smbusers
    • smb.conf is the main config file. This is where you define your shares.
    • lmhosts maps NetBIOS names to IP addresses. Most setups do not need to change it.
    • smbusers maps Windows user names to Linux user names. It is optional, and newer Samba packages may not include it.

    A simple share in smb.conf (an example added later; change the name in brackets and the values to suit you) looks like this:

    [share]
    path = <dirname>
    valid users = <username>
    read only = no
  3. Create a Linux user and give it a Samba password:

    Terminal window
    useradd <username>
    smbpasswd -a <username>

    Samba users must also exist as Linux users, which is why useradd comes first. smbpasswd -a adds the user to Samba’s own password database and asks you to set a Samba password. This can be different from the Linux login password.

  4. Make the user the owner of the shared folder:

    Terminal window
    chown <username> <dirname>

    If the folder does not exist yet, create it first with mkdir -p <dirname>.

  5. Check whether there are any errors in the config:

    Terminal window
    smbparm

    Correction (added later): the command is testparm, not smbparm:

    Terminal window
    testparm

    testparm reads smb.conf, reports any errors and prints the shares it found.

  6. Restart Samba:

    Terminal window
    service samba restart

    Correction (added later): on Red Hat-family systems the service is called smb (with nmb for NetBIOS name service), not samba:

    Terminal window
    service smb restart

    On Debian and Ubuntu the main service is smbd.

How to check it worked

From the server itself, list the shares as your Samba user:

Terminal window
smbclient -L localhost -U <username>

You should see your share in the list. From Windows, open File Explorer and type \\server-ip\share in the address bar, using your server’s IP and your share name.

Common problems

  • Permission denied even with the right password: on Red Hat-family systems with SELinux enforcing, the shared folder needs the samba_share_t label:

    Terminal window
    sudo semanage fcontext -a -t samba_share_t "<dirname>(/.*)?"
    sudo restorecon -Rv <dirname>

    Labelling the folder is the right fix. Do not disable SELinux to make Samba work.

  • Cannot connect at all: the firewall may be blocking Samba. With firewalld:

    Terminal window
    sudo firewall-cmd --permanent --add-service=samba
    sudo firewall-cmd --reload
  • User cannot log in: check that you ran smbpasswd -a for that user. A normal Linux password alone is not enough.

Notes for today

  • On systemd systems (CentOS 7 and later, Rocky Linux, AlmaLinux) restart and enable Samba with:

    Terminal window
    sudo systemctl restart smb
    sudo systemctl enable smb
  • yum became dnf on newer Red Hat-family systems (yum still works as an alias). On Debian and Ubuntu install with sudo apt install samba.

  • Security: share only the folders you need, use valid users to limit who can connect, and do not set chmod 777 on shared folders.

  • Documentation: https://www.samba.org/samba/docs/