20

I am using Ubuntu 12.04 and made some customization. I will also make it a new iso which is customized ubuntu.

What I want to ask is how can I change my username. There are plenty much configuration in my current home folder. So I don't want to mess up with changing username.

So is there any best practice or workflow to do that?

22

The really right way? Say you want to change user 'peter' to 'paul'.

groupadd paul
usermod -d /home/paul -m -g paul -l paul peter

This changes the name, the group, the home directory and ownership and group of of that directory to the new ones, along with all the files. You end up with a user indistinguishable from having been originally created as 'paul'.

Another way would be to edit the first fields of peter's entries in the /etc/passwd and /etc/shadow files. Then change every occurrence of 'peter' to 'paul' in /etc/group. Then rename the home directory from /home/peter to /home/paul. And then chown -R paul /home/peter to get the ownerships and groupships of the directory and all the files and dirs under it to have the new ones.

  • 3
    No need for the chown if you've edited /etc/{passwd,shadow,group} - the inode stores owner & groups numerically – Graham Nicholls Aug 16 '18 at 15:23
4

I would recommend not attempting to change a user's name. This is generally wrought with problems when the user's name is statically referenced in configuration files in the form of the user's home directory, /home/<username>. These are almost never written in a generic way so it's usually best to just create a new username and then migrate the user's files and data over to the new account.

  • 1
    Thanks for the comment. I created new user and change the userid/ group and home directory regarding the old one. it seems everything is working cool. Do you think this method may also cause problems? – CorpusCallosum Nov 2 '13 at 16:39
3

Try the usermod command.

exec sudo su
usermod --login newname oldname
  • Just do 'sudo whatever you were going to do'. Doing sudo su won't properly log the commands. – Keith Twombley Oct 22 '15 at 15:57
3

You can change it; see How do I change my username? on AskUbuntu. Those instructions worked well for me. To preserve your configuration, just make a symlink for your old home directory as described in the answers to that question.

1

Use usermod to change the username and home directory. Change user peter to paul:

usermod -l paul peter
grep paul /etc/passwd /etc/shadow       # check

change home directory and move everything there (created if necessary)

usermod -m -d /home/paul paul
grep paul /etc/passwd               # check

Change the group name, but don't use usermod as it requires you to first make another group (eg. GID 1001) and although it will update the GID for everything within the $HOME directory, you will have to search for and change the files and directories elsewhere with the old group ownership. Instead, just change the group name manually.

nano /etc/group

and change

peter:x:1000:

to

paul:x:1000:

You will have to change the filenames used by cron, at, and other services that use the username instead of the UID. Use

find /var -name "*peter*"

to find these files.

You will also have to change the config files used by some services such as you mail handling applications.

Finally, scripts using "/home/peter/" will need to be edited. Change occurences of "/home/peter/" to "$HOME".

It isn't a lot of work to change user identities, you just need to be thorough.

  • Upvoted because this is the same approach that I use (for the same reasons with regard to preserving GID). BTW, you can actually combine the two usermod commands into one and you should also remember to edit /etc/gshadow along with /etc/group; The two of them can be changed with GNU sed: sudo sed -i 's/oldname/newname/g' /etc/{group,gshadow} – or better still with sudo groupmod -n newname oldname. Well done for highlighting some of the other gotchas such as having to manually change the name of the user's crontab (if they have one). – Anthony Geoghegan Mar 21 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.