How to create a Samba share
Samba allows for Windows and other clients to connect to file share directories on Linux hosts. It implements the server message block (SMB) protocol. This guide covers creating a shared file location on a Fedora machine that can be accessed by other computers on the local network.
Install and enable Samba
The following commands install Samba and set it to run via systemctl. This also sets the firewall to allow access to Samba from other computers.
$ sudo dnf install samba
$ sudo systemctl enable smb --now
$ firewall-cmd --get-active-zones
$ sudo firewall-cmd --permanent --zone=FedoraWorkstation --add-service=samba
$ sudo firewall-cmd --reload
Sharing a directory inside /home
In this example you will share a directory inside your home directory, accessible only by your user.
Samba does not use the operating system users for authentication, so your user account must be duplicated in Samba. So if your account is jane on the host, the user jane must also be added to Samba. While the usernames must match, the passwords can be different.
Create a user called jane in Samba:
$ sudo smbpasswd -a jane
Create a directory to be the share for jane, and set the correct SELinux context:
$ mkdir /home/jane/share
$ sudo semanage fcontext --add --type "samba_share_t" "/home/jane/share(/.*)?"
$ sudo restorecon -R ~/share
Samba configuration lives in the /etc/samba/smb.conf file. Adding the following section at the end of the file will instruct Samba to set up a share for jane called "share" at the /home/jane/share directory just created.
[share]
comment = My Share
path = /home/jane/share
writeable = yes
browseable = yes
public = yes
create mask = 0644
directory mask = 0755
write list = user
Restart Samba for the changes to take effect:
$ sudo systemctl restart smb
Sharing a directory for many users
In this example, you will share a directory (outside your home directory) and create a group of users with the ability to read and write to the share.
Remember that a Samba user must also be a system user, in order to respect filesystem permissions. This example creates a system group myfamily for two new users jack and maria.
$ sudo groupadd myfamily
$ sudo useradd -G myfamily jack
$ sudo useradd -G myfamily maria
|
You could create these users without a system password. This would prevent access to the system via SSH or local login. |
Add jack and maria to Samba and create their passwords:
$ sudo smbpasswd -a jack
$ sudo smbpasswd -a maria
Setting up the shared folder:
$ sudo mkdir /home/share
$ sudo chgrp myfamily /home/share
$ sudo chmod 770 /home/share
$ sudo semanage fcontext --add --type "samba_share_t" "/home/share(/.*)?"
$ sudo restorecon -R /home/share
Each share is described by its own section in the /etc/samba/smb.conf file. Add this section to the bottom of the file:
[family]
comment = Family Share
path = /home/share
writeable = yes
browseable = yes
public = yes
valid users = @myfamily
create mask = 0660
directory mask = 0770
force group = +myfamily
Explanation of the above:
-
valid users: only users of the groupfamilyhave access rights. The @ denotes a group name. -
force group = +myfamily: files and directories are created with this group, instead of the user group. -
create mask = 0660: files in the share are created with permissions to allow all group users to read and write files created by other users. -
directory mask = 0770: as before, but for directories.
Restart Samba for the changes to take effect:
$ sudo systemctl restart smb
Managing Samba Users
Change a samba user password
|
Remember: the system user and Samba user passwords can be different. The system user is needed in order to handle filesystem permissions.
=== Remove a samba user
If you don’t need the system user, remove it as well:
== Troubleshooting and logs Samba log files are located in
You can increase the verbosity by adding this to the
To validate the syntax of the configuration file
To display current samba connections, use the
Some things to check if you cannot access the share.
$ sudo pdbedit -L | grep maria maria:1002: +
Confirm that +
+ . Check if the shared directory and sub-directories have the correct SELinux context. +
+ . Check if the system user has access permission to the shared directory. +
+
In this case, the user should be in the
[family]
comment = Family Share
path = /home/share
writeable = yes
browseable = yes
public = yes
valid users = @myfamily
create mask = 0660
directory mask = 0770
force group = +myfamily
+
In this example, the user should be in the
|
Want to help? Learn how to contribute to Fedora Docs ›