Linux: How do you rename a user account in linux?

In Linux, there is no command which will rename a user account. If you make a mistake creating a user account, user changes their name or if user does not like his user name, there is no real easy way of going and making the change. Only thing I know you can do is to go through some files and rename user manually. Let us say that we have a user who is named joe and we want to rename him to john.

Note: you must be logged in as root to do following.

vi /etc/passwd
find joe and change it to john, save/exit

vi /etc/group
find joe and change it to john, save/exit

vi /etc/shadow
find joe and change it to john. This file is read only and you have to force overwrite it. In vi it is :w! once saved, quit.

cd /home
mv joe john

And that should do the trick.

[Edited] Right after I posted this post, I was contacted and was told to look at utility called usermod. Read more about it man usermod. Got to love the quick responses.

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

18 thoughts on “Linux: How do you rename a user account in linux?

  1. Stoner

    The comment from ‘she’ is correct. The usermod command will let you do just about everything. If you find that you must edit the passwd or group files, you should never use vi to edit them. Instead, you should use vipw and vigr for that. After editing, they will prompt you to edit the shadow files as well. They do a few others things, like making sure the temporary file used is more secure from prying eyes and general naughtiness.

    Another thing to note, if the user created files or directories outside of their home directory before the username changed, you’ll need to find those files and change the ownership. A few that spring to mind are mail in the /var/mail directory, crontab files and at jobs.

  2. Stoner

    I left out an important clause from my last paragraph. You need to find and change ownership of files if you change the UID or GID numbers for a user.

  3. sapphirecat

    I’d still only recommend this for a new user, not one who is established. Last time I did this, I had a number of problems with things which had hardcoded the equivalent of /home/joe/… into their configuration. (Mozilla in particular. I had to start afresh there, and it was painful. But maybe they’ve fixed that in the intervening 5 years.)

  4. Johan L. Cerezo

    Additional info, If your linux box is a mail server, you must also change the name of the file found in /var/spool/mail. Example from above joe to john. So you can still retrieve your old mail found in your inbox using thunderbird, outlook(client) and alike.

  5. Eu

    Sorry but this article is misleading, first says theres no way to change the username in linux and then crosses that, then gives a manual procedure which is absolutely not pratical because all of it can be done with a single linux command… i suspect that simply replacing all the text in this article with: “usermod -l old_name new_name” would do us all a great favor!

  6. kopper

    would this be enough

    killall -u old
    id old
    usermod -l new old
    groupmod -n new old
    usermod -d /home/new -m new
    usermod -c “New Real Name” new
    id new

  7. noiamsamiam

    @kopper
    Your instructions were a big help for me to rename my user and move my home folder on 9.10

    Using kopper’s instructions my approach was this for Ubuntu 9.10

    1. Login to your user account
    2. Go to Menu System > Administration > Users and Groups
    3. Unlock by clicking on keys “Unlock to make changes”
    4. Enter your user account password when prompted
    5. User settings now unlocked > Select Add User
    6. In Basic Settings tab define you new username for your admin account and real name (This is the friendly name you are shown in the Ubuntu 9.10 login screen
    7. Select the profile drop down box and select administrator
    8. Create your new password (something decent since this account has more privileges than the standard desktop account)
    9. Logout of your current account
    10. At Ubuntu login screen login to the newly created admin account
    11. Open a terminal window from Menu Applications > Accessories > Terminal
    12. type command sudo -i to get root access
    13. execute command killall -u oldaccountusername
    14. execute command id oldaccount username
    15. execute command usermod -l newaccountusername oldaccountusername
    16. execute command groupmod -n newaccountusername oldaccountusername
    17. execute command usermod -d /home/newaccountusername -m newaccountusername
    18. execute usermod -c “New User Account Friendly Name for Logon Screen” newaccountusername
    19. execute id new
    20. execute ls /home/ and you should see a new home folder matching the new username that you changed from the old username and if you ls /home/newaccountusername you should see the all the files and folders that were in the home folder for your oldaccountusername
    21. close the terminal window
    22. logout of the adminuser account on Ubuntu
    23. Ubuntu login screen should now show only the admin account and your changed useraccountname.
    24. Login in to this user account using your old password from the same account before it was renamed.
    25. You should now be logged in to your account under the new username and have access to all the same files, settings and applications as the account before it was renamed.

    I’m sure there is a quicker approach to achieve this but this is what worked for me.

    Thanks again to kopper who contributed the nuts and bolts of getting this working

  8. alaakhalaf

    Hallo all
    I think firs you should install package ‘libuser’ using this command
    sudo apt-get install libuser
    and then execute this command
    sudo lusermod -l newname oldname

  9. Pingback: Some useful things for Linux server management – PCR's notepad

  10. 86Mathew

    I see you don’t monetize your site, don’t waste your traffic, you can earn additional
    cash every month because you’ve got high quality content. If you
    want to know how to make extra $$$, search for: best adsense alternative Wrastain’s tools

  11. pandora charms

    I want to point out my affection for your generosity supporting persons that have the need for help with this one content. Your real dedication to passing the solution all around appeared to be incredibly informative and have usually empowered girls just like me to get to their pursuits. Your amazing warm and helpful suggestions means a lot to me and a whole lot more to my colleagues. Thank you; from everyone of us.

  12. supreme clothing

    I needed to create you one bit of word so as to give thanks yet again relating to the unique basics you’ve contributed above. This is so extremely open-handed of people like you to grant without restraint all that a number of people might have sold for an e book to generate some dough on their own, primarily considering the fact that you could possibly have done it in the event you decided. The things likewise worked as a good way to be aware that other people online have similar passion much like my own to figure out very much more pertaining to this condition. I’m sure there are lots of more enjoyable instances up front for folks who scan your site.

Leave a Reply

Your email address will not be published. Required fields are marked *