How to share a DIR with samba share in linux
To share a directory in Linux using Samba, you need to install the Samba server package, create a dedicated directory for sharing, configure the smb.conf file, and set up a Samba user with a password. This process makes the directory accessible to other computers on the network, including Windows, macOS, and other Linux machines.1
Step-by-Step Guide
1. Install Samba2
First, you need to install the Samba package.3 The command varies depending on your Linux distribution.
- For Debian/Ubuntu:
sudo apt update
sudo apt install samba - For Fedora/RHEL/CentOS:
sudo dnf install samba samba-common
2. Configure a Shared Directory4
You’ll need to create a directory that you want to share and set the appropriate permissions.5 While you can share an existing directory, it’s often best practice to create a new one.
- Create the directory:
sudo mkdir /srv/samba/shared_folder
Using /srv/samba/ is a common practice for Samba shares, but you can choose any location. - Set permissions and ownership:
sudo chown nobody:nogroup /srv/samba/shared_folder
sudo chmod 0777 /srv/samba/shared_folder
This command makes the folder accessible to all users. Adjust these permissions for more granular control if needed.
3. Edit the Samba Configuration File6
The main configuration file for Samba is smb.conf, located at /etc/samba/. 7Before editing, it’s a good idea to back up the original file.
- Backup the original file:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak - Open the configuration file for editing:
sudo nano /etc/samba/smb.conf - Add a new share section:
Scroll to the end of the file and add a new section for your shared directory.8 A typical configuration looks like this:
Ini, TOML[shared_folder] comment = My Samba Share path = /srv/samba/shared_folder read only = no browsable = yes guest ok = yes
- [shared_folder] is the name that will appear on the network.
- path is the absolute path to the directory you want to share.9
- read only = no allows users to write to the share.10 Change to yes for read-only access.11
- browsable = yes makes the share visible in network browsers.
- guest ok = yes allows access without a password.12 For a more secure setup, you would set this to no and configure valid users.13
4. Create a Samba User
If you don’t want to allow guest access, you’ll need to create a Samba user and a password for them.14 The user must be a system user on your Linux machine first.
- Create a system user (if they don’t exist):
sudo adduser username - Create a Samba password for the user:
sudo smbpasswd -a username
You will be prompted to enter and confirm a new password. This password is separate from their Linux system password. - Configure the share for a specific user:
Change the share section in smb.conf to remove guest access and specify a valid user.
Ini, TOML[shared_folder] comment = My Secure Samba Share path = /srv/samba/shared_folder read only = no browsable = yes guest ok = no valid users = username
5. Restart Samba Service and Update Firewall15
After making changes to the configuration file, you must restart the Samba service for the changes to take effect. You may also need to open the necessary ports in your firewall.16
- Restart the service:
sudo systemctl restart smbd - Open firewall ports (if using firewalld):
sudo firewall-cmd –add-service=samba –permanent
sudo firewall-cmd –reload - Test your configuration:
testparm
This command checks your smb.conf file for any syntax errors.