How to easily migrate local users and groups from one system to another system

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 4, 5, 6, 7

Issue

  • All local users and groups (within a certain UID/GID number range) need to be migrated from one old system to another freshly-installed system without manually creating said users on the new system.

Resolution


Disclaimer: This information has been provided by Red Hat, but is outside the scope of our posted Service Level Agreements and support procedures. The information is provided as-is and any configuration settings or installed applications made from the information in this solution could make your Operating System unsupported by Red Hat Support Services. The intent of this solution is to provide you with information to accomplish your system needs. Use the information in this solution at your own risk.

Important:Take a complete backup before starting this activity and test these steps in a test environment first.


In order to accomplish this task, it will be necessary to utilize some rudimentary bash and awk scripting.

Copy-pasteable commands are included below. The only thing that potentially needs to be modified is the first line, where the start of the UID & GID ranges is set as a variable. On a RHEL 5 and RHEL 6, normal users & groups typically begin at #500; however, on a RHEL 7 system, this range starts at 1000 by default.

The awk command could be adapted to only grab UIDs and GIDs within specific ranges -- as it stands, it will grab all users and groups with UIDs and GIDs higher than 500.

Step 1, on source

  • Run the following commands as root on the old (source) system which has users configured

    ID_minimum=500
    for f in /etc/{passwd,group}; do awk -F: -vID=$ID_minimum '$3>=ID && $1!="nfsnobody"' $f |sort -nt: -k3 > ${f#/etc/}.bak; done
    while read line; do grep -w "^${line%%:*}" /etc/shadow; done <passwd.bak >shadow.bak
    while read line; do grep -w "^${line%%:*}" /etc/gshadow; done <group.bak >gshadow.bak
    
  • After running the above as root on the original source system, 4 new files will be present in the current directory (passwd.bak, group.bak, shadow.bak, and gshadow.bak). Inspect them to ensure the appropriate users and groups were gathered.

  • Next, transfer each of the 4 files onto the new destination system, using rsync, scp, or some other method.

Step 2, on destination

  • Run the following command as root on the new (destination) system in a directory containing the four .bak files created in the previous step

    # for f in {passwd,group,shadow,gshadow}.bak; do cat $f >>/etc/${f%.bak}; done
    
  • Note that this will not do any checking to prevent UID/GID collisions with existing users.

Step 3, on destination

  • Run the following final compound command (copy & paste the whole block) on the new (destination) system in the same directory as the previous step

    for uidgid in $(cut -d: -f3,4 passwd.bak); do
        dir=$(awk -F: /$uidgid/{print\$6} passwd.bak)
        mkdir -vm700 "$dir"; cp -r /etc/skel/.[[:alpha:]]* "$dir"
        chown -R $uidgid "$dir"; ls -ld "$dir"
    done
    
  • This final command will create home directories for the new users, along with setting proper ownership/permissions and copying the default config files from /etc/skel.

Note: Scripting the backup and transfer of the files in each user's home directory is beyond the scope of this simple article, but don't forget this if it is necessary.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments