Every Linux user has a unique ID in order to distinguish them. These user IDs are used in different areas like permission, log, and kernel-level operations. The regular user ID’s start from 1000 and are incremented with every new user. You may ask if you can change the existing user ID in Linux. The answer is Yes
you can change the existing user ID in Linux. For example, we can change the user ID from 1003 to 2003 in different ways.
List Users ID
The user information is stored in the /etc/passwd
file and this file contains the USER ID information two. The User Id is provided as the 3rd column where the first column is the user name. We can list the users existing ID by printing this file with the cat
command.
$ cat /etc/passwd
We can also filter the user specifically by using the grep
command. In the following example, we only print the user “ismail” ID.
$ cat /etc/passwd | grep "ismail"
ismail:x:1000:1000:İsmail Baydan,,,:/home/ismail:/bin/bash
The first 1000 is the user ID and the second 1000 is the group ID.
Change User ID with usermod Command
The usermod
command is generally used to modify an existing user and related information in Linux. We can use the usermod command in order to change User ID. The -u
option is used to provide the new User ID. The user name is provided as the last parameter to the usermod command.
In the following example, we change the user ismail Id to 2003.
$ sudo usermod -u 2003 ismail
Change Group ID with groupmod Command
Every user in Linux also has a private group with the same username by default. This group also has a group ID. We can use the groupmod
command in order to change the user’s group ID. In the following example, we change the group ismail ID to 3003.
$ sudo groupmod -g 3003 ismail
Change Files and Folders Owner User ID
After changing the User ID its home directory ID and all contents of the user’s home directory is changed into the new ID. But in other files user ID is not changed automatically. To solve this issue the find
command can be used to change to new User ID files owned by this user. We use the users old User Id to find files and use the chown
command to set new user Id by refreshing with the new user name which sets the new User ID.
$ sudo find / -user 1003 -exec chown -h ismail {} \;
Change Files and Folders Owner Group ID
Similar to the user ID we can update the group ID for the user by using the chgrp
command like below.
$ sudo find / -user 1003 -exec chgrp -h ismail {} \;