Move user accounts from one Linux to another

From Initq

Jump to: navigation, search

Many time i have come across a situation where i need to transfer accounts from one machine to another. You do not need to transfer system accounts since things may have changed in the new distro.

Contents

Directories Needed for the Move

Following files/dirs are required for traditional Linux user management:

  • /etc/passwd - various information about the users
  • /etc/shadow - encrypted passwords for the users
  • /etc/group - group to which users belong
  • /etc/gshadow - encrypted passwords for groups
  • /var/spool/mail - mail store directory for users
  • /home - all users home directories

Backup Files on the old Linux Box

We will create a tar ball on our old system with all the needed files.

Create directory
mkdir /root/move
Create a variable to define starting user/group id number

Users that are added to the Linux system always start with a UID and GID values specified by the distro or admin.
RHEL/CentOS/Fedora : default is 500 - 65534 (/etc/libuser.conf).
Debian/Ubuntu : default is 1000 - 29999 (/etc/adduser.conf).

export UGIDLIMIT=1000
Copy the /etc/passwd with awk

The -v flag will define a variable, -F defines a field separator.

awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=29999)' /etc/passwd > /root/move/passwd.mig
Copy /etc/group file with awk
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && (3!=29999)' /etc/group > /root/move/group.mig
Copy /etc/shadow file
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=29999) {print $1}' /etc/shadow | \
tee - | egrep -f - /etc/shadow > /root/move/shadow.mig
Copy /etc/gshadow
cp /etc/gshadow /root/move/gshadow.mig
Make backup of /home /var/spool/mail /var/spool/postfix and /var/spool/cron
tar -zcvpf /root/move/home.tar.gz /home
tar -zcvpf /root/move/mail.tar.gz /var/spool/mail
tar -zcvpf /root/move/cron.tar.gz /var/spool/cron
tar -zcvpf /root/move/postfix.tar.gz /var/spool/postfix

Download files

You may upload or download files using scp. We are pulling files to our new server so we run the folloing command on our new box.

scp -rv root@mysite.com:/root/move/* .

Restore Files on the new Linux Box

Personal tools