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.... May 21, 2015 at 12:47
  • 3
    No difference once command, script or howto :) The question is how to do it :)
    – Sfisioza
    May 22, 2015 at 9:17
  • So I answer you ... May 22, 2015 at 9:19

6 Answers 6


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 May 26, 2018 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, 2019 at 19:13
  • See this question where a user had issues with this script hosing their OS.
    – KGIII
    Feb 4, 2021 at 14:45
  • I ran this, not as a script, but as each line copied/pasted individually. And I skipped the SHELL part. Seems to work for me on a freshly installed and updated copy of Raspbian / Raspberry Pi OS.
    – AaronD
    Sep 23, 2021 at 3:27
  • 1
    @KGIII I just now realized that I'm using the Lite version, which doesn't have a GUI at all, and the hosed OS that you linked to appears to be a problem with a graphical login. That might be why mine worked and that one didn't.
    – AaronD
    Sep 23, 2021 at 4:21
  • 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, 2016 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, 2016 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, 2018 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, 2019 at 19:14
  • This script made almost everything that I need, except wallpaper and desktop configuration. How to maintain the same wallpaper and desktop settings? Apr 15, 2021 at 14:42

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.




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)

usermod --groups ${SRC_GROUPS}  $2

P.S. Whoever it was that typed /user/local/bin/clone-usermod - It is listed as "code" so that people know to create the required file. I also did not indicate that you had to make the file executable. You obviously did not read the path which was specific and not a typo. Therefore, you are not an informed linux user so I will ignore your rudeness. If you have a specific question, please ask it. If you want to pass judgement, please move on...these aren't the droids you're looking for, nothing to see here - I'm really not interested. Have a nice day.

  • 1
    This is very unclear. Following this advice I cannot clone a user on Debian 11. Starting with the fact that I don't have /user/local/bin/clone-usermod. You should edit the answer so that a person reading it doesn't have to search for clarifications elsewhere. Mar 16, 2023 at 13:51
  • If you have a question, ask it. Passing judgment is not helpful. All you had to say is "I was not able to use it - how do I do so?" - easy - copy paste INTO /usr/local/bin/clone-usermod, and then make it executable. Obviously I did not add all the necessary steps and used shorthand. If that offended you, I can't help you. If you need help, feel free to ask. Commenting that "it is confusing" when you haven't asked a question AND are busy mistyping the path and then passing judgement, is not a helpful comment. Have a nice day. Mar 17, 2023 at 16:37
  • My apology, my intention wasn't rudeness; I judge neither you nor your script. Your answer came up in review, and I wanted to point out that it's incomplete. For an intermediate user it's understandable, but a lot of people around are absolute beginners. These sites also come frequently up in web searches. Adding an explanation what exactly the script does and how it should be used would improve it, as it removes the need to ask in the first place. I doubt you'll always be around to provide the explanation when someone actually does need it. Mar 17, 2023 at 17:14
  • Perhaps you should have started with "I have been tasked with reviewing your post. A newbie user cannot follow these instructions." Your post sounded literally like what I said. Also, I literally copied the majority of the code from the posting above so I really didn't think it required additional explanations as it was already explained - I just adjsuted certain parts where we are cloning to an existing user instead of a new user. Anyway. Have a nice day. If you don't like my post, please feel free to remove it. I was just giving back based on the knowledge already provided. Mar 18, 2023 at 18:06
  • Yeah, as I said, sorry for the tone. Happens sometimes. Reviewing isn't a question of liking, and only mods can delete posts. An answer should ideally be self-contained, so even if everyone else removes their postings yours would still provide a full answer. Same logic as applies to links to external resources. Mar 18, 2023 at 19:18

Try this command

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .