36

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?

3

6 Answers 6

41

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/paul to get the ownerships and groupships of the directory and all the files and dirs under it to have the new ones.

5
  • 3
    No need for the chown if you've edited /etc/{passwd,shadow,group} - the inode stores owner & groups numerically Aug 16, 2018 at 15:23
  • fyi. Just tested in Ubuntu 20.04, works great. Jul 16, 2020 at 4:33
  • 2
    for usermod I getting user <here_peter> is currently used by process 2243 Oct 28, 2020 at 8:56
  • 4
    You can also just rename the group independently. groupmod -n paul peter. Which avoids changing gid.
    – MaxNoe
    Feb 15, 2021 at 16:44
  • I also got "currently used by process". I rebooted, have no password access. Now reinstalling Ubuntu 20.04. Lost a years data. Thanks for the bad information. Jun 27, 2021 at 19:34
7

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
  • 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? Nov 2, 2013 at 16:39
6

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.

4
  • 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). Mar 21, 2019 at 19:50
  • 2
    Quick addendum to this... you can't be logged in while doing this. Make new temp user that is part of sudoers group, login with that, make the changes, logout from new temp accound, login to the renamed one, and delete the temp account.
    – J-a-n-u-s
    Jun 6, 2019 at 16:44
  • @AnthonyGeoghegan: groupmod is no good; it only changes the group name, but not memberships in other groups. Your sed command is better.
    – EML
    Aug 11, 2022 at 17:18
  • And don't forget to change the comment field in /etc/passwd; this shows up in some program output (finger, etc). usermod -c or, better, # chfn paul.
    – EML
    Aug 11, 2022 at 17:40
5

Try the usermod command.

exec sudo su
usermod --login newname oldname
1
  • Just do 'sudo whatever you were going to do'. Doing sudo su won't properly log the commands. Oct 22, 2015 at 15:57
4

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.

0

Building on @Steve Bergman 's answer,

groupmod -n paul peter && usermod -d /home/paul/ -m -l paul peter

will change both the user and group 'peter' to 'paul'. This way no created files will be left with the wrong group.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .