Is it possible to rename a Unix user account?

I installed Ubuntu on a computer that is now used by somebody else. I renamed the account with her name, but it only changes the fullname, not the user name, which is still displayed in the top right (in the fast-user-switch-applet). Is there a command to rename an Unix user account?

I’ve thought of creating a new user account with the new name, and then copying everything in the “old” home to the home of the new account. Would it be enough? But then I think the files would have the old account’s permissions’ owner? So should I do chown -R newuser ~?

Is there a simpler/recommended way to do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try

usermod --move-home --login <new-login-name> --home <new-home-dir> <old-login-name>

The --move-home option moves the old home directory’s contents to the new one given by the --home option which is created if it doesn’t already exist.

If you want the primary user group to match the new-login-name, add --gid <new-login-name> to the command above, but the group must be pre-existing.

See the man page for more info:

man usermod

Method 2

While it’s possible to rename a user, and usermod will do some of the job, this is quite likely to cause trouble. Here’s a list of places where the user name may appear; I do not claim that this list is exhaustive.

  • The user entry in /etc/passwd and related files (e.g. /etc/shadow, /etc/master.passwd).
  • Group entries in /etc/group (and /etc/gshadow).
  • Possibly entries in /etc/sudoers.
  • The system mailbox, typically /var/mail/$USER or /var/spool/mail/$USER.
  • The crontab, typically /var/spool/cron/crontabs/$USER
  • In at jobs (/var/spool/cron/atjobs/*)

The home directory is likely to appear in even more places. Many applications write absolute paths in various dot files. If you decide to rename the home directory as well (while there’s no requirement that user alice‘s home directory be /home/alice, having it be /home/bob would be confusing), you’ll need to take care of those. A global replacement will work for most applications, but I make no promises. If possible, keep the old home directory name as a symbolic link to the renamed directory.

Method 3

Here’s a summary of the commands I used on a Debian system:

groupadd NEW
usermod -l NEW -m -d /home/NEW -g NEW OLD
chfn -f "New Fullname" NEW
cd /home
ln -s NEW OLD

The last two commands create a symbolic link from the name of the old home directory (/home/OLD) to the name of the new one (/home/NEW).

Method 4

all above are elegant methods, but why don’t just create a new user, copy all it’s home content from the first one, and that’s it?
of course, if it’s about a service (application) account, perhaps it’s not the best practice


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x