I would like to set a password for setting up samba share directory using a shell script. I wrote the following script test.sh:


(echo "$pass"; echo "$pass") | smbpasswd -s -a $(whoami)

This prints the following:

When run by root:
    smbpasswd [options] [username]
    smbpasswd [options]

  -L                   local mode (must be first option)
  -h                   print this usage message
  -s                   use stdin for password prompt
  -c smb.conf file     Use the given path to the smb.conf file
  -D LEVEL             debug level
  -r MACHINE           remote machine
  -U USER              remote username
extra options when run by root or in local mode:
  -a                   add user
  -d                   disable user
  -e                   enable user
  -i                   interdomain trust account
  -m                   machine trust account
  -n                   set no password
  -W                   use stdin ldap admin password
  -w PASSWORD          ldap admin password
  -x                   delete user
  -R ORDER             name resolve order

As it points out, I was not running it as root, when I run it as root, i.e., sudo ./test.sh, it runs fine. But the catch is, it adds root instead of noobuser, which is my logged in user.

How can I add noobuser by doing something similar (I have a feeling I'm missing something here)?

  • why not replace $(whoami) by $(echo noobuser) ?
    – Archemar
    Jun 2, 2017 at 7:01
  • I would like to make it a generic script, which can be run in other systems as well. The username there could be anything. Jun 2, 2017 at 7:09

4 Answers 4





if [ -z "$SUDO_USER" ]; then
    echo "This script is only allowed to run from sudo";
    exit -1;

(echo "$pass"; echo "$pass") | smbpasswd -s -a "$SUDO_USER"


sudo ./test.sh
  • I like this answer, but would like to know if it possible without passing an argument. Jun 2, 2017 at 8:26
  • 1
    @skrowten_hermit: It depends. You can use logname or who am i as @Sagar stated, but it will work only if the user you want to be used for script has been the very first one to log in to shell within chain logins. I. e. you cannot login as root, then sudo to noobuser and then use this method.
    – DevilaN
    Jun 2, 2017 at 8:35
  • Okay. I was not aware of that. I tried replacing with both logname and who am i in @Sagar's answer. But, that didn't help as well. The problem here starts with running the script as sudo. Is there a way to cheat the system to believing it is still noobuser and not root while using sudo? Jun 2, 2017 at 8:45
  • Assuming that you will always run this from sudo, you can use $SUDO_USER enviroment variable I've made proper corrections to the answer.
    – DevilaN
    Jun 2, 2017 at 10:47
  • $SUDO_USER, $USER are all environment variables that can be changed right? I prefer not to use them. That's why I was using whoami. Jun 5, 2017 at 8:22

This is a fragment of how i automate smbpasswd -a via bash script, works like charm.



echo -e "$pass\n$pass" | smbpasswd -a -s $username

this one is extended, it inserts the current username for you. (without awk it's shorter & cleaner)


echo -e "$pass\n$pass" | smbpasswd -a -s $(id -un)


find more details below:


echo -e

The -e option is used to enable echo's interpretation of additional instances of the newline character as well as the interpretation of other special characters, such as a horizontal tab, which is represented by \t.

pipe symbol “|”

In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing.



credit goes to: LawrenceC, Wikipedia

smbpasswd -s

The -s option causes smbpasswd to be silent (i.e. not issue prompts) and to read its old and new passwords from standard input, rather than from /dev/tty (like the passwd(1) program does). This option is to aid people writing scripts to drive smbpasswd

id -u -n

id displays the system identifications of a specified user.

outputs: uid=0(root) gid=0(root) grupy=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

Display a name instead of a UID/GID

By default, id command displays number for the -G, -g and -u options. You can force id command to display the name of the UID or GID instead of the number for the -G, -g and -u options by passing the -n option as follows:

id -ng {UserNameHere}
id -nu {UserNameHere}
id -nG {UserNameHere}
id -nG vivek

read more about id: https://www.cyberciti.biz/faq/unix-linux-id-command-examples-usage-syntax/

Credit goes to: Brett (solution), Vivek Gite (deep infos)

a little hint in case of problems (Credit to: Paul): https://superuser.com/a/343163/1169381


if you dont want to pass argument to script then you may try this


echo $pass
echo $pass | smbpasswd -s -a $(who am i | awk '{print $1}')

and then invoke the script.

  • It still doesn't work for me - echo $pass | smbpasswd -s -a $(whoami | awk '{print $1}') or echo $pass | smbpasswd -s -a $(who am i | awk '{print $1}') Jun 2, 2017 at 8:40
  • what was the stdout after executing this ?
    – Sagar
    Jun 3, 2017 at 9:09
  • The same I had pasted in my question! Jun 5, 2017 at 6:55

I'm create file name smb.sh, content like this


mkdir /home/user_data/$1

#add owner
chown -R $1 /home/user_data/$1
#owner only 
chmod -R 700 /home/user_data/$1


#sleep 3
smbpasswd -a $user<<EOF

and you can send params to smb.sh like this

./smb.sh myusername  mypassword

like simple, you can use file smb.sh with PHP language, use PHP exec function

  • Well, I guess that it’s fairly obvious that this needs to be run as root, but you should say so anyway. Is a here document better than echo |? If so, how? … … … … … … … … … … … … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. Apr 5, 2022 at 7:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .