I want to use kpasswd to change my password. Since I have to do it for multiple realms, I'd like to automate it passing on the password. I want to set a variable in bash and pass that on to kpasswd, somehow. :) Variable setting is no problem, but passing it on to kpasswd, that is an issue.

How would I do that?

From man kpasswd it seems that there isn't some flag which I can pass along, so I was thinking using printf or some other solution. Unfortunately this doesn't work:

printf "$PWD\n$PWD\n" | kpasswd $KERBEROS_USERNAME@DOMAIN

Some other details:

$ bash --version

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

$ kpasswd --version

kpasswd (Heimdal 1.5.1apple1)

1 Answer 1


This is traditionally done with an expect script. Eg

    #!/usr/bin/expect --
    set user [lindex $argv 0]
    set oldpassword [lindex $argv 1]
    set password [lindex $argv 2]
    spawn kpasswd $user
    expect "password"
    send "$oldpassword\r"
    expect "password"
    send "$password\r"
    expect "password"
    send "$password\r"
    expect eof

Put this script in a file, say mysetpw, do chmod +x mysetpw, then run it with ./mysetpw principalname oldpassword newpassword. I don't have kpasswd, but the examples on the web seem to suggest it needs the old password too, as in this scipt. You may need to change the prompts (eg "password") that the script looks for to know when it should send the next line.

  • Thanks a lot meuh! Will give it a try and post the results asap.
    – Sander
    Apr 12, 2016 at 10:42
  • Works! Learned something today, about 'expect'. Didn't know that it existed, but this can be really useful!
    – Sander
    Apr 12, 2016 at 10:52
  • notice that for Mac, you need to change the first expect to be Password (capital P) because of the different implementation. The rest works! thanks a lot! May 13, 2019 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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