120

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?

Lii
  • 113
  • 4
Szymon Jeż
  • 3,267
  • 3
  • 16
  • 17
  • Why do I get down votes? Because I answered myself? http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Szymon Jeż Oct 11 '12 at 15:07
  • 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
  • I'm guessing that you're getting downvoted because your question is poorly worded and give out very little details of the task you're trying to accomplish. – pauska Oct 11 '12 at 15:09
  • @HopelessN00b So I should ask that on superuser.com? – Szymon Jeż Oct 11 '12 at 15:09
  • @pauska This is such a simple task that it's hard to write much about it. – Szymon Jeż Oct 11 '12 at 15:11
  • I wouldn't ask it anywhere, honestly. Like you said, `This is such a simple task that it's hard to write much about it.` Seems to be a question/answer not worth having or putting effort into, IMO. – HopelessN00b Oct 11 '12 at 15:12
  • 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
  • 14
    @boburob "you also answered your own question as soon as you asked it. Seemed a waste of time" please see: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Szymon Jeż Oct 11 '12 at 15:14
  • 10
    @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
  • 1
    Specifically, copied from here: http://askubuntu.com/questions/131180/changing-short-name-of-user – boburob Oct 11 '12 at 15:34
  • 3
    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 '17 at 12:12

3 Answers3

172

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
user3804598
  • 101
  • 3
Szymon Jeż
  • 3,267
  • 3
  • 16
  • 17
  • 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 '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
  • 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``. – alxs Oct 22 '14 at 18:41
  • 1
    For systems using [`autofs`](https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-nfs-client-config-autofs.html), you should also update `/etc/auto.home`. I just ran into this. – Steven C. Howell Oct 04 '16 at 14:50
  • 2
    Might have to run visudo again to put the new username in the sudoers list if applicable – Nagev Nov 30 '17 at 10:17
  • Make sure to stop all or any `cron` jobs in the name of old user first! – FractalSpace Jul 12 '18 at 21:02
  • 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 '18 at 20:38
16

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.

David Birks
  • 366
  • 1
  • 5
mimoralea
  • 261
  • 2
  • 6
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.

voretaq7
  • 79,345
  • 17
  • 128
  • 213