Sub topic 4
Creating Samba users
This chapter assumes you can create Linux users and groups, read ls -l permissions, and restart services with systemctl. It also assumes Samba is installed and has a minimal smb.conf, as in the previous chapter. Here you will create the accounts that clients use to authenticate. The next chapter attaches them to shares.
Why Samba has its own passwords
SMB clients prove their identity with a challenge-response exchange based on the NT hash of the password. Linux stores passwords in /etc/shadow as salted hashes (SHA-512 or yescrypt, depending on the distribution). The NT hash cannot be computed from those hashes, and the server never sees the plain-text password during the exchange. Samba therefore keeps a second password store.
With the default passdb backend = tdbsam, that store is a small database, typically /var/lib/samba/private/passdb.tdb. It holds an NT hash and account flags for each Samba user.
Each Samba user must also exist as a Unix account on a standalone server. Samba uses the Unix account for the numeric UID and the group memberships. When a client writes a file, the kernel sees that UID.
| Item | Where it lives | Used for |
|---|---|---|
| Unix account (UID, groups) | `/etc/passwd`, `/etc/group` | File ownership and permission checks |
| Samba password and flags | Samba passdb (`passdb.tdb`) | SMB authentication |
| Login password | `/etc/shadow` | Local and SSH logins. Not used by SMB. |
Creating a user step by step
First create the Unix side. A file-sharing user usually needs no shell and no home directory, so the account cannot be used to log in over SSH.
sudo groupadd staff
sudo useradd -M -s /usr/sbin/nologin -G staff aliceThe options do the following:
-Mskips the home directory.-s /usr/sbin/nologinblocks interactive logins. On some systems the path is/sbin/nologin.-G staffaddsaliceto the supplementary groupstaff.
Then add the Samba password:
sudo smbpasswd -a alicesmbpasswd prompts twice for the password. The -a option adds the user to the passdb and enables the account. This password can differ from any Unix password. Because alice has nologin and no Unix password set, the Samba password is the only credential she has.
If you run smbpasswd -a for a name with no Unix account, it fails:
Failed to add entry for user bob.Newer versions may print Unable to find user bob. Either way, create the Unix account first.
Managing accounts
smbpasswd handles the common tasks. pdbedit is the more complete tool and works on any passdb backend.
| Task | Command |
|---|---|
| Change a password (as admin) | `sudo smbpasswd alice` |
| Change your own password | `smbpasswd` |
| Disable an account | `sudo smbpasswd -d alice` |
| Re-enable an account | `sudo smbpasswd -e alice` |
| Delete from the passdb | `sudo smbpasswd -x alice` |
| List Samba users | `sudo pdbedit -L` |
| Show details and flags | `sudo pdbedit -Lv alice` |
Deleting a user with smbpasswd -x removes only the Samba entry. The Unix account and the files it owns stay. If you remove the Unix account with userdel, the passdb entry stays behind until you delete it. Do both when you retire a user.
In the pdbedit -Lv output, look at the Account Flags line. [U ] means a normal, enabled user. [DU ] means the account is disabled.
Scripting user creation
For several users, pass the password on standard input. The -s option makes smbpasswd read the new password twice from stdin instead of prompting.
#!/bin/bash
# create-smb-user.sh usage: sudo ./create-smb-user.sh alice staff
set -euo pipefail
user="$1"
group="$2"
getent group "$group" >/dev/null || groupadd "$group"
id "$user" >/dev/null 2>&1 || useradd -M -s /usr/sbin/nologin -G "$group" "$user"
read -r -s -p "Samba password for $user: " pw
echo
printf '%s\n%s\n' "$pw" "$pw" | smbpasswd -a -s "$user"
unset pw
pdbedit -L | grep "^$user:"The script reads the password with read -s so it never appears on screen or in shell history. Do not put it on the command line as an argument, because other users can see arguments in ps output.
Group changes and open connections
Samba reads a user's group list when the connection is established. If you add alice to a new group with usermod -aG, her existing SMB sessions do not see it. She must disconnect and reconnect. You can list current sessions with sudo smbstatus and, if needed, restart smbd to drop them.
A related trap is Windows clients. Windows keeps one set of credentials per server name. After you change group membership or a password, a client may still reuse the old session until the user logs off or runs net use * /delete.
Testing the account
Test from the server itself before involving a remote client:
sudo pdbedit -L
smbclient -L //localhost -U aliceA correct password lists the shares. Common failures are:
NT_STATUS_LOGON_FAILURE: wrong password, or the user is missing from the passdb. The error is the same for both on purpose, so checkpdbedit -Lto tell them apart.NT_STATUS_ACCOUNT_DISABLED: the account has theDflag. Runsmbpasswd -e.NT_STATUS_BAD_NETWORK_NAME: the login worked, but the share name is wrong. This is not an account problem.- Login works, but writes fail: the problem is share or filesystem permissions, covered in the next chapter.
Also confirm that [global] contains map to guest = never. Without it, a mistyped username may fall back to guest access on some configurations, which hides account problems.
Other backends and name mapping
tdbsam is the right choice for a standalone server with up to a few hundred users. For more users, ldapsam stores Samba attributes in an LDAP directory. If the server joins an Active Directory domain, accounts and passwords come from the domain through winbind and you do not use smbpasswd at all. That case is covered in the last chapter.
The username map option in [global] lets a client name such as [email protected] resolve to the Unix user alice. You rarely need it. Prefer matching names on both sides.
Exercise
You run sudo smbpasswd -a carol and it fails because the user does not exist. After creating the account with useradd -M -s /usr/sbin/nologin carol, the command succeeds. Carol then connects and can see the shares, but a week later you add her to the group staff and she still cannot write to the staff share. Name the likely cause and the fix.
Show answer
Samba reads group membership when the SMB session starts, so Carol's existing session still has the old group list. She must disconnect and reconnect (or the admin can restart smbd or end the session shown in smbstatus). If it still fails after reconnecting, check that the group has write permission on the directory, which is a share permission issue.