42

I am trying to change my username, as per advice here however after running the following command:

CurrentName@HostName ~ $ sudo usermod -l TheNameIWantToChange -d /home/TheNameIWantToChange -m CurrentName

Terminal responds with:

CurrentName@HostName ~ $ usermod: user CurrentName is currently used by process 2491

And the username stays the same. Does anybody know how I could fix this and change my username after all?

11 Answers 11

25

To quote man usermod :

CAVEATS

You must make certain that the named user is not executing any processes when this command is being executed if the user's numerical user ID, the user's name, or the user's 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.

So, you need to make sure the user you're renaming is not logged in.

Also, I note you're not running this as root. Either run it as root, or run with sudo usermod.

3
  • 7
    if you can't kill the process without it restarting, combine the command e.g. kill -9 23162 && sudo usermod -l TheNameIWantToChange -d /home/TheNameIWantToChange -m CurrentName Commented Apr 19, 2017 at 11:45
  • 4
    The program usermod seems to be broken by design. If the user management is done in a LDAP server, changing a user attribute is always possible and does not depend on the running system. I am using Ansible which uses usermod and the hole system is unreliable, because changes are blocked by users logged in.
    – ceving
    Commented Aug 2, 2017 at 14:17
  • 8
    so uh... how do you change your user id if you're the only account on the system? i'm assuming the case when you can't log in directly as root (e.g. login is only via ssh) and may have many automatically started processes that you can't kill without losing your connection. @RichardFrank is your solution intended to address this?
    – Michael
    Commented Sep 19, 2020 at 4:00
10

Rather than hand-editing the files you know about (bad), or opening up a root password (very bad), simply use at. Read man at, and using the full path for usermod (type -p usermod):

sudo at "now +5 minutes"
your usermod command goes here
^D

^D is Ctrl-D

Then, quickly logoff, wait 7 minutes, and logon.

8

Try these steps:

  1. Set the password for root user:
    sudo passwd root
    
  2. Reboot ubuntu
  3. Login as root after boot up. This will allow Ubuntu not to generate any process for current user.
  4. Change the username
    usermod -l newuser -d /home/newuser -m olduser
    
  5. Verify the username has changed by checking the folder name with new username in /home directory.
2
  • Is there an option to choose user when doing in this with WSL? Commented Jun 8, 2022 at 11:57
  • additionally might need to delete the previous user old directories like /media/olduser
    – Andry
    Commented Sep 27, 2022 at 18:46
4

In case you are running on WSL (Windows Subsystem for Linux), open PowerShell as admin and type these commands:

  1. Open graphical process manager as root to avoid starting normal (non-root) processes:

    wsl -d distributionname -u root htop
    

    Note: change "distributionname" to your target distro, example: ubuntu-20.04.

  2. Kill active processes

    After opening htop, you'll see an interface looking like this:
    htop output
    Now choose the process(es) that are not root (the ones you want to change username) and press F9 to kill those processes.  A panel will appear to the left after you press F9; it looks like this:
    htop with Kill Process (F9) dialog
    Choose signal 9, which is SIGKILL.  After this there should be no warning like "usermod: user ... is currently running used by process ..."

  3. Change username

    The command below will attempt to change username:

    wsl -d distributionname -u root usermod -l newusername oldusername

    Note: replace "distributionname" with name of the Linux distro, replace "newusername" with what you want and "oldusername" with current username.

Final result: in my case, I have changed my username from elvisubuntu to ubuntu20.04.

To check username, run:

wsl -d distributionname whoami
3
  • 1
    (1) Note that the question wasn’t actually about WSL. (2) Does it need to be PowerShell?  Wouldn’t Command Prompt be just as good?  (3) In my opinion, making your user name the same as the operating system name is a little peculiar.  (4) Why would you say -u root whoami?  Wouldn’t that just say “root”?  (5) I deleted your third screenshot because I believe that it doesn’t really contribute to your answer. Commented Jul 29, 2022 at 20:51
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 4, 2022 at 15:31
  • you are right, i have removed the -u root part
    – Elvis Van
    Commented Dec 17, 2023 at 14:45
2

This is mainly a problem with Ubutu where you don't have a root account to login from the first place. So this create a contradiction: I am a user in the sudoer group. I cannot change my own uid.

Not sure this is the best solution. I first create a fake account foo, add this to the sudo list. Then I login to the foo account and execute usermod -g MYOWNGID mylogin

1

I think that you should run the command with another user. Login with root, or other user, and try again. If you are in the x window with the user that you want to change, sounds logical that the commands fails.

1

You cant change the name of the user through which you are currently logged in. You need to switch to another user with the sudo access.

If you don't have any other user and you have already set the password for root and you are using Ubuntu with GUI (e.g. ubuntu desktop) then you can simply follow these steps

  1. press Ctrl + Alt + F5 to switch to a TTY at login screen
  2. type the user as root and provide the root password which you already set
  3. fire this command: sudo usermod -l <new_user> <old_user> , please replace <new_user> with the actual new user and <old_user> with the actual old user.
  4. press Ctrl + Alt + F1 to switch to a GUI at login screen
0

I have the same issue using usermod, it fixed using the -m parameter, that move the content of the home directory to a new localization. Use this option in combination with -d (modify the user main directory, where there user ).

I suggest to see the man usermod command

0

Log in as root, then open terminal and run

kill -9 -u [old_username] && username -l [old_username] [new_username]

After that run

adduser [new_username] sudo

to be able to run sudo scripts with the new username.

3
  • 1
    The command you are referring to is 'killall', not kill. killall -u <username>. Commented Nov 17, 2020 at 11:18
  • kill: invalid argument u
    – damd
    Commented Jan 12, 2022 at 18:54
  • this works but only with killall
    – user85155
    Commented Mar 1, 2022 at 14:49
0
  1. Create new_user if not other existing user.

  2. If you turned on auto login option, you need to disable that option and reboot then login to new_user created.

  3. Add new_user to the sudoers file.

  4. Now you can use chmod from the new_user account that you have just created. For example:

    sudo usermod -l new_username -d /home/new_directory -m old_username
    
-2

If you could login with another user, you could use this command to kill all processes owned by that user:

sudo pkill -u <username>

Then you can run usermod without issue.

2
  • The user wants to change their own username. If they run pkill -u username with sudo while logged in as username, don't you think that would probably cause them to be thrown out of the system? How would that help them?
    – Kusalananda
    Commented Mar 16, 2023 at 0:24
  • Sorry I didn't include the step to login as another user first. I edited my answer. @Kusalananda
    – Teddy C
    Commented Mar 17, 2023 at 3:43

You must log in to answer this question.

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