71

I have a user named hedgehog and I want him to be named squirrel, but I don't want to change his numeric user ID.
How can I accomplish this?

  • 2
    Probably because renaming a user isn't exactly a task a professional SA would struggle with. (For the question downvotes.) – HopelessN00b Oct 11 '12 at 15:08
  • 2
    @Jeznet I downvoted because not only is this an incredibly simple task that could be solved by typing your title into google, you also answered your own question as soon as you asked it. Seemed a waste of time. – boburob Oct 11 '12 at 15:13
  • 9
    @boburob "you also answered your own question as soon as you asked it. Seemed a waste of time" please see: blog.stackoverflow.com/2011/07/… – Szymon Jeż Oct 11 '12 at 15:14
  • 9
    @boburob Did you see that tick box that says Answer your own question – share your knowledge, Q&A-style when you ask a question? It's there for a reason, and wouldn't be there if they didn't want it used. (Ideally on better questions, but regardless, downvoting someone for answering their own question is bad form.) – HopelessN00b Oct 11 '12 at 15:19
  • 1
    Yes, I have also answered my own question before but to me, asking a question and pasting the google result in the same minute is a bit of a waste of time – boburob Oct 11 '12 at 15:32
112

Under Linux, the usermod command changes user names. It modifies the system account files to reflect the changes that are specified on the command line.

To change just the username:

usermod --login new_username old_username

To change the username and home directory name:

usermod --login new_username --move-home --home /new/home/dir old_username

You may also want to change the name of the group associated with the user:

groupmod --new-name new_username old_username
  • 10
    You can use usermod to do the whole thing usermod -l new-user-name -m -d /new/home/dir old-user-name – Iain Oct 11 '12 at 15:20
  • @Iain Thanks. I will extend my answer to reflect that. BTW That's why I asked this "trivial" question - to get an answer that is better than mine (and also because I could not find it already at SF). – Szymon Jeż Oct 11 '12 at 15:23
  • 4
    Also note, that the group is not changed either. If you need to rename the old-user-name group as well, use # groupmod -n new-user-name old-user-name. – alxs Oct 22 '14 at 18:41
  • 1
    Might have to run visudo again to put the new username in the sudoers list if applicable – Nagev Nov 30 '17 at 10:17
  • 1
    You'll also need to rename the crontab file located in /var/spool/cron/crontabs/ (if you have any jobs installed) – Daniel F Dec 16 '18 at 20:38
8

NOTE: don't try this if your directory is encrypted! If this is your case you might want to check first: https://askubuntu.com/questions/107410/can-you-unencrypt-remove-encryption-from-a-user-home-folder

The straight out way of doing this is:

  1. Create a new temp account with sudo rights:

    sudo adduser temp
    sudo adduser temp sudo
    
  2. Log out from your current account and back in with the temp account.

  3. Rename your username and directory:

    sudo usermod -l new-username -m -d /home/new-username old-username
    
  4. Rename your username default's group:

    sudo groupmod -n new-username old-username
    
  5. Log out from temp account and log back into your account with new-username.

  6. Remove temp account:

    sudo userdel -r temp
    

Otherwise, you just (1) create a new user and (2) rsync the old user home folder to the new and then (3) chown it.

  • In CentOS I also needed to `passwd temp' and 'usermod -aG wheel temp'. – Brian Z Nov 25 '18 at 16:01
4

Generally you can rename a user by changing their username in the /etc/passwd (and /etc/shadow, if applicable) files. On most unix systems the vipw command is used to edit these files (and on many systems includes some safeguards to ensure that you don't mess things up too badly).
See the man pages for passwd(5), shadow(5), and vipw(8) for more information.

Note that the method above does not rename other things which may bear the original username (home directories being the prime example, per-user personal groups (on systems which use them) being another). You may wish to clean these up as well for consistency, by changing the appropriate fields in the passwd file and renaming the directories.


Several operating systems provide a system-specific way of renaming users. For example many Linux systems include the usermod(8) command, and on AIX you can change account names using SMIT (or smitty in a terminal).
These commands will often handle the cleanup items like renaming home directories, if you ask them to.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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