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












7















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?










share|improve this question













migrated from serverfault.com May 22 '15 at 2:32


This question came from our site for system and network administrators.
















  • 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
















7















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?










share|improve this question













migrated from serverfault.com May 22 '15 at 2:32


This question came from our site for system and network administrators.
















  • 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














7












7








7


3






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?










share|improve this question














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?







linux debian






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 21 '15 at 12:30









SfisiozaSfisioza

139114




139114




migrated from serverfault.com May 22 '15 at 2:32


This question came from our site for system and network administrators.






migrated from serverfault.com May 22 '15 at 2:32


This question came from our site for system and network administrators.















  • 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



















  • 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

















Linux doesnt has any canonical way, You have to write a script....

– PersianGulf
May 21 '15 at 12:47





Linux doesnt has any canonical way, You have to write a script....

– PersianGulf
May 21 '15 at 12:47




2




2





No difference once command, script or howto :) The question is how to do it :)

– Sfisioza
May 22 '15 at 9:17





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





So I answer you ...

– PersianGulf
May 22 '15 at 9:19










6 Answers
6






active

oldest

votes


















5














This script will do it:



#!/bin/bash
SRC=$1
DEST=$2

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

useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
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.






share|improve this answer
























  • 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



















4















  • 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.






share|improve this answer


























  • 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






  • 1





    @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



















1














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!



#!/bin/bash

# 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
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

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

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

echo

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

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

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

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

echo
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"
echo
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
echo "Now we are going to change the names of files and folders to the new user"
echo

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

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


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






share|improve this answer
























  • 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



















0














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.






share|improve this answer































    0














    John in Oregon! Great script thumbs up!





    share








    New contributor




    Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




























      -1














      Try this command



      useradd -N -g gid -G gid2,gid3 -m





      share|improve this answer



















      • 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








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204970%2fclone-linux-user-copy-user-based-on-another-one%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      This script will do it:



      #!/bin/bash
      SRC=$1
      DEST=$2

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

      useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
      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.






      share|improve this answer
























      • 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
















      5














      This script will do it:



      #!/bin/bash
      SRC=$1
      DEST=$2

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

      useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
      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.






      share|improve this answer
























      • 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














      5












      5








      5







      This script will do it:



      #!/bin/bash
      SRC=$1
      DEST=$2

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

      useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
      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.






      share|improve this answer













      This script will do it:



      #!/bin/bash
      SRC=$1
      DEST=$2

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

      useradd --groups ${SRC_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST}
      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 8 '16 at 14:30









      Mike AndersonMike Anderson

      5111




      5111













      • 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



















      • 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

















      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





      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













      4















      • 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.






      share|improve this answer


























      • 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






      • 1





        @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
















      4















      • 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.






      share|improve this answer


























      • 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






      • 1





        @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














      4












      4








      4








      • 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.






      share|improve this answer
















      • 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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 10 '16 at 8:05

























      answered May 22 '15 at 9:44









      YoMismoYoMismo

      3,0761825




      3,0761825













      • 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






      • 1





        @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



















      • 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






      • 1





        @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

















      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





      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




      1




      1





      @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





      @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











      1














      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!



      #!/bin/bash

      # 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
      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

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

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

      echo

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

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

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

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

      echo
      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"
      echo
      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
      echo "Now we are going to change the names of files and folders to the new user"
      echo

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

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


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






      share|improve this answer
























      • 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
















      1














      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!



      #!/bin/bash

      # 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
      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

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

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

      echo

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

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

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

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

      echo
      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"
      echo
      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
      echo "Now we are going to change the names of files and folders to the new user"
      echo

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

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


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






      share|improve this answer
























      • 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














      1












      1








      1







      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!



      #!/bin/bash

      # 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
      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

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

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

      echo

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

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

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

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

      echo
      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"
      echo
      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
      echo "Now we are going to change the names of files and folders to the new user"
      echo

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

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


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






      share|improve this answer













      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!



      #!/bin/bash

      # 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
      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

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

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

      echo

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

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

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

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

      echo
      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"
      echo
      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
      echo "Now we are going to change the names of files and folders to the new user"
      echo

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

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


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







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 9 '16 at 20:45









      OregonJohnOregonJohn

      112




      112













      • 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



















      • 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

















      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





      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











      0














      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.






      share|improve this answer




























        0














        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.






        share|improve this answer


























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 22 '15 at 9:30









          PersianGulfPersianGulf

          6,90543461




          6,90543461























              0














              John in Oregon! Great script thumbs up!





              share








              New contributor




              Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                John in Oregon! Great script thumbs up!





                share








                New contributor




                Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  John in Oregon! Great script thumbs up!





                  share








                  New contributor




                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  John in Oregon! Great script thumbs up!






                  share








                  New contributor




                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 2 mins ago









                  Joe FeigelmanJoe Feigelman

                  1




                  1




                  New contributor




                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Joe Feigelman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.























                      -1














                      Try this command



                      useradd -N -g gid -G gid2,gid3 -m





                      share|improve this answer



















                      • 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
















                      -1














                      Try this command



                      useradd -N -g gid -G gid2,gid3 -m





                      share|improve this answer



















                      • 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














                      -1












                      -1








                      -1







                      Try this command



                      useradd -N -g gid -G gid2,gid3 -m





                      share|improve this answer













                      Try this command



                      useradd -N -g gid -G gid2,gid3 -m






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 21 '15 at 13:56









                      BoothBooth

                      1




                      1








                      • 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














                      • 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








                      1




                      1





                      It's not answer.Poster wants to clone user.

                      – PersianGulf
                      May 21 '15 at 14:07





                      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





                      This code was stolen without link and description!

                      – kyb
                      Oct 12 '17 at 10:25


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204970%2fclone-linux-user-copy-user-based-on-another-one%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Entries order in /etc/network/interfaces

                      新発田市

                      Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)