Operating System - Linux
1829118 Members
1888 Online
109986 Solutions
New Discussion

add existing passwords to /etc/shadow

 
joseph wholey
Regular Advisor

add existing passwords to /etc/shadow

I'm looking for a ksh script that will insert an encrpyted password into the /etc/shadow on a new server.
I can extract the encrypted password from the /etc/shadow on another server, but I'm having problems inserting it between the first and second colon ":".
But even before I do that, I need to remove what is already beween the first and second colon (:).
(colon).
I can't be the first person to do this. thx.
5 REPLIES 5
Ivan Ferreira
Honored Contributor

Re: add existing passwords to /etc/shadow

You can use adduser -p or usermod -p to insert the encrypted password.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
joseph wholey
Regular Advisor

Re: add existing passwords to /etc/shadow

Ivan... maybe you didn't understand, or maybe I don't understand your response... these users have an existing encrypted password on all of our other servers throughout the enterprise. I want to insert the existing encrypted password into the /etc/shadow on the new server. So when the user logs in, he need only enter the password that he enters everywhere else. It will be business as usual for the user. I'm just looking for the ksh/awk/sed syntax to remove what comes after the second colon in the /etc/shadow, and insert the encrypted password that I've extracted from an existing server.
Jared Middleton
Frequent Advisor

Re: add existing passwords to /etc/shadow

Joseph,

Instead of trying to edit the /etc/passwd or /etc/shadow files on the target system directly (sed, awk, etc.), use the native commands that are provided to do it.

To add a new user w/encrypted password:
/usr/sbin/useradd -p '$1$cNAj3C63$eCYknWT349xrwT7/Fc936/' user1

To change password on existing user:
/usr/sbin/usermod -p '$1$cNAj3C63$eCYknWT349xrwT7/Fc936/' user1

You can use your awk/sed skills to parse/capture the encrypted passwords from /etc/shadow on your source system into a temp file, then build a little shell script to loop through each temp record executing either "useradd" or "usermod" (depending on whether your target users already exist or not).

Regards,
Jared
Justin_99
Valued Contributor

Re: add existing passwords to /etc/shadow

Quickly thrown together and does not incoporate any remote connection, so you may be able to weave it into your already existing extraction script. Have not tested this just thrown together. :)

#!/usr/bin/ksh

#Usage: sync.sh
SHADOW=/etc/shadow #a copy from old server

FILE=$1

for i in `cat $FILE`
do
SHADOWENTRY=`grep "${i}" ${SHADOW}`
ENCRYPT=`echo ${SHADOWENTRY} | awk -F: '{print $2}'`

/usr/sbin/useradd -p ${ENCRYPT} ${i}

#You can also try using chpasswd -e below just uncomment
#echo "${i}:${ENCRYPT}" | chpasswd -e && chage -m 7 -M 90 -W 14 ${i} > /dev/null

done
joseph wholey
Regular Advisor

Re: add existing passwords to /etc/shadow

Perfect... thanks... Looks like I didn't understand.