51

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?

4 Answers 4

56

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
4
  • 1
    Thanks! I didn't know about this command. :) However, that's the only account on this computer and the man says this: « You must make certain that the named user is not executing any processes when this command is being executed if the users numerical user ID, the users name, or the users home directory is being changed. usermod checks this on Linux, but only check if the user is logged in according to utmp on other architectures. » Would you suggest using a live cd to do this or creating a temp account that I will delete once it's done? Aug 18, 2010 at 10:30
  • 2
    Since you're using Ubuntu and can't login as root, I would create another account called, say, admin that you use for just this type of task. A temporary account will work just as well but you might as well keep it since you could be doing similar things in the future.
    – gvkv
    Aug 18, 2010 at 15:23
  • 3
    Be aware that usermod --home must contain the full path, e.g. /home/user.name – otherwise the new home directory will be created inside the current working directory.
    – feeela
    Oct 27, 2014 at 10:35
  • That won’t change the group! Apr 9, 2017 at 14:35
24

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.

0
4

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

2
  • What is the groupadd for? Jul 11, 2014 at 11:06
  • 2
    Instead of groupadd, I guess it might be preferable to groupmod -n NEW OLD to avoid changing the gid. At least on Ubuntu a group of the same name as the user is setup by default, so one would wish to change that name as well.
    – jamadagni
    Aug 31, 2014 at 16:45
1

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

1
  • 7
    It seems that this might be better placed as a comment rather than a full answer post which adds almost nothing except mentioning a solution that was already mentioned in the OP. You should consider adding some information as to why you think this solution is better, or even acceptable.
    – HalosGhost
    Jul 9, 2014 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .