quite often I have to set the same password on new servers as it is on the old one, I can do it manually simple paste hash from the shadow, but I'd like to learn to do it automatically.

old machine A
new machine B

on new machine B exists users from A but also additional users which do not exist on A ( so shadow is not sorted, we can skip them of course )

is there a way via sed/awk to:

take hash from machine A , and replace exists users on new machine B where pwd is not set. (xxx:!:xxxx)

1 Answer 1

awk 'BEGIN{ OFS=FS=":" }
  if (NR==FNR) {
  } else {
    if ($2 ~ /^!/ && $1 in pwd) $2=pwd[$1]
}' shadowA shadowB

When the first file shadowA is processed (NR==FNR), save the password hash in array pwd with the username as index.

When the second file shadowB is processed (else) and the second field starts with a ! (could be ! or !!) and the username is present in the password array ($1 in pwd), then update the value of the second field. After that print the current line of shadowB.

You must log in to answer this question.

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