Clone Linux user (copy user, based on another one)
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
migrated from serverfault.com May 22 '15 at 2:32
This question came from our site for system and network administrators.
add a comment |
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
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
add a comment |
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
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
linux debian
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
add a comment |
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
add a comment |
6 Answers
6
active
oldest
votes
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.
Yes, interesting yet quick and dirty: will fail if a login name is a prefix or suffix of any group name. E.g. login isjohn, one of the groups isjohnny. 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
add a comment |
- 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 newuserto 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
1
@Wildcard, but then it wouldn't be a clone, it would be a new user and that task can be accomplished with a simpleuseradd. Now that I think about it, you can do it the other way around, create a new user withuseraddand then modify UID and GID to clone the first one.
– YoMismo
Jun 10 '16 at 6:45
add a comment |
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
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
add a comment |
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:
- Find uid and gid of the given user
- Find its home directory.
- Find groups whom user is member of them.
- Read
/etc/suduersand state of your user. - It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
- According to previous number compress its home dir.
- Crate a meta dir according to other spec such as configurations and so on.
scpon your target.- 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.
add a comment |
John in Oregon! Great script thumbs up!
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.
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Yes, interesting yet quick and dirty: will fail if a login name is a prefix or suffix of any group name. E.g. login isjohn, one of the groups isjohnny. 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
add a comment |
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.
Yes, interesting yet quick and dirty: will fail if a login name is a prefix or suffix of any group name. E.g. login isjohn, one of the groups isjohnny. 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
add a comment |
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.
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.
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 isjohn, one of the groups isjohnny. 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
add a comment |
Yes, interesting yet quick and dirty: will fail if a login name is a prefix or suffix of any group name. E.g. login isjohn, one of the groups isjohnny. 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
add a comment |
- 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 newuserto 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
1
@Wildcard, but then it wouldn't be a clone, it would be a new user and that task can be accomplished with a simpleuseradd. Now that I think about it, you can do it the other way around, create a new user withuseraddand then modify UID and GID to clone the first one.
– YoMismo
Jun 10 '16 at 6:45
add a comment |
- 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 newuserto 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
1
@Wildcard, but then it wouldn't be a clone, it would be a new user and that task can be accomplished with a simpleuseradd. Now that I think about it, you can do it the other way around, create a new user withuseraddand then modify UID and GID to clone the first one.
– YoMismo
Jun 10 '16 at 6:45
add a comment |
- 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 newuserto 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.
- 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 newuserto 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.
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 simpleuseradd. Now that I think about it, you can do it the other way around, create a new user withuseraddand then modify UID and GID to clone the first one.
– YoMismo
Jun 10 '16 at 6:45
add a comment |
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 simpleuseradd. Now that I think about it, you can do it the other way around, create a new user withuseraddand 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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:
- Find uid and gid of the given user
- Find its home directory.
- Find groups whom user is member of them.
- Read
/etc/suduersand state of your user. - It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
- According to previous number compress its home dir.
- Crate a meta dir according to other spec such as configurations and so on.
scpon your target.- 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.
add a comment |
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:
- Find uid and gid of the given user
- Find its home directory.
- Find groups whom user is member of them.
- Read
/etc/suduersand state of your user. - It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
- According to previous number compress its home dir.
- Crate a meta dir according to other spec such as configurations and so on.
scpon your target.- 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.
add a comment |
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:
- Find uid and gid of the given user
- Find its home directory.
- Find groups whom user is member of them.
- Read
/etc/suduersand state of your user. - It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
- According to previous number compress its home dir.
- Crate a meta dir according to other spec such as configurations and so on.
scpon your target.- 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.
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:
- Find uid and gid of the given user
- Find its home directory.
- Find groups whom user is member of them.
- Read
/etc/suduersand state of your user. - It's very important to you distinguish between hidden files, link files , garbage files, and files related to your native machine.
- According to previous number compress its home dir.
- Crate a meta dir according to other spec such as configurations and so on.
scpon your target.- 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.
answered May 22 '15 at 9:30
PersianGulfPersianGulf
6,90543461
6,90543461
add a comment |
add a comment |
John in Oregon! Great script thumbs up!
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.
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
add a comment |
John in Oregon! Great script thumbs up!
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.
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
add a comment |
John in Oregon! Great script thumbs up!
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!
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.
answered 17 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.
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
add a comment |
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
Welcome to U&L! No need to post a "thanks" as an Answer; rather, build up enough reputation points to be able to vote-up their post. Thanks!
– Jeff Schaller
4 mins ago
add a comment |
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
add a comment |
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
add a comment |
Try this command
useradd -N -g gid -G gid2,gid3 -m
Try this command
useradd -N -g gid -G gid2,gid3 -m
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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