130

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?

13
  • 2
    Probably because renaming a user isn't exactly a task a professional SA would struggle with. (For the question downvotes.) Oct 11, 2012 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, 2012 at 15:13
  • 18
    @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/… Oct 11, 2012 at 15:14
  • 13
    @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.) Oct 11, 2012 at 15:19
  • 5
    This is such a "simple" task that all three answers are incomplete; the Desktop Environment (Gnome/KDE/etc) may have its own idea about user names. That will typically come with a User Mgmt tool which synchronizes down; but building blocks like usermod do not synchronize up. Using the accepted answers will leave you with a login screen showing "hedgehog", but the log you in as "squirrel".
    – MSalters
    Mar 20, 2017 at 12:12

3 Answers 3

186

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 path_to_the_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
7
  • 15
    You can use usermod to do the whole thing usermod -l new-user-name -m -d /new/home/dir old-user-name
    – user9517
    Oct 11, 2012 at 15:20
  • 6
    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.
    – alekosot
    Oct 22, 2014 at 18:41
  • 2
    For systems using autofs, you should also update /etc/auto.home. I just ran into this. Oct 4, 2016 at 14:50
  • 2
    Might have to run visudo again to put the new username in the sudoers list if applicable
    – Nagev
    Nov 30, 2017 at 10:17
  • 3
    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, 2018 at 20:38
21

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.

1
  • In CentOS I also needed to `passwd temp' and 'usermod -aG wheel temp'.
    – Brian Z
    Nov 25, 2018 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.

You must log in to answer this question.

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