Clone Linux user (copy user, based on another one)

por | 8 noviembre, 2020
#!/bin/bash
SRC=$1
DEST=$2

SRC_GROUPS=$(id -Gn ${SRC} | sed "s/ /,/g" | sed -r 's/\<'${SRC}'\>\b,?//g')
SRC_SHELL=$(awk -F : -v name=${SRC} '(name == $1) { print $7 }' /etc/passwd)

sudo useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
sudo passwd ${DEST}

It gets the source user’s groups (not including the group that’s the same as their login) and shell, then creates a new user with the same shell and secondary groups.

Usage: clone-user src_user_name new_user_name

There is no error checking, it’s just a quick and dirty clone script.