Monday, 18 May 2009

Reconstructing user directories from a passwd file

Sometimes I'm asked to set up a box with the same users as another box. The easiest way for me to do this is to copy the /etc/passwd, /etc/group and /etc/shadow files over, and then run the following shell fragment:

for LINE in `cat /etc/passwd|sed s/' '/'SPACE'/g`;do
DIR=`echo $LINE|awk -F: '{print $6}'|sed s/'SPACE'/' '/g`
USER=`echo $LINE|awk -F: '{print $1}'|sed s/'SPACE'/' '/g`
GROUP_NUM=`echo $LINE|awk -F: '{print $4}'`
GROUP=`grep ":${GROUP_NUM}:" /etc/group|awk -F: '{print $1}'`
if [[ ! -d ${DIR} ]];then
echo $DIR doesn\'t exist
echo mkdir ${DIR}
mkdir ${DIR}
echo cp -r /export/home/appman/* ${DIR}
cp -r /export/home/appman/* ${DIR}
echo cp -r /export/home/appman/.profile ${DIR}
cp -r /export/home/appman/.profile ${DIR}
echo chown -R ${USER}:${GROUP} ${DIR}
chown -R ${USER}:${GROUP} ${DIR}

No comments: