How to Move User Account from Linux Server to Another Server

Users can be people, accounts tied to physical users or accounts which exist for a specific application. Linux user can be divided to two types, one is system users and another one is normal user or application users. The system user has default ID values 0 to 499 where normal user has ID value 500 to 60,000.

This tutorial shows you how to cleanly moved user accounts from one Linux server (say Redhat or centos) to another server and its all related files.

Migrate User Accounts

First, we have to understand user account related configuration files. The following are the important files to consider while moving users from one server to another, hence we need to back up these files.

• /etc/passwd contains information about the user.
• /etc/group contains information about the group.
• /etc/shadow contains encrypted password for the user.
• /var/spool/mail contains user mails are stored in this file.
• /home/user contains users home directory, hence it may contain user data.

Lets first backup user accounts. Here we consider copying user account which has user id above 500.

The following command will filter accounts having user id above 500 from the file '/etc/passwd' and copy to the file named '/root/accountbackup/passwd.olds'.

# mkdir accountbackup
# export UGIDLIMIT=500
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/accountbackup/passws.olds

Similarly, copy the selected groups in /etc/group file:

# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group >

Now copy the shadow file:

# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/accountbackup/shadow.old

Lets back up the users home directory and users mails:

# tar -zcvpf /root/accountbackup/home.tar.gz /home

Finally back up the user's mails:

# tar -zcvpf /root/move/mail.tar.gz /var/spool/mail

Now we have a backup of all user account related files. Copy all the data in a pen drive or a new server by scp command.

Before starting the migration to the new server, make sure to backup all user-related files in the new server.

Restore the data into the new server.

# cat passwd.old >> /etc/passwd
# cat group.old >> /etc/group
# cat shadow.old >> /etc/shadow
# /bin/cp gshadow.old /etc/gshadow

Similarly, restore the user's data as well as the user's mails to the new server.

# tar -zxvf /path/to/location/home.tar.gz
# tar -zxvf /path/to/location/mail.tar.gz


In this tutorial, we learned how to migrate user accounts from a Linux box to another box. I hope this helped you understand user account-related files and what all needs to be moved.

Leave a Comment