In this article, we will discuss how to create a Samba share with custom access control for a directory and local users. Samba is an open-source software suite that enables interoperability between Linux and Windows systems, allowing file and print services to be shared between machines on a network.
Prerequisites
Before proceeding, ensure that you have installed Samba on your Linux system. You can install it using the package manager of your distribution. For example, on Ubuntu, you can use the following command:
sudo apt-get install samba
Creating a Directory for the Samba Share
First, create a directory for the Samba share. For example, you can use the following command to create a directory called "share" in the root directory:
sudo mkdir /share
Setting Up Access Control for the Directory
Next, we will set up access control for the directory using Access Control Lists (ACLs). We will use the command line tool "setfacl" to set up the ACLs. The following command sets the owner and group of the directory to the current user:
sudo setfacl -R -m u:$(whoami):rwx /share
sudo setfacl -R -m g:$(whoami):rwx /share
The following command sets the permissions for other users:
sudo setfacl -R -m o::rx /share
Configuring Samba for the Share
Next, we will configure Samba for the share. Open the Samba configuration file using a text editor such as nano:
sudo nano /etc/samba/smb.conf
Add the following lines to the end of the file:
[share]
path = /share
writeable = yes
create mask = 0775
directory mask = 0775
force user = $(whoami)
force group = $(whoami)
The "writeable" option sets the share to be writable. The "create mask" and "directory mask" options set the permissions for newly created files and directories. The "force user" and "force group" options set the default user and group for the share. Save and close the file.
Enabling Samba
Next, enable Samba by restarting the Samba service:
sudo service smbd restart
Adding Local Users
Next, we will add local users who will have access to the Samba share. Use the following command to add a user:
sudo useradd -m -s /bin/bash username
Replace "username" with the desired username. The "-m" option creates a home directory for the user, and the "-s" option sets the default shell for the user.
Setting Up Access Control for Local Users
Next, we will set up access control for the local users. Use the following command to set the permissions for a user:
sudo setfacl -m u:username:rwx /share
Replace "username" with the desired username. This command sets the permissions for the user to read, write, and execute for the directory.
Testing the Samba Share
Finally, test the Samba share by connecting to it from a Windows machine or another Linux machine. The Samba share should be visible in the file explorer, and you should be able to access it using the credentials of the local user you created.
- Installed Samba and created a directory for the Samba share.
- Set up access control for the directory using ACLs.
- Configured Samba for the share by adding a section to the Samba configuration file.
- Enabled Samba by restarting the Samba service.
- Added local users and set up access control for the local users.
- Tested the Samba share by connecting to it from a Windows machine or another Linux machine.