# Rename a user/Change a username



## hm2k (Dec 23, 2010)

What is the recommended method to rename a user or change a username?

Or is it a case of add new, remove old, rename home directory, chown?

Thanks.


----------



## SirDice (Dec 23, 2010)

Just use vipw(8) and change the username and home directory. Rename the home directory itself and you're done. You may also want to rename the corresponding group name.


----------



## anomie (Dec 23, 2010)

chpass(1) can also be used to change the username. (Keep in mind that various types of access control lists - and even app customizations - _might_ be relying on the old username).


----------



## hm2k (Dec 23, 2010)

Thanks, I decided to write a little script in the end I called chuser.sh.

Enjoy!


----------



## SirDice (Dec 23, 2010)

As with all things *nix, there are several roads leading to Rome 

Keep in mind that your script might do something really unexpected if the user doesn't exist.


----------



## jcd (Nov 9, 2012)

[CMD="ggg"][/CMD]I want to point out that chpass(1) does not work to change the name of an account as suggested.

I am testing  under FreeBSD 9.0, and after editing the name of the account using this command, the result is a new record in /etc/passwd with the new name (the rest of the fields containing the same data as the original record), and the original record remains unchanged.  

So the result is two records with the same user id, which is incorrect.

Any other possibility besides manually editing?/etc/passwd and, of course, /etc/group (to change the name of the corresponding group plus any other group that the account might be part of.

Cheers.


----------



## jea (Feb 14, 2018)

This thread is old, but I was searching for how to do this and came across a method I preferred, using pw(8).

`pw usermod -n oldname -l newname -d /usr/home/newname
pw groupmod -n oldname -l newname
mv /usr/home/oldname /usr/home/newname
chown -R newname:newname /usr/home/newname`

*Warning: I've only done minimal testing so far.*


----------



## grahamperrin@ (Jun 16, 2022)

For chown(8) above, in edge cases there might be files that (properly) are not owned by the owner of the home directory. 

For cases such as these, you can make combined use of chown with find(1) (e.g. <https://superuser.com/a/648166/84988> and <https://forums.freebsd.org/posts/291414>) or bfs(1).


----------

