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 exampleamar.<dirname>is the full path of the folder you want to share, for example/srv/samba/share.
Steps
-
Install Samba and the Samba client tools:
Terminal window yum install sambayum install samba-clientsambais the server.samba-clientgives you tools such assmbclientfor testing. -
Go to the Samba config folder and edit the files:
Terminal window cd /etc/sambavi /etc/samba/lmhostsvi /etc/samba/smb.confvi /etc/samba/smbuserssmb.confis the main config file. This is where you define your shares.lmhostsmaps NetBIOS names to IP addresses. Most setups do not need to change it.smbusersmaps 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 -
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
useraddcomes first.smbpasswd -aadds 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. -
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>. -
Check whether there are any errors in the config:
Terminal window smbparmCorrection (added later): the command is
testparm, notsmbparm:Terminal window testparmtestparmreadssmb.conf, reports any errors and prints the shares it found. -
Restart Samba:
Terminal window service samba restartCorrection (added later): on Red Hat-family systems the service is called
smb(withnmbfor NetBIOS name service), notsamba:Terminal window service smb restartOn Debian and Ubuntu the main service is
smbd.
How to check it worked
From the server itself, list the shares as your Samba user:
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_tlabel: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=sambasudo firewall-cmd --reload -
User cannot log in: check that you ran
smbpasswd -afor 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 smbsudo systemctl enable smb -
yumbecamednfon newer Red Hat-family systems (yumstill works as an alias). On Debian and Ubuntu install withsudo apt install samba. -
Security: share only the folders you need, use
valid usersto limit who can connect, and do not setchmod 777on shared folders. -
Documentation: https://www.samba.org/samba/docs/