Guest User

Clone User Linux

a guest
Mar 26th, 2021
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # clone a user
  4. # usage:
  5. # if you named this as below then
  6. # change to the directory and run this command
  7. # sudo bash clone-user.sh
  8.  
  9. echo "============="
  10. echo "this script will create a new user"
  11. echo "based on an existing user's data"
  12. echo
  13. echo "You will be shown a list of users who can currently log on"
  14. echo "Remember which user you would like to clone."
  15. echo "You will be asked for the new user's name, their password"
  16. echo "and the old user to clone".
  17. echo "============="
  18. echo
  19.  
  20. echo -n "New user's name: "
  21. read newuser
  22.  
  23. echo -n "New user's password: "
  24. read newpassword
  25.  
  26. echo
  27.  
  28. echo "Current users you can clone:"
  29. echo "----"
  30. sudo awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
  31. echo
  32.  
  33. echo -n "Old user to clone: "
  34. read olduser
  35.  
  36. echo
  37. echo "You have selected: "
  38. echo "----"
  39. echo "new user: $newuser"
  40. echo "new user password: $newpassword"
  41. echo "old user: $olduser"
  42. echo
  43.  
  44. olduser_GROUPS=$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g")
  45. olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd)
  46.  
  47. echo "old user groups: "
  48. echo "----"
  49. echo $olduser_GROUPS
  50. echo "olduser shell: "
  51. echo $olduser_SHELL
  52. read -rsp $'Press any key to continue or ctrl-c to exit...\n' -n1 key
  53.  
  54. sudo useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser
  55.  
  56. echo $newuser:$newpassword | chpasswd
  57.  
  58. read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key
  59.  
  60. sudo mkdir /home/$newuser
  61. sudo chown -R $newuser:$newuser /home/$newuser
  62.  
  63. echo
  64. echo "Script should be done now."
  65. echo
  66. echo "Do you see your new users name below?"
  67. echo
  68. sudo awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
  69.  
  70. echo
  71. echo "We are now going to copy the old user's home folder to the new user"
  72. echo "then change ownership to the new user"
  73. echo
  74. read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key
  75.  
  76. sudo rsync -aPv /home/$olduser/. /home/$newuser/
  77. sudo chown -R --from=$olduser $newuser:$newuser /home/$newuser
  78.  
  79. echo
  80. echo "Now we are going to change the names of files and folders to the new user"
  81. echo
  82.  
  83. sudo grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g'
  84.  
  85. echo
  86. echo "Done now."
  87. echo
  88. read -rsp $'Press any key to exit...\n' -n1 key
  89. echo
  90. echo
  91.  
Advertisement
Add Comment
Please, Sign In to add comment