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?
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
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
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:
Create a new temp account with sudo rights:
sudo adduser temp sudo adduser temp sudo
Log out from your current account and back in with the temp account.
Rename your username and directory:
sudo usermod -l new-username -m -d /home/new-username old-username
Rename your username default's group:
sudo groupmod -n new-username old-username
Log out from temp account and log back into your account with new-username.
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)
Generally you can rename a user by changing their username in the
/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
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.