How to Restrict Folder Passwords in Samba Shares

Abstract: Restrict access to Samba shares by setting guest ok no and defining valid users in smb.conf.

Created: 2026-07-31 by
To restrict folder passwords in Samba shares, you must configure the [/etc/samba/smb.conf] file to enforce authentication at the share level. By setting guest ok = no and defining valid users, you ensure that only authenticated individuals can access specific directories. This method leverages Samba’s built-in user management to provide granular control over public and restricted resources on your network.

Understanding Samba Authentication Models

Samba operates primarily with two authentication models: guest access and user-based authentication. In a guest access model, specified shares allow anonymous connections without requiring credentials. This is suitable for public directories where data integrity is less critical. However, for sensitive information, you need to switch to user-based authentication. This model requires every connecting client to provide a valid username and password that exists within the Samba database. The core configuration directive that controls this behavior is guest ok. When set to yes, the server allows connections from any user, mapping them to the guest account or the system's nobody user. To restrict access, you must explicitly set this to no. Additionally, you must define which users are permitted to connect using the valid users directive. Without these settings, Samba defaults to allowing broader access, which poses a significant security risk in enterprise environments. For more details on securing your shared drives, see our guide on Troubleshooting Network-Shared Hard Drive Connection Issues with Samba. Understanding connection errors often helps diagnose why authentication might be failing unexpectedly.

Configuring the Samba Configuration File

The primary configuration file for Samba is located at /etc/samba/smb.conf. You should edit this file using a text editor with root privileges. The structure of the file consists of global settings followed by individual share definitions. Each share is enclosed in square brackets, such as [public] or [restricted]. To create a restricted folder, add a new section to the file. Inside this section, specify the path to the directory on your local filesystem. Set read only to no if you want authorized users to write files, or yes for read-only access. Crucially, set guest ok = no. This prevents anonymous users from entering the share. Finally, use the valid users directive to list the usernames allowed to access this folder. You can separate multiple users with commas. Example configuration for a restricted share:
[restricted_data]
   path = /srv/samba/restricted
   browseable = yes
   read only = no
   guest ok = no
   valid users = admin, developer
   create mask = 0660
   directory mask = 0750
In this example, only admin and developer can access the restricted_data share. Other users attempting to connect will receive an "Access Denied" error. The create mask and directory mask ensure that newly created files inherit appropriate permissions, enhancing security further. If you encounter issues accessing shares after configuration changes, check your firewall settings and SELinux policies. For general troubleshooting of shared drive connectivity, refer to How to List Available SMB Shares in Nautilus/Gnome. Verifying visibility in the client OS helps confirm that the server is broadcasting the share correctly.

Managing Samba Users

Defining valid users in smb.conf is not enough; those users must exist in the Samba password database. Linux system users do not automatically become Samba users. You must create a Samba password for each user who needs access. Use the smbpasswd command or the newer pdbedit utility to manage these accounts. To add a new Samba user, run the following command in the terminal:
sudo smbpasswd -a username
You will be prompted to enter and confirm a password. This password is stored separately from the Linux system password unless you have integrated LDAP or other backend services. It is important to note that the Samba user does not necessarily need a corresponding login shell on the Linux machine. They just need an entry in the Samba database. If you are managing multiple users, consider using scripts to automate the creation process. Always test the credentials after adding them. Attempting to connect to the share from another machine using the new credentials verifies that the setup is correct. Remember that changing the Linux user password does not update the Samba password. You must update both independently if they are not synchronized via a central identity provider.

Security Best Practices

Restricting folder passwords is only the first step in securing your Samba environment. Ensure that the underlying directory permissions on the Linux filesystem align with your Samba configuration. If the Samba config allows a user to write, but the OS file permissions deny it, the operation will fail. Use chmod and chown to set the correct ownership and permissions on the shared directories. Additionally, enable logging to monitor access attempts. Set log level in the global section of smb.conf to capture detailed information about authentication failures. This helps identify brute-force attacks or misconfigured clients. Regularly audit your valid users lists to remove access for former employees or unused accounts. For users experiencing password prompts despite having SSH keys configured, understand that Samba uses its own authentication mechanism. See Sudo asks for password, even the ssh key method is setup for newly created user for insights into credential handling differences between protocols. While unrelated directly, understanding credential separation helps avoid confusion between SSH and SMB auth. Finally, consider implementing network-level restrictions. Use hosts allow and hosts deny directives to limit which IP addresses can connect to your Samba server. This adds a layer of defense in depth, ensuring that even if credentials are compromised, the attacker cannot reach the service from unauthorized networks.

Frequently Asked Questions

Why can't I access the share after setting valid users?

The most common reason is that the user lacks a Samba password. You must run smbpasswd -a username to create the account in the Samba database. Simply creating a Linux user is insufficient for Samba authentication.

Does changing the Linux password update the Samba password?

No, Samba maintains a separate password database by default. Changing your Linux password with passwd does not affect your Samba credentials. You must update the Samba password separately using smbpasswd unless you are using a centralized authentication service like LDAP.

Can I allow different users to have different permission levels?

Yes, you can use the write list directive to grant write access to specific users while others remain read-only. Alternatively, you can create multiple shares pointing to the same directory with different valid users and read only settings to achieve granular control.

What happens if I forget the Samba administrator password?

If you lose the password for the Samba admin user, you can reset it using smbpasswd -a admin if the user exists, or smbpasswd -r admin to change an existing password. You need root access to the server to perform these operations.

Is it safe to use guest access for all folders?

Guest access is convenient but insecure for sensitive data. It allows anyone on the network to read or write to the share without identification. For production environments, always enforce user authentication to maintain accountability and protect confidential information.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More