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?

  • 1
    Linux doesnt has any canonical way, You have to write a script.... – PersianGulf May 21 '15 at 12:47
  • 2
    No difference once command, script or howto :) The question is how to do it :) – Sfisioza May 22 '15 at 9:17
  • So I answer you ... – PersianGulf May 22 '15 at 9:19

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


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.

  • 1
    Yes, interesting yet quick and dirty: will fail if a login name is a prefix or suffix of any group name. E.g. login is john, one of the groups is johnny. Here's what happens: SRC=john ; echo "$SRC group1 johnny group3" | sed "s/ ${SRC}//g" | sed "s/ /,/g" Output: john,group1ny,group3 – Stéphane Gourichon May 26 '18 at 5:44
  • Thanks for creating this script. I suggest changing the SRC_GROUPS as such SRC_GROUPS=$(id -Gn ${SRC} | sed "s/ /,/g" | sed -r 's/\<'${SRC}'\>\b,?//g') That will properly remove the exact user group name as opposed to a partial string match. Ex. If a user's Id is 'pi' and a user has groups including 'pi,gpio,spi, etc' it will only remove 'pi' and not the other partial string matches. – Phil Nov 25 '19 at 19:13
  • See this question where a user had issues with this script hosing their OS. – KGIII Feb 4 at 14:45
  • Edit /etc/passwd and duplicate the line of the user you want an exact copy of. Modify the logon name, real name and the home directory.
  • Edit /etc/shadow and again duplicate the line of the original user. Modify the logon name.
  • Finally execute passwd newuser to modify the password.

Be aware that system wise both users are the same (same UID), so one will be able to enter the other one's home directory and modify at will.

  • This is almost certainly the simplest way. However it might be a good idea to modify the UID as well if you don't intend for them to actually be the same user.... – Wildcard Jun 9 '16 at 21:44
  • 2
    @Wildcard, but then it wouldn't be a clone, it would be a new user and that task can be accomplished with a simple useradd. Now that I think about it, you can do it the other way around, create a new user with useradd and then modify UID and GID to clone the first one. – YoMismo Jun 10 '16 at 6:45

For Linux Mint 18 Mate

Based on Mike Anderson's script, I made one that asks questions about the new user, the old user, the new password, and then copies the old user's home directory and replaces all instances of the old user's name in the new home directory with the new user's name.

The main difference in my script regarding the useradd line is that the passwd fails in Linux Mint 18, replaced by chpasswd. To get the password to work I had the create a new line: echo $newuser:$newpassword | chpasswd.

Another difference is that I couldn't get --create-home to work so I just used mkdir in a new line instead.

Watch out if you have a huge old user's home directory.

Take what you need and leave the rest. You are responsible for any code you copy -- make backups!


# clone a user
# usage:
# if you named this as below then
# change to the directory and run this command
# sudo bash clone-user.sh

echo "============="
echo "this script will create a new user"
echo "based on an existing user's data"
echo "You will be shown a list of users who can currently log on"
echo "Remember which user you would like to clone."
echo "You will be asked for the new user's name, their password"
echo "and the old user to clone".
echo "============="

echo -n "New user's name: "
read newuser

echo -n "New user's password: "
read newpassword


echo "Current users you can clone:"
echo "----"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd

echo -n "Old user to clone: "
read olduser

echo "You have selected: "
echo "----"
echo "new user: $newuser"
echo "new user password: $newpassword"
echo "old user: $olduser"

olduser_GROUPS=$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g")
olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd)

echo "old user groups: "
echo "----"
echo $olduser_GROUPS
echo "olduser shell: "
echo $olduser_SHELL
read -rsp $'Press any key to continue or ctrl-c to exit...\n' -n1 key

useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser

echo $newuser:$newpassword | chpasswd

read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key

mkdir /home/$newuser
chown -R $newuser:$newuser /home/$newuser

echo "Script should be done now."
echo "Do you see your new users name below?"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd

echo "We are now going to copy the old user's home folder to the new user"
echo "then change ownership to the new user"
read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key

rsync -aPv /home/$olduser/. /home/$newuser/
chown -R --from=$olduser $newuser:$newuser /home/$newuser

echo "Now we are going to change the names of files and folders to the new user"

grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g'

echo "Done now."
read -rsp $'Press any key to exit...\n' -n1 key

Thanks to everyone in the world who helped me with this script. John in Oregon

  • Suggestions: check for correct permissions, I tried running without sudo the first time, the script gets pretty far. Allso would be helpful to grep .bashrc/.zshrc etc for "/home/$olduser" and warn or offer to replace with $HOME if found, it's very confusing when references to the old user's home directory remain – Mike Nov 11 '18 at 14:25
  • Thanks for creating this. I suggest changing the olduser_GROUPS as such olduser_GROUPS=$(id -Gn ${olduser} | sed "s/ /,/g" | sed -r 's/\<'${olduser}'\>\b,?//g') That will properly remove the exact user group name as opposed to a partial string match. Ex. If a user's Id is 'pi' and a user has groups including 'pi,gpio,spi, etc' it will only remove 'pi' and not the other partial string matches. – Phil Nov 25 '19 at 19:14

As you know , Unix users as UIDs not name , For exampel : mohsen known as 1001 or group mohsen known as 1001.
You have to write an script and do step by step the following steps:

  1. Find uid and gid of the given user
  2. Find its home directory.
  3. Find groups whom user is member of them.
  4. Read /etc/suduers and state of your user.
  5. It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
  6. According to previous number compress its home dir.
  7. Crate a meta dir according to other spec such as configurations and so on.
  8. scp on your target.
  9. Of course, uncompress and use of home dir is itself has a big concept.

NOTE: Don't use script and use above notes step by step. Even you can insert to above.


Try this command

useradd -N -g gid -G gid2,gid3 -m
  • 1
    It's not answer.Poster wants to clone user. – PersianGulf May 21 '15 at 14:07
  • This code was stolen without link and description! – kyb Oct 12 '17 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.