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
- JOIN passwd and shadow together by username,-1 fileone -2 filetwo.
- Use Awk to split it up
- Filter deaktivated/no password(!,*), which are daemon-users in most cases.
- gsub – Escape the $ Symbols.
- 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






