Migrate User Accounts From One Linux Server To Another

Migrate User Accounts From One Linux Server To Another

Sometimes it can be very irritating when your awesome setup of server runs out of space or lacks ram or low processor and makes the server crash several times and slows the server. The solution to this problem is very simple, just migrate the Linux server to other new server following the given steps.

You can migrate users from old Linux server to new Linux sever with standard commands such as tar, awk, scp and others. This is also useful if you are using old Linux distribution such as Redhat 9 or Debian 2.x.

In order for the migration to take place successfully all the below given files and directories should be copied to the new Linux server. The most important files required for Linux User Management:

  • /home: All data of a user is stored in /home directory.
  • /var/spool/mail: Emails are stored in /var/spool/mail directory.
  • /etc/passwd: Holds information about all user accounts.
  • /etc/shadow: Holds the encrypted password information for all user’s accounts.
  • /etc/gshadow: group shadow file (contains the encrypted password for group).
  • /etc/group: defines the groups to which users belong.

To migrate the files and directories from Old Linux Server, type the following command:

First make a directory named “old” by typing:

Setup UID filter limit:

Now copy /etc/passwd accounts to /root/old/passwd.mov using awk to filter out system account (i.e. only copy user accounts):

Copy /etc/group file:

Copy /etc/shadow file:

Copy /etc/gshadow:

Make a backup of /home and /var/spool/mail directories:

  • Users that are added to the Linux system always start with UID and GID values of as specified by Linux distribution or set by admin. Limits according to different Linux distro:
  • RHEL/CentOS/Fedora Core : Default is 500 and upper limit is 65534 (/etc/libuser.conf).
  • Debian and Ubuntu Linux : Default is 1000 and upper limit is 29999 (/etc/adduser.conf).
  • You should never ever create any new system user accounts on the newly installed Cent OS Linux. So above awk command filter out UID according to Linux distro.
  • export UGIDLIMIT=500 – setup UID start limit for normal user account. Set this value as per your Linux distro.
  • awk -v LIMIT=$UGIDLIMIT -F: ‘($3>=LIMIT) && ($3!=65534)’ /etc/passwd > /root/old/passwd.mov – You need to pass UGIDLIMIT variable to awk using -v option (it assigns value of shell variable UGIDLIMIT to awk program variable LIMIT). Option -F: sets the field separator to : . Finally awk read each line from /etc/passwd, filter out system accounts and generates new file /root/old/passwd.mov. Same logic is applies to rest of awk command.
  • tar -zcvpf /root/old/home.tar.gz /home – Make a backup of users /home directory.
  • tar -zcvpf /root/old/mail.tar.gz /var/spool/mail – Make a backup of users mail directory.

Now use scp or usb drive to copy /root/old to a new Linux system.

Once all done successfully, now type following commands in new Linux System.

First and foremost back up the current users and passwords of the system.

Now restore passwd and other files in /etc/

Warning: You must use >> (append) and not > (create) shell redirection.

Now copy and extract home.tar.gz to new server /home

Now copy and extract mail.tar.gz (Mails) to new server /var/spool/mail

Now reboot system; when the Linux comes back, your user accounts will work as they did before on old system:

Warning: If you are new to Linux then run above commands in a sandbox environment.

Above technique can be used for UNIX to UNIX OR UNIX to Linux account migration. You need to make couple of changes but overall the concept remains the same.