38

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?

Paradox
  • 722
  • 2
  • 8
  • 25

11 Answers11

23

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.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
seumasmac
  • 2,015
  • 13
  • 14
  • 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` – Richard Frank Apr 19 '17 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 Aug 02 '17 at 14:17
  • 2
    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 Sep 19 '20 at 04:00
7

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.
AdminBee
  • 21,637
  • 21
  • 47
  • 71
Rajib Hasan
  • 71
  • 1
  • 2
3

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.

waltinator
  • 4,439
  • 1
  • 16
  • 21
3

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 -u root whoami
Elvis Van
  • 31
  • 2
  • 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. – G-Man Says 'Reinstate Monica' Jul 29 '22 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](/help/how-to-answer). – Community Aug 04 '22 at 15:31
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

Kemin Zhou
  • 347
  • 3
  • 8
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.

krivos
  • 29
  • 3
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
nomadSK25
  • 111
  • 3
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.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
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
    
Greenonline
  • 1,759
  • 7
  • 16
  • 21
-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.

Teddy C
  • 447
  • 2
  • 11
  • 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 Mar 16 '23 at 00:24
  • Sorry I didn't include the step to login as another user first. I edited my answer. @Kusalananda – Teddy C Mar 17 '23 at 03:43