79

There is only one user on my system. How can I change its user ID from the default of 1000?

If there are additional steps that would be required in order to avoid breaking the login process on a typical desktop installation, those should be included as part of the answer.

Braiam
  • 67,791
  • 32
  • 179
  • 269
ændrük
  • 76,794
  • 6
    Out of curiosity, what are the reasons for changing a user ID? – Olivier Lalonde Dec 09 '10 at 02:27
  • 24
    If you commonly exchange data with other systems via NFS, or just some copy method which preserves the UID, it will help if you use the same UID for the same username across systems. – João Pinto Dec 09 '10 at 11:16
  • 3
    João got it right. In particular, I'm hoping to match up UIDs with users in Mac OS X so that a shared filesystem carries the intended permissions across operating systems. – ændrük Dec 09 '10 at 16:16
  • 2
    The current version of Nethack (3.6.0) saves games including the current uid in the filename. If you want to sync a save game between machines, you need to match uids. (using a symlink isn't practical) – Jonathan Hartley Sep 29 '16 at 01:53
  • 1
    For all serious intents and purposes, it is unsupported and all answers here are hacky and risky-to-use. – aderchox Nov 27 '21 at 05:57

9 Answers9

66

You can change it in /etc/passwd, /etc/group and /etc/shadow or you use one of the preferred possibilties above. But - most important - you have to change the ownership of all files belonging to the user.

For instance, if the old user id is 1000 and the new one is 5000:

find / -uid 1000 -exec chown -h 5000 {} +

And the same for the group id (if you change it as well).

find / -gid 1000 -exec chgrp -h 5000 {} +
heemayl
  • 91,753
ddeimeke
  • 3,089
  • 10
    This is a really important point. Many things will break if you follow the techniques given in the other answers without also doing this. – poolie Dec 09 '10 at 06:41
  • 6
    I just learnt one should better use sudo find / -xdev -uid 1000 -exec chown 5000 '{}' \+, otherwise one would include mounted data (in /mnt, which is possibly undesired) and files you better not mess with in /proc and /dev. See this post – Sebastian Jan 12 '12 at 16:42
  • 4
    This is not a good idea. Given the case that /home is on a different filesystem, you will not even change the ownership of your own files. – ddeimeke Jan 13 '12 at 08:37
  • 1
    On this answer you should change the group and uid permissions, before editing the files in /etc, right? – MrDaniel Mar 27 '12 at 19:49
  • It does not matter, what you do first. – ddeimeke Mar 28 '12 at 09:06
  • 2
    You also want to change ACL permissions on /media/<USERNAME> as in this comment. – oulenz Nov 27 '20 at 18:25
  • 1
    I tried the above commands and now I’m not able to the system anymore. I wish the OP posted a step by step solution than providing fix to others solutions. – SKPS Mar 01 '22 at 20:15
  • 1
    why /etc/shadow is required? It just stores the password, right? – kstn Jul 19 '23 at 08:29
40

Complete solution based on @AlexandreP. and @ddeimeke + official documentation. No reboot necessary.

The Debian/Ubuntu policy is that if there is a user jim with user ID 1001, there is also a group jim with group ID 1001. This solution also updates those group IDs.

  1. Enable the root account:

    sudo passwd root
    
  2. If the user is logged in, then log out (also on virtual terminals)
  3. Go to VT1: Ctrl-Alt-F1
  4. Log in as root and run this with the user name and old/new UID supplied:

    # put the information we need in variables
    username=...
    old_uid=`id -u $username`  # looks up current (old) uid
    new_uid=...
    
    # update the user ID and group ID for $username
    usermod -u $new_uid $username
    groupmod -g $new_uid $username
    
    # update the file ownerships
    # NB: you cannot combine the next two chowns, or files where 
    # only the uid xor the gid matches won't be updated  
    chown -Rhc --from=$old_uid $new_uid /    # change the user IDs
    chown -Rhc --from=:$old_uid :$new_uid /  # change the group IDs
    
  5. Log out
  6. Log in as $username
  7. Disable the root account:

    sudo passwd -dl root
    
Esteis
  • 290
l0b0
  • 8,819
  • 1
    Enabling the root account shouldn't be necessary; just use Recovery Mode. – ændrük Jul 24 '12 at 13:56
  • 4
    This should be faster than rebooting, which is why I posted it. – l0b0 Jul 24 '12 at 18:25
  • 6
    In Ubuntu 14.04 and 14.10 you also need to fix the extended ACL permissions in /media otherwise automounting (in Nautilus/Nemo) breaks as the old UID remains.

    sudo setfacl -m "u:<NEWUID>:r-x" /media/<USERNAME> sudo setfacl -x "u:<OLDUID>" /media/<USERNAME>

    – richud.com Nov 07 '14 at 19:29
  • 1
    This worked like a charm for me. Only had to change the uid, though. – chmike May 06 '15 at 08:36
  • 2
    enabling the root account sounds easier (and less scary) to me than using Recovery Mode. – Jonathan Hartley Sep 29 '16 at 02:03
  • @richud.com this is still true as per 20.04. As far as I could find, the only other files with such ACL permissions are in /var/log/journal, I think it's save to ignore those, but I have no idea what they're for. – oulenz Nov 27 '20 at 18:37
  • At least since Ubuntu 18.04, usermod updates automatically the uid of files inside the user's home directory, see man usermod. So chown is only needed for files outside home. It's still needed to update the group id though. – Pierre H. Jan 06 '22 at 20:54
39

The problem is that, like you mentioned, you cannot change your user's UID when it is logged in a session. You have to use another user account to proceed.

But you don't have to create a new user account, promote it to admin, log out, log in to the new admin account, change your primary account's UID, log out, log in to your primary account then delete the new admin user just change your UID. ;)

You can boot into recovery mode (it's an option that appears when you start up your computer, or hold shift right after the BIOS messages complete; Use ESC on Dell machines running OEM-Ubuntu). This will log you in a root session. Being logged in root and not your usual user account, you will be able to modify your UID.

Because the recovery mode only works in command line interface, once logged into a root session, you will have to:

  1. Use BubbaJ's instructions to remount the root file system in read-write mode: mount -o remount,rw /.

  2. Use Luis Alvarado's command: usermod -u NEW_UID your_username.

  3. Follow ddeimeke's instructions to update file permissions.
  4. Then, reboot your computer (reboot), so you can boot in normal mode.
Alexandre P.
  • 1,031
  • 2
    Doesn't work for Ubuntu 14.04. After doing usermod -u NEW_UID your_username and find / -uid 1000 -exec chown -h 5000 '{}' \+, reboot leads to a guess-session-only GUI login. – Kay Apr 28 '14 at 07:19
  • 1
    Maybe, hidden files in the home are not changed properly? Do you use encryption for your home? – math Jun 18 '14 at 07:53
  • 1
    You may wish to check your /etc/login.defs and /etc/adduser.conf files if you have changed your uid outside of the policies provided in those configuration files. Users outside the policy limits are not displayed in the login loop, but you may change the policy. See this answer – K Markey Mar 20 '18 at 22:22
  • 1
    usermod: user user_name is currently used by process 1118 @Alexandre P. – alper May 14 '18 at 12:17
  • 1
    Answer is missing advice from @KMarkey's comment. If you change the user ID outside of the configured range in /etc/login.defs and /etc/adduser.conf, then there is no Gnome login. This last step fixed it for me. – Kaz Dec 16 '19 at 18:32
13

If you go to console and type: usermod --help you will get one of the parameters saying:

-u, --uid UID new UID for the user account

so if you want to change the UID for user cyrex then do:

usermod -u 1000 cyrex

that would change the uid for cyrex from whatever value it had before to 1000

If you want to do it visually then do this:

Go to

SYSTEM --> ADMINISTRATION --> USERS AND GROUPS

Select yourself from the list and click on ADVANCED SETTINGS

UID is at the end of that window.

Jorge Castro
  • 71,754
Luis Alvarado
  • 211,503
  • 2
    I tried the graphical method you suggested, but there is a message saying "You can't change user ID while the user is logged in." Have you tested this method? – ændrük Dec 09 '10 at 23:13
  • 3
    Hehe well that is true. I tried with another account. You need to login via root or any other admin account. Then do the graphical way. Sorry for that. – Luis Alvarado Dec 10 '10 at 13:19
3
  • Goto System>>Administration>>Users and Groups

alt text

  • Click Advanced Settings and goto Advanced Tab there in the bottom you will see your user id.
  • Change it and click ok.
    alt text
karthick87
  • 81,947
  • 2
    Should I just ignore the warning in your screenshot, "You can't change user ID while the user is logged in."? – ændrük Dec 09 '10 at 16:20
  • 1
    You have to change it from another another user account to get rid of this warning. – karthick87 Dec 09 '10 at 16:42
  • 8
    My question is "How can I change my own user ID?". If you intend your answer to be that I should create a new user just to edit this value, you should edit your answer to indicate so. – ændrük Dec 09 '10 at 23:10
2

This is summed up, what at least since Ubuntu 20.04 is needed (for keeping to be able using Automounting, the change in Extended ACL permissions is needed):

I wanted to change a user id and group id for example from default (uid=) 1000 (and gid=1000) to (uid=) 5000 (and gid=5000), so access to mobile drives formatted with ext4 or similar capable systems with uid & gid) are no problem, if the files and archives have the same user id or group id 5000, no matter if another computer or operating system writes data to this (this enables sharing files easily between computers if all the own users have the same uid and gid).

First I created an additional user, logged as new user, changed in terminal with sudo su - to the root and then I used the commands

find / -uid 1000 -exec chown -h 5000 {} +

for changing user id and

find / -gid 1000 -exec chgrp -h 5000 {} +

for changing group ID. (Take care: This changes the rights on all mounted devices. If not wanted, unmount all not desired or narrow the commands in the part find / to find /home/<user> or else.)

In Fedora this worked but not in Ubuntu totally. For example Automounting did not work. Then I read one has to fix also the extended ACL permissions, otherwise things like Automounting breaks (perhaps problem because of ACL permissions do not appear if no user had this UID & GID already but in my case this was the problem). The change of the Extended ACL permissions seems to be required since Ubuntu 14.04 and 14.10.

These are the commands which gave the possibility of Automounting back again:

sudo setfacl -m "u:<NEWUID>:r-x" /media/<USERNAME>

, which sets UID permissions to the own folder in 'media' and

sudo setfacl -x "u:<OLDUID>" /media/<USERNAME>

, which deletes permissions to the own user folder for the old UID (yes, this command is not needed for getting Automounting work again, but it ensures another user with the old UID has not access to the media, too).

BTW: A user says, usermod already changes all permissions in home folder since Ubuntu 18.04, but for Automounting at least until Ubuntu 20.04.4 this seems not to be enough yet.

source of information from where I got it and practically tested it since about a month: Answers contain all I explain but a bit chaotically and only one advices to fix the Extended ACL permissions for keeping to be able of Automounting.

PS: If someone asks why I did not post this as a question with the solution, the I have to say, I do not have enough "karma" in this sites for doing this. So please vote this answer up.

bit15
  • 31
1

First you need to login as root, at least for a while, so let us make this possible:

sudo passwd root

Reboot, ctrl + alt + f1 to switch to the console, login as root, do the following (ownership in home dir will be taken care of automatically):

groupmod --gid NEWGID username
usermod  --uid NEWUID username 

For your files in places other than your home dir, to this:

sudo chown -R username:username /path/to/files

Delete and lock the passwd of root, if you want. (I tend to keep it):

passwd -dl root
qed
  • 453
1

With KDE: In the Change Advanced User Settings Menu:

Changing from another user does not work for 1000 which is the default user.

you have to do that manually - my experience

skidzo
  • 129
1

I followed ddeimeke's instructions with the following changes:

  1. I did not logout and instead did sudo su
  2. I did not touch /etc/shadow

I have seen it mentioned in the other answers that you should either boot to recovery or login as root. Mine was a fresh Ubuntu 14 install so I was willing to test whether it would work without logging in as root. Also I was doing this on an EC2 instance over SSH.

KalenGi
  • 395