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.

(1 votes, average: 5 out of 5)
Loading ... Loading ...

7 Responses to “ Linux: How do you rename a user account in linux? ”

  1. she
    May 9th, 2008 | 5:09 am

    hmm the linux tools should take care of that IMO

  2. May 9th, 2008 | 5:35 am

    Yeah as I updated the post, there is a utility: usermod

  3. May 9th, 2008 | 6:25 am

    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.

  4. May 9th, 2008 | 6:29 am

    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.

  5. May 9th, 2008 | 9:17 pm

    Thanks for the great information Stoner.

  6. May 10th, 2008 | 6:06 am

    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.)

  7. Johan L. Cerezo
    October 22nd, 2008 | 8:39 pm

    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.

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image