How to: Export + Import Linux Users.

Here is an simple way of exporting your Local Users and import them back somewhere else.
To retain the user password, use the -p flag which will allow the use of a pre-encrypted password.

Command

join -t":" -11 -21 /etc/passwd /etc/shadow|awk -F":" '{if($8 !~ /^!/ && $8 !~ /^\*/ && $3 > 0) {
gsub(/\$/,"\\$",$8);
print("useradd -g " $4 " -m -d \"" $6 "\" -c \"" $5 "\" -u " $3 " -p \"" $8 "\" "$1) } }'

Details

  1. JOIN passwd and shadow together by username,-1 fileone -2 filetwo.
  2. Use Awk to split it up
  3. Filter deaktivated/no password(!,*), which are daemon-users in most cases.
  4. gsub – Escape the $ Symbols.
  5. Create the useradd lines to import it again.

Example

useradd -g 100 -c "" -u 1004 -p \$1\$2M2U2"GsdE2Mf/" user1
useradd -g 100 -c "" -u 1007 -p "\$1\$U23ApZpl.Lz31" user4
useradd -g 100 -c "" -u 1008 -p "\$1\$22/us.e9jL0" user99
Bookmark and Share

Leave a Reply