How to copy / clone user account in Linux?

Task: Copy / clone user account, so the both users have the very same settings in their user home directory.

Copying of the user's home directory (e.g. olduser) to new user (e.g. newuser) is easy:
1. Create new user: adduser newuser
2. Copy all special hidden (dot) files to new user's home directory: cp --recursive /home/olduser/.[a-zA-Z0-9]* /home/newuser
3. Copy other standard files to new user directory: cp --recursive /home/olduser/* /home/newuser
4. Set new user's directory and files owner to new user for hidden dot files: chown --recursive newuser:users /home/newuser/.*
5. Set new user's directory and files owner to new user for normal files: chown --recursive newuser:users /home/newuser/*

You are done. In some cases you would need to change user group (users in this case).

Now just logout and try to login as the new user. All the settings for the programs should be the very same as for the old user. You can for example compare the settings by running KDE and checking the wallpaper and other settings of the new user. If the copying of the user folder was successful, everything will look the same.

:: 04.11.2006 :: section English :: submitted by Daniel :: [*] ::

Discussion:

This is basically what I did several months ago to allow a friend to use my Gentoo Linux system without messing up my files and desktop. It sort of works but really it's more complex than that. The problem is that there are some hard references in the cloned user's dot files to the original user's configuration files. I get various problems such as an error (can't make a DCOP connection) every time user A starts KDE for the first time after user B has used it, and vice versa. Also, changes to the cloned user's desktop sometimes interfere with settings belonging to the original user. Really, I need to go through all the dot files, find all the references to the original user's config files, and change them to point to the new user's files. (It might be possible to do this with a script.)

Could I appeal to developers not to put full paths in these dot files, but make them relative to ~ instead? That would go a long way to solving this problem.

wrote Peter Clarke on 27.01.2007 at 18:21:47


No problems with DCOP and KDE here. Maybe there are some program that use hardcoded absolute paths, but no errors encountered here.

wrote dusoft on 27.01.2007 at 23:27:36


There is a problem with the 4). it should be like /.[a-zA-Z0-9]* and not like /.*

Otherwise it is a real good piece of information

wrote laurent on 25.04.2007 at 21:13:35


Danger! Using chown with /home/newuser/.* will set your own home!

wrote hollerith on 31.10.2007 at 00:26:10


ssl_enable=YES
ssl_tlsv1=YES
rsa_cert_file=/etc/vsftpd/vsftpd-cert-key.pem
force_local_logins_ssl=YES
force_local_data_ssl=YES

wrote on 18.03.2009 at 13:47:29


Better way:

Copy recursively all files (dotted and otherwise:

cp -R /home/existing_username/ /home/new_username

(don't forget the "/" after the existing username)

chown -R new_username /home/new_username/

(again - don't forget the last "/"!!!)

wrote Tyson Brown on 24.08.2010 at 18:04:29


hollerith is right
do not use this code

wrote Someone on 08.10.2010 at 19:05:37


soooo.. how do I fix this?

wrote on 29.12.2011 at 22:41:09


to fix it:

sudo chmod root:root /home
sudo chmod newuser:users /home/newuser
sudo chmod olduser:users /home/olduser

wrote on 30.12.2011 at 00:28:55


Tyson Brown's way worked fine. Thanks

wrote on 17.12.2012 at 22:00:40