How can I rename an unix user?

Solution 1:

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

Solution 2:

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.


Solution 3:

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.