Contact

Sometimes it is necessary to make a user's password identical on several hosts in an environment where it doesn't matter if this is discovered, and the same encrypted password field can be used on each of the hosts.

The new encrypted password can be generated by 'makekey' (if it exists - usually in somewhere like /usr/lbin), or extracted from a passwd file that has had the new password applied.

# where xxxxxxxxxxxxx is the new 13 char encrypted password.
PW="xxxxxxxxxxxxx"
# and
username is the name of the user whose password is to be changed
USER="username"

awk -v user="$USER" -v pw="$PW" -F: '
        $1 == user {
                printf("%ds:%s:%s:\nw\nq\n", NR, $2, pw)
        }' /etc/passwd | ed /etc/passwd

Note the use of ':' as the delimiter for the 'ed' 'substitute' command. It will not get confused with the ':'s in the line, and is guaranteed not to exist in either the old or new password strings.

This can even be done across hosts provided root has the permission to do so, and can ssh (or remsh) to the other host without entering a password.

ssh otherhost cat /etc/passwd | awk -F: '
        $1 == user {
                printf("%ds:%s:%s:\nw\nq\n", NR, $2, pw)
        }' | ssh otherhost ed /etc/passwd

This can be built into a loop to perform the change on all the required hosts.