Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help with Samba configuration.

What I want to achieve is configuration, where windows user on share see only his files. This is simply achived With configuration like this:

Users are authorized by Windows AD Server

  path = /storage/BACKUP
  read only = no
  browseable = yes
  writable = yes
  create mode = 0600
  directory mode = 0700
  force directory mode = 0700
  force create mode = 0600
  access based share enum = yes
  hide unreadable = yes

It works ok but... On the server side, everything in directory /storage/BACKUP keeps files of every user.

So I would like to create directory here for every user (I Can't use [home], because it's already used)

So i Would like to keep it like this:


So when i change path, and add %U at end, everything is almost ok.

The problem is that i need to manually create directory /storage/BACKUP/username.

So what i need is somehow force Samba to create this directory before user to access this share.

I've tried adding add user script = /path/to/mkdir /storage/BACKUP/%U But this is not working because:

  1. I don't know why ;)
  2. I've already have users logged in before
  3. It should start for new created users, this will be ok for me, but it not works.

I'm not creating users in linux, after they login, so i'm not using add user/machine script anywhere else.

So i want to force Samba to create directory for user, when this user tries to connect. I'm searching google from couple of hours, and didn't find a way to do it that will work for me.

I need to keep /server/BACKUP location for everyone, but on the server side, Need to keep files in separated directories per user, so creating a new share is also not a solution.

share|improve this question
If users are authorized via AD, the add user script directive will never trigger because Samba isn't notified when a new user is created in AD, so this can't work. – Sven Feb 17 '14 at 11:54
up vote 3 down vote accepted

You can use the preexec or root preexec options for this. They specify a script that is run upon connection to a share. In case of preexec the share is run as the connecting user, and as root with root preexec.

In your share:

root preexec = /etc/samba/ %u

where /etc/samba/ looks somewhat like this:

if [ ! -d "$DIRECTORY" ]; then
   mkdir $DIRECTORY

Depending on your requirements, add chown and/or chmod statements to the script.

share|improve this answer
This is what i needed. Works like charm. Thank You – Lisek Feb 17 '14 at 12:25
You can avoid the need for a separate script. I use root preexec = bash -c '[[ -d /home/%U ]] || mkdir -m 0700 /home/%U && chown %U:"Domain Users" /home/%U' – starfry Feb 24 '15 at 12:21
You can use -p switch on mkdir and omit the prepending test for existance. – OldPostDude Apr 5 at 13:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.