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

debianlinux

How can I create a new system user, an exact copy of another one (having the same groups, permissions, privileges and settings), but with different username, password and home directory?

Best Answer

This script will do it (updated as per comments):

#!/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.