How can I pipe the new password to smbpasswd so I can automate my installation process.
7 Answers
Thanks to Mark I found the answer:
(echo newpassword; echo confirmNewPassword) | smbpasswd -s
BTW: (echo oldpasswd; echo newpasswd) | smbpasswd -s does not work.

- 88,102
- 65
- 184
- 229
-
5-1 This is exactly the answer of Mark, just changing variable names. – JorgeeFG Aug 26 '13 at 14:35
-
11+1 to offset JorgeeFG; I think that the new variable names contain a distinction, if you read closely. Mark's answer implies that smbpasswd is expecting an old password and a new one, but this answer implies that smbpasswd is expecting a password and a password confirmation. The second interpretation is the correct one, and so I feel Mark's answer is less helpful than this one. – Matt Aug 12 '15 at 00:09
I use the following in one of my scripts:
echo -ne "$PASS\n$PASS\n" | smbpasswd -a -s $LOGIN
With echo:
-e : escape sequences, like \n
-n : don't add implicit newline at end
With smbpasswd:
-a : add new user
-s : silent

- 45,466
- 8
- 54
- 65
-
1
-
smbpasswd’s -s stands for “use stdin for password prompt”, not “silent” – Michael Aug 25 '16 at 06:58
-
@Michael according to https://www.samba.org/samba/docs/man/manpages/smbpasswd.8.html it means both "silent" (no prompts) and "read from stdin" (instead of /dev/ptty) – Bruno De Fraine Aug 25 '16 at 08:29
Try something like this:
(echo oldpasswd; echo newpasswd) | smbpasswd -s

- 297,451
- 125
- 333
- 465
Use this
echo 'somepassword' | tee - | smbpasswd -s

- 708
- 9
- 20
-
3thanks, fwiw that's the only syntax I could get working in a Dockerfile: `RUN PASS=myrootpassword ; echo ${PASS} | tee - | smbpasswd -a -s` – jamshid Aug 01 '14 at 18:43
-
I also found this answer useful as the only syntax I could get working in a Puppet exec resource; the approach beginning with a paren failed because Puppet couldn't find the command "(echo". Thank you! – dbrewer Jul 19 '16 at 21:48
I had to create a new Samba user in a Puppet 5.x Exec resource and for various reasons none of the above worked. Fortunately this rather silly-looking command worked:
yes vagrant|head -n 2|smbpasswd -a -s vagrant
Password here is of course "vagrant".

- 21
- 1
This unfortunately is not desirable for two reasons: 1) if the user uses a combination of '\n' in the password there will be a mismatch in the input 2) if there are unix users on the system, then a user using the utility ps may see the password
A better way would be to put the names in a file and read from the file and use python pexpect to read them, not like below, but the simple script is enough to see how to use pexpect
#!/usr/bin/python
#converted from: http://pexpect.sourceforge.net/pexpect.html
#child = pexpect.spawn('scp foo myname@host.example.com:.')
#child.expect ('Password:')
#child.sendline (mypassword)
import pexpect
import sys
user=sys.argv[1]
passwd=sys.argv[2]
child = pexpect.spawn('/usr/bin/smbpasswd -a '+str(user))
child.expect('New SMB password:')
child.sendline (passwd)
child.expect ('Retype new SMB password:')
child.sendline (passwd)
then try: ./smbpasswd.py userName1 'f#@(&*(_\n895'

- 11
- 3