Piping a password to passwd

Jeremy Huntwork jhuntwork at linuxfromscratch.org
Tue Jan 25 04:50:40 PST 2005

Michael Kipper wrote:

> Hi,
> I'm not sure if this is a -dev question, but here goes:
> I build my LFS by scripts, and am having trouble automating the password
> portion. I think the following should work:
> yes "myr00tpassword" | passwd root
> But it hangs at the console, waiting for input:
> root at localhost:/# yes "myr00tpasswd" | passwd root
> Changing password for root
> Enter the new password (minimum of 5, maximum of 127 characters)
> Please use a combination of upper and lower case letters and numbers.
> New password:
> Why is this? Is there a 'proper' way to do this?

Use expect. Of course, that's assuming you've got expect built from Chapter
5 and that you've got devpts mounted.

cat > setpasswd << "EOF"
spawn passwd root

# Wait for the string "password:" and send a password
# I just use a blank password so I just do 'send "\n"'
expect "password:"
send "password\n"

# Send it again for the verify password part
expect "password:"
send "password\n"

# If you use a weak password, like I do, repeat the above lines
# once more.
expect "password:"
send "password\n"
expect eof

In my scripts, I actually echo each line into the new file and then run the
file, you may need to do the same, or just create that script manually
first and have it available for your build script.


Jeremy Huntwork

More information about the lfs-dev mailing list